• STM32测温枪


    前段时间有靓仔私聊问我,要关于字节流串口通信的代码,今天根据项目代码进行公示

    main主函数

    1. #include <stdio.h>
    2. #include <ctype.h>
    3. #include <stdlib.h>
    4. #include <string.h>
    5. #include "stm32f10x.h"
    6. #include "debug.h"
    7. #include "stm32f10x_it.h"
    8. #include "m3_systick.h"
    9. #include "bsp_key.h"
    10. #include "ble.h"
    11. #include "led.h"
    12. #include "temp.h"
    13. uint8_t display_meun=0;
    14. typedef struct temp_body
    15. {
    16. uint8_t onebyte;
    17. uint8_t twobyte;
    18. uint8_t threebyte;
    19. uint8_t fourbyte;
    20. }temp_test;
    21. temp_test mytemp;
    22. void temp_start(temp_test *mytemp);
    23. void temp_send_data(temp_test *mytemp,uint8_t temp_len);
    24. //***************************全局变量**********************************************//
    25. extern char BLE_USART_BUF[256]; //蓝牙串口数据缓冲区
    26. extern uint8_t temp_buffer[9]; //测温串口数据缓冲区
    27. /**
    28. * @brief 主函数
    29. * @param 无
    30. * @retval 无
    31. */
    32. int main(void)
    33. {
    34. uint8_t data_flag=0;
    35. char dud[15]={0};
    36. //环境温度
    37. static uint16_t environment_temp=0;
    38. static uint8_t environment_temp_h=0;
    39. static uint8_t environment_temp_l=0;
    40. static float final_environment_temp=0;
    41. //被测物体温度
    42. uint16_t object_temp=0;
    43. uint8_t object_temp_h=0;
    44. uint8_t object_temp_l=0;
    45. float final_object_temp=0;
    46. //人体温度
    47. uint16_t body_temp=0;
    48. uint8_t body_temp_h=0;
    49. uint8_t body_temp_l=0;
    50. float final_body_temp=0;
    51. m3_Systick_Init();
    52. LED_GPIO_Config();
    53. LED_ON;
    54. /*初始化USART1 配置模式为 9600 8-N-1,用于串口调试*/
    55. Debug_Config(); //串口调试();
    56. /*初始化UART4 配置模式为 9600 8-N-1,中断接收 接收发蓝牙数据*/
    57. BLE_Init();
    58. /*初始化USART3 配置模式为 9600 8-N-1,中断接收 接收发测温数据*/
    59. TEMP_Config(); //
    60. while(1)
    61. {
    62. if(strstr(BLE_USART_BUF,"temp")!=NULL)
    63. {
    64. data_flag=1;
    65. sprintf(dud,"temp_on");
    66. macBLE_Usart(dud);
    67. memset(dud,'\0',15);
    68. memset(BLE_USART_BUF,'\0',256);
    69. LED_ON;
    70. printf("temp_start\r\n");
    71. }
    72. //***************条件模式选择*************************//
    73. //测温的********************************start******************//
    74. if(data_flag==1)
    75. {
    76. temp_start(&mytemp);
    77. temp_send_data(&mytemp,4);
    78. printf("%x-%x-%x-%x-%x-%x-%x-%x-%x\r\n",temp_buffer[0],temp_buffer[1],temp_buffer[2],temp_buffer[3],temp_buffer[4],temp_buffer[5],temp_buffer[6],temp_buffer[7],temp_buffer[8]);
    79. //环境温度
    80. environment_temp_h = temp_buffer[2];
    81. environment_temp_l = temp_buffer[3];
    82. environment_temp = environment_temp_h<<8;
    83. environment_temp |= environment_temp_l;
    84. final_environment_temp = (float)(environment_temp/10.0f);
    85. //被测物体温度
    86. object_temp_h = temp_buffer[4];
    87. object_temp_l = temp_buffer[5];
    88. object_temp = object_temp_h<<8;
    89. object_temp |= object_temp_l;
    90. final_object_temp = (float)(object_temp/10.0f);
    91. //人体温度
    92. body_temp_h = temp_buffer[6];
    93. body_temp_l = temp_buffer[7];
    94. body_temp = body_temp_h<<8;
    95. body_temp |= body_temp_l;
    96. final_body_temp = (float)(body_temp/10.0f);
    97. if(final_body_temp>34.0f)
    98. {
    99. printf("%.2f-%.2f-%.2f\r\n",final_environment_temp,final_object_temp,final_body_temp);
    100. sprintf(dud,"temp:%.2f,%.2f,%.2f",final_environment_temp,final_object_temp,final_body_temp);
    101. macBLE_Usart(dud);
    102. LED_OFF;
    103. data_flag=66;
    104. }
    105. printf("temp:%.2f,%.2f,%.2f",final_environment_temp,final_object_temp,final_body_temp);
    106. memset(temp_buffer,'\0',9);
    107. memset(dud,'\0',15);
    108. Delay_ms(20);
    109. }
    110. if(data_flag==66) //缓冲区
    111. {
    112. printf("data_flag:66\r\n");
    113. LED_OFF;
    114. }
    115. }
    116. }
    117. //**************************************测温计***************************************//
    118. //开始测试 5a 01 00 5b
    119. void temp_start(temp_test *mytemp)
    120. {
    121. mytemp->onebyte = 0x5A;
    122. mytemp->twobyte = 0x01;
    123. mytemp->threebyte = 0x00;
    124. mytemp->fourbyte = 0x5B;
    125. }
    126. void temp_send_data(temp_test *mytemp,uint8_t temp_len)
    127. {
    128. static uint8_t date=0,i=0;
    129. for(i=0;i<temp_len;i++)
    130. {
    131. date = *(((uint8_t *)&mytemp->onebyte)+i);
    132. USART_SendData(TEMP_USARTx,date);
    133. while(USART_GetFlagStatus(TEMP_USARTx,USART_FLAG_TC)!= SET);
    134. }
    135. }
    136. /*********************************************END OF FILE**********************/

    ble部分代码

    ble.c

    1. #include "ble.h"
    2. #include <stdio.h>
    3. #include <stdbool.h>
    4. #include <stdarg.h>
    5. #include <string.h>
    6. #include <math.h>
    7. #include "m3_systick.h"
    8. static void BLE_USART_Config( void );
    9. static void BLE_USART_NVIC_Configuration( void );
    10. struct STRUCT_USARTx_Fram strBLE_Fram_Record = { 0 };
    11. static char * itoa( int value, char * string, int radix );
    12. /*
    13. * 函数名:USART2_printf
    14. * 描述 :格式化输出,类似于C库中的printf,但这里没有用到C库
    15. * 输入 :-USARTx 串口通道,这里只用到了串口2,即USART2
    16. * -Data 要发送到串口的内容的指针
    17. * -... 其他参数
    18. * 输出 :无
    19. * 返回 :无
    20. * 调用 :外部调用
    21. * 典型应用USART2_printf( USART2, "\r\n this is a demo \r\n" );
    22. * USART2_printf( USART2, "\r\n %d \r\n", i );
    23. * USART2_printf( USART2, "\r\n %s \r\n", j );
    24. */
    25. void USART_printf ( USART_TypeDef * USARTx, char * Data, ... )
    26. {
    27. const char *s;
    28. int d;
    29. char buf[16];
    30. va_list ap;
    31. va_start(ap, Data);
    32. while ( * Data != 0 ) // 判断是否到达字符串结束符
    33. {
    34. if ( * Data == 0x5c ) //'\'
    35. {
    36. switch ( *++Data )
    37. {
    38. case 'r': //回车符
    39. USART_SendData(USARTx, 0x0d);
    40. Data ++;
    41. break;
    42. case 'n': //换行符
    43. USART_SendData(USARTx, 0x0a);
    44. Data ++;
    45. break;
    46. default:
    47. Data ++;
    48. break;
    49. }
    50. }
    51. else if ( * Data == '%')
    52. { //
    53. switch ( *++Data )
    54. {
    55. case 's': //字符串
    56. s = va_arg(ap, const char *);
    57. for ( ; *s; s++)
    58. {
    59. USART_SendData(USARTx,*s);
    60. while( USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET );
    61. }
    62. Data++;
    63. break;
    64. case 'd':
    65. //十进制
    66. d = va_arg(ap, int);
    67. itoa(d, buf, 10);
    68. for (s = buf; *s; s++)
    69. {
    70. USART_SendData(USARTx,*s);
    71. while( USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET );
    72. }
    73. Data++;
    74. break;
    75. default:
    76. Data++;
    77. break;
    78. }
    79. }
    80. else USART_SendData(USARTx, *Data++);
    81. while ( USART_GetFlagStatus ( USARTx, USART_FLAG_TXE ) == RESET );
    82. }
    83. }
    84. /*
    85. * 函数名:itoa
    86. * 描述 :将整形数据转换成字符串
    87. * 输入 :-radix =10 表示10进制,其他结果为0
    88. * -value 要转换的整形数
    89. * -buf 转换后的字符串
    90. * -radix = 10
    91. * 输出 :无
    92. * 返回 :无
    93. * 调用 :被USART2_printf()调用
    94. */
    95. static char * itoa( int value, char *string, int radix )
    96. {
    97. int i, d;
    98. int flag = 0;
    99. char *ptr = string;
    100. /* This implementation only works for decimal numbers. */
    101. if (radix != 10)
    102. {
    103. *ptr = 0;
    104. return string;
    105. }
    106. if (!value)
    107. {
    108. *ptr++ = 0x30;
    109. *ptr = 0;
    110. return string;
    111. }
    112. /* if this is a negative value insert the minus sign. */
    113. if (value < 0)
    114. {
    115. *ptr++ = '-';
    116. /* Make the value positive. */
    117. value *= -1;
    118. }
    119. for (i = 10000; i > 0; i /= 10)
    120. {
    121. d = value / i;
    122. if (d || flag)
    123. {
    124. *ptr++ = (char)(d + 0x30);
    125. value -= (d * i);
    126. flag = 1;
    127. }
    128. }
    129. /* Null terminate the string. */
    130. *ptr = 0;
    131. return string;
    132. } /* NCL_Itoa */
    133. /**
    134. * @brief 初始化BLE用到的 USART
    135. * @param 无
    136. * @retval 无
    137. */
    138. static void BLE_USART_Config ( void )
    139. {
    140. GPIO_InitTypeDef GPIO_InitStructure;
    141. USART_InitTypeDef USART_InitStructure;
    142. /* config USART clock */
    143. macBLE_USART_APBxClock_FUN ( macBLE_USART_CLK, ENABLE );
    144. macBLE_USART_GPIO_APBxClock_FUN ( macBLE_USART_GPIO_CLK, ENABLE );
    145. /* USART GPIO config */
    146. /* Configure USART Tx as alternate function push-pull */
    147. GPIO_InitStructure.GPIO_Pin = macBLE_USART_TX_PIN;
    148. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    149. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    150. GPIO_Init(macBLE_USART_TX_PORT, &GPIO_InitStructure);
    151. /* Configure USART Rx as input floating */
    152. GPIO_InitStructure.GPIO_Pin = macBLE_USART_RX_PIN;
    153. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    154. GPIO_Init(macBLE_USART_RX_PORT, &GPIO_InitStructure);
    155. /* USART1 mode config */
    156. USART_InitStructure.USART_BaudRate = macBLE_USART_BAUD_RATE;
    157. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    158. USART_InitStructure.USART_StopBits = USART_StopBits_1;
    159. USART_InitStructure.USART_Parity = USART_Parity_No ;
    160. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    161. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    162. USART_Init(macBLE_USARTx, &USART_InitStructure);
    163. /* 中断配置 */
    164. USART_ITConfig ( macBLE_USARTx, USART_IT_RXNE, ENABLE ); //使能串口接收中断
    165. USART_ITConfig ( macBLE_USARTx, USART_IT_IDLE, ENABLE ); //使能串口总线空闲中断
    166. BLE_USART_NVIC_Configuration ();
    167. USART_Cmd(macBLE_USARTx, ENABLE);
    168. }
    169. /**
    170. * @brief 配置 BLE USART 的 NVIC 中断
    171. * @param 无
    172. * @retval 无
    173. */
    174. static void BLE_USART_NVIC_Configuration ( void )
    175. {
    176. NVIC_InitTypeDef NVIC_InitStructure;
    177. /* Configure the NVIC Preemption Priority Bits */
    178. NVIC_PriorityGroupConfig ( macNVIC_PriorityGroup_x );
    179. /* Enable the USART2 Interrupt */
    180. NVIC_InitStructure.NVIC_IRQChannel = macBLE_USART_IRQ;
    181. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    182. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    183. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    184. NVIC_Init(&NVIC_InitStructure);
    185. }
    186. void USART3_SendByte(uint8_t ch)
    187. {
    188. /* 发送一个字节数据到USART */
    189. USART_SendData(macBLE_USARTx,ch);
    190. /* 等待发送数据寄存器为空 */
    191. while (USART_GetFlagStatus(macBLE_USARTx, USART_FLAG_TXE) == RESET);
    192. }
    193. /***************** 发送字符串 **********************/
    194. void USART3_SendString(char *str)
    195. {
    196. unsigned int k=0;
    197. do
    198. {
    199. USART3_SendByte(*(str + k));
    200. k++;
    201. } while(*(str + k)!='\0');
    202. /* 等待发送完成 */
    203. while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET)
    204. {}
    205. }
    206. /*
    207. * 函数名:BLE_Cmd
    208. * 描述 :对WF-BLE模块发送AT指令
    209. * 输入 :cmd,待发送的指令
    210. * reply1,reply2,期待的响应,为NULL表不需响应,两者为或逻辑关系
    211. * waittime,等待响应的时间
    212. * 返回 : 1,指令发送成功
    213. * 0,指令发送失败
    214. * 调用 :被外部调用
    215. */
    216. bool BLE_Cmd ( char * cmd, char * reply1, char * reply2, u32 waittime )
    217. {
    218. memset(strBLE_Fram_Record .Data_RX_BUF,0,strBLE_Fram_Record .InfBit .FramLength);
    219. strBLE_Fram_Record .InfBit .FramLength = 0; //从新开始接收新的数据包
    220. macBLE_Usart ( "%s", cmd );
    221. printf ( "%s\r\n", cmd );
    222. if ( ( reply1 == 0 ) && ( reply2 == 0 ) ) //不需要接收数据
    223. return true;
    224. Delay_ms ( waittime ); //延时
    225. strBLE_Fram_Record .Data_RX_BUF [ strBLE_Fram_Record .InfBit .FramLength ] = '\0';
    226. printf ( "%s", strBLE_Fram_Record .Data_RX_BUF );
    227. if ( ( reply1 != 0 ) && ( reply2 != 0 ) )
    228. return ( ( bool ) strstr ( strBLE_Fram_Record .Data_RX_BUF, reply1 ) ||
    229. ( bool ) strstr ( strBLE_Fram_Record .Data_RX_BUF, reply2 ) );
    230. else if ( reply1 != 0 )
    231. return ( ( bool ) strstr ( strBLE_Fram_Record .Data_RX_BUF, reply1 ) );
    232. else
    233. return ( ( bool ) strstr ( strBLE_Fram_Record .Data_RX_BUF, reply2 ) );
    234. }
    235. /**
    236. * @brief BLE初始化函数
    237. * @param 无
    238. * @retval 无
    239. */
    240. void BLE_Init ( void )
    241. {
    242. BLE_USART_Config ();
    243. }
    244. char BLE_USART_BUF[256] = {0};
    245. /**
    246. * @brief This function handles macESP8266_USARTx Handler.
    247. * @param None
    248. * @retval None
    249. */
    250. void macBLE_USART_INT_FUN ( void )
    251. {
    252. uint8_t ucCh;
    253. static u8 i;
    254. if( USART_GetITStatus ( macBLE_USARTx, USART_IT_RXNE ) != RESET )
    255. {
    256. ucCh = USART_ReceiveData( macBLE_USARTx );
    257. BLE_USART_BUF[i++] = ucCh;
    258. }
    259. if( USART_GetITStatus( macBLE_USARTx, USART_IT_IDLE ) == SET ) //???????
    260. {
    261. ucCh = USART_ReceiveData( macBLE_USARTx );
    262. BLE_USART_BUF[i] = '\0';
    263. // printf("%s\r\n",BLE_USART_BUF);
    264. // recvDataFromBleCustomerAndProcess(BLE_USART_BUF);
    265. i = 0;
    266. }
    267. }

    ble.h

    1. #ifndef _BLE_H_
    2. #define _BLE_H_
    3. #include "stm32f10x.h"
    4. #include <stdio.h>
    5. #include <stdbool.h>
    6. #include <stdarg.h>
    7. #include <stdbool.h>
    8. #include <math.h>
    9. #if defined ( __CC_ARM )
    10. #pragma anon_unions
    11. #endif
    12. #define macNVIC_PriorityGroup_x NVIC_PriorityGroup_2
    13. void USART_printf ( USART_TypeDef * USARTx, char * Data, ... );
    14. /******************************* ESP8266 外部全局变量声明 ***************************/
    15. #define RX_BUF_MAX_LEN 256 //最大接收缓存字节数
    16. extern struct STRUCT_USARTx_Fram //串口数据帧的处理结构体
    17. {
    18. char Data_RX_BUF [ RX_BUF_MAX_LEN ];
    19. union {
    20. __IO u16 InfAll;
    21. struct {
    22. __IO u16 FramLength :15; // 14:0
    23. __IO u16 FramFinishFlag :1; // 15
    24. } InfBit;
    25. };
    26. } strBLE_Fram_Record;
    27. //位带操作,实现51类似的GPIO控制功能
    28. //具体实现思想,参考<<CM3权威指南>>第五章(87页~92页).
    29. //IO口操作宏定义
    30. #define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
    31. #define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
    32. #define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
    33. //IO口地址映射
    34. #define GPIOA_ODR_Addr (GPIOA_BASE+12) //0x4001080C
    35. #define GPIOB_ODR_Addr (GPIOB_BASE+12) //0x40010C0C
    36. #define GPIOC_ODR_Addr (GPIOC_BASE+12) //0x4001100C
    37. #define GPIOD_ODR_Addr (GPIOD_BASE+12) //0x4001140C
    38. #define GPIOE_ODR_Addr (GPIOE_BASE+12) //0x4001180C
    39. #define GPIOF_ODR_Addr (GPIOF_BASE+12) //0x40011A0C
    40. #define GPIOG_ODR_Addr (GPIOG_BASE+12) //0x40011E0C
    41. #define GPIOA_IDR_Addr (GPIOA_BASE+8) //0x40010808
    42. #define GPIOB_IDR_Addr (GPIOB_BASE+8) //0x40010C08
    43. #define GPIOC_IDR_Addr (GPIOC_BASE+8) //0x40011008
    44. #define GPIOD_IDR_Addr (GPIOD_BASE+8) //0x40011408
    45. #define GPIOE_IDR_Addr (GPIOE_BASE+8) //0x40011808
    46. #define GPIOF_IDR_Addr (GPIOF_BASE+8) //0x40011A08
    47. #define GPIOG_IDR_Addr (GPIOG_BASE+8) //0x40011E08
    48. //IO口操作,只对单一的IO口!
    49. //确保n的值小于16!
    50. #define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n) //输出
    51. #define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n) //输入
    52. #define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n) //输出
    53. #define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n) //输入
    54. #define PCout(n) BIT_ADDR(GPIOC_ODR_Addr,n) //输出
    55. #define PCin(n) BIT_ADDR(GPIOC_IDR_Addr,n) //输入
    56. #define PDout(n) BIT_ADDR(GPIOD_ODR_Addr,n) //输出
    57. #define PDin(n) BIT_ADDR(GPIOD_IDR_Addr,n) //输入
    58. #define PEout(n) BIT_ADDR(GPIOE_ODR_Addr,n) //输出
    59. #define PEin(n) BIT_ADDR(GPIOE_IDR_Addr,n) //输入
    60. #define PFout(n) BIT_ADDR(GPIOF_ODR_Addr,n) //输出
    61. #define PFin(n) BIT_ADDR(GPIOF_IDR_Addr,n) //输入
    62. #define PGout(n) BIT_ADDR(GPIOG_ODR_Addr,n) //输出
    63. #define PGin(n) BIT_ADDR(GPIOG_IDR_Addr,n) //输入
    64. //#define macBLE_RST_LOW_LEVEL() GPIO_ResetBits(macBLE_RST_PORT,macBLE_RST_PIN)
    65. //#define macBLE_RST_HIGH_LEVEL() GPIO_SetBits(macBLE_RST_PORT,macBLE_RST_PIN)
    66. //#define Read_macBLE_READY() GPIO_ReadInputDataBit(macBLE_READY_PORT,macBLE_READY_PIN)
    67. //#define Read_macBLE_LINK() GPIO_ReadInputDataBit(macBLE_LINK_PORT,macBLE_LINK_PIN)
    68. #define macBLE_RST PBout(9)
    69. #define macBLE_MODE PBout(8)
    70. #define macBLE_DISC PBout(7)
    71. #define macBLE_LINK PBin(6)
    72. /********************************wifi串口配置宏*****************************************/
    73. #define macBLE_USART_BAUD_RATE 9600
    74. #define macBLE_USARTx UART4
    75. #define macBLE_USART_APBxClock_FUN RCC_APB1PeriphClockCmd
    76. #define macBLE_USART_CLK RCC_APB1Periph_UART4
    77. #define macBLE_USART_GPIO_APBxClock_FUN RCC_APB2PeriphClockCmd
    78. #define macBLE_USART_GPIO_CLK RCC_APB2Periph_GPIOC
    79. #define macBLE_USART_TX_PORT GPIOC
    80. #define macBLE_USART_TX_PIN GPIO_Pin_10
    81. #define macBLE_USART_RX_PORT GPIOC
    82. #define macBLE_USART_RX_PIN GPIO_Pin_11
    83. #define macBLE_USART_IRQ UART4_IRQn
    84. #define macBLE_USART_INT_FUN UART4_IRQHandler
    85. /*********************************************** ESP8266 函数宏定义 *******************************************/
    86. #define macBLE_Usart( fmt, ... ) USART_printf ( macBLE_USARTx, fmt, ##__VA_ARGS__ )
    87. #define macPC_Usart( fmt, ... ) printf ( fmt, ##__VA_ARGS__ )
    88. /****************************************** ESP8266 函数声明 ***********************************************/
    89. void BLE_Init ( void );
    90. void USART3_SendByte(uint8_t ch);
    91. void USART3_SendString(char *str);
    92. bool BLE_Cmd ( char * cmd, char * reply1, char * reply2, u32 waittime );
    93. #endif

    测温模块部分

    temp.c

    1. #include "temp.h"
    2. #include "debug.h"
    3. /**
    4. * @brief 配置嵌套向量中断控制器NVIC
    5. * @param 无
    6. * @retval 无
    7. */
    8. //static void NVIC_Configuration(void)
    9. //{
    10. //}
    11. /**
    12. * @brief USART GPIO 配置,工作参数配置
    13. * @param 无
    14. * @retval 无
    15. */
    16. void TEMP_Config(void)
    17. {
    18. GPIO_InitTypeDef GPIO_InitStructure;
    19. USART_InitTypeDef USART_InitStructure;
    20. // 打开串口GPIO的时钟
    21. TEMP_USART_GPIO_APBxClkCmd(TEMP_USART_GPIO_CLK, ENABLE);
    22. // 打开串口外设的时钟
    23. TEMP_USART_APBxClkCmd(TEMP_USART_CLK, ENABLE);
    24. // 将USART Tx的GPIO配置为推挽复用模式
    25. GPIO_InitStructure.GPIO_Pin = TEMP_USART_TX_GPIO_PIN;
    26. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    27. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    28. GPIO_Init(TEMP_USART_TX_GPIO_PORT, &GPIO_InitStructure);
    29. // 将USART Rx的GPIO配置为浮空输入模式
    30. GPIO_InitStructure.GPIO_Pin = TEMP_USART_RX_GPIO_PIN;
    31. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    32. GPIO_Init(TEMP_USART_RX_GPIO_PORT, &GPIO_InitStructure);
    33. // 配置串口的工作参数
    34. // 配置波特率
    35. USART_InitStructure.USART_BaudRate = TEMP_USART_BAUDRATE;
    36. // 配置 针数据字长
    37. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    38. // 配置停止位
    39. USART_InitStructure.USART_StopBits = USART_StopBits_1;
    40. // 配置校验位
    41. USART_InitStructure.USART_Parity = USART_Parity_No ;
    42. // 配置硬件流控制
    43. USART_InitStructure.USART_HardwareFlowControl =
    44. USART_HardwareFlowControl_None;
    45. // 配置工作模式,收发一起
    46. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    47. // 完成串口的初始化配置
    48. USART_Init(TEMP_USARTx, &USART_InitStructure);
    49. // 串口中断优先级配置
    50. NVIC_InitTypeDef NVIC_InitStructure;
    51. /* 嵌套向量中断控制器组选择 */
    52. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    53. /* 配置USART为中断源 */
    54. NVIC_InitStructure.NVIC_IRQChannel = TEMP_USART_IRQ;
    55. /* 抢断优先级*/
    56. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    57. /* 子优先级 */
    58. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
    59. /* 使能中断 */
    60. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    61. /* 初始化配置NVIC */
    62. NVIC_Init(&NVIC_InitStructure);
    63. // 使能串口接收中断
    64. USART_ITConfig(TEMP_USARTx, USART_IT_RXNE, ENABLE);
    65. // 使能串口空闲中断
    66. USART_ITConfig(TEMP_USARTx, USART_IT_IDLE, ENABLE);
    67. // 使能串口
    68. USART_Cmd(TEMP_USARTx, ENABLE);
    69. }
    70. /重定向c库函数printf到串口,重定向后可使用printf函数
    71. //int fputc(int ch, FILE *f)
    72. //{
    73. // /* 发送一个字节数据到串口 */
    74. // USART_SendData(WEIGHT_USARTx, (uint8_t) ch);
    75. //
    76. // /* 等待发送完毕 */
    77. // while (USART_GetFlagStatus(WEIGHT_USARTx, USART_FLAG_TXE) == RESET);
    78. //
    79. // return (ch);
    80. //}
    81. /重定向c库函数scanf到串口,重写向后可使用scanf、getchar等函数
    82. //int fgetc(FILE *f)
    83. //{
    84. // /* 等待串口输入数据 */
    85. // while (USART_GetFlagStatus(WEIGHT_USARTx, USART_FLAG_RXNE) == RESET);
    86. // return (int)USART_ReceiveData(WEIGHT_USARTx);
    87. //}
    88. /***************** 发送一个字节 **********************/
    89. void TEMP_SendByte( USART_TypeDef * pUSARTx, uint8_t ch)
    90. {
    91. /* 发送一个字节数据到USART */
    92. USART_SendData(pUSARTx,ch);
    93. /* 等待发送数据寄存器为空 */
    94. while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
    95. }
    96. /****************** 发送8位的数组 ************************/
    97. void TEMP_SendArray( USART_TypeDef * pUSARTx, uint8_t *array, uint16_t num)
    98. {
    99. uint8_t i;
    100. for(i=0; i<num; i++)
    101. {
    102. /* 发送一个字节数据到USART */
    103. TEMP_SendByte(pUSARTx,array[i]);
    104. }
    105. /* 等待发送完成 */
    106. while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET);
    107. }
    108. /***************** 发送字符串 **********************/
    109. void TEMP_SendString( USART_TypeDef * pUSARTx, char *str)
    110. {
    111. unsigned int k=0;
    112. do
    113. {
    114. TEMP_SendByte( pUSARTx, *(str + k) );
    115. printf("%c",*str);
    116. k++;
    117. } while(*(str + k)!='\0');
    118. /* 等待发送完成 */
    119. while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET)
    120. {}
    121. }
    122. /***************** 发送一个16位数 **********************/
    123. void TEMP_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch)
    124. {
    125. uint8_t temp_h, temp_l;
    126. /* 取出高八位 */
    127. temp_h = (ch&0XFF00)>>8;
    128. /* 取出低八位 */
    129. temp_l = ch&0XFF;
    130. /* 发送高八位 */
    131. USART_SendData(pUSARTx,temp_h);
    132. while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
    133. /* 发送低八位 */
    134. USART_SendData(pUSARTx,temp_l);
    135. while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
    136. }

    temp.h

    1. #ifndef __TEMP_H
    2. #define __TEMP_H
    3. #include "stm32f10x.h"
    4. #include <stdio.h>
    5. /**
    6. * 串口宏定义,不同的串口挂载的总线和IO不一样,移植时需要修改这几个宏
    7. * 1-修改总线时钟的宏,uart1挂载到apb2总线,其他uart挂载到apb1总线
    8. * 2-修改GPIO的宏
    9. */
    10. // 串口5-UART5
    11. #define TEMP_USARTx USART3
    12. #define TEMP_USART_CLK RCC_APB1Periph_USART3
    13. #define TEMP_USART_APBxClkCmd RCC_APB1PeriphClockCmd
    14. #define TEMP_USART_BAUDRATE 9600
    15. // USART GPIO 引脚宏定义
    16. #define TEMP_USART_GPIO_CLK (RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO)
    17. #define TEMP_USART_GPIO_APBxClkCmd RCC_APB2PeriphClockCmd
    18. #define TEMP_USART_TX_GPIO_PORT GPIOB
    19. #define TEMP_USART_TX_GPIO_PIN GPIO_Pin_10
    20. #define TEMP_USART_RX_GPIO_PORT GPIOB
    21. #define TEMP_USART_RX_GPIO_PIN GPIO_Pin_11
    22. #define TEMP_USART_IRQ USART3_IRQn
    23. #define TEMP_USART_IRQHandler USART3_IRQHandler
    24. //#define HRM_USARTx USART3
    25. //#define HRM_USART_CLK RCC_APB1Periph_USART3
    26. //#define HRM_USART_APBxClkCmd RCC_APB1PeriphClockCmd
    27. //#define HRM_USART_BAUDRATE 38400
    28. USART GPIO ?????
    29. //#define HRM_USART_GPIO_CLK (RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO)
    30. //#define HRM_USART_GPIO_APBxClkCmd RCC_APB2PeriphClockCmd
    31. //
    32. //#define HRM_USART_TX_GPIO_PORT GPIOB
    33. //#define HRM_USART_TX_GPIO_PIN GPIO_Pin_10
    34. //#define HRM_USART_RX_GPIO_PORT GPIOB
    35. //#define HRM_USART_RX_GPIO_PIN GPIO_Pin_11
    36. //#define HRM_USART_IRQ USART3_IRQn
    37. //#define HRM_USART_IRQHandler USART3_IRQHandler
    38. void TEMP_Config(void);
    39. void TEMP_SendByte( USART_TypeDef * pUSARTx, uint8_t ch);
    40. void TEMP_SendArray( USART_TypeDef * pUSARTx, uint8_t *array, uint16_t num);
    41. void TEMP_SendString( USART_TypeDef * pUSARTx, char *str);
    42. void TEMP_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch);
    43. #endif /* __USART_H */

    中断代码

    1. /**
    2. ******************************************************************************
    3. * @file Project/STM32F10x_StdPeriph_Template/stm32f10x_it.c
    4. * @author MCD Application Team
    5. * @version V3.5.0
    6. * @date 08-April-2011
    7. * @brief Main Interrupt Service Routines.
    8. * This file provides template for all exceptions handler and
    9. * peripherals interrupt service routine.
    10. ******************************************************************************
    11. * @attention
    12. *
    13. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
    14. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
    15. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
    16. * DIRECT, INDIRECT OR CONSEQUENTI
    17. AL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
    18. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
    19. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
    20. *
    21. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
    22. ******************************************************************************
    23. */
    24. /* Includes ------------------------------------------------------------------*/
    25. #include "stm32f10x_it.h"
    26. #include "debug.h"
    27. #include <string.h>
    28. #include <stdlib.h>
    29. #include "m3_systick.h"
    30. #include "temp.h"
    31. /** @addtogroup STM32F10x_StdPeriph_Template
    32. * @{
    33. */
    34. /* Private typedef -----------------------------------------------------------*/
    35. /* Private define ------------------------------------------------------------*/
    36. /* Private macro -------------------------------------------------------------*/
    37. /* Private variables ---------------------------------------------------------*/
    38. /* Private function prototypes -----------------------------------------------*/
    39. /* Private functions ---------------------------------------------------------*/
    40. /******************************************************************************/
    41. /* Cortex-M3 Processor Exceptions Handlers */
    42. /******************************************************************************/
    43. /**
    44. * @brief This function handles NMI exception.
    45. * @param None
    46. * @retval None
    47. */
    48. void NMI_Handler(void)
    49. {
    50. }
    51. /**
    52. * @brief This function handles Hard Fault exception.
    53. * @param None
    54. * @retval None
    55. */
    56. void HardFault_Handler(void)
    57. {
    58. /* Go to infinite loop when Hard Fault exception occurs */
    59. while (1)
    60. {
    61. }
    62. }
    63. /**
    64. * @brief This function handles Memory Manage exception.
    65. * @param None
    66. * @retval None
    67. */
    68. void MemManage_Handler(void)
    69. {
    70. /* Go to infinite loop when Memory Manage exception occurs */
    71. while (1)
    72. {
    73. }
    74. }
    75. /**
    76. * @brief This function handles Bus Fault exception.
    77. * @param None
    78. * @retval None
    79. */
    80. void BusFault_Handler(void)
    81. {
    82. /* Go to infinite loop when Bus Fault exception occurs */
    83. while (1)
    84. {
    85. }
    86. }
    87. /**
    88. * @brief This function handles Usage Fault exception.
    89. * @param None
    90. * @retval None
    91. */
    92. void UsageFault_Handler(void)
    93. {
    94. /* Go to infinite loop when Usage Fault exception occurs */
    95. while (1)
    96. {
    97. }
    98. }
    99. /**
    100. * @brief This function handles SVCall exception.
    101. * @param None
    102. * @retval None
    103. */
    104. void SVC_Handler(void)
    105. {
    106. }
    107. /**
    108. * @brief This function handles Debug Monitor exception.
    109. * @param None
    110. * @retval None
    111. */
    112. void DebugMon_Handler(void)
    113. {
    114. }
    115. /**
    116. * @brief This function handles PendSVC exception.
    117. * @param None
    118. * @retval None
    119. */
    120. void PendSV_Handler(void)
    121. {
    122. }
    123. /**
    124. * @brief This function handles SysTick Handler.
    125. * @param None
    126. * @retval None
    127. */
    128. void SysTick_Handler(void)
    129. {
    130. TimingDelay_Decrement();
    131. }
    132. int TEMPRx=0;
    133. uint8_t temp_buffer[9]; //»º³åÇø
    134. // ²âδ®¿Ú5ÖжϷþÎñº¯Êý
    135. void TEMP_USART_IRQHandler(void)
    136. {
    137. static uint8_t clear=0;
    138. USART_ClearFlag(TEMP_USARTx,USART_FLAG_TC);
    139. if(USART_GetITStatus(TEMP_USARTx,USART_IT_RXNE)!=Bit_RESET)
    140. {
    141. temp_buffer[TEMPRx++] = TEMP_USARTx->DR;
    142. }
    143. else if(USART_GetFlagStatus(TEMP_USARTx,USART_FLAG_IDLE)!=Bit_RESET)
    144. {
    145. clear=TEMP_USARTx->SR;
    146. clear=TEMP_USARTx->DR;
    147. TEMPRx=0;
    148. // Usart_SendString(DEBUG_USARTx,rece_buffer);
    149. // printf("%c%c%c%c%c%c%c%c%c%c",weight_buffer[0],weight_buffer[1],weight_buffer[2],weight_buffer[3],weight_buffer[4],weight_buffer[5],weight_buffer[6],weight_buffer[7],weight_buffer[9],weight_buffer[10]);
    150. // myflag=1;
    151. }
    152. }
    153. /**
    154. * @brief This function handles PPP interrupt request.
    155. * @param None
    156. * @retval None
    157. */
    158. /*void PPP_IRQHandler(void)
    159. {
    160. }*/
    161. /**
    162. * @}
    163. */
    164. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

    demo代码链接:

    链接:https://pan.baidu.com/s/16dod5S_lxpwTnI12HvqYPQ 
    提取码:8442

  • 相关阅读:
    【mia】live2rtc 的adapter及RTCPeer发布
    测试需要写测试用例吗?
    C++学习规划“的 PPT 大纲设计
    【web前端期末大作业】html网上在线书城大学生静态网页 大学生html当当书城仿站 网上书城购物网页作业HTML
    简单理解MySQL的存储引擎
    餐饮机器人AB面:有人离场、有人挺进
    小谈设计模式(13)—外观模式
    Controller返回JSON数据
    奖励 CSDN 社区的领军人物
    Biotin-LC(CAS:72040-64-3)含有的 NHS 酯起到哪些化学效应了?
  • 原文地址:https://blog.csdn.net/weixin_41583631/article/details/125509058