前段时间有靓仔私聊问我,要关于字节流串口通信的代码,今天根据项目代码进行公示
main主函数
- #include <stdio.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <string.h>
- #include "stm32f10x.h"
- #include "debug.h"
- #include "stm32f10x_it.h"
- #include "m3_systick.h"
- #include "bsp_key.h"
- #include "ble.h"
- #include "led.h"
- #include "temp.h"
-
-
- uint8_t display_meun=0;
-
-
- typedef struct temp_body
- {
- uint8_t onebyte;
- uint8_t twobyte;
- uint8_t threebyte;
- uint8_t fourbyte;
- }temp_test;
-
-
-
- temp_test mytemp;
-
-
- void temp_start(temp_test *mytemp);
- void temp_send_data(temp_test *mytemp,uint8_t temp_len);
-
- //***************************全局变量**********************************************//
- extern char BLE_USART_BUF[256]; //蓝牙串口数据缓冲区
- extern uint8_t temp_buffer[9]; //测温串口数据缓冲区
-
- /**
- * @brief 主函数
- * @param 无
- * @retval 无
- */
-
-
- int main(void)
- {
- uint8_t data_flag=0;
- char dud[15]={0};
-
-
- //环境温度
- static uint16_t environment_temp=0;
- static uint8_t environment_temp_h=0;
- static uint8_t environment_temp_l=0;
- static float final_environment_temp=0;
-
- //被测物体温度
- uint16_t object_temp=0;
- uint8_t object_temp_h=0;
- uint8_t object_temp_l=0;
- float final_object_temp=0;
-
- //人体温度
- uint16_t body_temp=0;
- uint8_t body_temp_h=0;
- uint8_t body_temp_l=0;
- float final_body_temp=0;
-
- m3_Systick_Init();
-
- LED_GPIO_Config();
- LED_ON;
-
- /*初始化USART1 配置模式为 9600 8-N-1,用于串口调试*/
- Debug_Config(); //串口调试();
-
- /*初始化UART4 配置模式为 9600 8-N-1,中断接收 接收发蓝牙数据*/
- BLE_Init();
-
- /*初始化USART3 配置模式为 9600 8-N-1,中断接收 接收发测温数据*/
- TEMP_Config(); //
-
- while(1)
- {
-
-
-
- if(strstr(BLE_USART_BUF,"temp")!=NULL)
- {
- data_flag=1;
- sprintf(dud,"temp_on");
- macBLE_Usart(dud);
- memset(dud,'\0',15);
- memset(BLE_USART_BUF,'\0',256);
- LED_ON;
- printf("temp_start\r\n");
- }
-
- //***************条件模式选择*************************//
- //测温的********************************start******************//
- if(data_flag==1)
- {
- temp_start(&mytemp);
- temp_send_data(&mytemp,4);
- 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]);
-
- //环境温度
- environment_temp_h = temp_buffer[2];
- environment_temp_l = temp_buffer[3];
- environment_temp = environment_temp_h<<8;
- environment_temp |= environment_temp_l;
- final_environment_temp = (float)(environment_temp/10.0f);
-
- //被测物体温度
- object_temp_h = temp_buffer[4];
- object_temp_l = temp_buffer[5];
- object_temp = object_temp_h<<8;
- object_temp |= object_temp_l;
- final_object_temp = (float)(object_temp/10.0f);
-
- //人体温度
- body_temp_h = temp_buffer[6];
- body_temp_l = temp_buffer[7];
- body_temp = body_temp_h<<8;
- body_temp |= body_temp_l;
- final_body_temp = (float)(body_temp/10.0f);
-
- if(final_body_temp>34.0f)
- {
-
- printf("%.2f-%.2f-%.2f\r\n",final_environment_temp,final_object_temp,final_body_temp);
- sprintf(dud,"temp:%.2f,%.2f,%.2f",final_environment_temp,final_object_temp,final_body_temp);
- macBLE_Usart(dud);
- LED_OFF;
- data_flag=66;
- }
-
- printf("temp:%.2f,%.2f,%.2f",final_environment_temp,final_object_temp,final_body_temp);
-
- memset(temp_buffer,'\0',9);
- memset(dud,'\0',15);
- Delay_ms(20);
- }
-
-
- if(data_flag==66) //缓冲区
- {
-
- printf("data_flag:66\r\n");
- LED_OFF;
- }
-
- }
- }
-
-
-
- //**************************************测温计***************************************//
- //开始测试 5a 01 00 5b
- void temp_start(temp_test *mytemp)
- {
- mytemp->onebyte = 0x5A;
- mytemp->twobyte = 0x01;
- mytemp->threebyte = 0x00;
- mytemp->fourbyte = 0x5B;
- }
-
- void temp_send_data(temp_test *mytemp,uint8_t temp_len)
- {
- static uint8_t date=0,i=0;
- for(i=0;i<temp_len;i++)
- {
- date = *(((uint8_t *)&mytemp->onebyte)+i);
- USART_SendData(TEMP_USARTx,date);
- while(USART_GetFlagStatus(TEMP_USARTx,USART_FLAG_TC)!= SET);
- }
- }
-
-
- /*********************************************END OF FILE**********************/
ble部分代码
ble.c
- #include "ble.h"
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdarg.h>
- #include <string.h>
- #include <math.h>
- #include "m3_systick.h"
-
-
- static void BLE_USART_Config( void );
- static void BLE_USART_NVIC_Configuration( void );
-
-
- struct STRUCT_USARTx_Fram strBLE_Fram_Record = { 0 };
-
-
- static char * itoa( int value, char * string, int radix );
-
- /*
- * 函数名:USART2_printf
- * 描述 :格式化输出,类似于C库中的printf,但这里没有用到C库
- * 输入 :-USARTx 串口通道,这里只用到了串口2,即USART2
- * -Data 要发送到串口的内容的指针
- * -... 其他参数
- * 输出 :无
- * 返回 :无
- * 调用 :外部调用
- * 典型应用USART2_printf( USART2, "\r\n this is a demo \r\n" );
- * USART2_printf( USART2, "\r\n %d \r\n", i );
- * USART2_printf( USART2, "\r\n %s \r\n", j );
- */
- void USART_printf ( USART_TypeDef * USARTx, char * Data, ... )
- {
- const char *s;
- int d;
- char buf[16];
-
-
- va_list ap;
- va_start(ap, Data);
-
- while ( * Data != 0 ) // 判断是否到达字符串结束符
- {
- if ( * Data == 0x5c ) //'\'
- {
- switch ( *++Data )
- {
- case 'r': //回车符
- USART_SendData(USARTx, 0x0d);
- Data ++;
- break;
-
- case 'n': //换行符
- USART_SendData(USARTx, 0x0a);
- Data ++;
- break;
-
- default:
- Data ++;
- break;
- }
- }
-
- else if ( * Data == '%')
- { //
- switch ( *++Data )
- {
- case 's': //字符串
- s = va_arg(ap, const char *);
-
- for ( ; *s; s++)
- {
- USART_SendData(USARTx,*s);
- while( USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET );
- }
-
- Data++;
-
- break;
-
- case 'd':
- //十进制
- d = va_arg(ap, int);
-
- itoa(d, buf, 10);
-
- for (s = buf; *s; s++)
- {
- USART_SendData(USARTx,*s);
- while( USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET );
- }
-
- Data++;
-
- break;
-
- default:
- Data++;
-
- break;
-
- }
- }
-
- else USART_SendData(USARTx, *Data++);
-
- while ( USART_GetFlagStatus ( USARTx, USART_FLAG_TXE ) == RESET );
-
- }
- }
-
-
- /*
- * 函数名:itoa
- * 描述 :将整形数据转换成字符串
- * 输入 :-radix =10 表示10进制,其他结果为0
- * -value 要转换的整形数
- * -buf 转换后的字符串
- * -radix = 10
- * 输出 :无
- * 返回 :无
- * 调用 :被USART2_printf()调用
- */
- static char * itoa( int value, char *string, int radix )
- {
- int i, d;
- int flag = 0;
- char *ptr = string;
-
- /* This implementation only works for decimal numbers. */
- if (radix != 10)
- {
- *ptr = 0;
- return string;
- }
-
- if (!value)
- {
- *ptr++ = 0x30;
- *ptr = 0;
- return string;
- }
-
- /* if this is a negative value insert the minus sign. */
- if (value < 0)
- {
- *ptr++ = '-';
-
- /* Make the value positive. */
- value *= -1;
-
- }
-
- for (i = 10000; i > 0; i /= 10)
- {
- d = value / i;
-
- if (d || flag)
- {
- *ptr++ = (char)(d + 0x30);
- value -= (d * i);
- flag = 1;
- }
- }
-
- /* Null terminate the string. */
- *ptr = 0;
-
- return string;
-
- } /* NCL_Itoa */
-
-
-
-
-
-
- /**
- * @brief 初始化BLE用到的 USART
- * @param 无
- * @retval 无
- */
- static void BLE_USART_Config ( void )
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
-
-
- /* config USART clock */
- macBLE_USART_APBxClock_FUN ( macBLE_USART_CLK, ENABLE );
- macBLE_USART_GPIO_APBxClock_FUN ( macBLE_USART_GPIO_CLK, ENABLE );
-
- /* USART GPIO config */
- /* Configure USART Tx as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = macBLE_USART_TX_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(macBLE_USART_TX_PORT, &GPIO_InitStructure);
-
- /* Configure USART Rx as input floating */
- GPIO_InitStructure.GPIO_Pin = macBLE_USART_RX_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(macBLE_USART_RX_PORT, &GPIO_InitStructure);
-
- /* USART1 mode config */
- USART_InitStructure.USART_BaudRate = macBLE_USART_BAUD_RATE;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No ;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(macBLE_USARTx, &USART_InitStructure);
-
-
- /* 中断配置 */
- USART_ITConfig ( macBLE_USARTx, USART_IT_RXNE, ENABLE ); //使能串口接收中断
- USART_ITConfig ( macBLE_USARTx, USART_IT_IDLE, ENABLE ); //使能串口总线空闲中断
-
- BLE_USART_NVIC_Configuration ();
-
-
- USART_Cmd(macBLE_USARTx, ENABLE);
-
-
- }
-
-
- /**
- * @brief 配置 BLE USART 的 NVIC 中断
- * @param 无
- * @retval 无
- */
- static void BLE_USART_NVIC_Configuration ( void )
- {
- NVIC_InitTypeDef NVIC_InitStructure;
-
-
- /* Configure the NVIC Preemption Priority Bits */
- NVIC_PriorityGroupConfig ( macNVIC_PriorityGroup_x );
-
- /* Enable the USART2 Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = macBLE_USART_IRQ;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- }
-
-
- void USART3_SendByte(uint8_t ch)
- {
- /* 发送一个字节数据到USART */
- USART_SendData(macBLE_USARTx,ch);
-
- /* 等待发送数据寄存器为空 */
- while (USART_GetFlagStatus(macBLE_USARTx, USART_FLAG_TXE) == RESET);
- }
-
-
- /***************** 发送字符串 **********************/
- void USART3_SendString(char *str)
- {
- unsigned int k=0;
- do
- {
- USART3_SendByte(*(str + k));
- k++;
- } while(*(str + k)!='\0');
-
- /* 等待发送完成 */
- while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET)
- {}
- }
-
- /*
- * 函数名:BLE_Cmd
- * 描述 :对WF-BLE模块发送AT指令
- * 输入 :cmd,待发送的指令
- * reply1,reply2,期待的响应,为NULL表不需响应,两者为或逻辑关系
- * waittime,等待响应的时间
- * 返回 : 1,指令发送成功
- * 0,指令发送失败
- * 调用 :被外部调用
- */
- bool BLE_Cmd ( char * cmd, char * reply1, char * reply2, u32 waittime )
- {
- memset(strBLE_Fram_Record .Data_RX_BUF,0,strBLE_Fram_Record .InfBit .FramLength);
- strBLE_Fram_Record .InfBit .FramLength = 0; //从新开始接收新的数据包
- macBLE_Usart ( "%s", cmd );
- printf ( "%s\r\n", cmd );
- if ( ( reply1 == 0 ) && ( reply2 == 0 ) ) //不需要接收数据
- return true;
-
- Delay_ms ( waittime ); //延时
-
- strBLE_Fram_Record .Data_RX_BUF [ strBLE_Fram_Record .InfBit .FramLength ] = '\0';
-
- printf ( "%s", strBLE_Fram_Record .Data_RX_BUF );
-
- if ( ( reply1 != 0 ) && ( reply2 != 0 ) )
- return ( ( bool ) strstr ( strBLE_Fram_Record .Data_RX_BUF, reply1 ) ||
- ( bool ) strstr ( strBLE_Fram_Record .Data_RX_BUF, reply2 ) );
-
- else if ( reply1 != 0 )
- return ( ( bool ) strstr ( strBLE_Fram_Record .Data_RX_BUF, reply1 ) );
-
- else
- return ( ( bool ) strstr ( strBLE_Fram_Record .Data_RX_BUF, reply2 ) );
-
- }
-
-
- /**
- * @brief BLE初始化函数
- * @param 无
- * @retval 无
- */
- void BLE_Init ( void )
- {
-
- BLE_USART_Config ();
-
- }
-
- char BLE_USART_BUF[256] = {0};
-
- /**
- * @brief This function handles macESP8266_USARTx Handler.
- * @param None
- * @retval None
- */
- void macBLE_USART_INT_FUN ( void )
- {
- uint8_t ucCh;
- static u8 i;
- if( USART_GetITStatus ( macBLE_USARTx, USART_IT_RXNE ) != RESET )
- {
- ucCh = USART_ReceiveData( macBLE_USARTx );
- BLE_USART_BUF[i++] = ucCh;
- }
- if( USART_GetITStatus( macBLE_USARTx, USART_IT_IDLE ) == SET ) //???????
- {
- ucCh = USART_ReceiveData( macBLE_USARTx );
- BLE_USART_BUF[i] = '\0';
- // printf("%s\r\n",BLE_USART_BUF);
- // recvDataFromBleCustomerAndProcess(BLE_USART_BUF);
- i = 0;
- }
- }
-
-
ble.h
- #ifndef _BLE_H_
- #define _BLE_H_
-
- #include "stm32f10x.h"
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdarg.h>
- #include <stdbool.h>
- #include <math.h>
-
-
- #if defined ( __CC_ARM )
- #pragma anon_unions
- #endif
-
-
- #define macNVIC_PriorityGroup_x NVIC_PriorityGroup_2
- void USART_printf ( USART_TypeDef * USARTx, char * Data, ... );
-
- /******************************* ESP8266 外部全局变量声明 ***************************/
- #define RX_BUF_MAX_LEN 256 //最大接收缓存字节数
-
- extern struct STRUCT_USARTx_Fram //串口数据帧的处理结构体
- {
- char Data_RX_BUF [ RX_BUF_MAX_LEN ];
-
- union {
- __IO u16 InfAll;
- struct {
- __IO u16 FramLength :15; // 14:0
- __IO u16 FramFinishFlag :1; // 15
- } InfBit;
- };
-
- } strBLE_Fram_Record;
-
-
- //位带操作,实现51类似的GPIO控制功能
- //具体实现思想,参考<<CM3权威指南>>第五章(87页~92页).
- //IO口操作宏定义
- #define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
- #define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
- #define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
- //IO口地址映射
- #define GPIOA_ODR_Addr (GPIOA_BASE+12) //0x4001080C
- #define GPIOB_ODR_Addr (GPIOB_BASE+12) //0x40010C0C
- #define GPIOC_ODR_Addr (GPIOC_BASE+12) //0x4001100C
- #define GPIOD_ODR_Addr (GPIOD_BASE+12) //0x4001140C
- #define GPIOE_ODR_Addr (GPIOE_BASE+12) //0x4001180C
- #define GPIOF_ODR_Addr (GPIOF_BASE+12) //0x40011A0C
- #define GPIOG_ODR_Addr (GPIOG_BASE+12) //0x40011E0C
-
- #define GPIOA_IDR_Addr (GPIOA_BASE+8) //0x40010808
- #define GPIOB_IDR_Addr (GPIOB_BASE+8) //0x40010C08
- #define GPIOC_IDR_Addr (GPIOC_BASE+8) //0x40011008
- #define GPIOD_IDR_Addr (GPIOD_BASE+8) //0x40011408
- #define GPIOE_IDR_Addr (GPIOE_BASE+8) //0x40011808
- #define GPIOF_IDR_Addr (GPIOF_BASE+8) //0x40011A08
- #define GPIOG_IDR_Addr (GPIOG_BASE+8) //0x40011E08
- //IO口操作,只对单一的IO口!
- //确保n的值小于16!
- #define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n) //输出
- #define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n) //输入
-
- #define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n) //输出
- #define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n) //输入
-
- #define PCout(n) BIT_ADDR(GPIOC_ODR_Addr,n) //输出
- #define PCin(n) BIT_ADDR(GPIOC_IDR_Addr,n) //输入
-
- #define PDout(n) BIT_ADDR(GPIOD_ODR_Addr,n) //输出
- #define PDin(n) BIT_ADDR(GPIOD_IDR_Addr,n) //输入
-
- #define PEout(n) BIT_ADDR(GPIOE_ODR_Addr,n) //输出
- #define PEin(n) BIT_ADDR(GPIOE_IDR_Addr,n) //输入
-
- #define PFout(n) BIT_ADDR(GPIOF_ODR_Addr,n) //输出
- #define PFin(n) BIT_ADDR(GPIOF_IDR_Addr,n) //输入
-
- #define PGout(n) BIT_ADDR(GPIOG_ODR_Addr,n) //输出
- #define PGin(n) BIT_ADDR(GPIOG_IDR_Addr,n) //输入
-
-
-
- //#define macBLE_RST_LOW_LEVEL() GPIO_ResetBits(macBLE_RST_PORT,macBLE_RST_PIN)
- //#define macBLE_RST_HIGH_LEVEL() GPIO_SetBits(macBLE_RST_PORT,macBLE_RST_PIN)
- //#define Read_macBLE_READY() GPIO_ReadInputDataBit(macBLE_READY_PORT,macBLE_READY_PIN)
- //#define Read_macBLE_LINK() GPIO_ReadInputDataBit(macBLE_LINK_PORT,macBLE_LINK_PIN)
-
- #define macBLE_RST PBout(9)
- #define macBLE_MODE PBout(8)
- #define macBLE_DISC PBout(7)
- #define macBLE_LINK PBin(6)
- /********************************wifi串口配置宏*****************************************/
- #define macBLE_USART_BAUD_RATE 9600
-
- #define macBLE_USARTx UART4
- #define macBLE_USART_APBxClock_FUN RCC_APB1PeriphClockCmd
- #define macBLE_USART_CLK RCC_APB1Periph_UART4
-
- #define macBLE_USART_GPIO_APBxClock_FUN RCC_APB2PeriphClockCmd
- #define macBLE_USART_GPIO_CLK RCC_APB2Periph_GPIOC
- #define macBLE_USART_TX_PORT GPIOC
- #define macBLE_USART_TX_PIN GPIO_Pin_10
- #define macBLE_USART_RX_PORT GPIOC
- #define macBLE_USART_RX_PIN GPIO_Pin_11
- #define macBLE_USART_IRQ UART4_IRQn
- #define macBLE_USART_INT_FUN UART4_IRQHandler
-
-
-
-
-
- /*********************************************** ESP8266 函数宏定义 *******************************************/
- #define macBLE_Usart( fmt, ... ) USART_printf ( macBLE_USARTx, fmt, ##__VA_ARGS__ )
- #define macPC_Usart( fmt, ... ) printf ( fmt, ##__VA_ARGS__ )
-
-
-
-
- /****************************************** ESP8266 函数声明 ***********************************************/
-
- void BLE_Init ( void );
- void USART3_SendByte(uint8_t ch);
- void USART3_SendString(char *str);
- bool BLE_Cmd ( char * cmd, char * reply1, char * reply2, u32 waittime );
-
- #endif
-
-
-
测温模块部分
temp.c
- #include "temp.h"
- #include "debug.h"
- /**
- * @brief 配置嵌套向量中断控制器NVIC
- * @param 无
- * @retval 无
- */
- //static void NVIC_Configuration(void)
- //{
-
- //}
-
- /**
- * @brief USART GPIO 配置,工作参数配置
- * @param 无
- * @retval 无
- */
- void TEMP_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
-
- // 打开串口GPIO的时钟
- TEMP_USART_GPIO_APBxClkCmd(TEMP_USART_GPIO_CLK, ENABLE);
-
- // 打开串口外设的时钟
- TEMP_USART_APBxClkCmd(TEMP_USART_CLK, ENABLE);
-
- // 将USART Tx的GPIO配置为推挽复用模式
- GPIO_InitStructure.GPIO_Pin = TEMP_USART_TX_GPIO_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(TEMP_USART_TX_GPIO_PORT, &GPIO_InitStructure);
-
- // 将USART Rx的GPIO配置为浮空输入模式
- GPIO_InitStructure.GPIO_Pin = TEMP_USART_RX_GPIO_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(TEMP_USART_RX_GPIO_PORT, &GPIO_InitStructure);
-
- // 配置串口的工作参数
- // 配置波特率
- USART_InitStructure.USART_BaudRate = TEMP_USART_BAUDRATE;
- // 配置 针数据字长
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- // 配置停止位
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- // 配置校验位
- USART_InitStructure.USART_Parity = USART_Parity_No ;
- // 配置硬件流控制
- USART_InitStructure.USART_HardwareFlowControl =
- USART_HardwareFlowControl_None;
- // 配置工作模式,收发一起
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- // 完成串口的初始化配置
- USART_Init(TEMP_USARTx, &USART_InitStructure);
-
- // 串口中断优先级配置
- NVIC_InitTypeDef NVIC_InitStructure;
-
- /* 嵌套向量中断控制器组选择 */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
-
- /* 配置USART为中断源 */
- NVIC_InitStructure.NVIC_IRQChannel = TEMP_USART_IRQ;
- /* 抢断优先级*/
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- /* 子优先级 */
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
- /* 使能中断 */
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- /* 初始化配置NVIC */
- NVIC_Init(&NVIC_InitStructure);
-
- // 使能串口接收中断
- USART_ITConfig(TEMP_USARTx, USART_IT_RXNE, ENABLE);
-
- // 使能串口空闲中断
- USART_ITConfig(TEMP_USARTx, USART_IT_IDLE, ENABLE);
-
- // 使能串口
- USART_Cmd(TEMP_USARTx, ENABLE);
- }
-
- /重定向c库函数printf到串口,重定向后可使用printf函数
- //int fputc(int ch, FILE *f)
- //{
- // /* 发送一个字节数据到串口 */
- // USART_SendData(WEIGHT_USARTx, (uint8_t) ch);
- //
- // /* 等待发送完毕 */
- // while (USART_GetFlagStatus(WEIGHT_USARTx, USART_FLAG_TXE) == RESET);
- //
- // return (ch);
- //}
-
- /重定向c库函数scanf到串口,重写向后可使用scanf、getchar等函数
- //int fgetc(FILE *f)
- //{
- // /* 等待串口输入数据 */
- // while (USART_GetFlagStatus(WEIGHT_USARTx, USART_FLAG_RXNE) == RESET);
-
- // return (int)USART_ReceiveData(WEIGHT_USARTx);
- //}
-
- /***************** 发送一个字节 **********************/
- void TEMP_SendByte( USART_TypeDef * pUSARTx, uint8_t ch)
- {
- /* 发送一个字节数据到USART */
- USART_SendData(pUSARTx,ch);
-
- /* 等待发送数据寄存器为空 */
- while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
- }
-
- /****************** 发送8位的数组 ************************/
- void TEMP_SendArray( USART_TypeDef * pUSARTx, uint8_t *array, uint16_t num)
- {
- uint8_t i;
-
- for(i=0; i<num; i++)
- {
- /* 发送一个字节数据到USART */
- TEMP_SendByte(pUSARTx,array[i]);
-
- }
- /* 等待发送完成 */
- while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET);
- }
-
- /***************** 发送字符串 **********************/
- void TEMP_SendString( USART_TypeDef * pUSARTx, char *str)
- {
- unsigned int k=0;
- do
- {
- TEMP_SendByte( pUSARTx, *(str + k) );
- printf("%c",*str);
- k++;
- } while(*(str + k)!='\0');
-
- /* 等待发送完成 */
- while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET)
- {}
- }
-
- /***************** 发送一个16位数 **********************/
- void TEMP_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch)
- {
- uint8_t temp_h, temp_l;
-
- /* 取出高八位 */
- temp_h = (ch&0XFF00)>>8;
- /* 取出低八位 */
- temp_l = ch&0XFF;
-
- /* 发送高八位 */
- USART_SendData(pUSARTx,temp_h);
- while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
-
- /* 发送低八位 */
- USART_SendData(pUSARTx,temp_l);
- while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
- }
-
-
-
temp.h
- #ifndef __TEMP_H
- #define __TEMP_H
-
-
- #include "stm32f10x.h"
- #include <stdio.h>
-
- /**
- * 串口宏定义,不同的串口挂载的总线和IO不一样,移植时需要修改这几个宏
- * 1-修改总线时钟的宏,uart1挂载到apb2总线,其他uart挂载到apb1总线
- * 2-修改GPIO的宏
- */
-
-
- // 串口5-UART5
- #define TEMP_USARTx USART3
- #define TEMP_USART_CLK RCC_APB1Periph_USART3
- #define TEMP_USART_APBxClkCmd RCC_APB1PeriphClockCmd
- #define TEMP_USART_BAUDRATE 9600
-
- // USART GPIO 引脚宏定义
- #define TEMP_USART_GPIO_CLK (RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO)
- #define TEMP_USART_GPIO_APBxClkCmd RCC_APB2PeriphClockCmd
-
- #define TEMP_USART_TX_GPIO_PORT GPIOB
- #define TEMP_USART_TX_GPIO_PIN GPIO_Pin_10
- #define TEMP_USART_RX_GPIO_PORT GPIOB
- #define TEMP_USART_RX_GPIO_PIN GPIO_Pin_11
-
- #define TEMP_USART_IRQ USART3_IRQn
- #define TEMP_USART_IRQHandler USART3_IRQHandler
-
-
- //#define HRM_USARTx USART3
- //#define HRM_USART_CLK RCC_APB1Periph_USART3
- //#define HRM_USART_APBxClkCmd RCC_APB1PeriphClockCmd
- //#define HRM_USART_BAUDRATE 38400
-
- USART GPIO ?????
- //#define HRM_USART_GPIO_CLK (RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO)
- //#define HRM_USART_GPIO_APBxClkCmd RCC_APB2PeriphClockCmd
- //
- //#define HRM_USART_TX_GPIO_PORT GPIOB
- //#define HRM_USART_TX_GPIO_PIN GPIO_Pin_10
- //#define HRM_USART_RX_GPIO_PORT GPIOB
- //#define HRM_USART_RX_GPIO_PIN GPIO_Pin_11
-
- //#define HRM_USART_IRQ USART3_IRQn
- //#define HRM_USART_IRQHandler USART3_IRQHandler
-
- void TEMP_Config(void);
- void TEMP_SendByte( USART_TypeDef * pUSARTx, uint8_t ch);
- void TEMP_SendArray( USART_TypeDef * pUSARTx, uint8_t *array, uint16_t num);
-
- void TEMP_SendString( USART_TypeDef * pUSARTx, char *str);
- void TEMP_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch);
-
- #endif /* __USART_H */
中断代码
- /**
- ******************************************************************************
- * @file Project/STM32F10x_StdPeriph_Template/stm32f10x_it.c
- * @author MCD Application Team
- * @version V3.5.0
- * @date 08-April-2011
- * @brief Main Interrupt Service Routines.
- * This file provides template for all exceptions handler and
- * peripherals interrupt service routine.
- ******************************************************************************
- * @attention
- *
- * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
- * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
- * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
- * DIRECT, INDIRECT OR CONSEQUENTI
-
- AL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
- * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
- * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
- *
- * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
- ******************************************************************************
- */
-
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x_it.h"
- #include "debug.h"
- #include <string.h>
- #include <stdlib.h>
- #include "m3_systick.h"
- #include "temp.h"
-
-
-
- /** @addtogroup STM32F10x_StdPeriph_Template
- * @{
- */
-
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
-
- /******************************************************************************/
- /* Cortex-M3 Processor Exceptions Handlers */
- /******************************************************************************/
-
- /**
- * @brief This function handles NMI exception.
- * @param None
- * @retval None
- */
- void NMI_Handler(void)
- {
- }
-
- /**
- * @brief This function handles Hard Fault exception.
- * @param None
- * @retval None
- */
- void HardFault_Handler(void)
- {
- /* Go to infinite loop when Hard Fault exception occurs */
- while (1)
- {
- }
- }
-
- /**
- * @brief This function handles Memory Manage exception.
- * @param None
- * @retval None
- */
- void MemManage_Handler(void)
- {
- /* Go to infinite loop when Memory Manage exception occurs */
- while (1)
- {
- }
- }
-
- /**
- * @brief This function handles Bus Fault exception.
- * @param None
- * @retval None
- */
- void BusFault_Handler(void)
- {
- /* Go to infinite loop when Bus Fault exception occurs */
- while (1)
- {
- }
- }
-
- /**
- * @brief This function handles Usage Fault exception.
- * @param None
- * @retval None
- */
- void UsageFault_Handler(void)
- {
- /* Go to infinite loop when Usage Fault exception occurs */
- while (1)
- {
- }
- }
-
- /**
- * @brief This function handles SVCall exception.
- * @param None
- * @retval None
- */
- void SVC_Handler(void)
- {
- }
-
- /**
- * @brief This function handles Debug Monitor exception.
- * @param None
- * @retval None
- */
- void DebugMon_Handler(void)
- {
- }
-
- /**
- * @brief This function handles PendSVC exception.
- * @param None
- * @retval None
- */
- void PendSV_Handler(void)
- {
- }
-
- /**
- * @brief This function handles SysTick Handler.
- * @param None
- * @retval None
- */
-
-
-
- void SysTick_Handler(void)
- {
- TimingDelay_Decrement();
- }
-
- int TEMPRx=0;
- uint8_t temp_buffer[9]; //»º³åÇø
-
- // ²âδ®¿Ú5ÖжϷþÎñº¯Êý
- void TEMP_USART_IRQHandler(void)
- {
- static uint8_t clear=0;
- USART_ClearFlag(TEMP_USARTx,USART_FLAG_TC);
-
- if(USART_GetITStatus(TEMP_USARTx,USART_IT_RXNE)!=Bit_RESET)
- {
- temp_buffer[TEMPRx++] = TEMP_USARTx->DR;
- }
- else if(USART_GetFlagStatus(TEMP_USARTx,USART_FLAG_IDLE)!=Bit_RESET)
- {
- clear=TEMP_USARTx->SR;
- clear=TEMP_USARTx->DR;
- TEMPRx=0;
- // Usart_SendString(DEBUG_USARTx,rece_buffer);
- // 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]);
- // myflag=1;
- }
- }
-
-
-
-
- /**
- * @brief This function handles PPP interrupt request.
- * @param None
- * @retval None
- */
- /*void PPP_IRQHandler(void)
- {
- }*/
-
- /**
- * @}
- */
-
-
- /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
demo代码链接:
链接:https://pan.baidu.com/s/16dod5S_lxpwTnI12HvqYPQ
提取码:8442