STM32 基于I2C的温湿度采集实验
一、任务要求
学习I2C总线通信协议,完成基于I2C硬件协议的AHT20温湿度传感器的数据采集,并将采集的温度-湿度值通过串口输出。具体任务:
1)解释什么是“软件I2C”和“硬件I2C”?
2)阅读AHT20数据手册,编程实现:每隔2秒钟采集一次温湿度数据,并通过串口发送到上位机(win10)。
二、软件I2C与硬件I2C
1、软件I2C
软件I2C一般是用GPIO管脚,由于不受管脚限制,接口比较灵活。软件I2C是程序员使用程序控制SCL,SDA线输出高低电平,模拟I2C协议的时序。一般较硬件I2C稳定,但是程序较为繁
2、硬件I2C
硬件I2C用法比较复杂,硬件I2C速度比模拟快,并且可以用DMA;并且硬件I2C程序员只要调用I2C的控制函数即可,不用直接的去控制SCL,SDA高低电平的输出
三、基于I2C实现温湿度采集
1、硬件连接
ANT20 | STM32 |
---|---|
SAL | B6 |
GND | GND |
VCC | 5V |
SDA | B7 |
2、代码
(1)打开野火给出的资料,找到下图程序
(2)添加如下几个代码,将其放在User文件夹下
①usart.c
#include "sys.h"
#include "usart.h"
//STM32F103o?D?°?ày3ì
//?aoˉêy°?±?ày3ì
/********** mcudev.taobao.com 3??· ********/
//
//è?1?ê1ó?ucos,?ò°üà¨????μ?í·???t?′?é.
#if SYSTEM_SUPPORT_UCOS
#include "includes.h" //ucos ê1ó?
#endif
//
//STM32?a·¢°?
//′??ú13?ê??ˉ
//
//
//?óè?ò???′ú??,?§3?printfoˉêy,??2?Dèòa????use MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//±ê×??aDèòaμ??§3?oˉêy
struct __FILE
{
int handle;
};
FILE __stdout;
//?¨ò?_sys_exit()ò?±ü?aê1ó?°??÷?ú?£ê?
void _sys_exit(int x)
{
x = x;
}
//???¨ò?fputcoˉêy
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);//?-?··¢?í,?±μ?·¢?ííê±?
USART1->DR = (u8) ch;
return ch;
}
#endif
/*ê1ó?microLibμ?·?·¨*/
/*
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (uint8_t) ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}
return ch;
}
int GetKey (void) {
while (!(USART1->SR & USART_FLAG_RXNE));
return ((int)(USART1->DR & 0x1FF));
}
*/
#if EN_USART1_RX //è?1?ê1?üá??óê?
//′??ú1?D??·t??3ìDò
//×¢òa,?áè?USARTx->SR?ü±ü?a?a??????μ?′í?ó
u8 USART_RX_BUF[USART_REC_LEN]; //?óê??o3?,×?′óUSART_REC_LEN??×??ú.
//?óê?×′ì?
//bit15£? ?óê?íê3é±ê??
//bit14£? ?óê?μ?0x0d
//bit13~0£? ?óê?μ?μ?óDD§×??úêy??
u16 USART_RX_STA=0; //?óê?×′ì?±ê??
void uart_init(u32 bound){
//GPIO???úéè??
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //ê1?üUSART1£?GPIOAê±?ó
//USART1_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //?′ó?í?íìê?3?
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART1_RX PA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//????ê?è?
GPIO_Init(GPIOA, &GPIO_InitStructure);
//Usart1 NVIC ????
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//?à??ó??è??3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×óó??è??3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1?ü
NVIC_Init(&NVIC_InitStructure); //?ù?Y???¨μ?2?êy3?ê??ˉVIC??′??÷
//USART 3?ê??ˉéè??
USART_InitStructure.USART_BaudRate = bound;//ò?°?éè???a9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×?3¤?a8??êy?Y??ê?
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò???í£?1??
USART_InitStructure.USART_Parity = USART_Parity_No;//?T????D£?é??
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//?Tó2?têy?Yá÷????
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //ê?·¢?£ê?
USART_Init(USART1, &USART_InitStructure); //3?ê??ˉ′??ú
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//?a???D??
USART_Cmd(USART1, ENABLE); //ê1?ü′??ú
}
void USART1_IRQHandler(void) //′??ú1?D??·t??3ìDò
{
u8 Res;
#ifdef OS_TICKS_PER_SEC //è?1?ê±?ó?ú??êy?¨ò?á?,?μ?÷òaê1ó?ucosIIá?.
OSIntEnter();
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //?óê??D??(?óê?μ?μ?êy?Y±?D?ê?0x0d 0x0a?á?2)
{
Res =USART_ReceiveData(USART1);//(USART1->DR); //?áè??óê?μ?μ?êy?Y
if((USART_RX_STA&0x8000)==0)//?óê??′íê3é
{
if(USART_RX_STA&0x4000)//?óê?μ?á?0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//?óê?′í?ó,??D??aê?
else USART_RX_STA|=0x8000; //?óê?íê3éá?
}
else //?1??ê?μ?0X0D
{
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;//?óê?êy?Y′í?ó,??D??aê??óê?
}
}
}
}
#ifdef OS_TICKS_PER_SEC //è?1?ê±?ó?ú?ò?á?,?μ?÷òaê1ó?ucosIIá?.
OSIntExit();
#endif
}
#endif
②usart.h
#ifndef __USART_H
#define __USART_H
#include "stdio.h"
#include "sys.h"
//STM32F103ºËÐÄ°åÀý³Ì
//¿âº¯Êý°æ±¾Àý³Ì
/********** mcudev.taobao.com ³öÆ· ********/
//
//STM32¿ª·¢°å
//´®¿Ú1³õʼ»¯
#define USART_REC_LEN 200 //¶¨Òå×î´ó½ÓÊÕ×Ö½ÚÊý 200
#define EN_USART1_RX 1 //ʹÄÜ£¨1£©/½ûÖ¹£¨0£©´®¿Ú1½ÓÊÕ
extern u8 USART_RX_BUF[USART_REC_LEN]; //½ÓÊÕ»º³å,×î´óUSART_REC_LEN¸ö×Ö½Ú.Ä©×Ö½ÚΪ»»Ðзû
extern u16 USART_RX_STA; //½ÓÊÕ״̬±ê¼Ç
//Èç¹ûÏë´®¿ÚÖжϽÓÊÕ£¬Ç벻ҪעÊÍÒÔϺ궨Òå
void uart_init(u32 bound);
#endif
③i2c.c(包含温湿度的计算函数)
#include "i2c.h"
#include "delay.h"
uint8_t ack_status=0;
uint8_t readByte[6];
uint8_t AHT20_status=0;
uint32_t H1=0; //Humility
uint32_t T1=0; //Temperature
uint8_t AHT20_OutData[4];
uint8_t AHT20sendOutData[10] = {
0xFA, 0x06, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF};
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //í?íìê?3?
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
IIC_SCL=1;
IIC_SDA=1;
}
void IIC_Start(void)