赞
踩
目录
阅读资料了解 STM32F103的RTC(实时时钟)原理,使用带SPI或IIC接口的OLED屏显模块实现以下功能:
1) 读取STM32F103C8T6 内部的时钟(年月日时分秒),日历(星期x),1秒周期,通过串口输出到PC上位机,;
2) 读取AHT20的温度和湿度,通过OLED,把年月份时分秒、日历和实时温度、湿度显示出来,2秒周期。
RTC (Real Time Clock):实时时钟。RTC是个独立的定时器。RTC模块拥有一个连续计数的计数器,在相应的软件配置下,可以提供时钟日历的功能。修改计数器的值可以重新设置当前时间和日期 RTC还包含用于管理低功耗模式的自动唤醒单元。
在断电情况下 RTC仍可以独立运行 只要芯片的备用电源一直供电,RTC上的时间会一直走。RTC实质是一个掉电后还继续运行的定时器,从定时器的角度来看,相对于通用定时器TIM外设,它的功能十分简单,只有计时功能(也可以触发中断)。但其高级指出也就在于掉电之后还可以正常运行。
两个 32 位寄存器包含二进码十进数格式 (BCD) 的秒、分钟、小时( 12 或 24 小时制)、星期几、日期、月份和年份。此外,还可提供二进制格式的亚秒值。系统可以自动将月份的天数补偿为 28、29(闰年)、30 和 31 天。
上电复位后,所有RTC寄存器都会受到保护,以防止可能的非正常写访问。无论器件状态如何(运行模式、低功耗模式或处于复位状态),只要电源电压保持在工作范围内,RTC使不会停止工作。
可编程的预分频系数:分频系数高为220。
32位的可编程计数器,可用于较长时间段的测量。
2个分离的时钟:用于APB1接口的PCLK1和RTC时钟(RTC时钟的频率必须小于PCLK1时钟 频率的四分之一以上)。
可以选择以下三种RTC的时钟源:
2个独立的复位类型:
3个专门的可屏蔽中断:
RTC时钟源:
三种不同的时钟源可被用来驱动系统时钟(SYSCLK):
这些设备有以下2种二级时钟源:
RTC时钟的框图是比较简单的,这里我们把他分成 两个部分:
(1)APB1 接口: 用来和 APB1 总线相连。 此单元还包含一组 16 位寄存器,可通过 APB1 总线对其进行读写操作。APB1 接口由 APB1 总 线时钟驱动,用来与 APB1 总线连接。通过APB1接口可以访问RTC的相关寄存器(预分频值,计数器值,闹钟值)。
(2)RTC 核心接口:由一组可编程计数器组成,分成 两个主要模块 :
——第一个模块是 RTC 的 预分频模块 ,它可编程产生 1 秒的 RTC 时间基准 TR_CLK。RTC 的预分频模块包含了一个 20 位的可编程分频器(RTC 预分频器)。如果在 RTC_CR 寄存器中设置了相应的允许位,则在每个 TR_CLK 周期中 RTC 产生一个中断(秒中断)。
——第二个模块是一个 32 位的可编程计数器 (RTC_CNT),可被初始化为当前的系统时间,一个 32 位的时钟计数器,按秒钟计算,可以记 录 4294967296 秒,约合 136 年左右,作为一般应用,这已经是足够了的。
RTC时钟主要由两个内部低速时钟和一个外部高速时钟构成。基本结构如下:
硬件电路原理图如下 :
RTC(Real Time Clock)实时时钟是一种高精度、低功耗的定时器,它可以在各种环境下提供精确的时间和日期信息。即使在系统处于低功耗模式时,RTC也可以继续运行,确保时间的连续性。此外,它还支持时钟校准功能,可以校正时钟的偏差,保证时间的准确性。 除了基本的时间提供功能,RTC还具有报警功能,用户可以设置报警功能,当达到指定时间时触发中断,这在实际应用中非常有用。例如,可以在系统到达预定维护时间时触发警报,提醒用户进行维护操作。
另外,RTC还支持外部电池备份,用户可以使用外部电池进行时间备份,即使在断电的情况下也能保持时间的准确。这一特性使得RTC在某些应用场景下特别适用,例如在应急照明系统、安全监控系统等需要持续、准确计时的情况下。
RTC还具有自动唤醒功能,用户可以根据需要设置自动唤醒条件,当满足预设条件时自动唤醒系统。这一功能在许多嵌入式系统和物联网设备中非常有用,可以大大降低系统的功耗。
RTCCLK经过RTC_DIV预分频,RTC_PRL设置预分频系数,然后得到TR_CLK时钟信号,我们一般设置其周期为1s,RTC_CNT计数器计数,假如1970设置为时间起点为0s,通过当前时间的秒数计算得到当前的时间。RTC_ALR是设置闹钟时间,RTC_CNT计数到RTC_ALR就会产生计数中断。RTC_Second为秒中断,用于刷新时间;RTC_Overflow是溢出中断;RTC Alarm 控制开关机。
使用HSE分频时钟或者LSI的时候,在主电源VDD掉电的情况下,这两个时钟来源都会受到影响,因此没法保证RTC正常工作.所以RTC一般都时钟低速外部时钟LSE,频率为实时时钟模块中常用的32.768KHz,因为32768 = 2^15,分频容易实现,所以被广泛应用到RTC模块.(在主电源VDD有效的情况下待机,RTC还可以配置闹钟事件使STM32退出待机模式)。
除了RTC_PRL、RTC_ALR、RTC_CNT和RTC_DIV寄存器外,所有的系统寄存器都由系统复位或电源复位进行异步复位。RTC_PRL、RTC_ALR、RTC_CNT和RTC_DIV寄存器仅能通过备份域复位信号复位。系统复位后,禁止访问后备寄存器和RCT,防止对后卫区域(BKP)的意外写操作。
(1)秒中断:
这里时钟自带一个秒中断,每当计数加一的时候就会触发一次秒中断,。注意,这里所说的秒中断并非一定是一秒的时间,它是由RTC时钟源和分频值决定的“秒”的时间,当然也是可以做到1秒钟中断一次。我们通过往秒中断里写更新时间的函数来达到时间同步的效果
(2)闹钟中断:
闹钟中断就是设置一个预设定的值,计数每自加多少次触发一次闹钟中断
请参考我的这篇博客:STM32使用HAL库点亮流水灯-CSDN博客
(1)打开STM32CubeMX,在主界面点击:ACCESS TO MCU SELECTOR:
(2)选择的单片机型号以及点击开始工程项目:
(3)点击system core,进入SYS,在debug下选择serial wire:
(4)配置RCC,(设置高速外部时钟,使能外部晶振LSE):
(5)配置RTC(激活时钟源(Activate Clock Source)和日历(Activate Calendar)),进入RTC,设置时间为创建时间2023/11/15/ 17:14:
接着:
(6)配置USART1,我们使用USART1进行数据传输:
(7)进入CLK Configuration (时钟配置)中,进行时钟配置 :
(8)进入Project Manager(工程管理),进行工程设置点击生成工程与代码。注意:路径不能包含中文和空格,不然生成的工程文件无法在Keil中打开:
1、头文件添加#include “stdio.h”:
2、在main.c文件中重写fputc函数,完成printf函数的重定向:
- //添加头文件#include "stdio.h"
- int fputc(int ch,FILE *f){
- uint8_t temp[1]={ch};
- HAL_UART_Transmit(&huart1,temp,1,2);
- return ch;
- }
3、在main.c中定义时间和日期的结构体用来获取时间和日期:
- RTC_DateTypeDef GetData; //获取日期结构体
-
- RTC_TimeTypeDef GetTime; //获取时间结构体
4、在main函数中while循环里面添加如下代码即可:
- /* Get the RTC current Time */
- HAL_RTC_GetTime(&hrtc, &GetTime, RTC_FORMAT_BIN);
- /* Get the RTC current Date */
- HAL_RTC_GetDate(&hrtc, &GetData, RTC_FORMAT_BIN);
-
- /* Display date Format : yy/mm/dd */
- printf("%02d/%02d/%02d\r\n",2000 + GetData.Year, GetData.Month, GetData.Date);
- /* Display date Format : weekday */
- if(GetData.WeekDay==1){
- printf("星期一\r\n");
- }else if(GetData.WeekDay==2){
- printf("星期二\r\n");
- }else if(GetData.WeekDay==3){
- printf("星期三\r\n");
- }else if(GetData.WeekDay==4){
- printf("星期四\r\n");
- }else if(GetData.WeekDay==5){
- printf("星期五\r\n");
- }else if(GetData.WeekDay==6){
- printf("星期六\r\n");
- }else if(GetData.WeekDay==7){
- printf("星期日\r\n");
- }
-
- /* Display time Format : hh:mm:ss */
- printf("%02d:%02d:%02d\r\n",GetTime.Hours, GetTime.Minutes, GetTime.Seconds);
-
- printf("\r\n");
-
- HAL_Delay(1000);
5、因为我们在代码里面重写了printf,所以在Target里面一定要勾选上Use MiscroLiB:
1、USB转TTL与STM32的连线,以及电脑上配置CH340等等烧录环境,请参考我的这篇博客:
2、编译与烧录,如何通过USB转TTL烧录进STM32中,也参考我的同一篇博客:
3、工程运行结果展示:
请参考我的这篇博客:
(1)提醒:
关于如何在keil里面通过库函数创建并且编写C文件与H文件,以及把它们都导入进库函数keil5工程中,还是参考我的这篇博客:基于标准外设库的LED流水灯-CSDN博客
(2)编写工程需要的文件代码:
(1:sys.h与sys.c文件代码编写:
sys.h:
- #ifndef __SYS_H
- #define __SYS_H
- #include "stm32f10x.h"
- //
-
-
- //STM32F103o?D?°?ày3ì
- //?aoˉêy°?±?ày3ì
- /********** mcudev.taobao.com 3??· ********/
-
-
-
- //
-
- //0,2??§3?ucos
- //1,?§3?ucos
- #define SYSTEM_SUPPORT_UCOS 0 //?¨ò??μí3???t?Dê?·??§3?UCOS
-
-
- //??′?2ù×÷,êμ??51àà??μ?GPIO????1|?ü
- //??ì?êμ??????,2???<<CM3è¨ít????>>μú????(87ò3~92ò3).
- //IO?ú2ù×÷oê?¨ò?
- #define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
- #define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
- #define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
- //IO?úμ??·ó3é?
- #define GPIOA_ODR_Addr (GPIOA_BASE+12) //0x4001080C
- #define GPIOB_ODR_Addr (GPIOB_BASE+12) //0x40010C0C
- #define GPIOC_ODR_Addr (GPIOC_BASE+12) //0x4001100C
- #define GPIOD_ODR_Addr (GPIOD_BASE+12) //0x4001140C
- #define GPIOE_ODR_Addr (GPIOE_BASE+12) //0x4001180C
- #define GPIOF_ODR_Addr (GPIOF_BASE+12) //0x40011A0C
- #define GPIOG_ODR_Addr (GPIOG_BASE+12) //0x40011E0C
-
- #define GPIOA_IDR_Addr (GPIOA_BASE+8) //0x40010808
- #define GPIOB_IDR_Addr (GPIOB_BASE+8) //0x40010C08
- #define GPIOC_IDR_Addr (GPIOC_BASE+8) //0x40011008
- #define GPIOD_IDR_Addr (GPIOD_BASE+8) //0x40011408
- #define GPIOE_IDR_Addr (GPIOE_BASE+8) //0x40011808
- #define GPIOF_IDR_Addr (GPIOF_BASE+8) //0x40011A08
- #define GPIOG_IDR_Addr (GPIOG_BASE+8) //0x40011E08
-
- //IO?ú2ù×÷,????μ¥ò?μ?IO?ú!
- //è·±£nμ??μD?óú16!
- #define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n) //ê?3?
- #define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n) //ê?è?
-
- #define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n) //ê?3?
- #define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n) //ê?è?
-
- #define PCout(n) BIT_ADDR(GPIOC_ODR_Addr,n) //ê?3?
- #define PCin(n) BIT_ADDR(GPIOC_IDR_Addr,n) //ê?è?
-
- #define PDout(n) BIT_ADDR(GPIOD_ODR_Addr,n) //ê?3?
- #define PDin(n) BIT_ADDR(GPIOD_IDR_Addr,n) //ê?è?
-
- #define PEout(n) BIT_ADDR(GPIOE_ODR_Addr,n) //ê?3?
- #define PEin(n) BIT_ADDR(GPIOE_IDR_Addr,n) //ê?è?
-
- #define PFout(n) BIT_ADDR(GPIOF_ODR_Addr,n) //ê?3?
- #define PFin(n) BIT_ADDR(GPIOF_IDR_Addr,n) //ê?è?
-
- #define PGout(n) BIT_ADDR(GPIOG_ODR_Addr,n) //ê?3?
- #define PGin(n) BIT_ADDR(GPIOG_IDR_Addr,n) //ê?è?
-
-
-
- void NVIC_Configuration(void);
-
-
-
- #endif
-
sys.c:
- #include "sys.h"
-
-
- //STM32F103o?D?°?ày3ì
- //?aoˉêy°?±?ày3ì
- /********** mcudev.taobao.com 3??· ********/
-
- //
-
- //STM32?a·¢°?
- //?μí3?D??·?×ééè???ˉ
-
- //********************************************************************************
- void NVIC_Configuration(void)
- {
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //éè??NVIC?D??·?×é2:2???à??ó??è??£?2???ìó|ó??è??
-
- }
-
(2:Timer.h与Timer.c文件编写:
Timer.h:
- #ifndef __TIMER_H
- #define __TIMER_H
-
- void Timer_Init(void);
-
- #endif
Timer.c:
- #include "stm32f10x.h" // Device header
-
-
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- void Timer_Init(void)
- {
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
-
- TIM_InternalClockConfig(TIM2);
-
-
- TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInitStructure.TIM_Period = 10000 - 1;
- TIM_TimeBaseInitStructure.TIM_Prescaler = 3600 - 1;
- TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
-
- TIM_ClearFlag(TIM2, TIM_FLAG_Update);
- TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
-
-
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_Init(&NVIC_InitStructure);
-
- TIM_Cmd(TIM2, ENABLE);
- }
-
- /*
- void TIM2_IRQHandler(void)
- {
- if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
- {
-
- TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
- }
- }
- */
(3:MyRTC.h与MyRTC.c文件编写:
MyRTC.h:
- #ifndef __MYRTC_H
- #define __MYRTC_H
-
- extern uint16_t MyRTC_Time[];
-
- void MyRTC_Init(void);
- void MyRTC_SetTime(void);
- void MyRTC_ReadTime(void);
-
- #endif
MyRTC.c:
- #include "stm32f10x.h" // Device header
- #include <time.h>
- #include "OLED.h"
- #include "delay.h"
-
- uint16_t MyRTC_Time[] = {2023, 11, 21, 21, 51, 05};
-
- void MyRTC_SetTime(void);
-
-
- char* weekday;
- uint16_t iWeek;
-
- void CalculateWeekDay(uint16_t year, uint16_t month, uint16_t day) {
- if (month == 1 || month ==2)
- {
- month +=12;
- year--;
- }
- iWeek = (day + 2 * month + 3 * (month + 1)/5 + year + year/4 - year/100 + year/400) % 7;
- switch (iWeek)
- {
- case 0: weekday = "Monday"; break;
- case 1: weekday = "Teusday"; break;
- case 2: weekday = "Wednesday"; break;
- case 3: weekday = "Thursday"; break;
- case 4: weekday = "Friday"; break;
- case 5: weekday = "Saturday"; break;
- case 6: weekday = "Sunday"; break;
- }
- }
-
-
-
- void MyRTC_Init(void)
- {
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP, ENABLE);
-
- PWR_BackupAccessCmd(ENABLE);
-
- if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
- {
- RCC_LSEConfig(RCC_LSE_ON);
- while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) != SET);
-
- RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
- RCC_RTCCLKCmd(ENABLE);
-
- RTC_WaitForSynchro();
- RTC_WaitForLastTask();
-
- RTC_SetPrescaler(32768 - 1);
- RTC_WaitForLastTask();
-
- MyRTC_SetTime();
-
- BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
- }
- else
- {
- RTC_WaitForSynchro();
- RTC_WaitForLastTask();
- }
- }
-
- //如果LSE无法起振导致程序卡死在初始化函数中
- //可将初始化函数替换为下述代码,使用LSI当作RTCCLK
- //LSI无法由备用电源供电,故主电源掉电时,RTC走时会暂停
- /*
- void MyRTC_Init(void)
- {
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP, ENABLE);
-
- PWR_BackupAccessCmd(ENABLE);
-
- if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
- {
- RCC_LSICmd(ENABLE);
- while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) != SET);
-
- RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
- RCC_RTCCLKCmd(ENABLE);
-
- RTC_WaitForSynchro();
- RTC_WaitForLastTask();
-
- RTC_SetPrescaler(40000 - 1);
- RTC_WaitForLastTask();
-
- MyRTC_SetTime();
-
- BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
- }
- else
- {
- RCC_LSICmd(ENABLE);
- while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) != SET);
-
- RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
- RCC_RTCCLKCmd(ENABLE);
-
- RTC_WaitForSynchro();
- RTC_WaitForLastTask();
- }
- }*/
-
- void MyRTC_SetTime(void)
- {
- time_t time_cnt;
- struct tm time_date;
-
- time_date.tm_year = MyRTC_Time[0] - 1900;
- time_date.tm_mon = MyRTC_Time[1] - 1;
- time_date.tm_mday = MyRTC_Time[2];
- time_date.tm_hour = MyRTC_Time[3];
- time_date.tm_min = MyRTC_Time[4];
- time_date.tm_sec = MyRTC_Time[5];
-
- time_cnt = mktime(&time_date) - 8 * 60 * 60;
-
- RTC_SetCounter(time_cnt);
- RTC_WaitForLastTask();
- }
-
- void MyRTC_ReadTime(void)
- {
- time_t time_cnt;
- struct tm time_date;
-
- time_cnt = RTC_GetCounter() + 8 * 60 * 60;//
-
- time_date = *localtime(&time_cnt);
-
- MyRTC_Time[0] = time_date.tm_year + 1900;
- MyRTC_Time[1] = time_date.tm_mon + 1;
- MyRTC_Time[2] = time_date.tm_mday;
- MyRTC_Time[3] = time_date.tm_hour;
- MyRTC_Time[4] = time_date.tm_min;
- MyRTC_Time[5] = time_date.tm_sec;
-
- CalculateWeekDay(MyRTC_Time[0], MyRTC_Time[1], MyRTC_Time[2]);
-
- //delay_ms(10);
- OLED_ShowNum(3, 1, MyRTC_Time[0], 4);
- OLED_ShowChar(3, 5, '-');
- OLED_ShowNum(3, 6, MyRTC_Time[1], 2);
- OLED_ShowChar(3, 8, '-');
- OLED_ShowNum(3, 9, MyRTC_Time[2], 2);
-
- OLED_ShowNum(4, 1, MyRTC_Time[3], 2);
- OLED_ShowChar(4, 3, ':');
- OLED_ShowNum(4, 4, MyRTC_Time[4], 2);
- OLED_ShowChar(4, 6, ':');
- OLED_ShowNum(4, 7, MyRTC_Time[5], 2);
-
- OLED_ShowString(4, 10, weekday);
-
- }
(4:OLED.h与OLED.c文件编写:
OLED.h:
- #ifndef __OLED_H
- #define __OLED_H
-
- void OLED_Init(void);
- void OLED_Clear(void);
- void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char);
- void OLED_ShowString(uint8_t Line, uint8_t Column, char *String);
- void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
- void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length);
- void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
- void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
-
- #endif
OLED.c:
- #include "bsp_i2c.h"
- #include "delay.h"
- #include "OLED.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;
-
- }
- //2úéúIIC?eê?D?o?
- void IIC_Start(void)
- {
- SDA_OUT(); //sda??ê?3?
- IIC_SDA=1;
- IIC_SCL=1;
- delay_us(4);
- IIC_SDA=0;//START:when CLK is high,DATA change form high to low
- delay_us(4);
- IIC_SCL=0;//?ˉ×?I2C×ü??£?×?±?·¢?í?ò?óê?êy?Y
- }
- //2úéúIICí£?1D?o?
- void IIC_Stop(void)
- {
- SDA_OUT();//sda??ê?3?
- IIC_SCL=0;
- IIC_SDA=0;//STOP:when CLK is high DATA change form low to high
- delay_us(4);
- IIC_SCL=1;
- IIC_SDA=1;//·¢?íI2C×ü???áê?D?o?
- delay_us(4);
- }
- //μè′yó|′eD?o?μ?à′
- //·μ???μ£o1£??óê?ó|′e꧰ü
- // 0£??óê?ó|′e3é1|
- u8 IIC_Wait_Ack(void)
- {
- u8 ucErrTime=0;
- SDA_IN(); //SDAéè???aê?è?
- IIC_SDA=1;delay_us(1);
- IIC_SCL=1;delay_us(1);
- while(READ_SDA)
- {
- ucErrTime++;
- if(ucErrTime>250)
- {
- IIC_Stop();
- return 1;
- }
- }
- IIC_SCL=0;//ê±?óê?3?0
- return 0;
- }
- //2úéúACKó|′e
- void IIC_Ack(void)
- {
- IIC_SCL=0;
- SDA_OUT();
- IIC_SDA=0;
- delay_us(2);
- IIC_SCL=1;
- delay_us(2);
- IIC_SCL=0;
- }
- //2?2úéúACKó|′e
- void IIC_NAck(void)
- {
- IIC_SCL=0;
- SDA_OUT();
- IIC_SDA=1;
- delay_us(2);
- IIC_SCL=1;
- delay_us(2);
- IIC_SCL=0;
- }
- //IIC·¢?íò???×??ú
- //·μ??′ó?úóD?Tó|′e
- //1£?óDó|′e
- //0£??Tó|′e
- void IIC_Send_Byte(u8 txd)
- {
- u8 t;
- SDA_OUT();
- IIC_SCL=0;//à-μíê±?ó?aê?êy?Y′?ê?
- for(t=0;t<8;t++)
- {
- IIC_SDA=(txd&0x80)>>7;
- txd<<=1;
- delay_us(2); //??TEA5767?aèy???óê±??ê?±?D?μ?
- IIC_SCL=1;
- delay_us(2);
- IIC_SCL=0;
- delay_us(2);
- }
- }
- //?á1??×??ú£?ack=1ê±£?·¢?íACK£?ack=0£?·¢?ínACK
- u8 IIC_Read_Byte(unsigned char ack)
- {
- unsigned char i,receive=0;
- SDA_IN();//SDAéè???aê?è?
- for(i=0;i<8;i++ )
- {
- IIC_SCL=0;
- delay_us(2);
- IIC_SCL=1;
- receive<<=1;
- if(READ_SDA)receive++;
- delay_us(1);
- }
- if (!ack)
- IIC_NAck();//·¢?ínACK
- else
- IIC_Ack(); //·¢?íACK
- return receive;
- }
-
- void IIC_WriteByte(uint16_t addr,uint8_t data,uint8_t device_addr)
- {
- IIC_Start();
-
- if(device_addr==0xA0) //eepromμ??·′óóú1×??ú
- IIC_Send_Byte(0xA0 + ((addr/256)<<1));//·¢?í??μ??·
- else
- IIC_Send_Byte(device_addr); //·¢?÷?tμ??·
- IIC_Wait_Ack();
- IIC_Send_Byte(addr&0xFF); //·¢?íμíμ??·
- IIC_Wait_Ack();
- IIC_Send_Byte(data); //·¢?í×??ú
- IIC_Wait_Ack();
- IIC_Stop();//2úéúò???í£?1ì??t
- if(device_addr==0xA0) //
- delay_ms(10);
- else
- delay_us(2);
- }
-
- uint16_t IIC_ReadByte(uint16_t addr,uint8_t device_addr,uint8_t ByteNumToRead) //?á??′??÷?ò?áêy?Y
- {
- uint16_t data;
- IIC_Start();
- if(device_addr==0xA0)
- IIC_Send_Byte(0xA0 + ((addr/256)<<1));
- else
- IIC_Send_Byte(device_addr);
- IIC_Wait_Ack();
- IIC_Send_Byte(addr&0xFF); //·¢?íμíμ??·
- IIC_Wait_Ack();
-
- IIC_Start();
- IIC_Send_Byte(device_addr+1); //·¢?÷?tμ??·
- IIC_Wait_Ack();
- if(ByteNumToRead == 1)//LM75???èêy?Y?a11bit
- {
- data=IIC_Read_Byte(0);
- }
- else
- {
- data=IIC_Read_Byte(1);
- data=(data<<8)+IIC_Read_Byte(0);
- }
- IIC_Stop();//2úéúò???í£?1ì??t
- return data;
- }
-
-
- /**********
- *é???2?·??aIO?ú?£?éI2C????
- *
- *′ó?aò????aê??aAHT20μ?????I2C
- *oˉêy??óDIICoíI2Cμ???±e£???×¢òa£?£?£?£?£?
- *
- *2020/2/23×?oóDT??è??ú
- *
- ***********/
- void read_AHT20_once(void)
- {
- //delay_ms(10);
-
- reset_AHT20();
- delay_ms(5);
-
- init_AHT20();
- delay_ms(5);
-
- startMeasure_AHT20();
- delay_ms(50);
-
- read_AHT20();
- //delay_ms(5);
- }
-
-
- void reset_AHT20(void)
- {
-
- I2C_Start();
-
- I2C_WriteByte(0x70);
- ack_status = Receive_ACK();
- if(ack_status) printf("1");
- else printf("1-n-");
- I2C_WriteByte(0xBA);
- ack_status = Receive_ACK();
- if(ack_status) printf("2");
- else printf("2-n-");
- I2C_Stop();
-
- /*
- AHT20_OutData[0] = 0;
- AHT20_OutData[1] = 0;
- AHT20_OutData[2] = 0;
- AHT20_OutData[3] = 0;
- */
- }
-
-
-
- void init_AHT20(void)
- {
- I2C_Start();
-
- I2C_WriteByte(0x70);
- ack_status = Receive_ACK();
- if(ack_status) printf("3");
- else printf("3-n-");
- I2C_WriteByte(0xE1);
- ack_status = Receive_ACK();
- if(ack_status) printf("4");
- else printf("4-n-");
- I2C_WriteByte(0x08);
- ack_status = Receive_ACK();
- if(ack_status) printf("5");
- else printf("5-n-");
- I2C_WriteByte(0x00);
- ack_status = Receive_ACK();
- if(ack_status) printf("6");
- else printf("6-n-");
- I2C_Stop();
- }
-
-
-
- void startMeasure_AHT20(void)
- {
- //------------
- I2C_Start();
-
- I2C_WriteByte(0x70);
- ack_status = Receive_ACK();
- if(ack_status) printf("7");
- else printf("7-n-");
- I2C_WriteByte(0xAC);
- ack_status = Receive_ACK();
- if(ack_status) printf("8");
- else printf("8-n-");
- I2C_WriteByte(0x33);
- ack_status = Receive_ACK();
- if(ack_status) printf("9");
- else printf("9-n-");
- I2C_WriteByte(0x00);
- ack_status = Receive_ACK();
- if(ack_status) printf("10");
- else printf("10-n-");
- I2C_Stop();
- }
-
-
-
- void read_AHT20(void)
- {
- uint8_t i;
-
- for(i=0; i<6; i++)
- {
- readByte[i]=0;
- }
-
- //-------------
- I2C_Start();
-
- I2C_WriteByte(0x71);
- ack_status = Receive_ACK();
- readByte[0]= I2C_ReadByte();
- Send_ACK();
-
- readByte[1]= I2C_ReadByte();
- Send_ACK();
-
- readByte[2]= I2C_ReadByte();
- Send_ACK();
-
- readByte[3]= I2C_ReadByte();
- Send_ACK();
-
- readByte[4]= I2C_ReadByte();
- Send_ACK();
-
- readByte[5]= I2C_ReadByte();
- SendNot_Ack();
- //Send_ACK();
-
- I2C_Stop();
-
- //--------------
- if( (readByte[0] & 0x68) == 0x08 )
- {
- H1 = readByte[1];
- H1 = (H1<<8) | readByte[2];
- H1 = (H1<<8) | readByte[3];
- H1 = H1>>4;
- H1 = (H1*1000)/1024/1024;
-
- T1 = readByte[3];
- T1 = T1 & 0x0000000F;
- T1 = (T1<<8) | readByte[4];
- T1 = (T1<<8) | readByte[5];
-
- T1 = (T1*2000)/1024/1024 - 500;
-
- AHT20_OutData[0] = (H1>>8) & 0x000000FF;
- AHT20_OutData[1] = H1 & 0x000000FF;
-
- AHT20_OutData[2] = (T1>>8) & 0x000000FF;
- AHT20_OutData[3] = T1 & 0x000000FF;
- }
- else
- {
- AHT20_OutData[0] = 0xFF;
- AHT20_OutData[1] = 0xFF;
-
- AHT20_OutData[2] = 0xFF;
- AHT20_OutData[3] = 0xFF;
- printf("lyy");
-
- }
- // printf("\r\n");
- // printf("温度:%d%d.%d",T1/100,(T1/10)%10,T1%10);
- // printf("湿度:%d%d.%d",H1/100,(H1/10)%10,H1%10);
- // printf("\r\n");
-
- OLED_ShowString(1, 1, "Temperature:");
- OLED_ShowNum(1, 13, T1/100, 1);
- OLED_ShowNum(1, 14, (T1/10)%10, 1);
- OLED_ShowChar(1, 15, '.');
- OLED_ShowNum(1, 16, T1%10, 1);
-
- OLED_ShowString(2, 1, "Humidity:");
- OLED_ShowNum(2, 10, H1/100, 1);
- OLED_ShowNum(2, 11, (H1/10)%10, 1);
- OLED_ShowChar(2, 12, '.');
- OLED_ShowNum(2, 13, H1%10, 1);
- }
-
-
-
-
- uint8_t Receive_ACK(void)
- {
- uint8_t result=0;
- uint8_t cnt=0;
-
- IIC_SCL = 0;
- SDA_IN();
- delay_us(4);
-
- IIC_SCL = 1;
- delay_us(4);
-
- while(READ_SDA && (cnt<100))
- {
- cnt++;
- }
-
- IIC_SCL = 0;
- delay_us(4);
-
- if(cnt<100)
- {
- result=1;
- }
- return result;
- }
-
-
-
- void Send_ACK(void)
- {
- SDA_OUT();
- IIC_SCL = 0;
- delay_us(4);
-
- IIC_SDA = 0;
- delay_us(4);
-
- IIC_SCL = 1;
- delay_us(4);
- IIC_SCL = 0;
- delay_us(4);
-
- SDA_IN();
- }
-
-
-
- void SendNot_Ack(void)
- {
- SDA_OUT();
- IIC_SCL = 0;
- delay_us(4);
-
- IIC_SDA = 1;
- delay_us(4);
-
- IIC_SCL = 1;
- delay_us(4);
-
- IIC_SCL = 0;
- delay_us(4);
-
- IIC_SDA = 0;
- delay_us(4);
- }
-
-
- void I2C_WriteByte(uint8_t input)
- {
- uint8_t i;
- SDA_OUT();
- for(i=0; i<8; i++)
- {
- IIC_SCL = 0;
- delay_ms(5);
-
- if(input & 0x80)
- {
- IIC_SDA = 1;
- //delaymm(10);
- }
- else
- {
- IIC_SDA = 0;
- //delaymm(10);
- }
-
- IIC_SCL = 1;
- delay_ms(5);
-
- input = (input<<1);
- }
-
- IIC_SCL = 0;
- delay_us(4);
-
- SDA_IN();
- delay_us(4);
- }
-
-
- uint8_t I2C_ReadByte(void)
- {
- uint8_t resultByte=0;
- uint8_t i=0, a=0;
-
- IIC_SCL = 0;
- SDA_IN();
- delay_ms(4);
-
- for(i=0; i<8; i++)
- {
- IIC_SCL = 1;
- delay_ms(3);
-
- a=0;
- if(READ_SDA)
- {
- a=1;
- }
- else
- {
- a=0;
- }
-
- //resultByte = resultByte | a;
- resultByte = (resultByte << 1) | a;
-
- IIC_SCL = 0;
- delay_ms(3);
- }
-
- SDA_IN();
- delay_ms(10);
-
- return resultByte;
- }
-
-
- void set_AHT20sendOutData(void)
- {
- /* --------------------------
- * 0xFA 0x06 0x0A temperature(2 Bytes) humility(2Bytes) short Address(2 Bytes)
- * And Check (1 byte)
- * -------------------------*/
- AHT20sendOutData[3] = AHT20_OutData[0];
- AHT20sendOutData[4] = AHT20_OutData[1];
- AHT20sendOutData[5] = AHT20_OutData[2];
- AHT20sendOutData[6] = AHT20_OutData[3];
-
- // AHT20sendOutData[7] = (drf1609.shortAddress >> 8) & 0x00FF;
- // AHT20sendOutData[8] = drf1609.shortAddress & 0x00FF;
-
- // AHT20sendOutData[9] = getXY(AHT20sendOutData,10);
- }
-
-
- void I2C_Start(void)
- {
- SDA_OUT();
- IIC_SCL = 1;
- delay_ms(4);
-
- IIC_SDA = 1;
- delay_ms(4);
- IIC_SDA = 0;
- delay_ms(4);
-
- IIC_SCL = 0;
- delay_ms(4);
- }
-
-
-
- void I2C_Stop(void)
- {
- SDA_OUT();
- IIC_SDA = 0;
- delay_ms(4);
-
- IIC_SCL = 1;
- delay_ms(4);
-
- IIC_SDA = 1;
- delay_ms(4);
- }
-
(5:OLED_Font.h文件编写:
- #ifndef __OLED_FONT_H
- #define __OLED_FONT_H
-
- /*OLED字模库,宽8像素,高16像素*/
- const uint8_t OLED_F8x16[][16]=
- {
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
-
- 0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1
-
- 0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2
-
- 0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,
- 0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3
-
- 0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,
- 0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4
-
- 0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,
- 0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5
-
- 0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,
- 0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6
-
- 0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7
-
- 0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,
- 0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8
-
- 0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,
- 0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9
-
- 0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,
- 0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10
-
- 0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,
- 0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11
-
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12
-
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13
-
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14
-
- 0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,
- 0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15
-
- 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
- 0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16
-
- 0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,
- 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17
-
- 0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,
- 0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18
-
- 0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,
- 0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19
-
- 0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,
- 0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20
-
- 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,
- 0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21
-
- 0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,
- 0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22
-
- 0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,
- 0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23
-
- 0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,
- 0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24
-
- 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
- 0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25
-
- 0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
-
- 0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27
-
- 0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,
- 0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28
-
- 0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,
- 0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29
-
- 0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,
- 0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30
-
- 0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,
- 0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31
-
- 0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,
- 0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32
-
- 0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,
- 0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33
-
- 0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,
- 0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34
-
- 0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,
- 0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35
-
- 0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,
- 0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36
-
- 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
- 0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
-
- 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
- 0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38
-
- 0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,
- 0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39
-
- 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
- 0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
-
- 0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,
- 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41
-
- 0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,
- 0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42
-
- 0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,
- 0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43
-
- 0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,
- 0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
-
- 0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,
- 0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
-
- 0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,
- 0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46
-
- 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
- 0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47
-
- 0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,
- 0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
-
- 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
- 0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49
-
- 0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,
- 0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
-
- 0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,
- 0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51
-
- 0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,
- 0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
-
- 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
- 0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53
-
- 0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,
- 0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54
-
- 0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,
- 0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55
-
- 0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,
- 0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56
-
- 0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,
- 0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57
-
- 0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,
- 0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58
-
- 0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,
- 0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59
-
- 0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60
-
- 0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,
- 0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61
-
- 0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
-
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63
-
- 0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
-
- 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
- 0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
-
- 0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,
- 0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66
-
- 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,
- 0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67
-
- 0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,
- 0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68
-
- 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
- 0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69
-
- 0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,
- 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70
-
- 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
- 0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71
-
- 0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,
- 0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72
-
- 0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,
- 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
-
- 0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,
- 0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74
-
- 0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,
- 0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75
-
- 0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,
- 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76
-
- 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
- 0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77
-
- 0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,
- 0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
-
- 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
- 0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79
-
- 0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,
- 0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80
-
- 0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,
- 0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81
-
- 0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
- 0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
-
- 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
- 0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83
-
- 0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,
- 0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84
-
- 0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,
- 0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85
-
- 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
- 0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86
-
- 0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,
- 0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87
-
- 0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
- 0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88
-
- 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
- 0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
-
- 0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
- 0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90
-
- 0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,
- 0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91
-
- 0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92
-
- 0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,
- 0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93
-
- 0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
- };
-
- #endif
(6:delay.h与delay.c文件编写:
delay.h:
- #ifndef __DELAY_H
- #define __DELAY_H
- #include "sys.h"
- //
-
- //STM32F103o?D?°?ày3ì
- //?aoˉêy°?±?ày3ì
- /********** mcudev.taobao.com 3??· ********/
-
-
- //ê1ó?SysTickμ???í¨??êy?£ê????ó3ù??DD1üàí
- //°üà¨delay_us,delay_ms
-
- //
- void delay_init(void);
- void delay_ms(u16 nms);
- void delay_us(u32 nus);
-
- #endif
delay.c:
- #include "delay.h"
- #include "sys.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·¢°?
- //ê1ó?SysTickμ???í¨??êy?£ê????ó3ù??DD1üàí
- //°üà¨delay_us,delay_ms
-
- //
- static u8 fac_us=0;//us?óê±±?3?êy
- static u16 fac_ms=0;//ms?óê±±?3?êy
- #ifdef OS_CRITICAL_METHOD //è?1?OS_CRITICAL_METHOD?¨ò?á?,?μ?÷ê1ó?ucosIIá?.
- //systick?D??·t??oˉêy,ê1ó?ucosê±ó?μ?
- void SysTick_Handler(void)
- {
- OSIntEnter(); //??è??D??
- OSTimeTick(); //μ÷ó?ucosμ?ê±?ó·t??3ìDò
- OSIntExit(); //′¥·¢è????D??èí?D??
- }
- #endif
-
- //3?ê??ˉ?ó3ùoˉêy
- //μ±ê1ó?ucosμ?ê±oò,′?oˉêy?á3?ê??ˉucosμ?ê±?ó?ú??
- //SYSTICKμ?ê±?ó1ì?¨?aHCLKê±?óμ?1/8
- //SYSCLK:?μí3ê±?ó
- void delay_init()
- {
-
- #ifdef OS_CRITICAL_METHOD //è?1?OS_CRITICAL_METHOD?¨ò?á?,?μ?÷ê1ó?ucosIIá?.
- u32 reload;
- #endif
- SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //????ía2?ê±?ó HCLK/8
- fac_us=SystemCoreClock/8000000; //?a?μí3ê±?óμ?1/8
-
- #ifdef OS_CRITICAL_METHOD //è?1?OS_CRITICAL_METHOD?¨ò?á?,?μ?÷ê1ó?ucosIIá?.
- reload=SystemCoreClock/8000000; //?????óμ???êy′?êy μ¥???aK
- reload*=1000000/OS_TICKS_PER_SEC;//?ù?YOS_TICKS_PER_SECéè?¨ò?3?ê±??
- //reload?a24????′??÷,×?′ó?μ:16777216,?ú72M??,??o?1.86s×óóò
- fac_ms=1000/OS_TICKS_PER_SEC;//′ú±íucos?éò??óê±μ?×?éùμ¥??
- SysTick->CTRL|=SysTick_CTRL_TICKINT_Msk; //?a??SYSTICK?D??
- SysTick->LOAD=reload; //??1/OS_TICKS_PER_SEC???D??ò?′?
- SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk; //?a??SYSTICK
- #else
- fac_ms=(u16)fac_us*1000;//·?ucos??,′ú±í????msDèòaμ?systickê±?óêy
- #endif
- }
-
- #ifdef OS_CRITICAL_METHOD //ê1ó?á?ucos
- //?óê±nus
- //nus?aòa?óê±μ?usêy.
- void delay_us(u32 nus)
- {
- u32 ticks;
- u32 told,tnow,tcnt=0;
- u32 reload=SysTick->LOAD; //LOADμ??μ
- ticks=nus*fac_us; //Dèòaμ??ú??êy
- tcnt=0;
- told=SysTick->VAL; //????è?ê±μ???êy?÷?μ
- while(1)
- {
- tnow=SysTick->VAL;
- if(tnow!=told)
- {
- if(tnow<told)tcnt+=told-tnow;//?aà?×¢òaò???SYSTICKê?ò???μY??μ???êy?÷?í?éò?á?.
- else tcnt+=reload-tnow+told;
- told=tnow;
- if(tcnt>=ticks)break;//ê±??3?1y/μèóúòa?ó3ùμ?ê±??,?òí?3?.
- }
- };
- }
- //?óê±nms
- //nms:òa?óê±μ?msêy
- void delay_ms(u16 nms)
- {
- if(OSRunning==TRUE)//è?1?osò??-?ú?üá?
- {
- if(nms>=fac_ms)//?óê±μ?ê±??′óóúucosμ?×?éùê±???ü?ú
- {
- OSTimeDly(nms/fac_ms);//ucos?óê±
- }
- nms%=fac_ms; //ucosò??-?T·¨ìá1??a?′D?μ??óê±á?,2éó???í¨·?ê??óê±
- }
- delay_us((u32)(nms*1000)); //??í¨·?ê??óê±,′?ê±ucos?T·¨???ˉμ÷?è.
- }
- #else//2?ó?ucosê±
- //?óê±nus
- //nus?aòa?óê±μ?usêy.
- void delay_us(u32 nus)
- {
- u32 temp;
- SysTick->LOAD=nus*fac_us; //ê±???ó??
- SysTick->VAL=0x00; //??????êy?÷
- SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //?aê?μ1êy
- do
- {
- temp=SysTick->CTRL;
- }
- while(temp&0x01&&!(temp&(1<<16)));//μè′yê±??μ?′?
- SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //1?±???êy?÷
- SysTick->VAL =0X00; //??????êy?÷
- }
- //?óê±nms
- //×¢òanmsμ?·??§
- //SysTick->LOAD?a24????′??÷,?ùò?,×?′ó?óê±?a:
- //nms<=0xffffff*8*1000/SYSCLK
- //SYSCLKμ¥???aHz,nmsμ¥???ams
- //??72Mì??t??,nms<=1864
- void delay_ms(u16 nms)
- {
- u32 temp;
- SysTick->LOAD=(u32)nms*fac_ms;//ê±???ó??(SysTick->LOAD?a24bit)
- SysTick->VAL =0x00; //??????êy?÷
- SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //?aê?μ1êy
- do
- {
- temp=SysTick->CTRL;
- }
- while(temp&0x01&&!(temp&(1<<16)));//μè′yê±??μ?′?
- SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //1?±???êy?÷
- SysTick->VAL =0X00; //??????êy?÷
- }
- #endif
(7:usart.h与usart.c文件编写:
usart.h:
- #ifndef __USART_H
- #define __USART_H
- #include "stdio.h"
- #include "sys.h"
-
- //STM32F103o?D?°?ày3ì
- //?aoˉêy°?±?ày3ì
- /********** mcudev.taobao.com 3??· ********/
-
- //
- //STM32?a·¢°?
- //′??ú13?ê??ˉ
-
- #define USART_REC_LEN 200 //?¨ò?×?′ó?óê?×??úêy 200
- #define EN_USART1_RX 1 //ê1?ü£¨1£?/???1£¨0£?′??ú1?óê?
-
- extern u8 USART_RX_BUF[USART_REC_LEN]; //?óê??o3?,×?′óUSART_REC_LEN??×??ú.??×??ú?a??DD·?
- extern u16 USART_RX_STA; //?óê?×′ì?±ê??
- //è?1???′??ú?D???óê?£???2?òa×¢êíò???oê?¨ò?
- void uart_init(u32 bound);
- #endif
-
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?ê±?ó?ú??êy?¨ò?á?,?μ?÷òaê1ó?ucosIIá?.
- OSIntExit();
- #endif
- }
- #endif
-
(8:bsp_i2c.h与bsp_i2c.c文件编写:
bsp_i2c.h:
- #ifndef __BSP_I2C_H
- #define __BSP_I2C_H
-
- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- //ê1ó?IIC1 1ò??M24C02,OLED,LM75AD,HT1382 PB6,PB7
-
- #define SDA_IN() {GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=(u32)8<<28;}
- #define SDA_OUT() {GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=(u32)3<<28;}
-
- //IO2ù×÷oˉêy
- #define IIC_SCL PBout(6) //SCL
- #define IIC_SDA PBout(7) //SDA
- #define READ_SDA PBin(7) //ê?è?SDA
-
-
- //IIC?ùóD2ù×÷oˉêy
- void IIC_Init(void); //3?ê??ˉIICμ?IO?ú
- void IIC_Start(void); //·¢?íIIC?aê?D?o?
- void IIC_Stop(void); //·¢?íIICí£?1D?o?
- void IIC_Send_Byte(u8 txd); //IIC·¢?íò???×??ú
- u8 IIC_Read_Byte(unsigned char ack);//IIC?áè?ò???×??ú
- u8 IIC_Wait_Ack(void); //IICμè′yACKD?o?
- void IIC_Ack(void); //IIC·¢?íACKD?o?
- void IIC_NAck(void); //IIC2?·¢?íACKD?o?
-
- void IIC_WriteByte(uint16_t addr,uint8_t data,uint8_t device_addr);
- uint16_t IIC_ReadByte(uint16_t addr,uint8_t device_addr,uint8_t ByteNumToRead);//??′??÷μ??·£??÷?tμ??·£?òa?áμ?×??úêy
-
-
- void read_AHT20_once(void);
- void reset_AHT20(void);
- void init_AHT20(void);
- void startMeasure_AHT20(void);
- void read_AHT20(void);
- uint8_t Receive_ACK(void);
- void Send_ACK(void);
- void SendNot_Ack(void);
- void I2C_WriteByte(uint8_t input);
- uint8_t I2C_ReadByte(void);
- void set_AHT20sendOutData(void);
- void I2C_Start(void);
- void I2C_Stop(void);
- #endif
-
bsp_i2c.c:
- #include "bsp_i2c.h"
- #include "delay.h"
- #include "OLED.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;
-
- }
- //2úéúIIC?eê?D?o?
- void IIC_Start(void)
- {
- SDA_OUT(); //sda??ê?3?
- IIC_SDA=1;
- IIC_SCL=1;
- delay_us(4);
- IIC_SDA=0;//START:when CLK is high,DATA change form high to low
- delay_us(4);
- IIC_SCL=0;//?ˉ×?I2C×ü??£?×?±?·¢?í?ò?óê?êy?Y
- }
- //2úéúIICí£?1D?o?
- void IIC_Stop(void)
- {
- SDA_OUT();//sda??ê?3?
- IIC_SCL=0;
- IIC_SDA=0;//STOP:when CLK is high DATA change form low to high
- delay_us(4);
- IIC_SCL=1;
- IIC_SDA=1;//·¢?íI2C×ü???áê?D?o?
- delay_us(4);
- }
- //μè′yó|′eD?o?μ?à′
- //·μ???μ£o1£??óê?ó|′e꧰ü
- // 0£??óê?ó|′e3é1|
- u8 IIC_Wait_Ack(void)
- {
- u8 ucErrTime=0;
- SDA_IN(); //SDAéè???aê?è?
- IIC_SDA=1;delay_us(1);
- IIC_SCL=1;delay_us(1);
- while(READ_SDA)
- {
- ucErrTime++;
- if(ucErrTime>250)
- {
- IIC_Stop();
- return 1;
- }
- }
- IIC_SCL=0;//ê±?óê?3?0
- return 0;
- }
- //2úéúACKó|′e
- void IIC_Ack(void)
- {
- IIC_SCL=0;
- SDA_OUT();
- IIC_SDA=0;
- delay_us(2);
- IIC_SCL=1;
- delay_us(2);
- IIC_SCL=0;
- }
- //2?2úéúACKó|′e
- void IIC_NAck(void)
- {
- IIC_SCL=0;
- SDA_OUT();
- IIC_SDA=1;
- delay_us(2);
- IIC_SCL=1;
- delay_us(2);
- IIC_SCL=0;
- }
- //IIC·¢?íò???×??ú
- //·μ??′ó?úóD?Tó|′e
- //1£?óDó|′e
- //0£??Tó|′e
- void IIC_Send_Byte(u8 txd)
- {
- u8 t;
- SDA_OUT();
- IIC_SCL=0;//à-μíê±?ó?aê?êy?Y′?ê?
- for(t=0;t<8;t++)
- {
- IIC_SDA=(txd&0x80)>>7;
- txd<<=1;
- delay_us(2); //??TEA5767?aèy???óê±??ê?±?D?μ?
- IIC_SCL=1;
- delay_us(2);
- IIC_SCL=0;
- delay_us(2);
- }
- }
- //?á1??×??ú£?ack=1ê±£?·¢?íACK£?ack=0£?·¢?ínACK
- u8 IIC_Read_Byte(unsigned char ack)
- {
- unsigned char i,receive=0;
- SDA_IN();//SDAéè???aê?è?
- for(i=0;i<8;i++ )
- {
- IIC_SCL=0;
- delay_us(2);
- IIC_SCL=1;
- receive<<=1;
- if(READ_SDA)receive++;
- delay_us(1);
- }
- if (!ack)
- IIC_NAck();//·¢?ínACK
- else
- IIC_Ack(); //·¢?íACK
- return receive;
- }
-
- void IIC_WriteByte(uint16_t addr,uint8_t data,uint8_t device_addr)
- {
- IIC_Start();
-
- if(device_addr==0xA0) //eepromμ??·′óóú1×??ú
- IIC_Send_Byte(0xA0 + ((addr/256)<<1));//·¢?í??μ??·
- else
- IIC_Send_Byte(device_addr); //·¢?÷?tμ??·
- IIC_Wait_Ack();
- IIC_Send_Byte(addr&0xFF); //·¢?íμíμ??·
- IIC_Wait_Ack();
- IIC_Send_Byte(data); //·¢?í×??ú
- IIC_Wait_Ack();
- IIC_Stop();//2úéúò???í£?1ì??t
- if(device_addr==0xA0) //
- delay_ms(10);
- else
- delay_us(2);
- }
-
- uint16_t IIC_ReadByte(uint16_t addr,uint8_t device_addr,uint8_t ByteNumToRead) //?á??′??÷?ò?áêy?Y
- {
- uint16_t data;
- IIC_Start();
- if(device_addr==0xA0)
- IIC_Send_Byte(0xA0 + ((addr/256)<<1));
- else
- IIC_Send_Byte(device_addr);
- IIC_Wait_Ack();
- IIC_Send_Byte(addr&0xFF); //·¢?íμíμ??·
- IIC_Wait_Ack();
-
- IIC_Start();
- IIC_Send_Byte(device_addr+1); //·¢?÷?tμ??·
- IIC_Wait_Ack();
- if(ByteNumToRead == 1)//LM75???èêy?Y?a11bit
- {
- data=IIC_Read_Byte(0);
- }
- else
- {
- data=IIC_Read_Byte(1);
- data=(data<<8)+IIC_Read_Byte(0);
- }
- IIC_Stop();//2úéúò???í£?1ì??t
- return data;
- }
-
-
- /**********
- *é???2?·??aIO?ú?£?éI2C????
- *
- *′ó?aò????aê??aAHT20μ?????I2C
- *oˉêy??óDIICoíI2Cμ???±e£???×¢òa£?£?£?£?£?
- *
- *2020/2/23×?oóDT??è??ú
- *
- ***********/
- void read_AHT20_once(void)
- {
- //delay_ms(10);
-
- reset_AHT20();
- delay_ms(5);
-
- init_AHT20();
- delay_ms(5);
-
- startMeasure_AHT20();
- delay_ms(50);
-
- read_AHT20();
- //delay_ms(5);
- }
-
-
- void reset_AHT20(void)
- {
-
- I2C_Start();
-
- I2C_WriteByte(0x70);
- ack_status = Receive_ACK();
- if(ack_status) printf("1");
- else printf("1-n-");
- I2C_WriteByte(0xBA);
- ack_status = Receive_ACK();
- if(ack_status) printf("2");
- else printf("2-n-");
- I2C_Stop();
-
- /*
- AHT20_OutData[0] = 0;
- AHT20_OutData[1] = 0;
- AHT20_OutData[2] = 0;
- AHT20_OutData[3] = 0;
- */
- }
-
-
-
- void init_AHT20(void)
- {
- I2C_Start();
-
- I2C_WriteByte(0x70);
- ack_status = Receive_ACK();
- if(ack_status) printf("3");
- else printf("3-n-");
- I2C_WriteByte(0xE1);
- ack_status = Receive_ACK();
- if(ack_status) printf("4");
- else printf("4-n-");
- I2C_WriteByte(0x08);
- ack_status = Receive_ACK();
- if(ack_status) printf("5");
- else printf("5-n-");
- I2C_WriteByte(0x00);
- ack_status = Receive_ACK();
- if(ack_status) printf("6");
- else printf("6-n-");
- I2C_Stop();
- }
-
-
-
- void startMeasure_AHT20(void)
- {
- //------------
- I2C_Start();
-
- I2C_WriteByte(0x70);
- ack_status = Receive_ACK();
- if(ack_status) printf("7");
- else printf("7-n-");
- I2C_WriteByte(0xAC);
- ack_status = Receive_ACK();
- if(ack_status) printf("8");
- else printf("8-n-");
- I2C_WriteByte(0x33);
- ack_status = Receive_ACK();
- if(ack_status) printf("9");
- else printf("9-n-");
- I2C_WriteByte(0x00);
- ack_status = Receive_ACK();
- if(ack_status) printf("10");
- else printf("10-n-");
- I2C_Stop();
- }
-
-
-
- void read_AHT20(void)
- {
- uint8_t i;
-
- for(i=0; i<6; i++)
- {
- readByte[i]=0;
- }
-
- //-------------
- I2C_Start();
-
- I2C_WriteByte(0x71);
- ack_status = Receive_ACK();
- readByte[0]= I2C_ReadByte();
- Send_ACK();
-
- readByte[1]= I2C_ReadByte();
- Send_ACK();
-
- readByte[2]= I2C_ReadByte();
- Send_ACK();
-
- readByte[3]= I2C_ReadByte();
- Send_ACK();
-
- readByte[4]= I2C_ReadByte();
- Send_ACK();
-
- readByte[5]= I2C_ReadByte();
- SendNot_Ack();
- //Send_ACK();
-
- I2C_Stop();
-
- //--------------
- if( (readByte[0] & 0x68) == 0x08 )
- {
- H1 = readByte[1];
- H1 = (H1<<8) | readByte[2];
- H1 = (H1<<8) | readByte[3];
- H1 = H1>>4;
- H1 = (H1*1000)/1024/1024;
-
- T1 = readByte[3];
- T1 = T1 & 0x0000000F;
- T1 = (T1<<8) | readByte[4];
- T1 = (T1<<8) | readByte[5];
-
- T1 = (T1*2000)/1024/1024 - 500;
-
- AHT20_OutData[0] = (H1>>8) & 0x000000FF;
- AHT20_OutData[1] = H1 & 0x000000FF;
-
- AHT20_OutData[2] = (T1>>8) & 0x000000FF;
- AHT20_OutData[3] = T1 & 0x000000FF;
- }
- else
- {
- AHT20_OutData[0] = 0xFF;
- AHT20_OutData[1] = 0xFF;
-
- AHT20_OutData[2] = 0xFF;
- AHT20_OutData[3] = 0xFF;
- printf("lyy");
-
- }
- // printf("\r\n");
- // printf("温度:%d%d.%d",T1/100,(T1/10)%10,T1%10);
- // printf("湿度:%d%d.%d",H1/100,(H1/10)%10,H1%10);
- // printf("\r\n");
-
- OLED_ShowString(1, 1, "Temperature:");
- OLED_ShowNum(1, 13, T1/100, 1);
- OLED_ShowNum(1, 14, (T1/10)%10, 1);
- OLED_ShowChar(1, 15, '.');
- OLED_ShowNum(1, 16, T1%10, 1);
-
- OLED_ShowString(2, 1, "Humidity:");
- OLED_ShowNum(2, 10, H1/100, 1);
- OLED_ShowNum(2, 11, (H1/10)%10, 1);
- OLED_ShowChar(2, 12, '.');
- OLED_ShowNum(2, 13, H1%10, 1);
- }
-
-
-
-
- uint8_t Receive_ACK(void)
- {
- uint8_t result=0;
- uint8_t cnt=0;
-
- IIC_SCL = 0;
- SDA_IN();
- delay_us(4);
-
- IIC_SCL = 1;
- delay_us(4);
-
- while(READ_SDA && (cnt<100))
- {
- cnt++;
- }
-
- IIC_SCL = 0;
- delay_us(4);
-
- if(cnt<100)
- {
- result=1;
- }
- return result;
- }
-
-
-
- void Send_ACK(void)
- {
- SDA_OUT();
- IIC_SCL = 0;
- delay_us(4);
-
- IIC_SDA = 0;
- delay_us(4);
-
- IIC_SCL = 1;
- delay_us(4);
- IIC_SCL = 0;
- delay_us(4);
-
- SDA_IN();
- }
-
-
-
- void SendNot_Ack(void)
- {
- SDA_OUT();
- IIC_SCL = 0;
- delay_us(4);
-
- IIC_SDA = 1;
- delay_us(4);
-
- IIC_SCL = 1;
- delay_us(4);
-
- IIC_SCL = 0;
- delay_us(4);
-
- IIC_SDA = 0;
- delay_us(4);
- }
-
-
- void I2C_WriteByte(uint8_t input)
- {
- uint8_t i;
- SDA_OUT();
- for(i=0; i<8; i++)
- {
- IIC_SCL = 0;
- delay_ms(5);
-
- if(input & 0x80)
- {
- IIC_SDA = 1;
- //delaymm(10);
- }
- else
- {
- IIC_SDA = 0;
- //delaymm(10);
- }
-
- IIC_SCL = 1;
- delay_ms(5);
-
- input = (input<<1);
- }
-
- IIC_SCL = 0;
- delay_us(4);
-
- SDA_IN();
- delay_us(4);
- }
-
-
- uint8_t I2C_ReadByte(void)
- {
- uint8_t resultByte=0;
- uint8_t i=0, a=0;
-
- IIC_SCL = 0;
- SDA_IN();
- delay_ms(4);
-
- for(i=0; i<8; i++)
- {
- IIC_SCL = 1;
- delay_ms(3);
-
- a=0;
- if(READ_SDA)
- {
- a=1;
- }
- else
- {
- a=0;
- }
-
- //resultByte = resultByte | a;
- resultByte = (resultByte << 1) | a;
-
- IIC_SCL = 0;
- delay_ms(3);
- }
-
- SDA_IN();
- delay_ms(10);
-
- return resultByte;
- }
-
-
- void set_AHT20sendOutData(void)
- {
- /* --------------------------
- * 0xFA 0x06 0x0A temperature(2 Bytes) humility(2Bytes) short Address(2 Bytes)
- * And Check (1 byte)
- * -------------------------*/
- AHT20sendOutData[3] = AHT20_OutData[0];
- AHT20sendOutData[4] = AHT20_OutData[1];
- AHT20sendOutData[5] = AHT20_OutData[2];
- AHT20sendOutData[6] = AHT20_OutData[3];
-
- // AHT20sendOutData[7] = (drf1609.shortAddress >> 8) & 0x00FF;
- // AHT20sendOutData[8] = drf1609.shortAddress & 0x00FF;
-
- // AHT20sendOutData[9] = getXY(AHT20sendOutData,10);
- }
-
-
- void I2C_Start(void)
- {
- SDA_OUT();
- IIC_SCL = 1;
- delay_ms(4);
-
- IIC_SDA = 1;
- delay_ms(4);
- IIC_SDA = 0;
- delay_ms(4);
-
- IIC_SCL = 0;
- delay_ms(4);
- }
-
-
-
- void I2C_Stop(void)
- {
- SDA_OUT();
- IIC_SDA = 0;
- delay_ms(4);
-
- IIC_SCL = 1;
- delay_ms(4);
-
- IIC_SDA = 1;
- delay_ms(4);
- }
-
(9:编写主文件main.c文件代码:
- #include "delay.h"
- #include "usart.h"
- #include "bsp_i2c.h"
- #include "OLED.h"
- #include "MyRTC.h"
- #include "Timer.h"
-
- int main(void)
- {
- delay_init();
- uart_init(9600);
- OLED_Init();
- MyRTC_Init();
- IIC_Init();
- Timer_Init();
-
- MyRTC_SetTime();
- while(1)
- {
-
-
- //delay_ms(1000);
- }
- }
- void TIM2_IRQHandler(void)
- {
- if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
- {
-
- read_AHT20_once();
- MyRTC_ReadTime();
- TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
- }
- }
到此,工程文件建立完毕!库函数工程搭建与完善成功!!!工程搭建与完善完成情况截图如下所示:
1、ST-Link的电脑环境配置以及与STM32的电路连接:
请参考我的这篇博客:stm32通过寄存器方式利用GPIO端口点亮LED_-HSheng的博客-CSDN博客
这里附上ST-Link与STM32的电路连接图:
2、基于IIC协议的4排针引脚的OLED与STM32的电路连接如下:
GND——GND;
VCC——3.3V;
SCL——PB8;
SDA——PB9;
3、温湿度采集模块AHT20与STM32的连接如下图所示:
这里附上AHT20的引脚简介图:
4、连接好的板子电路实物图展示:
1、编译与烧录,如何通过ST-Link烧录进STM32中,参考我的这篇博客:
stm32通过寄存器方式利用GPIO端口点亮LED_-HSheng的博客-CSDN博客
2、 工程运行结果展示:
在完成STM32作业的过程中,通过深入研究STM32F103的RTC原理,我成功实现了读取内部时钟信息和与PC上位机的串口通信。结合带有SPI或IIC接口的OLED屏幕模块,我实现了将时钟信息及AHT20传感器的温湿度数据显示在OLED上,完成了实验要求。这次经历使我更深入了解了嵌入式系统开发,提升了对RTC和传感器的应用能力。
同时本实验让我深刻体验了理论知识与实际应用的结合。通过与RTC和OLED屏幕的互联,我更加自信地处理了嵌入式系统中的硬件交互问题。这次项目培养了我的解决问题的能力,同时也加深了对实时时钟和传感器应用的理解,为未来的嵌入式开发奠定了坚实的基础。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。