温湿度传感器采集

1)解释什么是“软件I2C”和“硬件I2C”? (阅读野火配套教材的第23章“I2C--读写EEPROM”原理章节)

硬件I2C对应芯片上的I2C外设,没有相应I2C驱动电路,其所使用的的I2C管脚也是专用的,其效率远高于软件I2C;硬件I2C一般也较为稳定,但是程序更繁琐,硬件I2C是直接调用寄存器的

软件I2C一般是使用GPIO管脚,用软件控制管脚状态以模拟I2C通信波形,模拟I2C协议的时序。

2)阅读AHT20数据手册,编程实现:每隔2秒钟采集一次温湿度数据,并通过串口发送到上位机(win10)。

所需器材:1)STM32最小核心板(STM32F103C8T6)

​ 2)奥松AHT21B温湿度传感器

​ 3)USB转TTL(ch340芯片)

​ 4)IDE:keil5

注意:AHT20有四根针脚,分别为SCL、SOA、GND、VCC
GND接地;VCC接电源;SCL是传输线对应STM32核心板B6,SOA时钟线对应STM32核心板B7

代码:

main.c

#include "delay.h"
#include "usart.h"
#include "i2c.h"


int main(void)
{	
	delay_init();  
	uart_init(115200);
	IIC_Init();
		while(1)
	{
		printf("开始测量,请稍等:  ");
		read_AHT20_once();
		delay_ms(1500);
  }
}

delay.c

#include "delay.h"
#include "sys.h"
#if SYSTEM_SUPPORT_UCOS
#include "includes.h"	  
#endif
static u8  fac_us=0;
static u16 fac_ms=0;
#ifdef OS_CRITICAL_METHOD 	
void SysTick_Handler(void)
{				   
	OSIntEnter();		
    OSTimeTick();      
    OSIntExit();      
#endif

void delay_init()	 
{

#ifdef OS_CRITICAL_METHOD 	
	u32 reload;
#endif
	SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);	
	fac_us=SystemCoreClock/8000000;	 
	 
#ifdef OS_CRITICAL_METHOD 	
	reload=SystemCoreClock/8000000;		  
	reload*=1000000/OS_TICKS_PER_SEC;
	fac_ms=1000/OS_TICKS_PER_SEC;	   
	SysTick->CTRL|=SysTick_CTRL_TICKINT_Msk;
	SysTick->LOAD=reload;
	SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk; 
#else
	fac_ms=(u16)fac_us*1000;
#endif
}								    

#ifdef OS_CRITICAL_METHOD		    								   
void delay_us(u32 nus)
{		
	u32 ticks;
	u32 told,tnow,tcnt=0;
	u32 reload=SysTick->LOAD;	    	 
	ticks=nus*fac_us;   		 
	tcnt=0;
	told=SysTick->VAL; 
	while(1)
	{
		tnow=SysTick->VAL;	
		if(tnow!=told)
		{	    
			if(tnow<told)tcnt+=told-tnow;
			else tcnt+=reload-tnow+told;	    
			told=tnow;
			if(tcnt>=ticks)break;
		}  
	}; 									    
}

void delay_ms(u16 nms)
{	
	if(OSRunning==TRUE)    
	{		  
		if(nms>=fac_ms)
		{
   			OSTimeDly(nms/fac_ms);
		}
		nms%=fac_ms;		
	}
	delay_us((u32)(nms*1000));	
}
#else	    								   
void delay_us(u32 nus)
{		
	u32 temp;	    	 
	SysTick->LOAD=nus*fac_us;   		 
	SysTick->VAL=0x00;        
	SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ;    
	do
	{
		temp=SysTick->CTRL;
	}
	while(temp&0x01&&!(temp&(1<<16)));
	SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk;   
	SysTick->VAL =0X00;    
}

void delay_ms(u16 nms)
{	 		  	  
	u32 temp;		   
	SysTick->LOAD=(u32)nms*fac_ms;
	SysTick->VAL =0x00;     
	SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ;      
	do
	{
		temp=SysTick->CTRL;
	}
	while(temp&0x01&&!(temp&(1<<16))); 
	SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk;   
	SysTick->VAL =0X00;       	  	    
} 
#endif

usart.c

#include "sys.h"
#include "usart.h"


#if SYSTEM_SUPPORT_UCOS
#include "includes.h"	
#endif
#if 1
#pragma import(__use_no_semihosting)                            
struct __FILE 
{ 
	int handle; 

}; 

FILE __stdout;       
void _sys_exit(int x) 
{ 
	x = x; 
} 
int fputc2(int ch, FILE *f)
{      
	while((USART1->SR&0X40)==0){};//?-?··¢?í,?±μ?·¢?ííê±?   
    USART1->DR = (u8) ch;      
	return ch;
}
#endif 

#if EN_USART1_RX  	
u8 USART_RX_BUF[USART_REC_LEN];  
u16 USART_RX_STA=0;   
  
void uart_init(u32 bound){
  GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);	
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; 
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);  
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;	
	NVIC_Init(&NVIC_InitStructure);
	USART_InitStructure.USART_BaudRate = bound;
	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(USART1, &USART_InitStructure); 
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
    USART_Cmd(USART1, ENABLE); 

}
void USART1_IRQHandler2(void)  
	{
	u8 Res;
#ifdef OS_TICKS_PER_SEC	
	OSIntEnter();    
#endif
	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) 
		{
		Res =USART_ReceiveData(USART1);//(USART1->DR);	
		
		if((USART_RX_STA&0x8000)==0)
			{
			if(USART_RX_STA&0x4000)
				{
				if(Res!=0x0a)USART_RX_STA=0;
				else USART_RX_STA|=0x8000;
				}
			else 
				{	
				if(Res==0x0d)USART_RX_STA|=0x4000;
				else
					{
					USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
					USART_RX_STA++;
					if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;  
					}		 
				}
			}   		 
     } 
#ifdef OS_TICKS_PER_SEC	 
	OSIntExit();  											 
#endif
} 
#endif	



其余代码放在百度链接里面

链接:https://pan.baidu.com/s/1ji42_8ZQHCJMjWlKyRibTg
提取码:h0kv

结果图:
烧程序进去成功图

实验结果展示

posted @ 2020-12-22 21:05  一蓑烟雨任平生噶  阅读(367)  评论(0编辑  收藏  举报