STM32测温枪

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

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 +
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值