数字温度计
一.了解时钟RTC
RTC (Real Time Clock):实时时钟
RTC是个独立的定时器。RTC模块拥有一个连续计数的计数器,在相应的软件配置下,可以提供时钟日历的功能。修改计数器的值可以重新设置当前时间和日期 RTC还包含用于管理低功耗模式的自动唤醒单元。
RCT特征:
● 可编程的预分频系数:分频系数高为220。
● 32位的可编程计数器,可用于较长时间段的测量。
● 2个分离的时钟:用于APB1接口的PCLK1和RTC时钟(RTC时钟的频率必须小于PCLK1时钟 频率的四分之一以上)。
● 可以选择以下三种RTC的时钟源:
● HSE时钟除以128;
● LSE振荡器时钟;
● LSI振荡器时钟
● 2个独立的复位类型:
● APB1接口由系统复位;
● RTC核心(预分频器、闹钟、计数器和分频器)只能由后备域复位
● 3个专门的可屏蔽中断:
● 1.闹钟中断,用来产生一个软件可编程的闹钟中断。
● 2.秒中断,用来产生一个可编程的周期性中断信号(长可达1秒)。
● 3.溢出中断,指示内部可编程计数器溢出并回转为0的状态。
RTC时钟源:
三种不同的时钟源可被用来驱动系统时钟(SYSCLK):
● HSI振荡器时钟
● HSE振荡器时钟
● PLL时钟
开始实验
在软件方面,我们将使用STM32CubeIDE进行开发。通过编写相应的代码,我们将实现以下功能:
初始化:初始化STM32微控制器、AHT20传感器和SSD1306 OLED显示屏,建立各个组件之间的通信连接。
读取数据:通过STM32微控制器读取AHT20传感器的温度和湿度数据。
时间获取:通过STM32内部时钟或外部时钟模块,获取当前的时间。
数据处理:对读取到的温度、湿度和时间数据进行处理,确保其准确性和可用性。
数据显示:将处理后的数据通过SSD1306 OLED显示屏进行显示,实现实时的时间、温度和湿度信息展示。
循环更新:通过循环结构,不断更新和刷新显示的数据,以保证数据的实时性。
/*******************************************/
/*@????:?????????? */
/*@??:????????? */
/*@??:V1.2 */
/*******************************************/
//#include "main.h"
#include "AHT20.h"
#include "gpio.h"
#include "i2c.h"
void Delay_N10us(uint32_t t)//????
{
uint32_t k;
while(t--)
{
for (k = 0; k < 2; k++);//110
}
}
void SensorDelay_us(uint32_t t)//????
{
for(t = t-2; t>0; t--)
{
Delay_N10us(1);
}
}
void Delay_4us(void) //????
{
Delay_N10us(1);
Delay_N10us(1);
Delay_N10us(1);
Delay_N10us(1);
}
void Delay_5us(void) //????
{
Delay_N10us(1);
Delay_N10us(1);
Delay_N10us(1);
Delay_N10us(1);
Delay_N10us(1);
}
void Delay_1ms(uint32_t t) //????
{
while(t--)
{
SensorDelay_us(1000);//??1ms
}
}
//void AHT20_Clock_Init(void) //????
//{
// RCC_APB2PeriphClockCmd(CC_APB2Periph_GPIOB,ENABLE);
//}
void SDA_Pin_Output_High(void) //?PB7????? , ???????, PB7??I2C?SDA
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;//????
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB,& GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,GPIO_PIN_SET);
}
void SDA_Pin_Output_Low(void) //?P7????? ???????
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;//????
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB,& GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,GPIO_PIN_RESET);
}
void SDA_Pin_IN_FLOATING(void) //SDA???????
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;//??
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init( GPIOB,&GPIO_InitStruct);
}
void SCL_Pin_Output_High(void) //SCL?????,P14??I2C?SCL
{
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,GPIO_PIN_SET);
}
void SCL_Pin_Output_Low(void) //SCL?????
{
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,GPIO_PIN_RESET);
}
void Init_I2C_Sensor_Port(void) //???I2C??,??????
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;//????
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB,& GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,GPIO_PIN_SET);
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;//????
GPIO_InitStruct.Pin = GPIO_PIN_6;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB,& GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,GPIO_PIN_SET);
}
void I2C_Start(void) //I2C????START??
{
SDA_Pin_Output_High();
SensorDelay_us(8);
SCL_Pin_Output_High();
SensorDelay_us(8);
SDA_Pin_Output_Low();
SensorDelay_us(8);
SCL_Pin_Output_Low();
SensorDelay_us(8);
}
void AHT20_WR_Byte(uint8_t Byte) //?AHT20?????
{
uint8_t Data,N,i;
Data=Byte;
i = 0x80;
for(N=0;N<8;N++)
{
SCL_Pin_Output_Low();
Delay_4us();
if(i&Data)
{
SDA_Pin_Output_High();
}
else
{
SDA_Pin_Output_Low();
}
SCL_Pin_Output_High();
Delay_4us();
Data <<= 1;
}
SCL_Pin_Output_Low();
SensorDelay_us(8);
SDA_Pin_IN_FLOATING();
SensorDelay_us(8);
}
uint8_t AHT20_RD_Byte(void)//?AHT20??????
{
uint8_t Byte,i,a;
Byte = 0;
SCL_Pin_Output_Low();
SDA_Pin_IN_FLOATING();
SensorDelay_us(8);
for(i=0;i<8;i++)
{
SCL_Pin_Output_High();
Delay_5us();
a=0;
//if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)) a=1;
if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_7)) a=1;
Byte = (Byte<<1)|a;
//SCL_Pin_Output_Low();
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,GPIO_PIN_RESET);
Delay_5us();
}
SDA_Pin_IN_FLOATING();
SensorDelay_us(8);
return Byte;
}
uint8_t Receive_ACK(void) //?AHT20?????ACK
{
uint16_t CNT;
CNT = 0;
SCL_Pin_Output_Low();
SDA_Pin_IN_FLOATING();
SensorDelay_us(8);
SCL_Pin_Output_High();
SensorDelay_us(8);
while((HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_7)) && CNT < 100)
CNT++;
if(CNT == 100)
{
return 0;
}
SCL_Pin_Output_Low();
SensorDelay_us(8);
return 1;
}
void Send_ACK(void) //????ACK??
{
SCL_Pin_Output_Low();
SensorDelay_us(8);
SDA_Pin_Output_Low();
SensorDelay_us(8);
SCL_Pin_Output_High();
SensorDelay_us(8);
SCL_Pin_Output_Low();
SensorDelay_us(8);
SDA_Pin_IN_FLOATING();
SensorDelay_us(8);
}
void Send_NOT_ACK(void) //?????ACK
{
SCL_Pin_Output_Low();
SensorDelay_us(8);
SDA_Pin_Output_High();
SensorDelay_us(8);
SCL_Pin_Output_High();
SensorDelay_us(8);
SCL_Pin_Output_Low();
SensorDelay_us(8);
SDA_Pin_Output_Low();
SensorDelay_us(8);
}
void Stop_I2C(void) //??????
{
SDA_Pin_Output_Low();
SensorDelay_us(8);
SCL_Pin_Output_High();
SensorDelay_us(8);
SDA_Pin_Output_High();
SensorDelay_us(8);
}
uint8_t AHT20_Read_Status(void)//??AHT20??????
{
uint8_t Byte_first;
I2C_Start();
AHT20_WR_Byte(0x71);
Receive_ACK();
Byte_first = AHT20_RD_Byte();
Send_NOT_ACK();
Stop_I2C();
return Byte_first;
}
uint8_t AHT20_Read_Cal_Enable(void) //??cal enable??????
{
uint8_t val = 0;//ret = 0,
val = AHT20_Read_Status();
if((val & 0x68)==0x08)
return 1;
else return 0;
}
void AHT20_SendAC(void) //?AHT20??AC??
{
I2C_Start();
AHT20_WR_Byte(0x70);
Receive_ACK();
AHT20_WR_Byte(0xac);//0xAC????
Receive_ACK();
AHT20_WR_Byte(0x33);
Receive_ACK();
AHT20_WR_Byte(0x00);
Receive_ACK();
Stop_I2C();
}
//CRC????:CRC8/MAXIM
//???:X8+X5+X4+1
//Poly:0011 0001 0x31
//????????? 1000 1100 0x8c
//C????:
uint8_t Calc_CRC8(uint8_t *message,uint8_t Num)
{
uint8_t i;
uint8_t byte;
uint8_t crc=0xFF;
for(byte=0; byte<Num; byte++)
{
crc^=(message[byte]);
for(i=8;i>0;--i)
{
if(crc&0x80) crc=(crc<<1)^0x31;
else crc=(crc<<1);
}
}
return crc;
}
void AHT20_Read_CTdata(uint32_t *ct) //??CRC??,????AHT20????????
{
volatile uint8_t Byte_1th=0;
volatile uint8_t Byte_2th=0;
volatile uint8_t Byte_3th=0;
volatile uint8_t Byte_4th=0;
volatile uint8_t Byte_5th=0;
volatile uint8_t Byte_6th=0;
uint32_t RetuData = 0;
uint16_t cnt = 0;
AHT20_SendAC();//?AHT10??AC??
Delay_1ms(80);//??80ms??
cnt = 0;
while(((AHT20_Read_Status()&0x80)==0x80))//????bit[7]?0,???????,??1,?????
{
SensorDelay_us(1508);
if(cnt++>=100)
{
break;
}
}
I2C_Start();
AHT20_WR_Byte(0x71);
Receive_ACK();
Byte_1th = AHT20_RD_Byte();//???,??????0x98,??????,bit[7]?1;???0x1C,??0x0C,??0x08???????,bit[7]?0
Send_ACK();
Byte_2th = AHT20_RD_Byte();//??
Send_ACK();
Byte_3th = AHT20_RD_Byte();//??
Send_ACK();
Byte_4th = AHT20_RD_Byte();//??/??
Send_ACK();
Byte_5th = AHT20_RD_Byte();//??
Send_ACK();
Byte_6th = AHT20_RD_Byte();//??
Send_NOT_ACK();
Stop_I2C();
RetuData = (RetuData|Byte_2th)<<8;
RetuData = (RetuData|Byte_3th)<<8;
RetuData = (RetuData|Byte_4th);
RetuData =RetuData >>4;
ct[0] = RetuData;//??
RetuData = 0;
RetuData = (RetuData|Byte_4th)<<8;
RetuData = (RetuData|Byte_5th)<<8;
RetuData = (RetuData|Byte_6th);
RetuData = RetuData&0xfffff;
ct[1] =RetuData; //??
}
void AHT20_Read_CTdata_crc(uint32_t *ct) //CRC???,??AHT20????????
{
volatile uint8_t Byte_1th=0;
volatile uint8_t Byte_2th=0;
volatile uint8_t Byte_3th=0;
volatile uint8_t Byte_4th=0;
volatile uint8_t Byte_5th=0;
volatile uint8_t Byte_6th=0;
volatile uint8_t Byte_7th=0;
uint32_t RetuData = 0;
uint16_t cnt = 0;
// uint8_t CRCDATA=0;
uint8_t CTDATA[6]={0};//??CRC????
AHT20_SendAC();//?AHT10??AC??
Delay_1ms(80);//??80ms??
cnt = 0;
while(((AHT20_Read_Status()&0x80)==0x80))//????bit[7]?0,???????,??1,?????
{
SensorDelay_us(1508);
if(cnt++>=100)
{
break;
}
}
I2C_Start();
AHT20_WR_Byte(0x71);
Receive_ACK();
CTDATA[0]=Byte_1th = AHT20_RD_Byte();//???,??????0x98,??????,bit[7]?1;???0x1C,??0x0C,??0x08???????,bit[7]?0
Send_ACK();
CTDATA[1]=Byte_2th = AHT20_RD_Byte();//??
Send_ACK();
CTDATA[2]=Byte_3th = AHT20_RD_Byte();//??
Send_ACK();
CTDATA[3]=Byte_4th = AHT20_RD_Byte();//??/??
Send_ACK();
CTDATA[4]=Byte_5th = AHT20_RD_Byte();//??
Send_ACK();
CTDATA[5]=Byte_6th = AHT20_RD_Byte();//??
Send_ACK();
Byte_7th = AHT20_RD_Byte();//CRC??
Send_NOT_ACK(); //??: ?????NAK
Stop_I2C();
if(Calc_CRC8(CTDATA,6)==Byte_7th)
{
RetuData = (RetuData|Byte_2th)<<8;
RetuData = (RetuData|Byte_3th)<<8;
RetuData = (RetuData|Byte_4th);
RetuData =RetuData >>4;
ct[0] = RetuData;//??
RetuData = 0;
RetuData = (RetuData|Byte_4th)<<8;
RetuData = (RetuData|Byte_5th)<<8;
RetuData = (RetuData|Byte_6th);
RetuData = RetuData&0xfffff;
ct[1] =RetuData; //??
}
else
{
ct[0]=0x00;
ct[1]=0x00;//???????,????????????
}//CRC??
}
void AHT20_Init(void) //???AHT20
{
Init_I2C_Sensor_Port();
I2C_Start();
AHT20_WR_Byte(0x70);
Receive_ACK();
AHT20_WR_Byte(0xa8);//0xA8??NOR????
Receive_ACK();
AHT20_WR_Byte(0x00);
Receive_ACK();
AHT20_WR_Byte(0x00);
Receive_ACK();
Stop_I2C();
Delay_1ms(10);//??10ms??
I2C_Start();
AHT20_WR_Byte(0x70);
Receive_ACK();
AHT20_WR_Byte(0xbe);//0xBE?????,AHT20???????0xBE, AHT10???????0xE1
Receive_ACK();
AHT20_WR_Byte(0x08);//?????bit[3]?1,?????
Receive_ACK();
AHT20_WR_Byte(0x00);
Receive_ACK();
Stop_I2C();
Delay_1ms(10);//??10ms??
}
void JH_Reset_REG(uint8_t addr)
{
uint8_t Byte_first,Byte_second,Byte_third;
I2C_Start();
AHT20_WR_Byte(0x70);//???0x70
Receive_ACK();
AHT20_WR_Byte(addr);
Receive_ACK();
AHT20_WR_Byte(0x00);
Receive_ACK();
AHT20_WR_Byte(0x00);
Receive_ACK();
Stop_I2C();
Delay_1ms(5);//??5ms??
I2C_Start();
AHT20_WR_Byte(0x71);//
Receive_ACK();
Byte_first = AHT20_RD_Byte();
Send_ACK();
Byte_second = AHT20_RD_Byte();
Send_ACK();
Byte_third = AHT20_RD_Byte();
Send_NOT_ACK();
Stop_I2C();
Delay_1ms(10);//??10ms??
I2C_Start();
AHT20_WR_Byte(0x70);///
Receive_ACK();
AHT20_WR_Byte(0xB0|addr);//?????
Receive_ACK();
AHT20_WR_Byte(Byte_second);
Receive_ACK();
AHT20_WR_Byte(Byte_third);
Receive_ACK();
Stop_I2C();
Byte_second=0x00;
Byte_third =0x00;
}
void AHT20_Start_Init(void)
{
JH_Reset_REG(0x1b);
JH_Reset_REG(0x1c);
JH_Reset_REG(0x1e);
}
5.2 OLED的代码
参考链接: https://blog.csdn.net/weixin_63019977/article/details/134346345
通过汉字取模软件,将所需要的汉字取模出来:
OLED_ShowHZ(1,2,18);//ÈÕ
OLED_ShowHZ(1,4,20);//ÆÚ
OLED_ShowNum(1,7,year,4);//2023
OLED_ShowHZ(1,11,22);//Äê
OLED_ShowNum(1,13,month,2);//11
OLED_ShowHZ(1,15,24);//ÔÂ
OLED_ShowNum(2,1,day,2);//20
OLED_ShowHZ(2,3,26);//ÈÕ
OLED_ShowNum(2,5,hour,2);//15
OLED_ShowHZ(2,7,30);//ʱ
OLED_ShowNum(2,9,min,2);//40
OLED_ShowHZ(2,11,32);//·Ö
OLED_ShowNum(2,13,s,2);//s
OLED_ShowHZ(2,15,28);//Ãë
//OLED_ShowString(2,17,"Mon");
DHT11_REC_Data(); //½ÓÊÕdht11Êý¾Ý
OLED_ShowNum(3,10,rec_data[2]-5,2);
OLED_ShowNum(4,10,rec_data[0]-13,2);
s+=1;
if(s>=60)
{
s=0;
min++;
}
if(min>=60)
{
min=0;
hour++;
}
if(hour>=24)
{
hour=0;
day++;
}
if(day>=31)
{
month++;
day=1;
}
if(month>12)
{
year++;
month=1;
}
Delay_s(1);
总结,实验体会
在使用STM32F103的RTC时,我发现以下几点心得体会:
RTC的时钟源选择:STM32F103的RTC可以使用内部低速晶振(LSI)或外部低速晶振(LSE)作为时钟源。LSI的频率为40kHz,LSE的频率可以选择为32.768kHz或37kHz。选择合适的时钟源对于RTC的精确性非常重要。
RTC的初始化:在使用RTC之前,需要对RTC进行初始化。初始化包括设置时钟源、使能RTC时钟、设置预分频器等操作。在初始化过程中,需要注意时钟源的选择和预分频器的设置,以确保RTC的准确性。
RTC的时间和日期设置:通过RTC,可以设置当前的时间和日期。时间和日期的设置需要注意各个字段的范围和格式。例如,小时字段的范围是0-23,分钟字段的范围是0-59,日期字段的范围是1-31等等。在设置时间和日期时,需要确保输入的值在合理的范围内。
RTC的定时功能:RTC还可以实现定时功能,即在指定的时间点触发中断或事件。通过设置RTC的闹钟功能,可以实现定时唤醒或定时中断的功能。定时功能可以用于周期性的任务执行、定时唤醒等应用场景。
RTC的中断处理:在使用RTC时,可以通过中断来处理RTC的事件。例如,可以通过RTC的秒中断来更新显示屏上的时间,或者通过RTC的闹钟中断来触发特定的事件。在处理RTC中断时,需要注意中断优先级的设置和中断标志的清除,以确保中断的及时响应。
总的来说,STM32F103的RTC功能非常强大,可以实现精确的时间和日期记录,以及定时功能。在使用RTC时,需要注意时钟源的选择、初始化的设置、时间和日期的设置、定时功能的实现以及中断的处理等方面,以确保RTC的准确性和稳定性。