锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

STM32+二氧化碳传感器(FS00301)

时间:2023-04-24 23:07:00 立准传感器

配置串口4

uart.c

u8 USART4_RX_BUF[USART_REC_LEN];     USART_REC_LEN个字节u16 USART4_RX_STA=0;       //接收状态标记	void uart4_init(u32 bound){  //GPIO端口设置    GPIO_InitTypeDef GPIO_InitStructure;	USART_InitTypeDef USART_InitStructure;	NVIC_InitTypeDef NVIC_InitStructure;	 	RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);	//使能USART1,GPIOA时钟  	//USART2_TX   GPIOA.9  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PA.2  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	//复用推挽输出  GPIO_Init(GPIOC,&GPIO_InitStructure);//初始化GPIOA.9   USART2_RX	  GPIOA.10初始化  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//PA3  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;///浮空输入  GPIO_Init(GPIOC,&GPIO_InitStructure);//初始化GPIOA.10    ///Usart1 NVIC 配置  NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//抢占优先级1	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;		//子优先级1	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ通道使能	NVIC_Init(&NVIC_InitStructure);	//根据指定的参数初始化VIC   USART 初始化设置	USART_InitStructure.USART_BaudRate = bound;//串口波特率	USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式	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(UART4,&USART_InitStructure); ///初始化串口1    USART_ITConfig(UART4,USART_IT_RXNE,ENABLE);///打开串口接受中断   USART_Cmd(UART4,ENABLE);             void UART4_Send_Data(u8 *buf,u16 len){    u16 t;    for(t=0;t

uart.h

#ifndef __USART_H#define __USART_H#include "stdio.h"	#include "sys.h" #define USART_REC_LEN  			20  	//定义最大接收字节数 200extern u16 USART4_RX_STA;         		//接收状态标记	extern u8  USART4_RX_BUF[USART_REC_LEN];void uart4_init(u32 bound);void UART4_Send_Data(u8 *buf,u16 len);

二氧化碳传感器

CO2.C

#include "CO2.h"/**************************************************计算二氧化碳值的函数**************************************************/int hextoDec(int hex){	 int sum=0,mul=1;	 int i,r;	 int count=0;	 do{	  r=hex;	  for(i=0;iSR&0x0080)==0);UART4->DR=tr[i];     

CO2.H

#ifndef __CO2_H#define __CO2_H#include "stdio.h"	#include "sys.h" #include "usart.h"	int hextoDec(int hex);void DATA(void);#endif

主函数 main.c

CO2);			for(i=0;i<5;i  )			delay_ms(1000);}	  }CO2);			for(i=0;i<5;i  )			delay_ms(1000);}	  }

锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章