RISC-V MCU应用教程之赤菟V307多UART通信

本文章介绍使用CH32V307单片机实现主机轮回接收从机传输的数据,并在串口软件显示。
您需要的资料:CH32V307单片机数据手册及例程
一、单片机串口部分简介

由上图可知,CH32V307有8个串口(USART+UART)。

 由数据手册可知,USART有3个,USART1挂接在APB2总线上,USART2和USART2以及7个UART挂接在APB1总线上。
为了方便插拔引脚转接线,将数据手册中有关串口引脚陈列如下表格中。

防止原有映射引脚被其它外设占用,手册中定义的串口复用引脚陈列如下。

二、通讯原理及实现

1. Master程序为采集数据单片机烧录,Slave程序为数据发送单片机烧录;
2. 主程序使6个串口中断周期性的开关,达到每次只接受一个Slave数据,达到周期性采集6个Slave数据的目的。最多可外接8路串口,本例程中USART1用来与PC串口通讯,UART8因器件不够暂未使用到。Slave均使用USART2,分别连接到Master剩余的6串口上。
3. 若Master指定采集第x个Slave数据,则需先发送一个数据0作为启动Slave发送数据的请求命令,Slave判定请求命令正确后,在进行数据的发送。这种协议的约定,保证了Master接收Slave数据的完整性。(在Master接收到Slave数据后,可将原数据返回Slave做校验,本程序暂未实现,请读者自行尝试)
4. 硬件连接如下,因为没有足够的usb供电,Slave取电为板载级联杜邦线方式。中间一块为Master采集板,周边六块为Slave数据发送板。

 5. 程序如下

  1 //双机通讯,使用USART
  2 
  3 #include "debug.h"
  4 
  5 /* Global define */
  6 #define TxSize1    (size(TxBuffer1))
  7 /* Global Variable */
  8 u8 RxBuffer1 = 0; /* Send by UART2 */
  9 u8 RequestData=0;//请求发送数据
 10 u8 Rxfinish1 = 2;//先给个初始值,进入case 2,启动接收
 11 //void USART1_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
 12 void USART2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
 13 void USART3_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
 14 void UART4_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
 15 void UART5_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
 16 void UART6_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
 17 void UART7_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
 18 //void UART8_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
 19 
 20 /*********************************************************************
 21  * @fn      USARTx_CFG
 22  *
 23  * @brief   Initializes the USART2 & USART3 peripheral.
 24  *
 25  * @return  none
 26  */
 27 void USARTx_CFG(void)
 28 {
 29     GPIO_InitTypeDef  GPIO_InitStructure = {0};
 30     USART_InitTypeDef USART_InitStructure = {0};
 31     NVIC_InitTypeDef  NVIC_InitStructure = {0};
 32 
 33     RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2|RCC_APB1Periph_USART3|RCC_APB1Periph_UART4|RCC_APB1Periph_UART5|RCC_APB1Periph_UART6|RCC_APB1Periph_UART7|RCC_APB1Periph_UART8, ENABLE);
 34 //    RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1, ENABLE);
 35     RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE);
 36 
 37 //    /* USART1 TX-->A.9  RX-->A.10 */
 38 //    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
 39 //    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 40 //    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
 41 //    GPIO_Init(GPIOA, &GPIO_InitStructure);
 42 //    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
 43 //    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
 44 //    GPIO_Init(GPIOA, &GPIO_InitStructure);
 45     /* USART2 TX-->A.2  RX-->A.3 */
 46     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
 47     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 48     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
 49     GPIO_Init(GPIOA, &GPIO_InitStructure);
 50     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
 51     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
 52     GPIO_Init(GPIOA, &GPIO_InitStructure);
 53     /* USART3 TX-->B.10  RX-->B.11 */
 54     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
 55     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 56     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
 57     GPIO_Init(GPIOB, &GPIO_InitStructure);
 58     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
 59     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
 60     GPIO_Init(GPIOB, &GPIO_InitStructure);
 61     /* UART4 TX-->C.10  RX-->C.11 */
 62     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
 63     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 64     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
 65     GPIO_Init(GPIOC, &GPIO_InitStructure);
 66     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
 67     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
 68     GPIO_Init(GPIOC, &GPIO_InitStructure);
 69     /* UART5 TX-->C.12  RX-->D.12 */
 70     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
 71     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 72     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
 73     GPIO_Init(GPIOC, &GPIO_InitStructure);
 74     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
 75     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
 76     GPIO_Init(GPIOD, &GPIO_InitStructure);
 77     /* UART6 TX-->C.0  RX-->C.1 */
 78     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
 79     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 80     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
 81     GPIO_Init(GPIOC, &GPIO_InitStructure);
 82     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
 83     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
 84     GPIO_Init(GPIOC, &GPIO_InitStructure);
 85     /* UART7 TX-->C.2  RX-->C.3 */
 86     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
 87     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 88     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
 89     GPIO_Init(GPIOC, &GPIO_InitStructure);
 90     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
 91     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
 92     GPIO_Init(GPIOC, &GPIO_InitStructure);
 93 //    /* UART8 TX-->C.4  RX-->C.5 */
 94 //    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
 95 //    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 96 //    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
 97 //    GPIO_Init(GPIOC, &GPIO_InitStructure);
 98 //    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
 99 //    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
