当前位置:   article > 正文

STM32日历读取,设置和输出——实时时钟RTC_rtc输出

rtc输出

目录

一、问题导出

二、 RTC简介

2.1、RTC介绍:

 2.2、RTC特征:

 2.3、RTC原理框图:

 2.4、RTC结构:

2.5、RTC具体流程:

2.6、RTC时钟选择:

 2.7、RTC复位过程:

 2.8、RTC中断:

三、搭建STM32开发环境(HAL库环境)

四、利用HAL库新建第一小问的工程 (串口显示工程)

五、完善 keil5工程

六、第一小问工程烧录运行 

七、利用库函数搭建第二小问工程(OLED显示工程)

1、关于如何搭建STM32库函数环境与库函数工程:

2、完善keil5工程--->编写工程代码:

 八、第二小问的电路连接

九、第二小问库函数工程烧录运行

十、总结

十一、参考资料


一、问题导出

       阅读资料了解 STM32F103的RTC(实时时钟)原理,使用带SPI或IIC接口的OLED屏显模块实现以下功能:

1) 读取STM32F103C8T6 内部的时钟(年月日时分秒),日历(星期x),1秒周期,通过串口输出到PC上位机,;

2) 读取AHT20的温度和湿度,通过OLED,把年月份时分秒、日历和实时温度、湿度显示出来,2秒周期。

二、 RTC简介

2.1、RTC介绍:

       RTC (Real Time Clock):实时时钟。RTC是个独立的定时器。RTC模块拥有一个连续计数的计数器,在相应的软件配置下,可以提供时钟日历的功能。修改计数器的值可以重新设置当前时间和日期 RTC还包含用于管理低功耗模式的自动唤醒单元。

       在断电情况下 RTC仍可以独立运行 只要芯片的备用电源一直供电,RTC上的时间会一直走。RTC实质是一个掉电后还继续运行的定时器,从定时器的角度来看,相对于通用定时器TIM外设,它的功能十分简单,只有计时功能(也可以触发中断)。但其高级指出也就在于掉电之后还可以正常运行。

       两个 32 位寄存器包含二进码十进数格式 (BCD) 的秒、分钟、小时( 12 或 24 小时制)、星期几、日期、月份和年份。此外,还可提供二进制格式的亚秒值。系统可以自动将月份的天数补偿为 28、29(闰年)、30 和 31 天。

     上电复位后,所有RTC寄存器都会受到保护,以防止可能的非正常写访问。无论器件状态如何(运行模式、低功耗模式或处于复位状态),只要电源电压保持在工作范围内,RTC使不会停止工作。

 2.2、RTC特征:

  • 可编程的预分频系数:分频系数高为220。

  • 32位的可编程计数器,可用于较长时间段的测量。

  • 2个分离的时钟:用于APB1接口的PCLK1和RTC时钟(RTC时钟的频率必须小于PCLK1时钟 频率的四分之一以上)。

  • 可以选择以下三种RTC的时钟源

    • HSE时钟除以128;
    • LSE振荡器时钟;
    • SI振荡器时钟
  • 2个独立的复位类型

    • APB1接口由系统复位;
    • RTC核心(预分频器、闹钟、计数器和分频器)只能由后备域复位
  • 3个专门的可屏蔽中断:

    • 1.闹钟中断,用来产生一个软件可编程的闹钟中断
    • 2.秒中断,用来产生一个可编程的周期性中断信号(长可达1秒)。
    • 3.溢出中断,指示内部可编程计数器溢出并回转为0的状态。
  • RTC时钟源
    三种不同的时钟源可被用来驱动系统时钟(SYSCLK):

    • HSI振荡器时钟
    • HSE振荡器时钟
    • PLL时钟
  • 这些设备有以下2种二级时钟源

    • 40kHz低速内部RC,可以用于驱动独立看门狗和通过程序选择驱动RTC。 RTC用于从停机/待机模式下自动唤醒系统。
    • 32.768kHz低速外部晶体也可用来通过程序选择驱动RTC(RTCCLK)。

 2.3、RTC原理框图:

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 年左右,作为一般应用,这已经是足够了的。 

 2.4、RTC结构:

 RTC时钟主要由两个内部低速时钟和一个外部高速时钟构成。基本结构如下:

硬件电路原理图如下 :  

       RTC(Real Time Clock)实时时钟是一种高精度、低功耗的定时器,它可以在各种环境下提供精确的时间和日期信息。即使在系统处于低功耗模式时,RTC也可以继续运行,确保时间的连续性。此外,它还支持时钟校准功能,可以校正时钟的偏差,保证时间的准确性。 除了基本的时间提供功能,RTC还具有报警功能,用户可以设置报警功能,当达到指定时间时触发中断,这在实际应用中非常有用。例如,可以在系统到达预定维护时间时触发警报,提醒用户进行维护操作。

       另外,RTC还支持外部电池备份,用户可以使用外部电池进行时间备份,即使在断电的情况下也能保持时间的准确。这一特性使得RTC在某些应用场景下特别适用,例如在应急照明系统、安全监控系统等需要持续、准确计时的情况下。

      RTC还具有自动唤醒功能,用户可以根据需要设置自动唤醒条件,当满足预设条件时自动唤醒系统。这一功能在许多嵌入式系统和物联网设备中非常有用,可以大大降低系统的功耗。

2.5、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 控制开关机。

2.6、RTC时钟选择:

       使用HSE分频时钟或者LSI的时候,在主电源VDD掉电的情况下,这两个时钟来源都会受到影响,因此没法保证RTC正常工作.所以RTC一般都时钟低速外部时钟LSE,频率为实时时钟模块中常用的32.768KHz,因为32768 = 2^15,分频容易实现,所以被广泛应用到RTC模块.(在主电源VDD有效的情况下待机,RTC还可以配置闹钟事件使STM32退出待机模式)。

 2.7、RTC复位过程:

       除了RTC_PRL、RTC_ALR、RTC_CNT和RTC_DIV寄存器外,所有的系统寄存器都由系统复位或电源复位进行异步复位。RTC_PRL、RTC_ALR、RTC_CNT和RTC_DIV寄存器仅能通过备份域复位信号复位。系统复位后,禁止访问后备寄存器和RCT,防止对后卫区域(BKP)的意外写操作。

 2.8、RTC中断:

(1)秒中断:
       这里时钟自带一个秒中断,每当计数加一的时候就会触发一次秒中断,。注意,这里所说的秒中断并非一定是一秒的时间,它是由RTC时钟源和分频值决定的“秒”的时间,当然也是可以做到1秒钟中断一次。我们通过往秒中断里写更新时间的函数来达到时间同步的效果

(2)闹钟中断:
      闹钟中断就是设置一个预设定的值,计数每自加多少次触发一次闹钟中断

三、搭建STM32开发环境(HAL库环境)

 请参考我的这篇博客:STM32使用HAL库点亮流水灯-CSDN博客

四、利用HAL库新建第一小问的工程 (串口显示工程)

