//大舵机2.5%-12.5%满占空比调节 270度左右 PWM设置25-125 //一般舵机7%为中间值,4.5%-10%左右范围 void stepperMoterInit() { TIM5_Init();//左边的电机前四个 TIM8_Init();//用于手爪的吸力电机 TIM4_Init();//右边的电机前四个 TIM1_Init(); }
/******************************************** 飞控主函数入口 功能: 1.初始化各个硬件 2.初始化系统参数 3.开定时器4等待数据中断到来 4.开定时器3串口广播实时姿态以及相关信息 ********************************************/ int main(void) { //int i; SystemClock_HSE(9); //系统时钟初始化,时钟源外部晶振HSE //SystemClock_HSI(4); //系统时钟初始化,时钟源内部HSI UART1_init(SysClock,115200); //串口1初始化 NVIC_INIT(); //中断初始化 STMFLASH_Unlock(); //内部flash解锁 LedInit(); //IO初始化 delay_init(SysClock); //滴答延时初始化 BT_PowerInit(); //蓝牙电源初始化完成,默认关闭 MotorInit(); //马达初始化 BatteryCheckInit(); //电池电压监测初始化 IIC_Init(); //IIC初始化 MPU6050_DMP_Initialize(); //初始化DMP引擎 //HMC5883L_SetUp(); //初始化磁力计HMC5883L PID_INIT(); //PID参数初始化 ParameterRead(); //Flash参数读取 NRF24L01_INIT(); //NRF24L01初始化 SetRX_Mode(); //设无线模块为接收模式 PowerOn(); //开机等待 BT_on(); //蓝牙开 TIM3_Init(SysClock,10); //定时器3初始化,调试串口输出 TIM4_Init(SysClock,50); //定时器4初始化,定时采样传感器数据,更新PID输出 while (1); //等待数据更新中断到来 }
void main(void) { u16 trigpos; u8 keycode; SystemState = AutoRunMode; // 现在还是使用软件触发 ADCState = Triggered; // ADCState = WaitTrigger; CLK_Init(); // 主时钟初始化 TIM4_Init(); // TIM4 用于产生系统运行需要的定时信号 KEY_Init(); // 按键驱动初始化 KeyParse_Init(); // 按键处理模块初始化 LCD_Init(); // LCD驱动初始化 WDraw_Init(); // 波形显示模块初始化 TriggerInterruptInit(); // 外部触发中断初始化 ADC_Init(); // ADC采样程控模块初始化 DProc_Init(); // 数据处理模块初始化 enableInterrupts(); /* Infinite loop */ while (1) { if(flag_10ms_ok) { flag_10ms_ok = 0; keycode = KEY_Scan(); switch(GET_KTYPE(keycode)) { case KTYPE_NORMAL: KeyParse(GET_KCODE(keycode)); default: break; } } switch(SystemState) { // to do case AutoRunMode: case ManualMode: // 处理数据 if( ADCState == ADC_Buffer_Full ) { trigpos = GetTriggerPostion(0, 64); WDraw_DisplayUpdate(&ADC_Buffer[trigpos]); ADCState = Triggered; ADC_Index = 0; } break; default: break; } } }
void ENC28J60_Reset(void) { ENC28J60_SPI2_Init(); //re-init SPI2 SPI2_SetSpeed(SPI_BaudRatePrescaler_4); //9MHz TIM4_Init(1000,719);//setup a clock for ENC28J60 ENC28J60_RST_CLEAR(); //reset ENC28J60 ENC28J60_delayms(10); ENC28J60_RST_SET(); //finish reset ENC28J60_delayms(10); }
//初始化IO 串口2 //pclk1:PCLK1时钟频率(Mhz) //bound:波特率 void USART3_Init(u32 bound) { NVIC_InitTypeDef NVIC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // GPIOB时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); USART_DeInit(USART3 ); //复位串口1 //USART3_TX PB.10 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB.10 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化PB10 //USART3_RX PB.11 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入 GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化PB11 USART_InitStructure.USART_BaudRate = bound;//一般设置为9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式 USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位 USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式 USART_Init(USART3, &USART_InitStructure); //初始化串口 2 //波特率设置 // USART3->BRR=(pclk1*1000000)/(bound);// 波特率设置 //USART3->CR1|=0X200C; //1位停止,无校验位. USART_DMACmd(USART3,USART_DMAReq_Tx,ENABLE); //使能串口3的DMA发送 UART_DMA_Config(DMA1_Channel7,(u32)&USART3->DR,(u32)USART3_TX_BUF);//DMA1通道7,外设为串口2,存储器为USART3_TX_BUF USART_Cmd(USART3, ENABLE); //使能串口 #ifdef USART3_RX_EN //如果使能了接收 //使能接收中断 USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启中断 NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//抢占优先级3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器 TIM4_Init(99,7199); //10ms中断 USART3_RX_STA=0; //清零 TIM4_Set(0); //关闭定时器4 #endif }
/******************************************** 飞控主函数入口 功能: 1.初始化各个硬件 2.初始化系统参数 3.开定时器4等待数据中断到来 4.开定时器3串口广播实时姿态以及相关信息 ********************************************/ int main(void) { SystemClock(9); //系统时钟初始化 UART1_init(SysClock,115200); //串口1初始化 NVIC_INIT(); //中断初始化 STMFLASH_Unlock(); //内部flash解锁 LedInit(); //IO初始化 delay_init(SysClock); //滴答延时初始化 BT_PowerInit(); //蓝牙电源初始化完成,默认关闭 MotorInit(); //马达初始化 BatteryCheckInit(); //电池电压监测初始化 IIC_Init(); //IIC初始化 MPU6050_DMP_Initialize(); //初始化DMP引擎 PID_INIT(); //PID参数初始化 ParameterRead(); //Flash参数读取 NRF24L01_INIT(); //NRF24L01初始化 SetRX_Mode(); //设无线模块为接收模式 PowerOn(); //开机等待 BT_on(); //蓝牙开 TIM3_Init(36,2000); //定时器3初始化,调试串口输出 TIM4_Init(36,1000); //定时器4初始化,定时采样传感器数据,更新PID输出 while (1) //等待数据更新中断到来 { // switch(UART1_Get_Char())//检测串口发送数据,做相应处理 // { // case 'w': // LedA_on;LedB_on;LedC_on;LedD_on; // //TxBuf[0]++; // //UART1_Put_Package(TxBuf); // LedA_on;LedB_on;LedC_on;LedD_on; // MotoPWM+=10; // UART1_Put_Char(0x5a); // break; // case 's': // LedA_off;LedB_off;LedC_off;LedD_off; // UART1_Put_Char(0xaa); // MotoPWM-=10; // break; // default :break; // } // MotorPwmFlash(MotoPWM,MotoPWM,MotoPWM,MotoPWM); // } }
/************************************************************************************* * 函数名称:main() * 参数 :void * 返回值 :void * 描述 :程序主入口main函数 *************************************************************************************/ int main(void) { u16 i,j; u16 t=0; /*****************************初始化****************************************/ //初始化系统时钟,设置时钟为168Mhz // LED_Init(); SysTick_Init(168); TIM4_Init(); // TIM3_init();//pwm TIM5_PWM_Init(); TIM5_PWM_OUTPUT(40,20,10,50); COM6_Init(115200);//串口6初始化 bsp_InitDAC();//这样初始化adc和dac不会出问题 bsp_InitADC(); bsp_InitDAC(); TFTLCD_Init(); Touch_Init(); //EXTI_Configuration(); GUI_Init(); //GUI 初始化 GUI_Clear(); Timer2_Init(20); //初始化LED的GPIO配置 WaveTaskCreat(); while(1) { if(Wave_updat == 1) { Get_Signal(); //Clculate_Signal(); } Updata_signal(); GUI_Exec(); } }
int main(void) { SystemClock_HSI(9); //系统时钟设置,9倍频,36M LedInit(); //LED 初始化 Adc_Init(); //摇杆AD初始化 NRF24L01_Init(); //无线模块初始化 TX_Mode(); //发送模式 TIM4_Init(); //定时器初始化,定时周期为1ms,1ms发送一次摇杆AD值 KeyInit(); //按键初始化 TxBuf[30]=0xA5; //对应遥控器,该位写0xA5,保证飞机能在开机的时候能收到一个完整的32字节的数据包 GetAD(R_Mode); /*******************************************************/ //以下为遥控解锁段 /*******************************************************/ while((Throttle>5)||(Roll>5))//解锁条件:油门拉到最低,方向打到最左解锁 { GetAD(R_Mode);//得到各路AD值并显示 } TxBuf[28]=0; //对应遥控器,该字节写0,使飞机跳过写参数那一步 TxBuf[27]=0xA5;//解锁飞机 TxBuf[31]=0xA5;//解锁成功,使能飞控 /*******************************************************/ //解锁完成 /*******************************************************/ Led1=1; Led2=1; Led3=1; Led4=1; Led5=0; while(1) { GetAD(R_Mode); //得到各路AD值并显示 if(Key1==0){TxBuf[10]=0xA5;Led1=1; }//按键子程序,用于设置一键操作,如一键翻滚,一键返航等 else {TxBuf[10]=0;Led1=0;} } }
int main() { RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); SystemInit(); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(GPIOA, &GPIO_InitStruct); /*if((GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0))==1) { TIM4_Init(); PWM_Init_TIM4_GPIO(); }*/ TIM4_Init(); PWM_Init_TIM4_GPIO(); while(1); { } }
/********************************** 功能说明 *********************************** * 移植 定时器 *******************************************************************************/ void vMBM_Timers_Init( uint16_t u_sec ) { TIM4_Init( u_sec ); }
void main() { u16 tmp; u8 tmp8, tmp8_A; static tCANMsg RxMsgBuff; CLK_Init(CLK_HSE); disableInterrupts(); GPIO_Init(); TIM4_Init(); // TIM2_Init(); TIM1_Init(); ADC_Init(); /* Configure CAN - Interface */ CAN_Init(); // init CAN - interface enableInterrupts(); CAN_Start(); CAN_TxMsg1.Length = 4; CAN_TxMsg1.Xtd = false; CAN_TxMsg1.rtr = false; CAN_TxMsg1.ID = 0x280; CAN_TxMsg1.Data[0] =0 ; CAN_TxMsg1.Data[1] =0; //PWM_SetFrequency(1); do { if ( IsCAN_MSG1_Send()) { tmp = ADC_GetValue(ADC_REV_CHANEL); tmp = tmp *24; if ( tmp < 100 ) { OIL_PRESS = false; } else { OIL_PRESS = true; } CAN_TxMsg1.Data[3] =(u8)(tmp >> 8) ; CAN_TxMsg1.Data[2] =(u8)(tmp & 0x00FF); CAN_Write(&CAN_TxMsg1); } if ( IsSpeedAdjustTime()) { tmp = ADC_GetValue(ADC_SPEED_CHANEL); tmp = tmp * 10; tmp = tmp/ 34; PWM_SetFrequency(tmp); // 1023 = 300 Hz } /* if( CAN_GetMsg(&RxMsgBuff)== RET_OK){ if (RxMsgBuff.Xtd ){ // EID tmp8 = (u8)(RxMsgBuff.timeStamp >>3) & 0xE0U; if (RxMsgBuff.rtr) tmp8 |= 0x10; tmp8 |= (RxMsgBuff.Length & 0x0F); tmp8_A = (RxMsgBuff.ID>>24) & 0x001f; tmp8_A |= (RxMsgBuff.FilterID << 5); USART_SendBytesMessage (CAN_MSG_EXT_1, tmp8,(u8)((RxMsgBuff.timeStamp) & 0x00FF), tmp8_A ,(RxMsgBuff.ID>>16) & 0x00ffU); USART_SendBytesMessage (CAN_MSG_EXT_2, (RxMsgBuff.ID>>8) & 0x00ffU,RxMsgBuff.ID & 0x00ffU,RxMsgBuff.Data[0], RxMsgBuff.Data[1] ); if (RxMsgBuff.Length > 2) USART_SendBytesMessage (CAN_MSG_EXT_3, RxMsgBuff.Data[2], RxMsgBuff.Data[3], RxMsgBuff.Data[4], RxMsgBuff.Data[5]); if (RxMsgBuff.Length > 6 ) USART_SendBytesMessage (CAN_MSG_EXT_4, RxMsgBuff.Data[6], RxMsgBuff.Data[7],0,0); }else{ // standard ID tmp8 = (u8)(RxMsgBuff.timeStamp >>3) & 0xE0U; if (RxMsgBuff.rtr) tmp8 |= 0x10; tmp8 |= (RxMsgBuff.Length & 0x0F); tmp8_A = (RxMsgBuff.ID>>8) & 0x007f; tmp8_A |= (RxMsgBuff.FilterID << 3); USART_SendBytesMessage (CAN_MSG_STD_1, tmp8,(u8)((RxMsgBuff.timeStamp) & 0x00FF), tmp8_A ,RxMsgBuff.ID & 0x00ff); if (RxMsgBuff.Length > 0) USART_SendBytesMessage (CAN_MSG_STD_2, RxMsgBuff.Data[0], RxMsgBuff.Data[1], RxMsgBuff.Data[2], RxMsgBuff.Data[3]); if (RxMsgBuff.Length > 4 ) USART_SendBytesMessage (CAN_MSG_STD_3, RxMsgBuff.Data[4], RxMsgBuff.Data[5], RxMsgBuff.Data[6], RxMsgBuff.Data[7]); } }*/ /** LED Flashing **/ if (GetLedState()) { LED_ON; } else { LED_OFF; } } while (1); }
/****************************************************************************** * Function name : main * Description : Main testing loop * Input param : None * Return : None * See also : None *******************************************************************************/ void main (void) { /* peripheral initialization */ #ifdef FAST_I2C_MODE CLK->CKDIVR = 0x00; // sys clock / 1 #else CLK->CKDIVR = 0x01; // sys clock / 2 #endif // Set GPIO for LED uses GPIOH->DDR |= 0x0F; GPIOH->CR1 |= 0x0F; // initialize timer 4 mandatory for timout and tick measurement TIM4_Init(); // Initialize I2C for communication I2C_Init(); // initialization of dummy field for test purpose memcpy(Dummy, DUMMY_INIT, MAX_DUMMY); #ifndef _COSMIC_ err_save= 0; TIM4_tout= loop_count= 0; #endif // Enable all interrupts enableInterrupts(); /* main test loop */ while(1) { // switch on LED1 at the beginning of test switch_on(LED1); // write 1 data bytes with offset 8 from Dummy filed to slave memory set_tout_ms(10); I2C_WriteRegister(8, 1, &Dummy[8]); // read 1 byte with offset 8 back from the image at slave memory if(tout()) { set_tout_ms(10); I2C_ReadRegister(8, 1, &Dummy[8]); } // write 6 bytes with offset 2 from Dummy filed to slave memory if(tout()) { set_tout_ms(10); I2C_WriteRegister(2, 6, &Dummy[2]); } // read 6 bytes with offset 2 back from the image at slave memory if(tout()) { set_tout_ms(10); I2C_ReadRegister(2, 6, &Dummy[2]); } // write 1 byte with offset 9 from Dummy filed to slave memory if(tout()) { set_tout_ms(10); I2C_WriteRegister(9, 1, &Dummy[9]); } // read 1 byte with offset 9 back from the image at slave memory if(tout()) { set_tout_ms(10); I2C_ReadRegister(9, 1, &Dummy[9]); } // write 2 bytes with offset 0 from Dummy filed to slave memory if(tout()) { set_tout_ms(10); I2C_WriteRegister(0, 2, &Dummy[0]); } // read 2 bytes with offset 0 back from the image at slave memory if(tout()) { set_tout_ms(10); I2C_ReadRegister(0, 2, &Dummy[0]); } // if a timout error occures switch on LED2 if(!tout()) switch_on(LED2); // switch off LED1 at the end of test switch_off(LED1); // check if dummy field is not corrupted => switch on LED 4 if test not successful if(memcmp(Dummy, DUMMY_INIT, MAX_DUMMY) != 0) switch_on(LED4); delay(1); } }
int main(void) { static char ledsta; /***********************************/ SystemClock_HSI(9); //系统时钟初始化,时钟源内部HSI cycleCounterInit(); // Init cycle counter SysTick_Config(SystemCoreClock / 1000); //SysTick开启系统tick定时器并初始化其中断,1ms UART1_init(SysClock,uart1baudSet); //串口1初始化 NVIC_INIT(); //中断初始化 STMFLASH_Unlock(); //内部flash解锁 LoadParamsFromEEPROM(); //加载系统参数配置表 LedInit(); //IO初始化 Adc_Init(); //摇杆AD初始化 KeyInit(); //按键初始化 NRF24L01_INIT(); //NRF24L01初始化 SetTX_Mode(); //设无线模块为接收模式 controlClibra(); //遥控摇杆校准 #ifdef UART_DEBUG TIM3_Init(SysClock,2000); //定时器初始化,1s为周期打印摇杆值 #endif TIM4_Init(SysClock,TIME4_Preiod); //定时器4初始化,定时时间单位:(TIME4_Preiod)微秒 LedSet(led2,1); LedSet(led3,1); LoadRCdata(); //摇杆赋值 //RockerUnlockcrazepony(); //摆杆启动 Lockflag = 0; //解锁标志,1表示产生了一次按键操作,0表示该按键操作已经发送到飞控 LedSet(led2,0); LedSet(led3,0); while (1) { //10Hz loop if(flag10Hz == 1) //10Hz { flag10Hz = 0; /*status led*/ ledsta = !ledsta; LedSet(signalLED,ledsta); /*crazepony Lock*/ KeyLockcrazepony(); /*IMUcalibrate */ IMUcalibrate(); /*remote calibrate*/ Remotecalibrate(); } //50Hz loop if(flag50Hz == 1) { LoadRCdata(); flag50Hz = 0; } // 80Hz 12.5ms if(flag80Hz) { flag80Hz = 0; CommUAVUpload(MSP_SET_4CON); } } }
void main() { u16 tmp; u8 tmp8, tmp8_A,i; bool result; //RC => fcp //CLK_Init(CLK_HSI); // internal RC as a clock source CLK->CKDIVR = 0; disableInterrupts(); GPIO_Init(); TIM4_Init(); enableInterrupts(); OK = 0; FAIL = 0; do { if (START == 0) { //nastavim 1 vystup do 0 OUT1_ODR = OUT_group1_BASE; OUT2_ODR = OUT_group2_BASE; SetTimeoutTime(10); while (!IsTimeoutExpired()); // test zkratu na VCC - na zadnem vstupu nesmi byt 1 if ((IN1_IDR & IN_group1_BASE ) || (IN2_IDR & IN_group2_BASE )) { FAIL = 1;// - neni klidovy stav OK = 1; } else { result = true; for (i = 0; i < TEST_PAIRS; ++i) { SetTimeoutTime(10); while (!IsTimeoutExpired()); result &= TestStep(i); } if (result) { OK = 1; FAIL = 0; } else { OK = 0; FAIL = 1; } } //SetTimeoutTime(7000); //while (!IsTimeoutExpired()); while (!START); OK = 0; FAIL = 0; SetTimeoutTime(2000); while (!IsTimeoutExpired()); } /** LED Flashing **/ if (GetLedState()) { LED_ON; //CAN_Write(&CAN_TxMsg); } else { LED_OFF; } } while (1); }
/* *berif: Initialize the encoding module *param: None *reval: None */ void Init_Encoder(void) { TIM2_Init(); //Timer TIM3_Init(); //Counter(R) TIM4_Init(); //Counter(L) }