100 //    GPIO_Init(GPIOC, &GPIO_InitStructure);
101 
102     USART_InitStructure.USART_BaudRate = 115200;
103     USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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_Tx | USART_Mode_Rx;
108 
109 //    USART_Init(USART1, &USART_InitStructure);
110 //    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
111     USART_Init(USART2, &USART_InitStructure);
112     USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);//初始化全部关闭
113     USART_Init(USART3, &USART_InitStructure);
114     USART_ITConfig(USART3, USART_IT_RXNE, DISABLE);
115     USART_Init(UART4, &USART_InitStructure);
116     USART_ITConfig(UART4, USART_IT_RXNE, DISABLE);
117     USART_Init(UART5, &USART_InitStructure);
118     USART_ITConfig(UART5, USART_IT_RXNE, DISABLE);
119     USART_Init(UART6, &USART_InitStructure);
120     USART_ITConfig(UART6, USART_IT_RXNE, DISABLE);
121     USART_Init(UART7, &USART_InitStructure);
122     USART_ITConfig(UART7, USART_IT_RXNE, DISABLE);
123 //    USART_Init(UART8, &USART_InitStructure);
124 //    USART_ITConfig(UART8, USART_IT_RXNE, DISABLE);
125 
126 //    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
127 //    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
128 //    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
129 //    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
130 //    NVIC_Init(&NVIC_InitStructure);
131     NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
132     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
133     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
134     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
135     NVIC_Init(&NVIC_InitStructure);
136     NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
137     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
138     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
139     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
140     NVIC_Init(&NVIC_InitStructure);
141     NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
142     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
143     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
144     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
145     NVIC_Init(&NVIC_InitStructure);
146     NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
147     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
148     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
149     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
150     NVIC_Init(&NVIC_InitStructure);
151     NVIC_InitStructure.NVIC_IRQChannel = UART6_IRQn;
152     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
153     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
154     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
155     NVIC_Init(&NVIC_InitStructure);
156     NVIC_InitStructure.NVIC_IRQChannel = UART7_IRQn;
157     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
158     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
159     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
160     NVIC_Init(&NVIC_InitStructure);
161 //    NVIC_InitStructure.NVIC_IRQChannel = UART8_IRQn;
162 //    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
163 //    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
164 //    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
165 //    NVIC_Init(&NVIC_InitStructure);
166 
167 //    USART_Cmd(USART1, ENABLE);
168     USART_Cmd(USART2, ENABLE);
169     USART_Cmd(USART3, ENABLE);
170     USART_Cmd(UART4, ENABLE);
171     USART_Cmd(UART5, ENABLE);
172     USART_Cmd(UART6, ENABLE);
173     USART_Cmd(UART7, ENABLE);
174 //    USART_Cmd(UART8, ENABLE);
175 }
176 /*********************************************************************
177  * @fn      main
178  *
179  * @brief   Main program.
180  *
181  * @return  none
182  */
183 int main(void)
184 {
185     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
186     Delay_Init();
187     USART_Printf_Init(115200);
188     USARTx_CFG(); /* USART2 & USART3 INIT */
189 
190     printf("DATA RECEIVED START! \r\n");
191 
192     //USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
193     while(1)
194     {
195 
196         switch (Rxfinish1)
197         {
198 //        case 1:
199 //            printf("DATA RECEIVED FROM USART1 :%s\r\n",RxBuffer1);
200         //            Rxfinish1=0;
201 //            USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
202 
203 //            break;
204         case 2:
205             printf("DATA RECEIVED FROM SLAVE_2 :%p\r\n",RxBuffer1);//打印Slave发送来的数据
206             USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启下一个Slave中断,准备接受数据
207             USART_SendData(USART3, RequestData);//发送准备好接受命令请求,Slave收到校验后,就开启发送
208             while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
209             Delay_Ms(100);//给slave时间组织发送,时间太短会重新进入case 2.
210             break;
211         case 3:
212             printf("DATA RECEIVED FROM SLAVE_3 :%p\r\n",RxBuffer1);
213             USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
214             USART_SendData(UART4, RequestData);
215             while(USART_GetFlagStatus(UART4, USART_FLAG_TXE) == RESET);
216             Delay_Ms(100);
217             break;
218         case 4:
219             printf("DATA RECEIVED FROM SLAVE_4 :%p\r\n",RxBuffer1);
220             USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);
221             USART_SendData(UART5, RequestData);
222             while(USART_GetFlagStatus(UART5, USART_FLAG_TXE) == RESET);
223             Delay_Ms(100);
224             break;
225         case 5:
226             printf("DATA RECEIVED FROM SLAVE_5 :%p\r\n",RxBuffer1);
227             USART_ITConfig(UART6, USART_IT_RXNE, ENABLE);
228             USART_SendData(UART6, RequestData);
229             while(USART_GetFlagStatus(UART6, USART_FLAG_TXE) == RESET);
230             Delay_Ms(100);
231             break;
232         case 6:
233             printf("DATA RECEIVED FROM SLAVE_6 :%p\r\n",RxBuffer1);
234             USART_ITConfig(UART7, USART_IT_RXNE, ENABLE);
235             USART_SendData(UART7, RequestData);
236             while(USART_GetFlagStatus(UART7, USART_FLAG_TXE) == RESET);
237             Delay_Ms(100);
238             break;
239         case 7:
240             printf("DATA RECEIVED FROM SLAVE_7 :%p\r\n",RxBuffer1);
241             USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
242             USART_SendData(USART2, RequestData);
243             while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
244             Delay_Ms(100);
245             break;
246 //        case 8:
247 //            printf("DATA RECEIVED FROM UART8 :%p\r\n",RxBuffer1);
248 //            Rxfinish1=0;
249 //            Delay_Ms(500);
250 //            USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
251 //            USART_SendData(USART3, RequestData);
252 //            while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
253 //            break;
254         default:
255             printf("DATA RECEIVED error%p\r\n");
256             Delay_Ms(500);
257             USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
258             break;
259         }
260 
261     }
262 }
263 
264 /*********************************************************************
265  * @fn      USART3_IRQHandler
266  *
267  * @brief   This function handles USART3 global interrupt request.
268  *
269  * @return  none
270  */
271 //void USART1_IRQHandler(void)
272 //{
273 //    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
274  //   {
275  //       RxBuffer1 = USART_ReceiveData(USART1);
276   //      USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
277   //      Rxfinish1 = 1;
278  //   }
279 
280 //}
281 
282 void USART2_IRQHandler(void)
283 {
284     if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
285     {
286         RxBuffer1 = USART_ReceiveData(USART2);
287         USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);//关闭此中断
288         Rxfinish1 = 2;
289     }
290 
291 }
292 
293 void USART3_IRQHandler(void)
294 {
295     if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
296     {
297         RxBuffer1 = USART_ReceiveData(USART3);
298         USART_ITConfig(USART3, USART_IT_RXNE, DISABLE);
299         Rxfinish1 = 3;
300     }
301 
302 }
303 
304 void UART4_IRQHandler(void)
305 {
306     if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET)
307     {
308         RxBuffer1 = USART_ReceiveData(UART4);
309         USART_ITConfig(UART4, USART_IT_RXNE, DISABLE);
310         Rxfinish1 = 4;
311     }
312 
313 }
314 
315 void UART5_IRQHandler(void)
316 {
317     if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET)
318     {
319         RxBuffer1 = USART_ReceiveData(UART5);
320         USART_ITConfig(UART5, USART_IT_RXNE, DISABLE);
321         Rxfinish1 = 5;
322     }
323 
324 }
325 
326 void UART6_IRQHandler(void)
327 {
328     if(USART_GetITStatus(UART6, USART_IT_RXNE) != RESET)
329     {
330         RxBuffer1= USART_ReceiveData(UART6);
331         USART_ITConfig(UART6, USART_IT_RXNE, DISABLE);
332         Rxfinish1 = 6;
333     }
334 
335 }
336 
337 void UART7_IRQHandler(void)
338 {
339     if(USART_GetITStatus(UART7, USART_IT_RXNE) != RESET)
340     {
341         RxBuffer1 = USART_ReceiveData(UART7);
342         USART_ITConfig(UART7, USART_IT_RXNE, DISABLE);
343         Rxfinish1 = 7;
344     }
345 
346 }
347 
348 //void UART8_IRQHandler(void)
349 //{
350 //    if(USART_GetITStatus(UART8, USART_IT_RXNE) != RESET)
351 //    {
352 //        RxBuffer1 = USART_ReceiveData(UART8);
353 //        USART_ITConfig(UART8, USART_IT_RXNE, DISABLE);
354 //        Rxfinish1 = 8;
355 //    }
356 //
357 //}