(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中打开:

五、完善 keil5工程

1、头文件添加#include “stdio.h”:
在这里插入图片描述

2、在main.c文件中重写fputc函数,完成printf函数的重定向:

  1. //添加头文件#include "stdio.h"
  2. int fputc(int ch,FILE *f){
  3. uint8_t temp[1]={ch};
  4. HAL_UART_Transmit(&huart1,temp,1,2);
  5. return ch;
  6. }

3、在main.c中定义时间和日期的结构体用来获取时间和日期:

  1. RTC_DateTypeDef GetData; //获取日期结构体
  2. RTC_TimeTypeDef GetTime; //获取时间结构体

4、在main函数中while循环里面添加如下代码即可:

  1. /* Get the RTC current Time */
  2. HAL_RTC_GetTime(&hrtc, &GetTime, RTC_FORMAT_BIN);
  3. /* Get the RTC current Date */
  4. HAL_RTC_GetDate(&hrtc, &GetData, RTC_FORMAT_BIN);
  5. /* Display date Format : yy/mm/dd */
  6. printf("%02d/%02d/%02d\r\n",2000 + GetData.Year, GetData.Month, GetData.Date);
  7. /* Display date Format : weekday */
  8. if(GetData.WeekDay==1){
  9. printf("星期一\r\n");
  10. }else if(GetData.WeekDay==2){
  11. printf("星期二\r\n");
  12. }else if(GetData.WeekDay==3){
  13. printf("星期三\r\n");
  14. }else if(GetData.WeekDay==4){
  15. printf("星期四\r\n");
  16. }else if(GetData.WeekDay==5){
  17. printf("星期五\r\n");
  18. }else if(GetData.WeekDay==6){
  19. printf("星期六\r\n");
  20. }else if(GetData.WeekDay==7){
  21. printf("星期日\r\n");
  22. }
  23. /* Display time Format : hh:mm:ss */
  24. printf("%02d:%02d:%02d\r\n",GetTime.Hours, GetTime.Minutes, GetTime.Seconds);
  25. printf("\r\n");
  26. HAL_Delay(1000);

5、因为我们在代码里面重写了printf,所以在Target里面一定要勾选上Use MiscroLiB:

六、第一小问工程烧录运行 

1、USB转TTL与STM32的连线,以及电脑上配置CH340等等烧录环境,请参考我的这篇博客:

STM32使用HAL库中断控制串口通信-CSDN博客

2、编译与烧录,如何通过USB转TTL烧录进STM32中,也参考我的同一篇博客:

STM32使用HAL库中断控制串口通信-CSDN博客

3、工程运行结果展示:

七、利用库函数搭建第二小问工程(OLED显示工程)

1、关于如何搭建STM32库函数环境与库函数工程:

请参考我的这篇博客:

基于标准外设库的LED流水灯-CSDN博客

2、完善keil5工程--->编写工程代码:

(1)提醒:

        关于如何在keil里面通过库函数创建并且编写C文件与H文件,以及把它们都导入进库函数keil5工程中,还是参考我的这篇博客:基于标准外设库的LED流水灯-CSDN博客

(2)编写工程需要的文件代码:

(1:sys.h与sys.c文件代码编写:
sys.h:

  1. #ifndef __SYS_H
  2. #define __SYS_H
  3. #include "stm32f10x.h"
  4. //
  5. //STM32F103o?D?°?ày3ì
  6. //?aoˉêy°?±?ày3ì
  7. /********** mcudev.taobao.com 3??· ********/
  8. //
  9. //0,2??§3?ucos
  10. //1,?§3?ucos
  11. #define SYSTEM_SUPPORT_UCOS 0 //?¨ò??μí3???t?Dê?·??§3?UCOS
  12. //??′?2ù×÷,êμ??51àà??μ?GPIO????1|?ü
  13. //??ì?êμ??????,2???<<CM3è¨ít????>>μú????(87ò3~92ò3).
  14. //IO?ú2ù×÷oê?¨ò?
  15. #define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
  16. #define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
  17. #define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
  18. //IO?úμ??·ó3é?
  19. #define GPIOA_ODR_Addr (GPIOA_BASE+12) //0x4001080C
  20. #define GPIOB_ODR_Addr (GPIOB_BASE+12) //0x40010C0C
  21. #define GPIOC_ODR_Addr (GPIOC_BASE+12) //0x4001100C
  22. #define GPIOD_ODR_Addr (GPIOD_BASE+12) //0x4001140C
  23. #define GPIOE_ODR_Addr (GPIOE_BASE+12) //0x4001180C
  24. #define GPIOF_ODR_Addr (GPIOF_BASE+12) //0x40011A0C
  25. #define GPIOG_ODR_Addr (GPIOG_BASE+12) //0x40011E0C
  26. #define GPIOA_IDR_Addr (GPIOA_BASE+8) //0x40010808
  27. #define GPIOB_IDR_Addr (GPIOB_BASE+8) //0x40010C08
  28. #define GPIOC_IDR_Addr (GPIOC_BASE+8) //0x40011008
  29. #define GPIOD_IDR_Addr (GPIOD_BASE+8) //0x40011408
  30. #define GPIOE_IDR_Addr (GPIOE_BASE+8) //0x40011808
  31. #define GPIOF_IDR_Addr (GPIOF_BASE+8) //0x40011A08
  32. #define GPIOG_IDR_Addr (GPIOG_BASE+8) //0x40011E08
  33. //IO?ú2ù×÷,????μ¥ò?μ?IO?ú!
  34. //è·±£nμ??μD?óú16!
  35. #define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n) //ê?3?
  36. #define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n) //ê?è?
  37. #define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n) //ê?3?
  38. #define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n) //ê?è?
  39. #define PCout(n) BIT_ADDR(GPIOC_ODR_Addr,n) //ê?3?
  40. #define PCin(n) BIT_ADDR(GPIOC_IDR_Addr,n) //ê?è?
  41. #define PDout(n) BIT_ADDR(GPIOD_ODR_Addr,n) //ê?3?
  42. #define PDin(n) BIT_ADDR(GPIOD_IDR_Addr,n) //ê?è?
  43. #define PEout(n) BIT_ADDR(GPIOE_ODR_Addr,n) //ê?3?
  44. #define PEin(n) BIT_ADDR(GPIOE_IDR_Addr,n) //ê?è?
  45. #define PFout(n) BIT_ADDR(GPIOF_ODR_Addr,n) //ê?3?
  46. #define PFin(n) BIT_ADDR(GPIOF_IDR_Addr,n) //ê?è?
  47. #define PGout(n) BIT_ADDR(GPIOG_ODR_Addr,n) //ê?3?
  48. #define PGin(n) BIT_ADDR(GPIOG_IDR_Addr,n) //ê?è?
  49. void NVIC_Configuration(void);
  50. #endif

 sys.c:

  1. #include "sys.h"
  2. //STM32F103o?D?°?ày3ì
  3. //?aoˉêy°?±?ày3ì
  4. /********** mcudev.taobao.com 3??· ********/
  5. //
  6. //STM32?a·¢°?
  7. //?μí3?D??·?×ééè???ˉ
  8. //********************************************************************************
  9. void NVIC_Configuration(void)
  10. {
  11. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //éè??NVIC?D??·?×é2:2???à??ó??è??£?2???ìó|ó??è??
  12. }

(2:Timer.h与Timer.c文件编写:

Timer.h:

  1. #ifndef __TIMER_H
  2. #define __TIMER_H
  3. void Timer_Init(void);
  4. #endif

Timer.c:

  1. #include "stm32f10x.h" // Device header
  2. TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
  3. NVIC_InitTypeDef NVIC_InitStructure;
  4. void Timer_Init(void)
  5. {
  6. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  7. TIM_InternalClockConfig(TIM2);
  8. TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  9. TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  10. TIM_TimeBaseInitStructure.TIM_Period = 10000 - 1;
  11. TIM_TimeBaseInitStructure.TIM_Prescaler = 3600 - 1;
  12. TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
  13. TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
  14. TIM_ClearFlag(TIM2, TIM_FLAG_Update);
  15. TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
  16. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  17. NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  18. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  19. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  20. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  21. NVIC_Init(&NVIC_InitStructure);
  22. TIM_Cmd(TIM2, ENABLE);
  23. }
  24. /*
  25. void TIM2_IRQHandler(void)
  26. {
  27. if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
  28. {
  29. TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  30. }
  31. }
  32. */

(3:MyRTC.h与MyRTC.c文件编写:

MyRTC.h:

  1. #ifndef __MYRTC_H
  2. #define __MYRTC_H
  3. extern uint16_t MyRTC_Time[];
  4. void MyRTC_Init(void);
  5. void MyRTC_SetTime(void);
  6. void MyRTC_ReadTime(void);
  7. #endif

MyRTC.c:

  1. #include "stm32f10x.h" // Device header
  2. #include <time.h>
  3. #include "OLED.h"
  4. #include "delay.h"
  5. uint16_t MyRTC_Time[] = {2023, 11, 21, 21, 51, 05};
  6. void MyRTC_SetTime(void);
  7. char* weekday;
  8. uint16_t iWeek;
  9. void CalculateWeekDay(uint16_t year, uint16_t month, uint16_t day) {
  10. if (month == 1 || month ==2)
  11. {
  12. month +=12;
  13. year--;
  14. }
  15. iWeek = (day + 2 * month + 3 * (month + 1)/5 + year + year/4 - year/100 + year/400) % 7;
  16. switch (iWeek)
  17. {
  18. case 0: weekday = "Monday"; break;
  19. case 1: weekday = "Teusday"; break;
  20. case 2: weekday = "Wednesday"; break;
  21. case 3: weekday = "Thursday"; break;
  22. case 4: weekday = "Friday"; break;
  23. case 5: weekday = "Saturday"; break;
  24. case 6: weekday = "Sunday"; break;
  25. }
  26. }
  27. void MyRTC_Init(void)
  28. {
  29. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  30. RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP, ENABLE);
  31. PWR_BackupAccessCmd(ENABLE);
  32. if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
  33. {
  34. RCC_LSEConfig(RCC_LSE_ON);
  35. while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) != SET);
  36. RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  37. RCC_RTCCLKCmd(ENABLE);
  38. RTC_WaitForSynchro();
  39. RTC_WaitForLastTask();
  40. RTC_SetPrescaler(32768 - 1);
  41. RTC_WaitForLastTask();
  42. MyRTC_SetTime();
  43. BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
  44. }
  45. else
  46. {
  47. RTC_WaitForSynchro();
  48. RTC_WaitForLastTask();
  49. }
  50. }
  51. //如果LSE无法起振导致程序卡死在初始化函数中
  52. //可将初始化函数替换为下述代码,使用LSI当作RTCCLK
  53. //LSI无法由备用电源供电,故主电源掉电时,RTC走时会暂停
  54. /*
  55. void MyRTC_Init(void)
  56. {
  57. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  58. RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP, ENABLE);
  59. PWR_BackupAccessCmd(ENABLE);
  60. if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
  61. {
  62. RCC_LSICmd(ENABLE);
  63. while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) != SET);
  64. RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
  65. RCC_RTCCLKCmd(ENABLE);
  66. RTC_WaitForSynchro();
  67. RTC_WaitForLastTask();
  68. RTC_SetPrescaler(40000 - 1);
  69. RTC_WaitForLastTask();
  70. MyRTC_SetTime();
  71. BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
  72. }
  73. else
  74. {
  75. RCC_LSICmd(ENABLE);
  76. while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) != SET);
  77. RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
  78. RCC_RTCCLKCmd(ENABLE);
  79. RTC_WaitForSynchro();
  80. RTC_WaitForLastTask();
  81. }
  82. }*/
  83. void MyRTC_SetTime(void)
  84. {
  85. time_t time_cnt;
  86. struct tm time_date;
  87. time_date.tm_year = MyRTC_Time[0] - 1900;
  88. time_date.tm_mon = MyRTC_Time[1] - 1;
  89. time_date.tm_mday = MyRTC_Time[2];
  90. time_date.tm_hour = MyRTC_Time[3];
  91. time_date.tm_min = MyRTC_Time[4];
  92. time_date.tm_sec = MyRTC_Time[5];
  93. time_cnt = mktime(&time_date) - 8 * 60 * 60;
  94. RTC_SetCounter(time_cnt);
  95. RTC_WaitForLastTask();
  96. }
  97. void MyRTC_ReadTime(void)
  98. {
  99. time_t time_cnt;
  100. struct tm time_date;
  101. time_cnt = RTC_GetCounter() + 8 * 60 * 60;//
  102. time_date = *localtime(&time_cnt);
  103. MyRTC_Time[0] = time_date.tm_year + 1900;
  104. MyRTC_Time[1] = time_date.tm_mon + 1;
  105. MyRTC_Time[2] = time_date.tm_mday;
  106. MyRTC_Time[3] = time_date.tm_hour;
  107. MyRTC_Time[4] = time_date.tm_min;
  108. MyRTC_Time[5] = time_date.tm_sec;
  109. CalculateWeekDay(MyRTC_Time[0], MyRTC_Time[1], MyRTC_Time[2]);
  110. //delay_ms(10);
  111. OLED_ShowNum(3, 1, MyRTC_Time[0], 4);
  112. OLED_ShowChar(3, 5, '-');
  113. OLED_ShowNum(3, 6, MyRTC_Time[1], 2);
  114. OLED_ShowChar(3, 8, '-');
  115. OLED_ShowNum(3, 9, MyRTC_Time[2], 2);
  116. OLED_ShowNum(4, 1, MyRTC_Time[3], 2);
  117. OLED_ShowChar(4, 3, ':');
  118. OLED_ShowNum(4, 4, MyRTC_Time[4], 2);
  119. OLED_ShowChar(4, 6, ':');
  120. OLED_ShowNum(4, 7, MyRTC_Time[5], 2);
  121. OLED_ShowString(4, 10, weekday);
  122. }

 (4:OLED.h与OLED.c文件编写:

OLED.h:

  1. #ifndef __OLED_H
  2. #define __OLED_H
  3. void OLED_Init(void);
  4. void OLED_Clear(void);
  5. void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char);
  6. void OLED_ShowString(uint8_t Line, uint8_t Column, char *String);
  7. void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
  8. void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length);
  9. void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
  10. void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
  11. #endif

