stm32RS485串口通信——中断接收发送数据

stm32RS485串⼝通信——中断接收发送数据
功能:通过PC机的串⼝调试助⼿发送数据给串⼝A,串⼝B接收到串⼝A发送的数据,再由串⼝B将接收到的数据返回给PC机的串⼝调试助⼿。
PC->串⼝A->串⼝B->PC。
实验平台:stm32f407
追寻感动/*********************************************************************
本平台具有六个串⼝:
com1  485
com2
com3  232 需⼀个管脚控制DTU
com4 485
com5  调试串⼝  TTL
com6  485
*********************************************************************/
本实验⽤的的串⼝A对应为com1,串⼝B对应为com4。
本程序的设计思路:
通过PC机串⼝调试助⼿发送数据,当串⼝A发现有数据过来时,产⽣串⼝接收中断,把数据保存到⾃定义的接收缓冲区中,然后串⼝接收函数去缓冲区中去取数据保存在发送缓冲区中,再将数据通过串⼝B发送给串⼝调试助⼿。
代码:
multiple_serial.h
1. #ifndef multiple_serial_H
2. #define multiple_serial_H
3. #define MAX_RECV_BUF_LEN (64)
4. #define MAX_RECV_DATA_MASK (0x3f)
5. #define MAX_COM_COUNT (6)
6.
7. typedef signed char int8_t;
8. typedef signed short intint16_t;
9. typedef signed int int32_t;
10.
11. typedef unsigned char uint8_t;
12. typedef unsigned short intuint16_t;
13. typedef unsigned int uint32_t;
14.
15. #define NEXTAI_COM1_NAME"COM1"
16. #define NEXTAI_COM2_NAME"COM2"
17. #define NEXTAI_COM3_NAME"COM3"
18. #define NEXTAI_COM4_NAME"COM4"
19. #define NEXTAI_COM5_NAME"COM5"
20. #define NEXTAI_COM6_NAME"COM6"
21.
22. enum{
23. NEXTAI_COM1_ID=1,
24. NEXTAI_COM2_ID,
25. NEXTAI_COM3_ID,
26. NEXTAI_COM4_ID,
27. NEXTAI_COM5_ID,
28. NEXTAI_COM6_ID
29. };
30.
31. typedef struct _NEXTAI_CHANNEL_INFO_
32. {
33. uint8_t local_com_id;
34. //nextai_uint8 *databuf;
35. //nextai_uint16 data_read_p;
36. //nextai_uint16 data_write_p;
37.
38. uint8_t used_flag;
39. }NEXTAI_CHANNEL_INFO;
40.
41. /*******************************************************************************
42. * local_channel_data : 通道数据结构
43. * data_recv_flag : 是否有数据flag,1表⽰
44. *******************************************************************************/
45. typedef struct _NEXTAI_MGR_INFO_
48. uint8_t data_recv_flag;
49. }NEXTAI_MGR_INFO;
50.
51. static NEXTAI_MGR_INFO* local_com_mgr_p=0;
52. void serial_nvic_Configuration(uint8_t comID);
53. static void serial_gpio_configuration(uint8_t comID);
54. static void serial_rcc_configuration(uint8_t comID);
55. uint8_t serial_Configuration(uint8_t*comName);
56. uint8_t serial_send(uint8_t comID,uint8_t*databuf,uint32_t datalen);
57. uint32_t serial_recv(uint8_t comID,uint8_t*databuf,uint32_t datalen);
58. void clean_rebuff(void);
59. #endif
multiple_serial.c
1. #include"multiple_serial.h"水木年华镜片
2. #include"stm32f4xx_gpio.h"
3. #include"stm32f4xx_usart.h"
4. #include<string.h>
5.
6. unsigned char uart_buf[MAX_RECV_BUF_LEN]={0};
7.
8. uint8_t com1_recv_buf[MAX_RECV_BUF_LEN]={0};
9. int com1_gloabl_p=0;
10. int com1_read_p=0;东风烈
11.
灭滴灵
12. uint8_t com4_recv_buf[MAX_RECV_BUF_LEN]={0};
13. int com4_gloabl_p=0;
14. int com4_read_p=0;
15.
16. uint8_t com5_recv_buf[MAX_RECV_BUF_LEN]={0};
17. int com5_gloabl_p=0;
18. int com5_read_p=0;
19.
20. uint8_t com6_recv_buf[MAX_RECV_BUF_LEN]={0};
21. int com6_gloabl_p=0;
22. int com6_read_p=0;
23.
24. //串⼝gpio的配置
25. static void serial_gpio_configuration(uint8_t comID)
26. {
27. GPIO_InitTypeDef GPIO_InitStructure;
28. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
29. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
30. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
31. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
32.
33. /* Configure USART Tx and Rx as alternate function push-pull */
34. if(comID == NEXTAI_COM1_ID)
35. {
36. GPIO_StructInit(&GPIO_InitStructure);
37.
38. GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
39. GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
40.
41. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF;
42. GPIO_InitStructure.GPIO_Speed= GPIO_Speed_100MHz;
43. GPIO_InitStructure.GPIO_OType= GPIO_OType_PP;
44. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
45.
46. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_9;
47. GPIO_Init(GPIOA,&GPIO_InitStructure);
48.
49. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_10;
50. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
51. GPIO_Init(GPIOA,&GPIO_InitStructure);
52.
53. GPIO_StructInit(&GPIO_InitStructure);
54. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_OUT;
55. GPIO_InitStructure.GPIO_Speed= GPIO_Speed_100MHz;
56. GPIO_InitStructure.GPIO_OType= GPIO_OType_PP;
57. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
58. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_4;
59.
60. GPIO_Init(GPIOF,&GPIO_InitStructure);
61. }
62. elseif(comID== NEXTAI_COM4_ID)
63. {
65.
66. GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_UART4);
67. GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_UART4);
68.
69. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF;
70. GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
71. GPIO_InitStructure.GPIO_OType= GPIO_OType_PP;
72. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
73.
74. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_1;
75. GPIO_Init(GPIOA,&GPIO_InitStructure);
76.
77. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_0;
78. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
79. GPIO_Init(GPIOA,&GPIO_InitStructure);
80.
81. GPIO_StructInit(&GPIO_InitStructure);
82. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_OUT;
83. GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
84. GPIO_InitStructure.GPIO_OType= GPIO_OType_PP;
85. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
86. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_7;
87.
88. GPIO_Init(GPIOA,&GPIO_InitStructure);
89. }
90. elseif(comID== NEXTAI_COM5_ID)
91. {
92. GPIO_StructInit(&GPIO_InitStructure);
93.
94. GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5);
95. GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5);
96.
97. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF;
98. GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
99. GPIO_InitStructure.GPIO_OType= GPIO_OType_PP;
100. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
101.
102. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_2;
103. GPIO_Init(GPIOD,&GPIO_InitStructure);
104.
105. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_12;
106. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
107. GPIO_Init(GPIOC,&GPIO_InitStructure);
108. }
109. elseif(comID== NEXTAI_COM6_ID)
110. {
111. GPIO_StructInit(&GPIO_InitStructure);
112.
113. GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6); 114. GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6); 115.
116. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF;
117. GPIO_InitStructure.GPIO_Speed= GPIO_Speed_100MHz;
118. GPIO_InitStructure.GPIO_OType= GPIO_OType_PP;
119. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
120.
121. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_6;
122. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
123. GPIO_Init(GPIOC,&GPIO_InitStructure);
124.
125. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_7;
126. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
127. GPIO_Init(GPIOC,&GPIO_InitStructure);
128.
129. GPIO_StructInit(&GPIO_InitStructure);
130. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_OUT;
131. GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
132. GPIO_InitStructure.GPIO_OType= GPIO_OType_PP;
133. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
134.
135. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_8;
136. GPIO_Init(GPIOC,&GPIO_InitStructure);
137. }
138. }
139.
140. //串⼝时钟的配置
141. static void serial_rcc_configuration(uint8_t comID)
142. {
146. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); 147. }
两国集团148. elseif(comID== NEXTAI_COM4_ID)
149. {
150. RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE); 151. }
152. elseif(comID== NEXTAI_COM5_ID)
153. {
154. RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE); 155. }
156. elseif(comID== NEXTAI_COM6_ID)
157. {
158. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE); 159. }
160. }
161.
162. //配置各个串⼝的中断处理函数,优先级相同
163. void serial_nvic_Configuration(uint8_t comID)
164. {
165. NVIC_InitTypeDef NVIC_InitStructure;
166. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
167.
168. if(comID== NEXTAI_COM1_ID)
169. {
170. NVIC_InitStructure.NVIC_IRQChannel= USART1_IRQn;
171. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
172. NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
173. NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE;
174. NVIC_Init(&NVIC_InitStructure);
175. }
176. elseif(comID== NEXTAI_COM4_ID)
177. {
178. NVIC_InitStructure.NVIC_IRQChannel= UART4_IRQn;
179. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
180. NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
181. NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE;
182. NVIC_Init(&NVIC_InitStructure);
183. }
184. elseif(comID== NEXTAI_COM5_ID)
185. {
186. NVIC_InitStructure.NVIC_IRQChannel= UART5_IRQn;
187. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
188. NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
189. NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE;
190. NVIC_Init(&NVIC_InitStructure);
191. }
192. elseif(comID== NEXTAI_COM6_ID)
193. {
194. NVIC_InitStructure.NVIC_IRQChannel= USART6_IRQn;
195. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
196. NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
197. NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE;
198. NVIC_Init(&NVIC_InitStructure);
199. }
200. }
201.
202.
203. /*串⼝参数配置*/
204. uint8_t serial_Configuration(uint8_t*comName)
205. {
206. USART_InitTypeDef USART_InitStructure;
207. uint8_t comID =0;
208.
209. if(strcmp(NEXTAI_COM1_NAME,(constchar*)comName)==0) 210. {
211. comID= NEXTAI_COM1_ID;
212. }
213. elseif(strcmp(NEXTAI_COM4_NAME,(constchar*)comName)==0) 214. {
215. comID= NEXTAI_COM4_ID;
216. }
217. elseif(strcmp(NEXTAI_COM5_NAME,(constchar*)comName)==0) 218. {
219. comID= NEXTAI_COM5_ID;
220. }
221. elseif(strcmp(NEXTAI_COM6_NAME,(constchar*)comName)==0)
225.
226. /* System Clocks Configuration */
227. serial_rcc_configuration(comID);
228.
229. /* Configure the GPIO ports */
230. serial_gpio_configuration(comID);
231.
232. USART_InitStructure.USART_WordLength= USART_WordLength_8b;
233. USART_InitStructure.USART_Parity= USART_Parity_No;
234. USART_InitStructure.USART_HardwareFlowControl= USART_HardwareFlowControl_None; 235. USART_InitStructure.USART_Mode= USART_Mode_Rx| USART_Mode_Tx;
236.
237. if(comID== NEXTAI_COM1_ID)
238. {
239. USART_InitStructure.USART_BaudRate=9600;
240. USART_InitStructure.USART_StopBits= USART_StopBits_1;
241. USART_Init(USART1,&USART_InitStructure);
242. }
243. elseif(comID== NEXTAI_COM4_ID)
244. {
245. USART_InitStructure.USART_BaudRate=38400;
246. USART_InitStructure.USART_StopBits= USART_StopBits_1;
247. USART_Init(UART4,&USART_InitStructure);
248. }
249. elseif(comID== NEXTAI_COM5_ID)
250. {
251. USART_InitStructure.USART_BaudRate=115200;
252. USART_InitStructure.USART_StopBits= USART_StopBits_1;
253. USART_Init(UART5,&USART_InitStructure);
254. }
255. elseif(comID== NEXTAI_COM6_ID)
256. {
257. USART_InitStructure.USART_BaudRate=9600;
258. USART_InitStructure.USART_StopBits= USART_StopBits_1;
259. USART_Init(USART6,&USART_InitStructure);
260. }
261.
262. serial_nvic_Configuration(comID);
263.
264. if(comID== NEXTAI_COM1_ID)
265. {
266. USART_Cmd(USART1, ENABLE);
267. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
268. }
269. elseif(comID== NEXTAI_COM4_ID)
270. {
271. USART_Cmd(UART4, ENABLE);
272. USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
273. }
274. elseif(comID== NEXTAI_COM5_ID)
275. {
276. USART_Cmd(UART5, ENABLE);
277. USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);
278. }
279. elseif(comID== NEXTAI_COM6_ID)
280. {
281. USART_Cmd(USART6, ENABLE);
282. USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);
283. }
284.
285. return comID;
286. }
287.
288. /*******************************************************************************
289. * 函数名 : serial_send
290. * 描述 : 发送某个端⼝的数据
民国时期学生装291. * 输⼊ : comID: 串⼝标识号
292. databuf: 发送数据缓冲区
293. datalen: 发送数据长度
294. * 输出 : None
295. * 返回 : None
296. *******************************************************************************/
297. uint8_t serial_send(uint8_t comID,uint8_t*databuf,uint32_t datalen)
298. {
299. int i =0;
300. uint8_t nextai_timer =0;

本文发布于:2024-09-21 16:44:30,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/651391.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:数据   接收   保存   调试   PC机
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议