本帖最后由 逢dududu必shu 于 2021-8-5 22:01 编辑
附上源码
这是USART.C
004 | u8 USART2_RX_BUF[USART_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节. |
005 | u8 USART3_RX_BUF[USART_REC_LEN]; |
006 | u8 UART4_RX_BUF[USART_REC_LEN]; |
007 | u8 UART5_RX_BUF[USART_REC_LEN]; |
018 | u16 USART2_RX_STA=0; //接收状态标记//接收状态标记 |
020 | u16 UART4_RX_STA=0; //接收状态标记 |
025 | void uart_init(u32 bound){ |
027 | GPIO_InitTypeDef GPIO_InitStructure; |
028 | USART_InitTypeDef USART_InitStructure; |
029 | NVIC_InitTypeDef NVIC_InitStructure; |
031 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE); //使能USART1,GPIOA时钟 |
032 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE); |
038 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB1Periph_USART3, ENABLE); |
039 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4|RCC_APB1Periph_UART5, ENABLE); |
043 | USART_DeInit(USART1); //复位串口1`5 |
044 | USART_DeInit(USART2); |
045 | USART_DeInit(USART3); |
055 | //USART1_TX USART2_TX PA.9 PA.2 |
056 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_9; //PA.9与PA.2 |
057 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
058 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 |
059 | GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA9 |
061 | //USART1_RX USART2_RX PA.10 PA.3 |
062 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_10; |
063 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入 |
064 | GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA10 |
069 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB.10 |
070 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
071 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 |
072 | GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化PA9 |
075 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; |
076 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入 |
077 | GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化PA10 |
079 | //UART4_TX UART5_TX PC10 PC12 |
080 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_12; //PC10 PC12 |
081 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
082 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 |
083 | GPIO_Init(GPIOC, &GPIO_InitStructure); //初始化PC10 PC12 |
087 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; |
088 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入 |
089 | GPIO_Init(GPIOC, &GPIO_InitStructure); //初始化PC11 |
092 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; |
093 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入 |
094 | GPIO_Init(GPIOD, &GPIO_InitStructure); //初始化 PD2 |
102 | USART_InitStructure.USART_BaudRate = bound;//一般设置为9600; |
103 | USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式 |
104 | USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位 |
105 | USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位 |
106 | USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制 |
107 | USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式 |
109 | USART_Init(USART1, &USART_InitStructure); //初始化串口 |
113 | USART_InitStructure.USART_BaudRate = bound;//一般设置为9600; |
114 | USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式 |
115 | USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位 |
116 | USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位 |
117 | USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制 |
118 | USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式 |
120 | USART_Init(USART2, &USART_InitStructure); //初始化串口 |
126 | USART_InitStructure.USART_BaudRate = bound;//一般设置为9600; |
127 | USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式 |
128 | USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位 |
129 | USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位 |
130 | USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制 |
131 | USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式 |
133 | USART_Init(USART3, &USART_InitStructure); //初始化串口 |
139 | USART_InitStructure.USART_BaudRate = bound;//一般设置为9600; |
140 | USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式 |
141 | USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位 |
142 | USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位 |
143 | USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制 |
144 | USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式 |
146 | USART_Init(UART4, &USART_InitStructure); //初始化串口 |
152 | USART_InitStructure.USART_BaudRate = bound;//一般设置为9600; |
153 | USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式 |
154 | USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位 |
155 | USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位 |
156 | USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制 |
157 | USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式 |
159 | USART_Init(UART5, &USART_InitStructure); //初始化串口 |
172 | NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; |
173 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3 |
174 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3 |
175 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 |
176 | NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器 |
178 | USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启中断 |
181 | NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; |
182 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3 |
183 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3 |
184 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 |
185 | NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器 |
187 | USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启中断 |
191 | NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; |
192 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3 |
193 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3 |
194 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 |
195 | NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器 |
197 | USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);//开启中断 |
200 | NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn; |
201 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3 |
202 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3 |
203 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 |
204 | NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器 |
206 | USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);//开启中断 |
213 | USART_Cmd(USART1, ENABLE); //使能串口 |
214 | /* USART_Cmd(USART2, ENABLE); |
215 | USART_Cmd(USART3, ENABLE); |
216 | USART_Cmd(UART4, ENABLE); |
217 | USART_Cmd(UART5, ENABLE); */ |
223 | void USART2_IRQHandler(void) //串口2中断服务程序 |
228 | if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) // |
230 | Res2=USART_ReceiveData(USART2);//(USART2->DR); //读取接收到的数据 |
232 | USART2_RX_BUF[USART2_RX_STA]=Res2; |
240 | i=USART2_RX_BUF[0]+USART2_RX_BUF[1]+USART2_RX_BUF[2]+USART2_RX_BUF[3]; |
242 | if(USART2_RX_BUF[0]==0XFF&&USART2_RX_BUF[1]==0X81&&i==USART2_RX_BUF[4]) |
260 | void USART3_IRQHandler(void) //串口3中断服务程序 |
265 | if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) // |
267 | Res3=USART_ReceiveData(USART3);//(USART2->DR); //读取接收到的数据 |
269 | USART3_RX_BUF[USART3_RX_STA]=Res3; |
277 | i=USART3_RX_BUF[0]+USART3_RX_BUF[1]+USART3_RX_BUF[2]+USART3_RX_BUF[3]; |
279 | if(USART3_RX_BUF[0]==0XFF&&USART3_RX_BUF[1]==0X81&&i==USART3_RX_BUF[4]) |
298 | void UART4_IRQHandler(void) //串口4中断服务程序 |
303 | if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET) // |
305 | Res4=USART_ReceiveData(UART4);//读取接收到的数据 |
307 | UART4_RX_BUF[UART4_RX_STA]=Res4; |
315 | i=UART4_RX_BUF[0]+UART4_RX_BUF[1]+UART4_RX_BUF[2]+UART4_RX_BUF[3]; |
317 | if(UART4_RX_BUF[0]==0XFF&&UART4_RX_BUF[1]==0X81&&i==UART4_RX_BUF[4]) |
337 | void UART5_IRQHandler(void) //串口5中断服务程序 |
342 | if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET) |
344 | Res5=USART_ReceiveData(UART5);//读取接收到的数据 |
346 | UART5_RX_BUF[UART5_RX_STA]=Res5; |
354 | i=UART5_RX_BUF[0]+UART5_RX_BUF[1]+UART5_RX_BUF[2]+UART5_RX_BUF[3]; |
356 | if(UART5_RX_BUF[0]==0XFF&&UART5_RX_BUF[1]==0X81&&i==UART5_RX_BUF[4]) |
|