OLED.c:

  1. #include "bsp_i2c.h"
  2. #include "delay.h"
  3. #include "OLED.h"
  4. uint8_t ack_status=0;
  5. uint8_t readByte[6];
  6. uint8_t AHT20_status=0;
  7. uint32_t H1=0; //Humility
  8. uint32_t T1=0; //Temperature
  9. uint8_t AHT20_OutData[4];
  10. uint8_t AHT20sendOutData[10] = {0xFA, 0x06, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF};
  11. void IIC_Init(void)
  12. {
  13. GPIO_InitTypeDef GPIO_InitStructure;
  14. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );
  15. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
  16. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //í?íìê?3?
  17. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  18. GPIO_Init(GPIOB, &GPIO_InitStructure);
  19. IIC_SCL=1;
  20. IIC_SDA=1;
  21. }
  22. //2úéúIIC?eê?D?o?
  23. void IIC_Start(void)
  24. {
  25. SDA_OUT(); //sda??ê?3?
  26. IIC_SDA=1;
  27. IIC_SCL=1;
  28. delay_us(4);
  29. IIC_SDA=0;//START:when CLK is high,DATA change form high to low
  30. delay_us(4);
  31. IIC_SCL=0;//?ˉ×?I2C×ü??£?×?±?·¢?í?ò?óê?êy?Y
  32. }
  33. //2úéúIICí£?1D?o?
  34. void IIC_Stop(void)
  35. {
  36. SDA_OUT();//sda??ê?3?
  37. IIC_SCL=0;
  38. IIC_SDA=0;//STOP:when CLK is high DATA change form low to high
  39. delay_us(4);
  40. IIC_SCL=1;
  41. IIC_SDA=1;//·¢?íI2C×ü???áê?D?o?
  42. delay_us(4);
  43. }
  44. //μè′yó|′eD?o?μ?à′
  45. //·μ???μ£o1£??óê?ó|′e꧰ü
  46. // 0£??óê?ó|′e3é1|
  47. u8 IIC_Wait_Ack(void)
  48. {
  49. u8 ucErrTime=0;
  50. SDA_IN(); //SDAéè???aê?è?
  51. IIC_SDA=1;delay_us(1);
  52. IIC_SCL=1;delay_us(1);
  53. while(READ_SDA)
  54. {
  55. ucErrTime++;
  56. if(ucErrTime>250)
  57. {
  58. IIC_Stop();
  59. return 1;
  60. }
  61. }
  62. IIC_SCL=0;//ê±?óê?3?0
  63. return 0;
  64. }
  65. //2úéúACKó|′e
  66. void IIC_Ack(void)
  67. {
  68. IIC_SCL=0;
  69. SDA_OUT();
  70. IIC_SDA=0;
  71. delay_us(2);
  72. IIC_SCL=1;
  73. delay_us(2);
  74. IIC_SCL=0;
  75. }
  76. //2?2úéúACKó|′e
  77. void IIC_NAck(void)
  78. {
  79. IIC_SCL=0;
  80. SDA_OUT();
  81. IIC_SDA=1;
  82. delay_us(2);
  83. IIC_SCL=1;
  84. delay_us(2);
  85. IIC_SCL=0;
  86. }
  87. //IIC·¢?íò???×??ú
  88. //·μ??′ó?úóD?Tó|′e
  89. //1£?óDó|′e
  90. //0£??Tó|′e
  91. void IIC_Send_Byte(u8 txd)
  92. {
  93. u8 t;
  94. SDA_OUT();
  95. IIC_SCL=0;//à-μíê±?ó?aê?êy?Y′?ê?
  96. for(t=0;t<8;t++)
  97. {
  98. IIC_SDA=(txd&0x80)>>7;
  99. txd<<=1;
  100. delay_us(2); //??TEA5767?aèy???óê±??ê?±?D?μ?
  101. IIC_SCL=1;
  102. delay_us(2);
  103. IIC_SCL=0;
  104. delay_us(2);
  105. }
  106. }
  107. //1??×??ú£?ack=1ê±£?·¢?íACK£?ack=0£?·¢?ínACK
  108. u8 IIC_Read_Byte(unsigned char ack)
  109. {
  110. unsigned char i,receive=0;
  111. SDA_IN();//SDAéè???aê?è?
  112. for(i=0;i<8;i++ )
  113. {
  114. IIC_SCL=0;
  115. delay_us(2);
  116. IIC_SCL=1;
  117. receive<<=1;
  118. if(READ_SDA)receive++;
  119. delay_us(1);
  120. }
  121. if (!ack)
  122. IIC_NAck();//·¢?ínACK
  123. else
  124. IIC_Ack(); //·¢?íACK
  125. return receive;
  126. }
  127. void IIC_WriteByte(uint16_t addr,uint8_t data,uint8_t device_addr)
  128. {
  129. IIC_Start();
  130. if(device_addr==0xA0) //eepromμ??·′óóú1×??ú
  131. IIC_Send_Byte(0xA0 + ((addr/256)<<1));//·¢?í??μ??·
  132. else
  133. IIC_Send_Byte(device_addr); //·¢?÷?tμ??·
  134. IIC_Wait_Ack();
  135. IIC_Send_Byte(addr&0xFF); //·¢?íμíμ??·
  136. IIC_Wait_Ack();
  137. IIC_Send_Byte(data); //·¢?í×??ú
  138. IIC_Wait_Ack();
  139. IIC_Stop();//2úéúò???í£?1ì??t
  140. if(device_addr==0xA0) //
  141. delay_ms(10);
  142. else
  143. delay_us(2);
  144. }
  145. uint16_t IIC_ReadByte(uint16_t addr,uint8_t device_addr,uint8_t ByteNumToRead) //?á??′??÷?ò?áêy?Y
  146. {
  147. uint16_t data;
  148. IIC_Start();
  149. if(device_addr==0xA0)
  150. IIC_Send_Byte(0xA0 + ((addr/256)<<1));
  151. else
  152. IIC_Send_Byte(device_addr);
  153. IIC_Wait_Ack();
  154. IIC_Send_Byte(addr&0xFF); //·¢?íμíμ??·
  155. IIC_Wait_Ack();
  156. IIC_Start();
  157. IIC_Send_Byte(device_addr+1); //·¢?÷?tμ??·
  158. IIC_Wait_Ack();
  159. if(ByteNumToRead == 1)//LM75???èêy?Y?a11bit
  160. {
  161. data=IIC_Read_Byte(0);
  162. }
  163. else
  164. {
  165. data=IIC_Read_Byte(1);
  166. data=(data<<8)+IIC_Read_Byte(0);
  167. }
  168. IIC_Stop();//2úéúò???í£?1ì??t
  169. return data;
  170. }
  171. /**********
  172. *é???2?·??aIO?ú?£?éI2C????
  173. *
  174. *′ó?aò????aê??aAHT20μ?????I2C
  175. *oˉêy??óDIICoíI2Cμ???±e£???×¢òa£?£?£?£?£?
  176. *
  177. *2020/2/23×?oóDT??è??ú
  178. *
  179. ***********/
  180. void read_AHT20_once(void)
  181. {
  182. //delay_ms(10);
  183. reset_AHT20();
  184. delay_ms(5);
  185. init_AHT20();
  186. delay_ms(5);
  187. startMeasure_AHT20();
  188. delay_ms(50);
  189. read_AHT20();
  190. //delay_ms(5);
  191. }
  192. void reset_AHT20(void)
  193. {
  194. I2C_Start();
  195. I2C_WriteByte(0x70);
  196. ack_status = Receive_ACK();
  197. if(ack_status) printf("1");
  198. else printf("1-n-");
  199. I2C_WriteByte(0xBA);
  200. ack_status = Receive_ACK();
  201. if(ack_status) printf("2");
  202. else printf("2-n-");
  203. I2C_Stop();
  204. /*
  205. AHT20_OutData[0] = 0;
  206. AHT20_OutData[1] = 0;
  207. AHT20_OutData[2] = 0;
  208. AHT20_OutData[3] = 0;
  209. */
  210. }
  211. void init_AHT20(void)
  212. {
  213. I2C_Start();
  214. I2C_WriteByte(0x70);
  215. ack_status = Receive_ACK();
  216. if(ack_status) printf("3");
  217. else printf("3-n-");
  218. I2C_WriteByte(0xE1);
  219. ack_status = Receive_ACK();
  220. if(ack_status) printf("4");
  221. else printf("4-n-");
  222. I2C_WriteByte(0x08);
  223. ack_status = Receive_ACK();
  224. if(ack_status) printf("5");
  225. else printf("5-n-");
  226. I2C_WriteByte(0x00);
  227. ack_status = Receive_ACK();
  228. if(ack_status) printf("6");
  229. else printf("6-n-");
  230. I2C_Stop();
  231. }
  232. void startMeasure_AHT20(void)
  233. {
  234. //------------
  235. I2C_Start();
  236. I2C_WriteByte(0x70);
  237. ack_status = Receive_ACK();
  238. if(ack_status) printf("7");
  239. else printf("7-n-");
  240. I2C_WriteByte(0xAC);
  241. ack_status = Receive_ACK();
  242. if(ack_status) printf("8");
  243. else printf("8-n-");
  244. I2C_WriteByte(0x33);
  245. ack_status = Receive_ACK();
  246. if(ack_status) printf("9");
  247. else printf("9-n-");
  248. I2C_WriteByte(0x00);
  249. ack_status = Receive_ACK();
  250. if(ack_status) printf("10");
  251. else printf("10-n-");
  252. I2C_Stop();
  253. }
  254. void read_AHT20(void)
  255. {
  256. uint8_t i;
  257. for(i=0; i<6; i++)
  258. {
  259. readByte[i]=0;
  260. }
  261. //-------------
  262. I2C_Start();
  263. I2C_WriteByte(0x71);
  264. ack_status = Receive_ACK();
  265. readByte[0]= I2C_ReadByte();
  266. Send_ACK();
  267. readByte[1]= I2C_ReadByte();
  268. Send_ACK();
  269. readByte[2]= I2C_ReadByte();
  270. Send_ACK();
  271. readByte[3]= I2C_ReadByte();
  272. Send_ACK();
  273. readByte[4]= I2C_ReadByte();
  274. Send_ACK();
  275. readByte[5]= I2C_ReadByte();
  276. SendNot_Ack();
  277. //Send_ACK();
  278. I2C_Stop();
  279. //--------------
  280. if( (readByte[0] & 0x68) == 0x08 )
  281. {
  282. H1 = readByte[1];
  283. H1 = (H1<<8) | readByte[2];
  284. H1 = (H1<<8) | readByte[3];
  285. H1 = H1>>4;
  286. H1 = (H1*1000)/1024/1024;
  287. T1 = readByte[3];
  288. T1 = T1 & 0x0000000F;
  289. T1 = (T1<<8) | readByte[4];
  290. T1 = (T1<<8) | readByte[5];
  291. T1 = (T1*2000)/1024/1024 - 500;
  292. AHT20_OutData[0] = (H1>>8) & 0x000000FF;
  293. AHT20_OutData[1] = H1 & 0x000000FF;
  294. AHT20_OutData[2] = (T1>>8) & 0x000000FF;
  295. AHT20_OutData[3] = T1 & 0x000000FF;
  296. }
  297. else
  298. {
  299. AHT20_OutData[0] = 0xFF;
  300. AHT20_OutData[1] = 0xFF;
  301. AHT20_OutData[2] = 0xFF;
  302. AHT20_OutData[3] = 0xFF;
  303. printf("lyy");
  304. }
  305. // printf("\r\n");
  306. // printf("温度:%d%d.%d",T1/100,(T1/10)%10,T1%10);
  307. // printf("湿度:%d%d.%d",H1/100,(H1/10)%10,H1%10);
  308. // printf("\r\n");
  309. OLED_ShowString(1, 1, "Temperature:");
  310. OLED_ShowNum(1, 13, T1/100, 1);
  311. OLED_ShowNum(1, 14, (T1/10)%10, 1);
  312. OLED_ShowChar(1, 15, '.');
  313. OLED_ShowNum(1, 16, T1%10, 1);
  314. OLED_ShowString(2, 1, "Humidity:");
  315. OLED_ShowNum(2, 10, H1/100, 1);
  316. OLED_ShowNum(2, 11, (H1/10)%10, 1);
  317. OLED_ShowChar(2, 12, '.');
  318. OLED_ShowNum(2, 13, H1%10, 1);
  319. }
  320. uint8_t Receive_ACK(void)
  321. {
  322. uint8_t result=0;
  323. uint8_t cnt=0;
  324. IIC_SCL = 0;
  325. SDA_IN();
  326. delay_us(4);
  327. IIC_SCL = 1;
  328. delay_us(4);
  329. while(READ_SDA && (cnt<100))
  330. {
  331. cnt++;
  332. }
  333. IIC_SCL = 0;
  334. delay_us(4);
  335. if(cnt<100)
  336. {
  337. result=1;
  338. }
  339. return result;
  340. }
  341. void Send_ACK(void)
  342. {
  343. SDA_OUT();
  344. IIC_SCL = 0;
  345. delay_us(4);
  346. IIC_SDA = 0;
  347. delay_us(4);
  348. IIC_SCL = 1;
  349. delay_us(4);
  350. IIC_SCL = 0;
  351. delay_us(4);
  352. SDA_IN();
  353. }
  354. void SendNot_Ack(void)
  355. {
  356. SDA_OUT();
  357. IIC_SCL = 0;
  358. delay_us(4);
  359. IIC_SDA = 1;
  360. delay_us(4);
  361. IIC_SCL = 1;
  362. delay_us(4);
  363. IIC_SCL = 0;
  364. delay_us(4);
  365. IIC_SDA = 0;
  366. delay_us(4);
  367. }
  368. void I2C_WriteByte(uint8_t input)
  369. {
  370. uint8_t i;
  371. SDA_OUT();
  372. for(i=0; i<8; i++)
  373. {
  374. IIC_SCL = 0;
  375. delay_ms(5);
  376. if(input & 0x80)
  377. {
  378. IIC_SDA = 1;
  379. //delaymm(10);
  380. }
  381. else
  382. {
  383. IIC_SDA = 0;
  384. //delaymm(10);
  385. }
  386. IIC_SCL = 1;
  387. delay_ms(5);
  388. input = (input<<1);
  389. }
  390. IIC_SCL = 0;
  391. delay_us(4);
  392. SDA_IN();
  393. delay_us(4);
  394. }
  395. uint8_t I2C_ReadByte(void)
  396. {
  397. uint8_t resultByte=0;
  398. uint8_t i=0, a=0;
  399. IIC_SCL = 0;
  400. SDA_IN();
  401. delay_ms(4);
  402. for(i=0; i<8; i++)
  403. {
  404. IIC_SCL = 1;
  405. delay_ms(3);
  406. a=0;
  407. if(READ_SDA)
  408. {
  409. a=1;
  410. }
  411. else
  412. {
  413. a=0;
  414. }
  415. //resultByte = resultByte | a;
  416. resultByte = (resultByte << 1) | a;
  417. IIC_SCL = 0;
  418. delay_ms(3);
  419. }
  420. SDA_IN();
  421. delay_ms(10);
  422. return resultByte;
  423. }
  424. void set_AHT20sendOutData(void)
  425. {
  426. /* --------------------------
  427. * 0xFA 0x06 0x0A temperature(2 Bytes) humility(2Bytes) short Address(2 Bytes)
  428. * And Check (1 byte)
  429. * -------------------------*/
  430. AHT20sendOutData[3] = AHT20_OutData[0];
  431. AHT20sendOutData[4] = AHT20_OutData[1];
  432. AHT20sendOutData[5] = AHT20_OutData[2];
  433. AHT20sendOutData[6] = AHT20_OutData[3];
  434. // AHT20sendOutData[7] = (drf1609.shortAddress >> 8) & 0x00FF;
  435. // AHT20sendOutData[8] = drf1609.shortAddress & 0x00FF;
  436. // AHT20sendOutData[9] = getXY(AHT20sendOutData,10);
  437. }
  438. void I2C_Start(void)
  439. {
  440. SDA_OUT();
  441. IIC_SCL = 1;
  442. delay_ms(4);
  443. IIC_SDA = 1;
  444. delay_ms(4);
  445. IIC_SDA = 0;
  446. delay_ms(4);
  447. IIC_SCL = 0;
  448. delay_ms(4);
  449. }
  450. void I2C_Stop(void)
  451. {
  452. SDA_OUT();
  453. IIC_SDA = 0;
  454. delay_ms(4);
  455. IIC_SCL = 1;
  456. delay_ms(4);
  457. IIC_SDA = 1;
  458. delay_ms(4);
  459. }

