void u1sendtest(int num) { static uint8_t test[30]="0000 abcdefg1234567890\r\n"; static uint32_t lp; static uint32_t ini = 0; if( ini == 0 ) { ini = 1; UART1_init(115200); } while(num-- > 0) { test[0] = (num/1000)%10 + 48; test[1] = (num/100)%10 + 48; test[2] = (num/10)%10 + 48; test[3] = num%10 + 48; lp = 0; while( Cycbuf_WriteFrame(&U1SendBuf,test,24) < 0 ) { if(lp++ > 100 ) { lp = 0; rt_thread_delay(10); } }; } }
int main(void) { Stm32_Clock_Init(9); // u16 temp; NVIC_Configuration(); UART1_init(72,115200); delay_init(); //初始化串口1; pid_Init(); LED_Init(); EXTIX_Init(); Adc_Init(); Motor_Init(999,0); HC05_Init(); while(mpu_dmp_init()); // TIM5_Init(999,72); TIM2_Int_Init(999,72);//定时器3配置,1ms中断一次 while(1) { // CtrlAttiAng(); // PWM_test(); // COMMPC_TEST(); // Get_angle(); // u2_printf("hell"); test_SendPC();//测试向PC发送协议数据,用于调控 } }
/******************************************** 飞控主函数入口 功能: 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); //等待数据更新中断到来 }
// --------------------------------------------------------------------------------- // @brief : Common routine for initialising UART // @param : uartNum is (USART# - 1). so USART1 -> 0 // : baudrate // @retval: none void UART_init(int16_t uartNum, int baudrate){ switch(uartNum){ case 0:UART1_init(baudrate); break; //case 1:UART2_init(baudrate); break; case 2:UART3_init(baudrate); break; case 3:UART4_init(baudrate); break; case 4:UART5_init(baudrate); break; } }
/******************************************** 写蓝牙参数函数 (Write Bluetooth function parameters) ********************************************/ void BT_ATcmdWrite(void) { static u32 BT_CurBaud; BT_CurBaud = BT_CurBaud_Get(); if((BT_CurBaud == BT_BAUD_Set)) BTstate = BThavewrote;//检测到蓝牙当前的波特率和设定值不同,就写入设定值 //Bluetooth current baud rate detection and setting values are different, the set value is written else BTstate = BTneedwrite; if(BTstate == BTneedwrite) { LedA_off;LedB_off;LedC_off;LedD_off; UART1_init(SysClock,BT_CurBaud);//以当前波特率重新初始化串口 //In the current re-initialize the serial port baud rate /*开始配置蓝牙设备名,pin码,波特率*/ //Begin configuring the Bluetooth device name, pin code, baud rate Uart1SendaBTCmd(ATcmdAsk); //printf("\r\n与蓝牙通信中...\r\n"); //Bluetooth Communication if(CmdJudgement(ATcmdAnswer) == true)//有蓝牙返回,才往下写指令 //Bluetooth returned, only to write down instructions { Uart1SendaBTCmd(ATcmdNameAsk); if(CmdJudgement(ATcmdNameAnswer) == false) {Uart1SendaBTCmd(ATcmdNameSet);LedA_off;LedB_on;LedC_off;LedD_on; } else ; Uart1SendaBTCmd(ATcmdCodeAsk); if(CmdJudgement(ATcmdCodeAnswer) == false) {Uart1SendaBTCmd(ATcmdCodeSet); LedA_on;LedB_off;LedC_on;LedD_off; } else ; Uart1SendaBTCmd(ATcmdBaudAsk); if(CmdJudgement(ATcmdBaudAnswer) == false) { Uart1SendaBTCmd(ATcmdBaudSet); LedA_off;LedB_on;LedC_off;LedD_on; BTstate = BThavewrote; SaveParamsToEEPROM(); LedA_on;LedB_on;LedC_on;LedD_on; delay_ms(1000); LedA_off;LedB_off;LedC_off;LedD_off; }//最后修改波特率,并写入EEPROM //Last Modified baud rate, and write EEPROM else BTstate = BTneedwrite; } else {BTstate = BTneedwrite; printf("\r\n与蓝牙通信失败\r\n");} //Bluetooth communication and failure } else ; UART1_init(SysClock,BT_BAUD_Set); }
/************************************************************************* * Function Name: MCU_init * Parameters: none * Return: none * Description: initialization of peripheral modules after reset *************************************************************************/ void MCU_init(void) { Clock_init(); ADC0_init(); PDB_init(); FTM0_init(); FTM1_init(); FTM2_init(); SPI0_init(); UART1_init(); GPIO_init(); MC33927_config(); }
int main() { UART1_init(); uart_puts("hello stm32!\r\n"); while(1) { // loop back test uart_putc( uart_getc() ); } return 0; }
u8 Uart1_PutChar(u8 ch) { static uint32_t ini = 0; if( ini == 0 ) { ini = 1; UART1_init(115200); } USART_SendData(USART1, (u8) ch); while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET) { } return ch; }
void COM_restart() { #if (defined __18F8722_H) ||(defined __18F46K22_H) UART1_init(); #else UART_init(); //initialize uart #endif communication.rxPacketIndex = 0; communication.txPacketLength = 0; communication.state = COM_START; communication.txCode = IGNORE; communication.timeout = TIMEOUT; communication.prevAppTime = communication.curAppTime; communication.prevState = communication.state; }
/******************************************** 飞控主函数入口 功能: 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); // } }
void main() { UART1_init(9600); Delay_ms(100); //configurações gerais de interrupção RCON.IPEN = 1; INTCON.GIEH = 1; INTCON.GIEL = 1; //configuração de interrupção de recepção da USART IPR1.RCIP = 1; //alta prioridade PIR1.RCIF = 0; //flag de sinalização PIE1.RCIE = 1; //Enable da interrupção Serial RX while(1) { } }
//轮询蓝牙模块所有可能的波特率,获取当前波特率 //并且配置其波特率为115200, u32 BT_Scan_Buad(void) { //蓝牙波特率率表,将9600(默认波特率)和230400(hm-06遗留bug)放到最前 //115200(将要配置的波特率)放到最后 static u32 bandsel[9] = {230400,9600,1200,2400,4800,19200,38400,57600,115200}; u8 i; for(i=0; i<9; i++) { UART1_init(SysClock,bandsel[i]); Uart1SendaBTCmd(ATcmdAsk); if(CmdJudgement(ATcmdAnswer) == true) { Q_printf("BT Scan %d\r\n", bandsel[i]); return bandsel[i]; } } return 0; }
void control_unit_init(void) { DISPLAY_LED_PORT &=~(1<<DISPLAY_LED_PIN); // светодиод зажигается нулем DISPLAY_LED_DDR &=~(1<<DISPLAY_LED_PIN); // выход в Z - пока светодиод не горит DISPLAY_LE_PORT &=~(1<<DISPLAY_LE_PIN); //пин LE - прием данных DISPLAY_LE_DDR |=(1<<DISPLAY_LE_PIN); // LE - выход DISPLAY_OE_PORT &=~(1<<DISPLAY_OE_PIN); //OE- инверсный включается нулем DISPLAY_OE_DDR &=~(1<<DISPLAY_OE_PIN); // OE в Z-состояние. Там подтяг сам выключит индикатор // DISPLAY_CLK_PORT &=~(1<<DISPLAY_CLK_PIN); //пока не имеет значения DISPLAY_CLK_DDR |=(1<<DISPLAY_CLK_PIN); // CLK - выход UART1_init(); Dig_init(); }
u32 BT_CurBaud_Get(void) { static u32 bandsel[8] = {1200,2400,4800,9600,19200,38400,57600,115200};//蓝牙波特率率表 u8 i; BT_on(); //开蓝牙 //Open Bluetooth delay_ms(500); //等待蓝牙稳定 //Wait Bluetooth stable /**确定当前蓝牙波特率**/ //Bluetooth determine the current baud rate for(i=0;i<8;i++) { UART1_init(SysClock,bandsel[i]); Uart1SendaBTCmd(ATcmdAsk); if(CmdJudgement(ATcmdAnswer) == true) { //printf("\r\nHM-06 baud -->%d\r\n",bandsel[i]); break;//得到当前波特率为Bandsel[i] //Get the current baud rate Bandsel[i] } } return bandsel[i]; }
void COM1_restart() { UINT8 i; #if (defined __18F8722_H) ||(defined __18F46K22_H) UART1_init(UART1_BAUD); #ifdef UART2_ACTIVE UART2_init(UART2_BAUD); #endif #else UART_init(); //initialize uart #endif for( i = 0 ; i < ACTIVE_USARTS ; i++) { communication[i].rxPacketIndex = 0; communication[i].txPacketLength = 0; communication[i].state = COM_START; communication[i].txCode = IGNORE; communication[i].timeout = TIMEOUT; communication[i].prevAppTime = communication[i].curAppTime; communication[i].prevState = communication[i].state; } }
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 LCDPANELProcess_thread_entry(void* parameter) { // static uint8_t Cmd; static uint32_t rxlen; UART1_init(9600); /* Release 1 second cpu time solt for other module init function */ rt_thread_delay(100); //rt_kprintf("[CO]UART1 band=9600 init.\n"); MessageTX[12]=0X00; MessageTX[13]=0X3B; while(1) { MessageTX[7]=0X01; rt_thread_delay(10); rxlen = Sbuf_read(&U1RevBuf,QueryLCDRxBuf,20); if( rxlen == 10 ) { // if(((QueryLCDRxBuf[1]==0x01))&&(StartupMark==1))//LCD启动,而且车启动时 if(QueryLCDRxBuf[1]==0x01)//LCD启动,而且车启动时 { // delay(10); if((QueryLCDRxBuf[2]==0x01)|(GSM_flag==1))//LCD手动模式开,且GSM有信号时 { MD=0; //开风机 MessageTX[11]=0x01;//风机启动状态 //delay(100); fwsRlyWindOpen(RLY_WIND_MD_FLAG,FELAY_DELAY_FOREVER); //FwsRlySet(SW1,RLY_OFF,0); } else if((QueryLCDRxBuf[2]==0x00)&(GSM_flag==0))//LCD手动模式关,且GSM有信号时 { MD=1;//关风机 //MessageTX[11]=0x00; fwsRlyWindClose(RLY_WIND_MD_FLAG); } } if((QueryLCDRxBuf[1]==0x00)|(StartupMark==0))//LCD关闭,而且车熄火 { MD=1; //关风机 //MessageTX[11]=0x00; if(StartupMark==0) fwsRlyWindClose(RLY_WIND_ALLOFF_FLAG); else fwsRlyWindClose(RLY_WIND_MD_FLAG); } /* if(MD==0) { // WD_NUM=0X01; MESSAGE_SEND[8]=0x01; } if(MD==1) { MESSAGE_SEND[8]=0x00; } if( MESSAGE_SEND[8]!=0x01) { MESSAGE_SEND[8]=0x00; } */ if(QueryLCDRxBuf[7]==0x01)//自动模式时 { MessageTX[12]=QueryLCDRxBuf[5]; MessageTX[13]=QueryLCDRxBuf[6]; } if(TOLCDF) { Cycbuf_WriteFrame(&U1SendBuf,MessageTX,SENDTO_LCD_LEN); //TOLCDF=0; rt_thread_delay(50); } } if(!TOLCDF) // 第一次发送 { Cycbuf_WriteFrame(&U1SendBuf,MessageTX,SENDTO_LCD_LEN); TOLCDF=1; rt_thread_delay(80); } } }
/******************************************** 写蓝牙参数函数 ********************************************/ void BT_ATcmdWrite(void) { u8 i; static u32 BT_CurBaud; static u32 bandsel[9] = {230400,9600,1200,2400,4800,19200,38400,57600,115200}; Uart1SendaBTCmd(ATcmdAsk); if(CmdJudgement(ATcmdAnswer) == true) { //HM-11模块已经是115200波特率,无需配置 return ; } Q_printf("BT baund check and init begin. Q_printf is useless.\r\n\r\n"); BT_CurBaud = BT_Scan_Buad(); if (BT_CurBaud == 0) { Q_printf("BT scan buad failed!\r\n"); goto quit; } //首先检测蓝牙模块的串口是否已经配置为115200 if(BT_CurBaud != BT_BAUD_Set) { //蓝牙模块需要修改其名字,波特率 //修改蓝牙的名字为Crazepony Uart1SendaBTCmd(ATcmdNameSet); //修改蓝牙波特率为115200 Uart1SendaBTCmd(ATcmdBaudSet); //LED闪烁表示原来蓝牙模块是哪个波特率 for(i=0; i<9; i++) { LedA_on; LedB_on; LedC_on; LedD_on; delay_ms(1000); LedA_off; LedB_off; LedC_off; LedD_off; delay_ms(1000); if(BT_CurBaud == bandsel[i]) { break; } } } else { //已经是115200,可以直接通信 Q_printf("BT module baud is 115200 okay\r\n"); } quit: //最终STM32的UART波特率设置回115200 UART1_init(SysClock, BT_BAUD_Set); Q_printf("\r\nBT baund check and init end.\r\n"); }
void LCDPANELProcess_thread_entry(void* parameter) { static uint32_t rxlen; uint8_t i; uint8_t *p; UART1_init(9600); rt_thread_delay(50); //* Release 1 second cpu time solt for other module init function */ IWDG_ReloadCounter(); rt_thread_delay(50); // MessageTX[12]=0X00; // MessageTX[13]=0X3B; while(1) { IWDG_ReloadCounter(); MessageTX[12] = 0x00; MessageTX[13] = 0x00; MessageTX[7]=0X01; rt_thread_delay(40); memset(QueryLCDRxBuf, 0, QUERY_RX_LEN); rxlen = Sbuf_read(&U1RevBuf,QueryLCDRxBuf,25); if( rxlen >= 10 ) { p = memchr(QueryLCDRxBuf, 0xaa, rxlen); if(( p != NULL ) && (*(p+9) == 0xcc)) { memmove(QueryLCDRxBuf, p, 10); if((QueryLCDRxBuf[1]==0x01)&&(StartupMark==1))//LCD启动,而且车启动时 { if((QueryLCDRxBuf[2]==0x01)|(GSM_flag==1))//LCD手动模式开,且GSM有信号时 { MessageTX[12] = rxlen; MessageTX[13] = 0x03; MD=0; //开风机 MessageTX[11]=0x01;//风机启动状态 fwsRlyWindOpen(RLY_WIND_MD_FLAG,FELAY_DELAY_FOREVER); } else if((QueryLCDRxBuf[2]==0x00)&(GSM_flag==0))//LCD手动模式关,且GSM有信号时 { MessageTX[12] = rxlen; MessageTX[13] = 0x04; MD=1;//关风机 fwsRlyWindClose(RLY_WIND_MD_FLAG); } else { MessageTX[12] = rxlen; MessageTX[13] = 0x02; } } else if((QueryLCDRxBuf[1]==0x00)||(StartupMark==0))//LCD关闭,而且车熄火 { MessageTX[12] = StartupMark; MessageTX[13] = 0x05; MD=1; //关风机 if(StartupMark==0) fwsRlyWindClose(RLY_WIND_ALLOFF_FLAG); else fwsRlyWindClose(RLY_WIND_MD_FLAG); } else { for(i=0; i<10; i++) { MessageTX[i+1] = QueryLCDRxBuf[i]; } MessageTX[12] = rxlen; MessageTX[13] = 0x06; } // if(QueryLCDRxBuf[7]==0x01)//自动模式时 // { // MessageTX[12]=QueryLCDRxBuf[5]; // MessageTX[13]=QueryLCDRxBuf[6]; // } Cycbuf_WriteFrame(&U1SendBuf,MessageTX,SENDTO_LCD_LEN); } else { for(i=0; i<10; i++) { MessageTX[i+1] = QueryLCDRxBuf[i]; } MessageTX[12] = rxlen; MessageTX[13] = 0x07; Cycbuf_WriteFrame(&U1SendBuf,MessageTX,SENDTO_LCD_LEN); } } else { MessageTX[12] = rxlen; MessageTX[13] = 0x08; Cycbuf_WriteFrame(&U1SendBuf,MessageTX,SENDTO_LCD_LEN); } // if(( rxlen >= 10 ) && (QueryLCDRxBuf[0] == 0xaa) && (QueryLCDRxBuf[9] == 0xcc)) // { // MessageTX[13] = 0x01; // if((QueryLCDRxBuf[1]==0x01)&&(StartupMark==1))//LCD启动,而且车启动时 // { // MessageTX[13] = 0x02; // if((QueryLCDRxBuf[2]==0x01)|(GSM_flag==1))//LCD手动模式开,且GSM有信号时 // { // MessageTX[12] = rxlen; // MessageTX[13] = 0x03; // MD=0; //开风机 // MessageTX[11]=0x01;//风机启动状态 // fwsRlyWindOpen(RLY_WIND_MD_FLAG,FELAY_DELAY_FOREVER); // } // else if((QueryLCDRxBuf[2]==0x00)&(GSM_flag==0))//LCD手动模式关,且GSM有信号时 // { // MessageTX[12] = GSM_flag; // MessageTX[13] = 0x04; // MD=1;//关风机 // fwsRlyWindClose(RLY_WIND_MD_FLAG); // // } // } // else if((QueryLCDRxBuf[1]==0x00)||(StartupMark==0))//LCD关闭,而且车熄火 // { // MessageTX[12] = StartupMark; // MessageTX[13] = 0x05; // MD=1; //关风机 // if(StartupMark==0) // fwsRlyWindClose(RLY_WIND_ALLOFF_FLAG); // else // fwsRlyWindClose(RLY_WIND_MD_FLAG); // } // // if(QueryLCDRxBuf[7]==0x01)//自动模式时 // { //// MessageTX[12]=QueryLCDRxBuf[5]; //// MessageTX[13]=QueryLCDRxBuf[6]; // } // // Cycbuf_WriteFrame(&U1SendBuf,MessageTX,SENDTO_LCD_LEN); // } // else if(QueryLCDRxBuf[0] != 0xaa) // { // for(i=0; i<10; i++) // { // MessageTX[i+1] = QueryLCDRxBuf[i]; // } // MessageTX[12] = rxlen; // MessageTX[13] = 0x06; // Cycbuf_WriteFrame(&U1SendBuf,MessageTX,SENDTO_LCD_LEN); // } // else if(QueryLCDRxBuf[9] != 0xcc) // { // for(i=0; i<10; i++) // { // MessageTX[i+1] = QueryLCDRxBuf[i]; // } // MessageTX[12] = rxlen; // MessageTX[13] = 0x07; // Cycbuf_WriteFrame(&U1SendBuf,MessageTX,SENDTO_LCD_LEN); // } // else // { // MessageTX[12] = rxlen; // MessageTX[13] = 0x08; // Cycbuf_WriteFrame(&U1SendBuf,MessageTX,SENDTO_LCD_LEN); // } } }