6. Slave程序

 1 //双机通讯,使用USART
 2 
 3 #include "debug.h"
 4 /* Global Variable */
 5 u8 TxBuffer2 = 6;//这是发送的数据,可以分别配置Slave为不同的值
 6 u8 RequestData=0;//检测请求命令是否正确
 7 u8 RxBuffer2=0;
 8 u8 Rxfinish2;
 9 void USART2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
10 /*********************************************************************
11  * @fn      USARTx_CFG
12  *
13  * @brief   Initializes the USART2 & USART3 peripheral.
14  *
15  * @return  none
16  */
17 void USARTx_CFG(void)
18 {
19     GPIO_InitTypeDef  GPIO_InitStructure = {0};
20     USART_InitTypeDef USART_InitStructure = {0};
21     NVIC_InitTypeDef  NVIC_InitStructure = {0};
22 
23     RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2, ENABLE);
24     RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE);
25 
26     /* USART2 TX-->A.2  RX-->A.3 */
27     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
28     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
29     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
30     GPIO_Init(GPIOA, &GPIO_InitStructure);
31     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
32     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
33     GPIO_Init(GPIOA, &GPIO_InitStructure);
34 
35     USART_InitStructure.USART_BaudRate = 115200;
36     USART_InitStructure.USART_WordLength = USART_WordLength_8b;
37     USART_InitStructure.USART_StopBits = USART_StopBits_1;
38     USART_InitStructure.USART_Parity = USART_Parity_No;
39     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
40     USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
41 
42     USART_Init(USART2, &USART_InitStructure);
43     USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
44 
45     NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
46     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
47     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
48     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
49     NVIC_Init(&NVIC_InitStructure);
50     USART_Cmd(USART2, ENABLE);
51 }
52 
53 /*********************************************************************
54  * @fn      main
55  *
56  * @brief   Main program.
57  *
58  * @return  none
59  */
60 int main(void)
61 {
62     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
63     Delay_Init();
64     USART_Printf_Init(115200);
65     USARTx_CFG(); /* USART2 & USART3 INIT */
66 
67 
68     while(1)
69     {
70         if(RxBuffer2==RequestData)//校验数据请求命令
71         {
72             USART_SendData(USART2, TxBuffer2);//向Master发送数据
73             while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
74         }
75 
76     }
77 }
78 
79 void USART2_IRQHandler(void)
80 {
81     if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)//等待接受Master数据请求命令
82     {
83         RxBuffer2 = USART_ReceiveData(USART2);
84 
85     }
86 }

 注:主机先上电,从机再上电,否则数据接收混乱,正在处理这个问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值