(5:OLED_Font.h文件编写:

  1. #ifndef __OLED_FONT_H
  2. #define __OLED_FONT_H
  3. /*OLED字模库,宽8像素,高16像素*/
  4. const uint8_t OLED_F8x16[][16]=
  5. {
  6. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
  8. 0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,
  9. 0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1
  10. 0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,
  11. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2
  12. 0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,
  13. 0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3
  14. 0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,
  15. 0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4
  16. 0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,
  17. 0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5
  18. 0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,
  19. 0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6
  20. 0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,
  21. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7
  22. 0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,
  23. 0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8
  24. 0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,
  25. 0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9
  26. 0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,
  27. 0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10
  28. 0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,
  29. 0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11
  30. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31. 0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12
  32. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  33. 0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13
  34. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  35. 0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14
  36. 0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,
  37. 0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15
  38. 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
  39. 0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16
  40. 0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,
  41. 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17
  42. 0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,
  43. 0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18
  44. 0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,
  45. 0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19
  46. 0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,
  47. 0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20
  48. 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,
  49. 0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21
  50. 0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,
  51. 0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22
  52. 0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,
  53. 0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23
  54. 0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,
  55. 0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24
  56. 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
  57. 0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25
  58. 0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,
  59. 0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
  60. 0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,
  61. 0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27
  62. 0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,
  63. 0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28
  64. 0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,
  65. 0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29
  66. 0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,
  67. 0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30
  68. 0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,
  69. 0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31
  70. 0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,
  71. 0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32
  72. 0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,
  73. 0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33
  74. 0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,
  75. 0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34
  76. 0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,
  77. 0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35
  78. 0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,
  79. 0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36
  80. 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
  81. 0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
  82. 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
  83. 0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38
  84. 0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,
  85. 0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39
  86. 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
  87. 0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
  88. 0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,
  89. 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41
  90. 0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,
  91. 0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42
  92. 0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,
  93. 0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43
  94. 0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,
  95. 0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
  96. 0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,
  97. 0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
  98. 0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,
  99. 0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46
  100. 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
  101. 0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47
  102. 0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,
  103. 0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
  104. 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
  105. 0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49
  106. 0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,
  107. 0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
  108. 0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,
  109. 0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51
  110. 0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,
  111. 0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
  112. 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
  113. 0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53
  114. 0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,
  115. 0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54
  116. 0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,
  117. 0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55
  118. 0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,
  119. 0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56
  120. 0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,
  121. 0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57
  122. 0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,
  123. 0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58
  124. 0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,
  125. 0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59
  126. 0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,
  127. 0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60
  128. 0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,
  129. 0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61
  130. 0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,
  131. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
  132. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  133. 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63
  134. 0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,
  135. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
  136. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
  137. 0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
  138. 0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,
  139. 0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66
  140. 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,
  141. 0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67
  142. 0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,
  143. 0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68
  144. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
  145. 0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69
  146. 0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,
  147. 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70
  148. 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
  149. 0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71
  150. 0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,
  151. 0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72
  152. 0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,
  153. 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
  154. 0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,
  155. 0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74
  156. 0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,
  157. 0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75
  158. 0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,
  159. 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76
  160. 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
  161. 0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77
  162. 0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,
  163. 0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
  164. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
  165. 0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79
  166. 0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,
  167. 0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80
  168. 0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,
  169. 0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81
  170. 0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
  171. 0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
  172. 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
  173. 0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83
  174. 0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,
  175. 0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84
  176. 0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,
  177. 0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85
  178. 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
  179. 0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86
  180. 0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,
  181. 0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87
  182. 0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
  183. 0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88
  184. 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
  185. 0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
  186. 0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
  187. 0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90
  188. 0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,
  189. 0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91
  190. 0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,
  191. 0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92
  192. 0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,
  193. 0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93
  194. 0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,
  195. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
  196. };
  197. #endif

(6:delay.h与delay.c文件编写:

delay.h:

  1. #ifndef __DELAY_H
  2. #define __DELAY_H
  3. #include "sys.h"
  4. //
  5. //STM32F103o?D?°?ày3ì
  6. //?aoˉêy°?±?ày3ì
  7. /********** mcudev.taobao.com 3??· ********/
  8. //ê1ó?SysTickμ???í¨??êy?£ê????ó3ù??DD1üàí
  9. //°üà¨delay_us,delay_ms
  10. //
  11. void delay_init(void);
  12. void delay_ms(u16 nms);
  13. void delay_us(u32 nus);
  14. #endif

delay.c:

  1. #include "delay.h"
  2. #include "sys.h"
  3. //STM32F103o?D?°?ày3ì
  4. //?aoˉêy°?±?ày3ì
  5. /********** mcudev.taobao.com 3??· ********/
  6. //
  7. //è?11ó?ucos,?ò°üà¨????μ?í·???t?′?é.
  8. #if SYSTEM_SUPPORT_UCOS
  9. #include "includes.h" //ucos ê1ó?
  10. #endif
  11. //
  12. //STM32?a·¢°?
  13. //ê1ó?SysTickμ???í¨??êy?£ê????ó3ù??DD1üàí
  14. //°üà¨delay_us,delay_ms
  15. //
  16. static u8 fac_us=0;//us?óê±±?3?êy
  17. static u16 fac_ms=0;//ms?óê±±?3?êy
  18. #ifdef OS_CRITICAL_METHOD //è?1?OS_CRITICAL_METHOD?¨ò?á?,?μ?÷ê1ó?ucosIIá?.
  19. //systick?D??·t??oˉêy,ê1ó?ucosê±ó?μ?
  20. void SysTick_Handler(void)
  21. {
  22. OSIntEnter(); //??è??D??
  23. OSTimeTick(); //μ÷ó?ucosμ?ê±?ó·t??3ìDò
  24. OSIntExit(); //′¥·¢è????D??èí?D??
  25. }
  26. #endif
  27. //3?ê??ˉ?ó3ùoˉêy
  28. //μ±ê1ó?ucosμ?ê±oò,′?oˉêy?á3?ê??ˉucosμ?ê±?ó?ú??
  29. //SYSTICKμ?ê±?ó1ì?¨?aHCLKê±?óμ?1/8
  30. //SYSCLK:?μí3ê±?ó
  31. void delay_init()
  32. {
  33. #ifdef OS_CRITICAL_METHOD //è?1?OS_CRITICAL_METHOD?¨ò?á?,?μ?÷ê1ó?ucosIIá?.
  34. u32 reload;
  35. #endif
  36. SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //????ía2?ê±?ó HCLK/8
  37. fac_us=SystemCoreClock/8000000; //?a?μí3ê±?óμ?1/8
  38. #ifdef OS_CRITICAL_METHOD //è?1?OS_CRITICAL_METHOD?¨ò?á?,?μ?÷ê1ó?ucosIIá?.
  39. reload=SystemCoreClock/8000000; //?????óμ???êy′?êy μ¥???aK
  40. reload*=1000000/OS_TICKS_PER_SEC;//?ù?YOS_TICKS_PER_SECéè?¨ò?3?ê±??
  41. //reload?a24????′??÷,×?′ó?μ:16777216,?ú72M??,??o?1.86s×óóò
  42. fac_ms=1000/OS_TICKS_PER_SEC;//′ú±íucos?éò??óê±μ?×?éùμ¥??
  43. SysTick->CTRL|=SysTick_CTRL_TICKINT_Msk; //?a??SYSTICK?D??
  44. SysTick->LOAD=reload; //??1/OS_TICKS_PER_SEC???D??ò?′?
  45. SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk; //?a??SYSTICK
  46. #else
  47. fac_ms=(u16)fac_us*1000;//·?ucos??,′ú±í????msDèòaμ?systickê±?óêy
  48. #endif
  49. }
  50. #ifdef OS_CRITICAL_METHOD //ê1ó?á?ucos
  51. //?óê±nus
  52. //nus?aòa?óê±μ?usêy.
  53. void delay_us(u32 nus)
  54. {
  55. u32 ticks;
  56. u32 told,tnow,tcnt=0;
  57. u32 reload=SysTick->LOAD; //LOADμ??μ
  58. ticks=nus*fac_us; //Dèòaμ??ú??êy
  59. tcnt=0;
  60. told=SysTick->VAL; //????è?ê±μ???êy?÷?μ
  61. while(1)
  62. {
  63. tnow=SysTick->VAL;
  64. if(tnow!=told)
  65. {
  66. if(tnow<told)tcnt+=told-tnow;//?aà?×¢òaò???SYSTICKê?ò???μY??μ???êy?÷?í?éò?á?.
  67. else tcnt+=reload-tnow+told;
  68. told=tnow;
  69. if(tcnt>=ticks)break;//ê±??3?1y/μèóúòa?ó3ùμ?ê±??,?òí?3?.
  70. }
  71. };
  72. }
  73. //?óê±nms
  74. //nms:òa?óê±μ?msêy
  75. void delay_ms(u16 nms)
  76. {
  77. if(OSRunning==TRUE)//è?1?osò??-?ú?üá?
  78. {
  79. if(nms>=fac_ms)//?óê±μ?ê±??′óóúucosμ?×?éùê±???ü?ú
  80. {
  81. OSTimeDly(nms/fac_ms);//ucos?óê±
  82. }
  83. nms%=fac_ms; //ucosò??-?T·¨ìá1??a?′D?μ??óê±á?,2éó???í¨·?ê??óê±
  84. }
  85. delay_us((u32)(nms*1000)); //??í¨·?ê??óê±,′?ê±ucos?T·¨???ˉμ÷?è.
  86. }
  87. #else//2?ó?ucosê±
  88. //?óê±nus
  89. //nus?aòa?óê±μ?usêy.
  90. void delay_us(u32 nus)
  91. {
  92. u32 temp;
  93. SysTick->LOAD=nus*fac_us; //ê±???ó??
  94. SysTick->VAL=0x00; //??????êy?÷
  95. SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //?aê?μ1êy
  96. do
  97. {
  98. temp=SysTick->CTRL;
  99. }
  100. while(temp&0x01&&!(temp&(1<<16)));//μè′yê±??μ?′?
  101. SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //1?±???êy?÷
  102. SysTick->VAL =0X00; //??????êy?÷
  103. }
  104. //?óê±nms
  105. //×¢òanmsμ?·??§
  106. //SysTick->LOAD?a24????′??÷,?ùò?,×?′ó?óê±?a:
  107. //nms<=0xffffff*8*1000/SYSCLK
  108. //SYSCLKμ¥???aHz,nmsμ¥???ams
  109. //??72Mì??t??,nms<=1864
  110. void delay_ms(u16 nms)
  111. {
  112. u32 temp;
  113. SysTick->LOAD=(u32)nms*fac_ms;//ê±???ó??(SysTick->LOAD?a24bit)
  114. SysTick->VAL =0x00; //??????êy?÷
  115. SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //?aê?μ1êy
  116. do
  117. {
  118. temp=SysTick->CTRL;
  119. }
  120. while(temp&0x01&&!(temp&(1<<16)));//μè′yê±??μ?′?
  121. SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //1?±???êy?÷
  122. SysTick->VAL =0X00; //??????êy?÷
  123. }
  124. #endif

(7:usart.h与usart.c文件编写:

usart.h:

  1. #ifndef __USART_H
  2. #define __USART_H
  3. #include "stdio.h"
  4. #include "sys.h"
  5. //STM32F103o?D?°?ày3ì
  6. //?aoˉêy°?±?ày3ì
  7. /********** mcudev.taobao.com 3??· ********/
  8. //
  9. //STM32?a·¢°?
  10. //′??ú13?ê??ˉ
  11. #define USART_REC_LEN 200 //?¨ò?×?′ó?óê?×??úêy 200
  12. #define EN_USART1_RX 1 //ê1?ü£¨1£?/???1£¨0£?′??ú1?óê?
  13. extern u8 USART_RX_BUF[USART_REC_LEN]; //?óê??o3?,×?′óUSART_REC_LEN??×??ú.??×??ú?a??DD·?
  14. extern u16 USART_RX_STA; //?óê?×′ì?±ê??
  15. //è?1???′??ú?D???óê?£???2?òa×¢êíò???oê?¨ò?
  16. void uart_init(u32 bound);
  17. #endif

usart.c:

  1. #include "sys.h"
  2. #include "usart.h"
  3. //STM32F103o?D?°?ày3ì
  4. //?aoˉêy°?±?ày3ì
  5. /********** mcudev.taobao.com 3??· ********/
  6. //
  7. //è?11ó?ucos,?ò°üà¨????μ?í·???t?′?é.
  8. #if SYSTEM_SUPPORT_UCOS
  9. #include "includes.h" //ucos ê1ó?
  10. #endif
  11. //
  12. //STM32?a·¢°?
  13. //′??ú13?ê??ˉ
  14. //
  15. //
  16. //?óè?ò???′ú??,?§3?printfoˉêy,??2?Dèòa????use MicroLIB
  17. #if 1
  18. #pragma import(__use_no_semihosting)
  19. //±ê×??aDèòaμ??§3?oˉêy
  20. struct __FILE
  21. {
  22. int handle;
  23. };
  24. FILE __stdout;
  25. //?¨ò?_sys_exit()ò?±ü?aê1ó?°??÷?ú?£ê?
  26. void _sys_exit(int x)
  27. {
  28. x = x;
  29. }
  30. //???¨ò?fputcoˉêy
  31. int fputc(int ch, FILE *f)
  32. {
  33. while((USART1->SR&0X40)==0);//?-?··¢?í,?±μ?·¢?ííê±?
  34. USART1->DR = (u8) ch;
  35. return ch;
  36. }
  37. #endif
  38. /*ê1ó?microLibμ?·?·¨*/
  39. /*
  40. int fputc(int ch, FILE *f)
  41. {
  42. USART_SendData(USART1, (uint8_t) ch);
  43. while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}
  44. return ch;
  45. }
  46. int GetKey (void) {
  47. while (!(USART1->SR & USART_FLAG_RXNE));
  48. return ((int)(USART1->DR & 0x1FF));
  49. }
  50. */
  51. #if EN_USART1_RX //è?11?üá??óê?
  52. //′??ú1?D??·t??3ìDò
  53. //×¢òa,?áè?USARTx->SR?ü±ü?a?a??????μ?′í?ó
  54. u8 USART_RX_BUF[USART_REC_LEN]; //?óê??o3?,×?′óUSART_REC_LEN??×??ú.
  55. //?óê?×′ì?
  56. //bit15£? ?óê?íê3é±ê??
  57. //bit14£? ?óê?μ?0x0d
  58. //bit13~0£? ?óê?μ?μ?óDD§×??úêy??
  59. u16 USART_RX_STA=0; //?óê?×′ì?±ê??
  60. void uart_init(u32 bound){
  61. //GPIO???úéè??
  62. GPIO_InitTypeDef GPIO_InitStructure;
  63. USART_InitTypeDef USART_InitStructure;
  64. NVIC_InitTypeDef NVIC_InitStructure;
  65. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //ê1?üUSART1£?GPIOAê±?ó
  66. //USART1_TX PA.9
  67. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
  68. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  69. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //?′ó?í?íìê?3?
  70. GPIO_Init(GPIOA, &GPIO_InitStructure);
  71. //USART1_RX PA.10
  72. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  73. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//????ê?è?
  74. GPIO_Init(GPIOA, &GPIO_InitStructure);
  75. //Usart1 NVIC ????
  76. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  77. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//?à??ó??è??3
  78. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×óó??è??3
  79. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1
  80. NVIC_Init(&NVIC_InitStructure); //?ù?Y???¨μ?2?êy3?ê??ˉVIC??′??÷
  81. //USART 3?ê??ˉéè??
  82. USART_InitStructure.USART_BaudRate = bound;//ò?°?éè???a9600;
  83. USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×?3¤?a8??êy?Y??ê?
  84. USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò???í£?1??
  85. USART_InitStructure.USART_Parity = USART_Parity_No;//?T????D£?é??
  86. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//?Tó2?têy?Yá÷????
  87. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //ê?·¢?£ê?
  88. USART_Init(USART1, &USART_InitStructure); //3?ê??ˉ′??ú
  89. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//?a???D??
  90. USART_Cmd(USART1, ENABLE); //ê1?ü′??ú
  91. }
  92. void USART1_IRQHandler(void) //′??ú1?D??·t??3ìDò
  93. {
  94. u8 Res;
  95. #ifdef OS_TICKS_PER_SEC //è?1?ê±?ó?ú??êy?¨ò?á?,?μ?÷òaê1ó?ucosIIá?.
  96. OSIntEnter();
  97. #endif
  98. if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //?óê??D??(?óê?μ?μ?êy?Y±?D?ê?0x0d 0x0a?á?2)
  99. {
  100. Res =USART_ReceiveData(USART1);//(USART1->DR); //?áè??óê?μ?μ?êy?Y
  101. if((USART_RX_STA&0x8000)==0)//?óê??′íê3é
  102. {
  103. if(USART_RX_STA&0x4000)//?óê?μ?á?0x0d
  104. {
  105. if(Res!=0x0a)USART_RX_STA=0;//?óê?′í?ó,??D??aê?
  106. else USART_RX_STA|=0x8000; //?óê?íê3éá?
  107. }
  108. else //?1??ê?μ?0X0D
  109. {
  110. if(Res==0x0d)USART_RX_STA|=0x4000;
  111. else
  112. {
  113. USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
  114. USART_RX_STA++;
  115. if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//?óê?êy?Y′í?ó,??D??aê??óê?
  116. }
  117. }
  118. }
  119. }
  120. #ifdef OS_TICKS_PER_SEC //è?1?ê±?ó?ú??êy?¨ò?á?,?μ?÷òaê1ó?ucosIIá?.
  121. OSIntExit();
  122. #endif
  123. }
  124. #endif

(8:bsp_i2c.h与bsp_i2c.c文件编写:

bsp_i2c.h:

  1. #ifndef __BSP_I2C_H
  2. #define __BSP_I2C_H
  3. #include "sys.h"
  4. #include "delay.h"
  5. #include "usart.h"
  6. //ê1ó?IIC1 1ò??M24C02,OLED,LM75AD,HT1382 PB6,PB7
  7. #define SDA_IN() {GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=(u32)8<<28;}
  8. #define SDA_OUT() {GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=(u32)3<<28;}
  9. //IO2ù×÷oˉêy
  10. #define IIC_SCL PBout(6) //SCL
  11. #define IIC_SDA PBout(7) //SDA
  12. #define READ_SDA PBin(7) //ê?è?SDA
  13. //IIC?ùóD2ù×÷oˉêy
  14. void IIC_Init(void); //3?ê??ˉIICμ?IO?ú
  15. void IIC_Start(void); //·¢?íIIC?aê?D?o?
  16. void IIC_Stop(void); //·¢?íIICí£?1D?o?
  17. void IIC_Send_Byte(u8 txd); //IIC·¢?íò???×??ú
  18. u8 IIC_Read_Byte(unsigned char ack);//IIC?áè?ò???×??ú
  19. u8 IIC_Wait_Ack(void); //IICμè′yACKD?o?
  20. void IIC_Ack(void); //IIC·¢?íACKD?o?
  21. void IIC_NAck(void); //IIC2?·¢?íACKD?o?
  22. void IIC_WriteByte(uint16_t addr,uint8_t data,uint8_t device_addr);
  23. uint16_t IIC_ReadByte(uint16_t addr,uint8_t device_addr,uint8_t ByteNumToRead);//??′??÷μ??·£??÷?tμ??·£?òa?áμ?×??úêy
  24. void read_AHT20_once(void);
  25. void reset_AHT20(void);
  26. void init_AHT20(void);
  27. void startMeasure_AHT20(void);
  28. void read_AHT20(void);
  29. uint8_t Receive_ACK(void);
  30. void Send_ACK(void);
  31. void SendNot_Ack(void);
  32. void I2C_WriteByte(uint8_t input);
  33. uint8_t I2C_ReadByte(void);
  34. void set_AHT20sendOutData(void);
  35. void I2C_Start(void);
  36. void I2C_Stop(void);
  37. #endif

bsp_i2c.c:

  1. #include "bsp_i2c.h"
  2. #include "delay.h"
  3. #include "OLED.h"
  4. uint8_t ack_status=0;
  5. uint8_t readByte[6];
  6. uint8_t AHT20_status=0;
  7. uint32_t H1=0; //Humility
  8. uint32_t T1=0; //Temperature
  9. uint8_t AHT20_OutData[4];
  10. uint8_t AHT20sendOutData[10] = {0xFA, 0x06, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF};
  11. void IIC_Init(void)
  12. {
  13. GPIO_InitTypeDef GPIO_InitStructure;
  14. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );
  15. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
  16. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //í?íìê?3?
  17. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  18. GPIO_Init(GPIOB, &GPIO_InitStructure);
  19. IIC_SCL=1;
  20. IIC_SDA=1;
  21. }
  22. //2úéúIIC?eê?D?o?
  23. void IIC_Start(void)
  24. {
  25. SDA_OUT(); //sda??ê?3?
  26. IIC_SDA=1;
  27. IIC_SCL=1;
  28. delay_us(4);
  29. IIC_SDA=0;//START:when CLK is high,DATA change form high to low
  30. delay_us(4);
  31. IIC_SCL=0;//?ˉ×?I2C×ü??£?×?±?·¢?í?ò?óê?êy?Y
  32. }
  33. //2úéúIICí£?1D?o?
  34. void IIC_Stop(void)
  35. {
  36. SDA_OUT();//sda??ê?3?
  37. IIC_SCL=0;
  38. IIC_SDA=0;//STOP:when CLK is high DATA change form low to high
  39. delay_us(4);
  40. IIC_SCL=1;
  41. IIC_SDA=1;//·¢?íI2C×ü???áê?D?o?
  42. delay_us(4);
  43. }
  44. //μè′yó|′eD?o?μ?à′
  45. //·μ???μ£o1£??óê?ó|′e꧰ü
  46. // 0£??óê?ó|′e3é1|
  47. u8 IIC_Wait_Ack(void)
  48. {
  49. u8 ucErrTime=0;
  50. SDA_IN(); //SDAéè???aê?è?
  51. IIC_SDA=1;delay_us(1);
  52. IIC_SCL=1;delay_us(1);
  53. while(READ_SDA)
  54. {
  55. ucErrTime++;
  56. if(ucErrTime>250)
  57. {
  58. IIC_Stop();
  59. return 1;
  60. }
  61. }
  62. IIC_SCL=0;//ê±?óê?3?0
  63. return 0;
  64. }
  65. //2úéúACKó|′e
  66. void IIC_Ack(void)
  67. {
  68. IIC_SCL=0;
  69. SDA_OUT();
  70. IIC_SDA=0;
  71. delay_us(2);
  72. IIC_SCL=1;
  73. delay_us(2);
  74. IIC_SCL=0;
  75. }
  76. //2?2úéúACKó|′e
  77. void IIC_NAck(void)
  78. {
  79. IIC_SCL=0;
  80. SDA_OUT();
  81. IIC_SDA=1;
  82. delay_us(2);
  83. IIC_SCL=1;
  84. delay_us(2);
  85. IIC_SCL=0;
  86. }
  87. //IIC·¢?íò???×??ú
  88. //·μ??′ó?úóD?Tó|′e
  89. //1£?óDó|′e
  90. //0£??Tó|′e
  91. void IIC_Send_Byte(u8 txd)
  92. {
  93. u8 t;
  94. SDA_OUT();
  95. IIC_SCL=0;//à-μíê±?ó?aê?êy?Y′?ê?
  96. for(t=0;t<8;t++)
  97. {
  98. IIC_SDA=(txd&0x80)>>7;
  99. txd<<=1;
  100. delay_us(2); //??TEA5767?aèy???óê±??ê?±?D?μ?
  101. IIC_SCL=1;
  102. delay_us(2);
  103. IIC_SCL=0;
  104. delay_us(2);
  105. }
  106. }
  107. //1??×??ú£?ack=1ê±£?·¢?íACK£?ack=0£?·¢?ínACK
  108. u8 IIC_Read_Byte(unsigned char ack)
  109. {
  110. unsigned char i,receive=0;
  111. SDA_IN();//SDAéè???aê?è?
  112. for(i=0;i<8;i++ )
  113. {
  114. IIC_SCL=0;
  115. delay_us(2);
  116. IIC_SCL=1;
  117. receive<<=1;
  118. if(READ_SDA)receive++;
  119. delay_us(1);
  120. }
  121. if (!ack)
  122. IIC_NAck();//·¢?ínACK
  123. else
  124. IIC_Ack(); //·¢?íACK
  125. return receive;
  126. }
  127. void IIC_WriteByte(uint16_t addr,uint8_t data,uint8_t device_addr)
  128. {
  129. IIC_Start();
  130. if(device_addr==0xA0) //eepromμ??·′óóú1×??ú
  131. IIC_Send_Byte(0xA0 + ((addr/256)<<1));//·¢?í??μ??·
  132. else
  133. IIC_Send_Byte(device_addr); //·¢?÷?tμ??·
  134. IIC_Wait_Ack();
  135. IIC_Send_Byte(addr&0xFF); //·¢?íμíμ??·
  136. IIC_Wait_Ack();
  137. IIC_Send_Byte(data); //·¢?í×??ú
  138. IIC_Wait_Ack();
  139. IIC_Stop();//2úéúò???í£?1ì??t
  140. if(device_addr==0xA0) //
  141. delay_ms(10);
  142. else
  143. delay_us(2);
  144. }
  145. uint16_t IIC_ReadByte(uint16_t addr,uint8_t device_addr,uint8_t ByteNumToRead) //?á??′??÷?ò?áêy?Y
  146. {
  147. uint16_t data;
  148. IIC_Start();
  149. if(device_addr==0xA0)
  150. IIC_Send_Byte(0xA0 + ((addr/256)<<1));
  151. else
  152. IIC_Send_Byte(device_addr);
  153. IIC_Wait_Ack();
  154. IIC_Send_Byte(addr&0xFF); //·¢?íμíμ??·
  155. IIC_Wait_Ack();
  156. IIC_Start();
  157. IIC_Send_Byte(device_addr+1); //·¢?÷?tμ??·
  158. IIC_Wait_Ack();
  159. if(ByteNumToRead == 1)//LM75???èêy?Y?a11bit
  160. {
  161. data=IIC_Read_Byte(0);
  162. }
  163. else
  164. {
  165. data=IIC_Read_Byte(1);
  166. data=(data<<8)+IIC_Read_Byte(0);
  167. }
  168. IIC_Stop();//2úéúò???í£?1ì??t
  169. return data;
  170. }
  171. /**********
  172. *é???2?·??aIO?ú?£?éI2C????
  173. *
  174. *′ó?aò????aê??aAHT20μ?????I2C
  175. *oˉêy??óDIICoíI2Cμ???±e£???×¢òa£?£?£?£?£?
  176. *
  177. *2020/2/23×?oóDT??è??ú
  178. *
  179. ***********/
  180. void read_AHT20_once(void)
  181. {
  182. //delay_ms(10);
  183. reset_AHT20();
  184. delay_ms(5);
  185. init_AHT20();
  186. delay_ms(5);
  187. startMeasure_AHT20();
  188. delay_ms(50);
  189. read_AHT20();
  190. //delay_ms(5);
  191. }
  192. void reset_AHT20(void)
  193. {
  194. I2C_Start();
  195. I2C_WriteByte(0x70);
  196. ack_status = Receive_ACK();
  197. if(ack_status) printf("1");
  198. else printf("1-n-");
  199. I2C_WriteByte(0xBA);
  200. ack_status = Receive_ACK();
  201. if(ack_status) printf("2");
  202. else printf("2-n-");
  203. I2C_Stop();
  204. /*
  205. AHT20_OutData[0] = 0;
  206. AHT20_OutData[1] = 0;
  207. AHT20_OutData[2] = 0;
  208. AHT20_OutData[3] = 0;
  209. */
  210. }
  211. void init_AHT20(void)
  212. {
  213. I2C_Start();
  214. I2C_WriteByte(0x70);
  215. ack_status = Receive_ACK();
  216. if(ack_status) printf("3");
  217. else printf("3-n-");
  218. I2C_WriteByte(0xE1);
  219. ack_status = Receive_ACK();
  220. if(ack_status) printf("4");
  221. else printf("4-n-");
  222. I2C_WriteByte(0x08);
  223. ack_status = Receive_ACK();
  224. if(ack_status) printf("5");
  225. else printf("5-n-");
  226. I2C_WriteByte(0x00);
  227. ack_status = Receive_ACK();
  228. if(ack_status) printf("6");
  229. else printf("6-n-");
  230. I2C_Stop();
  231. }
  232. void startMeasure_AHT20(void)
  233. {
  234. //------------
  235. I2C_Start();
  236. I2C_WriteByte(0x70);
  237. ack_status = Receive_ACK();
  238. if(ack_status) printf("7");
  239. else printf("7-n-");
  240. I2C_WriteByte(0xAC);
  241. ack_status = Receive_ACK();
  242. if(ack_status) printf("8");
  243. else printf("8-n-");
  244. I2C_WriteByte(0x33);
  245. ack_status = Receive_ACK();
  246. if(ack_status) printf("9");
  247. else printf("9-n-");
  248. I2C_WriteByte(0x00);
  249. ack_status = Receive_ACK();
  250. if(ack_status) printf("10");
  251. else printf("10-n-");
  252. I2C_Stop();
  253. }
  254. void read_AHT20(void)
  255. {
  256. uint8_t i;
  257. for(i=0; i<6; i++)
  258. {
  259. readByte[i]=0;
  260. }
  261. //-------------
  262. I2C_Start();
  263. I2C_WriteByte(0x71);
  264. ack_status = Receive_ACK();
  265. readByte[0]= I2C_ReadByte();
  266. Send_ACK();
  267. readByte[1]= I2C_ReadByte();
  268. Send_ACK();
  269. readByte[2]= I2C_ReadByte();
  270. Send_ACK();
  271. readByte[3]= I2C_ReadByte();
  272. Send_ACK();
  273. readByte[4]= I2C_ReadByte();
  274. Send_ACK();
  275. readByte[5]= I2C_ReadByte();
  276. SendNot_Ack();
  277. //Send_ACK();
  278. I2C_Stop();
  279. //--------------
  280. if( (readByte[0] & 0x68) == 0x08 )
  281. {
  282. H1 = readByte[1];
  283. H1 = (H1<<8) | readByte[2];
  284. H1 = (H1<<8) | readByte[3];
  285. H1 = H1>>4;
  286. H1 = (H1*1000)/1024/1024;
  287. T1 = readByte[3];
  288. T1 = T1 & 0x0000000F;
  289. T1 = (T1<<8) | readByte[4];
  290. T1 = (T1<<8) | readByte[5];
  291. T1 = (T1*2000)/1024/1024 - 500;
  292. AHT20_OutData[0] = (H1>>8) & 0x000000FF;
  293. AHT20_OutData[1] = H1 & 0x000000FF;
  294. AHT20_OutData[2] = (T1>>8) & 0x000000FF;
  295. AHT20_OutData[3] = T1 & 0x000000FF;
  296. }
  297. else
  298. {
  299. AHT20_OutData[0] = 0xFF;
  300. AHT20_OutData[1] = 0xFF;
  301. AHT20_OutData[2] = 0xFF;
  302. AHT20_OutData[3] = 0xFF;
  303. printf("lyy");
  304. }
  305. // printf("\r\n");
  306. // printf("温度:%d%d.%d",T1/100,(T1/10)%10,T1%10);
  307. // printf("湿度:%d%d.%d",H1/100,(H1/10)%10,H1%10);
  308. // printf("\r\n");
  309. OLED_ShowString(1, 1, "Temperature:");
  310. OLED_ShowNum(1, 13, T1/100, 1);
  311. OLED_ShowNum(1, 14, (T1/10)%10, 1);
  312. OLED_ShowChar(1, 15, '.');
  313. OLED_ShowNum(1, 16, T1%10, 1);
  314. OLED_ShowString(2, 1, "Humidity:");
  315. OLED_ShowNum(2, 10, H1/100, 1);
  316. OLED_ShowNum(2, 11, (H1/10)%10, 1);
  317. OLED_ShowChar(2, 12, '.');
  318. OLED_ShowNum(2, 13, H1%10, 1);
  319. }
  320. uint8_t Receive_ACK(void)
  321. {
  322. uint8_t result=0;
  323. uint8_t cnt=0;
  324. IIC_SCL = 0;
  325. SDA_IN();
  326. delay_us(4);
  327. IIC_SCL = 1;
  328. delay_us(4);
  329. while(READ_SDA && (cnt<100))
  330. {
  331. cnt++;
  332. }
  333. IIC_SCL = 0;
  334. delay_us(4);
  335. if(cnt<100)
  336. {
  337. result=1;
  338. }
  339. return result;
  340. }
  341. void Send_ACK(void)
  342. {
  343. SDA_OUT();
  344. IIC_SCL = 0;
  345. delay_us(4);
  346. IIC_SDA = 0;
  347. delay_us(4);
  348. IIC_SCL = 1;
  349. delay_us(4);
  350. IIC_SCL = 0;
  351. delay_us(4);
  352. SDA_IN();
  353. }
  354. void SendNot_Ack(void)
  355. {
  356. SDA_OUT();
  357. IIC_SCL = 0;
  358. delay_us(4);
  359. IIC_SDA = 1;
  360. delay_us(4);
  361. IIC_SCL = 1;
  362. delay_us(4);
  363. IIC_SCL = 0;
  364. delay_us(4);
  365. IIC_SDA = 0;
  366. delay_us(4);
  367. }
  368. void I2C_WriteByte(uint8_t input)
  369. {
  370. uint8_t i;
  371. SDA_OUT();
  372. for(i=0; i<8; i++)
  373. {
  374. IIC_SCL = 0;
  375. delay_ms(5);
  376. if(input & 0x80)
  377. {
  378. IIC_SDA = 1;
  379. //delaymm(10);
  380. }
  381. else
  382. {
  383. IIC_SDA = 0;
  384. //delaymm(10);
  385. }
  386. IIC_SCL = 1;
  387. delay_ms(5);
  388. input = (input<<1);
  389. }
  390. IIC_SCL = 0;
  391. delay_us(4);
  392. SDA_IN();
  393. delay_us(4);
  394. }
  395. uint8_t I2C_ReadByte(void)
  396. {
  397. uint8_t resultByte=0;
  398. uint8_t i=0, a=0;
  399. IIC_SCL = 0;
  400. SDA_IN();
  401. delay_ms(4);
  402. for(i=0; i<8; i++)
  403. {
  404. IIC_SCL = 1;
  405. delay_ms(3);
  406. a=0;
  407. if(READ_SDA)
  408. {
  409. a=1;
  410. }
  411. else
  412. {
  413. a=0;
  414. }
  415. //resultByte = resultByte | a;
  416. resultByte = (resultByte << 1) | a;
  417. IIC_SCL = 0;
  418. delay_ms(3);
  419. }
  420. SDA_IN();
  421. delay_ms(10);
  422. return resultByte;
  423. }
  424. void set_AHT20sendOutData(void)
  425. {
  426. /* --------------------------
  427. * 0xFA 0x06 0x0A temperature(2 Bytes) humility(2Bytes) short Address(2 Bytes)
  428. * And Check (1 byte)
  429. * -------------------------*/
  430. AHT20sendOutData[3] = AHT20_OutData[0];
  431. AHT20sendOutData[4] = AHT20_OutData[1];
  432. AHT20sendOutData[5] = AHT20_OutData[2];
  433. AHT20sendOutData[6] = AHT20_OutData[3];
  434. // AHT20sendOutData[7] = (drf1609.shortAddress >> 8) & 0x00FF;
  435. // AHT20sendOutData[8] = drf1609.shortAddress & 0x00FF;
  436. // AHT20sendOutData[9] = getXY(AHT20sendOutData,10);
  437. }
  438. void I2C_Start(void)
  439. {
  440. SDA_OUT();
  441. IIC_SCL = 1;
  442. delay_ms(4);
  443. IIC_SDA = 1;
  444. delay_ms(4);
  445. IIC_SDA = 0;
  446. delay_ms(4);
  447. IIC_SCL = 0;
  448. delay_ms(4);
  449. }
  450. void I2C_Stop(void)
  451. {
  452. SDA_OUT();
  453. IIC_SDA = 0;
  454. delay_ms(4);
  455. IIC_SCL = 1;
  456. delay_ms(4);
  457. IIC_SDA = 1;
  458. delay_ms(4);
  459. }

(9:编写主文件main.c文件代码:

  1. #include "delay.h"
  2. #include "usart.h"
  3. #include "bsp_i2c.h"
  4. #include "OLED.h"
  5. #include "MyRTC.h"
  6. #include "Timer.h"
  7. int main(void)
  8. {
  9. delay_init();
  10. uart_init(9600);
  11. OLED_Init();
  12. MyRTC_Init();
  13. IIC_Init();
  14. Timer_Init();
  15. MyRTC_SetTime();
  16. while(1)
  17. {
  18. //delay_ms(1000);
  19. }
  20. }
  21. void TIM2_IRQHandler(void)
  22. {
  23. if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
  24. {
  25. read_AHT20_once();
  26. MyRTC_ReadTime();
  27. TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  28. }
  29. }

到此,工程文件建立完毕!库函数工程搭建与完善成功!!!工程搭建与完善完成情况截图如下所示:

​​​​​​​

 八、第二小问的电路连接

 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屏幕的互联,我更加自信地处理了嵌入式系统中的硬件交互问题。这次项目培养了我的解决问题的能力,同时也加深了对实时时钟和传感器应用的理解,为未来的嵌入式开发奠定了坚实的基础。   

十一、参考资料

 1、【精选】STM32日历读取,设置和输出_st32日期流程图_一只特立独行的猪 ️的博客-CSDN博客

2、STM32日历读取,设置和输出_stm32 日历_机智的橙子的博客-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/912877
推荐阅读
相关标签
  

闽ICP备14008679号