39
414
1524
助理工程师
使用特权
static void Bsp\_UART1\_GPIO\_Init(void) { GPIO\_InitTypeDef GPIO\_InitStruct; //Enable GPIOA Clock RCC\_AHBPeriphClockCmd(RCC\_AHBENR\_GPIOA, ENABLE); GPIO\_PinAFConfig(GPIOA, GPIO\_PinSource9, GPIO\_AF\_1); GPIO\_PinAFConfig(GPIOA, GPIO\_PinSource10, GPIO\_AF\_1); //PA9:UART1\_TX GPIO\_StructInit(&GPIO\_InitStruct); GPIO\_InitStruct.GPIO\_Pin = GPIO\_Pin\_9; GPIO\_InitStruct.GPIO\_Speed = GPIO\_Speed\_50MHz; GPIO\_InitStruct.GPIO\_Mode = GPIO\_Mode\_AF\_PP; GPIO\_Init(GPIOA, &GPIO\_InitStruct); //PA10:UART1\_RX GPIO\_InitStruct.GPIO\_Pin = GPIO\_Pin\_10; GPIO\_InitStruct.GPIO\_Mode = GPIO\_Mode\_IPU; GPIO\_Init(GPIOA, &GPIO\_InitStruct); }
void Bsp\_UART1\_NVIC\_Init(u32 baudrate) { UART\_InitTypeDef UART\_InitStruct; NVIC\_InitTypeDef NVIC\_InitStruct; //Enable UART1 Clock RCC\_APB2PeriphClockCmd(RCC\_APB2ENR\_UART1, ENABLE); Bsp\_UART1\_GPIO\_Init(); UART\_StructInit(&UART\_InitStruct); UART\_InitStruct.BaudRate = baudrate; //The word length is in 8-bit data format. UART\_InitStruct.WordLength = UART\_WordLength\_8b; UART\_InitStruct.StopBits = UART\_StopBits\_1; //No even check bit. UART\_InitStruct.Parity = UART\_Parity\_No; //No hardware data flow control. UART\_InitStruct.HWFlowControl = UART\_HWFlowControl\_None; UART\_InitStruct.Mode = UART\_Mode\_Rx | UART\_Mode\_Tx; UART\_Init(UART1, &UART\_InitStruct); UART\_ITConfig(UART1,UART\_IT\_RXIEN, ENABLE); //UART1 NVIC NVIC\_InitStruct.NVIC\_IRQChannel = UART1\_IRQn; NVIC\_InitStruct.NVIC\_IRQChannelPriority = 0; NVIC\_InitStruct.NVIC\_IRQChannelCmd = ENABLE; NVIC\_Init(&NVIC\_InitStruct); UART\_Cmd(UART1, ENABLE); }
//UART1 Tx Buffer u8 gUART1\_Tx\_Buf[UART1\_TXD\_LEN] = {0x00}; //UART1 Tx Lenth u8 gUART1\_Tx\_Lenth = 0; //UART1 Tx Count u8 gUART1\_Tx\_Real\_Cnt = 0; //UART1 Tx OK Flag u8 gUART1\_Tx\_OK\_Flag = 0;
void Bsp\_UART1\_Interrupt\_Send\_Data(UART\_TypeDef* Uart, u8 *pBuf,u8 data\_lenth) { gUART1\_Tx\_Real\_Cnt = 0; gUART1\_Tx\_Lenth = data\_lenth; memcpy(gUART1\_Tx\_Buf,pBuf,data\_lenth); UART\_ITConfig(Uart, UART\_IT\_TXIEN, ENABLE ); }
void UART1\_IRQHandler(void) { u8 Recv; if(UART\_GetITStatus(UART1, UART\_IT\_RXIEN) != RESET) { UART\_ClearITPendingBit(UART1, UART\_IT\_RXIEN); Recv = UART\_ReceiveData(UART1); gUART1\_Rx\_Buf[gUART1\_Rx\_Cnt] = Recv; if(gUART1\_Rx\_Cnt < UART1\_RXD\_LEN-1) { gUART1\_Rx\_Cnt++; } else { gUART1\_Rx\_Cnt = 0; } if(gUART1\_Rx\_Cnt == 8) { UART1\_Rx\_Flag = true; } } if(UART\_GetITStatus(UART1, UART\_IT\_TXIEN) != RESET) { UART\_ClearITPendingBit(UART1, UART\_IT\_TXIEN); if(gUART1\_Tx\_Real\_Cnt < gUART1\_Tx\_Lenth) { UART\_SendData(UART1,(u8)gUART1\_Tx\_Buf[gUART1\_Tx\_Real\_Cnt]); gUART1\_Tx\_Real\_Cnt++; } else { UART\_ITConfig(UART1, UART\_IT\_TXIEN, DISABLE); gUART1\_Tx\_OK\_Flag = 1; } } }
void Bsp\_UART1\_Recv\_Task(void) { if(UART1\_Rx\_Flag == true) { UART1\_Rx\_Flag = false; if((gUART1\_Rx\_Buf[0] == 0xAA) && (gUART1\_Rx\_Buf[1] == 0x01) && (gUART1\_Rx\_Buf[2] == 0x02) && (gUART1\_Rx\_Buf[3] == 0x03) && \ (gUART1\_Rx\_Buf[4] == 0x04) && (gUART1\_Rx\_Buf[5] == 0x05) && (gUART1\_Rx\_Buf[6] == 0x06) && (gUART1\_Rx\_Buf[7] == 0x55)) { Bsp\_UART1\_Interrupt\_Send\_Data(UART1,gUART1\_Rx\_Buf,gUART1\_Rx\_Cnt); } gUART1\_Rx\_Cnt = 0; memset(gUART1\_Rx\_Buf,0,sizeof(gUART1\_Rx\_Buf)); } }
int main(void) { //UART1 NVIC Init Baudrate:115200 Bsp\_UART1\_NVIC\_Init(UART1\_BAUDRATE); while(1) { //Test UART1 RX TX Bsp\_UART1\_Recv\_Task(); } }
8032862e6992c2cd80.png (143.67 KB )
下载附件
2022-7-31 23:01 上传
发表回复 本版积分规则 回帖后跳转到最后一页
0
扫码关注 21ic 官方微信
扫码关注嵌入式微处理器
扫码关注21ic项目外包
扫码关注21ic视频号
扫码关注21ic抖音号
本站介绍 | 申请友情链接 | 欢迎投稿 | 隐私声明 | 广告业务 | 网站地图 | 联系我们 | 诚聘英才 | 论坛帮助
京公网安备 11010802024343号