目录
一、任务要求
1) 读取STM32F103C8T6 内部的时钟(年月日时分秒),日历(星期x),1秒周期,通过串口输出到PC上位机,;
2) 读取AHT20的温度和湿度,通过OLED,把年月份时分秒、日历和实时温度、湿度显示出来,2秒周期。
二、RTC时钟简介
1. 概念
STM32的 RTC 外设(Real Time Clock),实质是一个掉电后还继续运行的定时器。从 定时器的角度来说,相对于通用定时器 TIM 外设,它十分简单,只有很纯粹的计时和触发 中断的功能;但从掉电还继续运行的角度来说,它却是 STM32 中唯一一个具有如此强大功能的外设。所以 RTC 外设的复杂之处并不在于它的定时功能,而在于它掉电还继续运行的特性。 RTC是一个独立的定时器。STM32 的 RTC 模块拥有一组连续计数的计数器,在相应软件配置下,可提供时钟日历的功能。修改计数器的值可以重新设置系统当 前的时间和日期。 RTC 模块和时钟配置系统(RCC_BDCR 寄存器)是在后备区域,即在系统复位或从待机模式 唤醒后 RTC 的设置和时间维持不变。但是在系统复位后,会自动禁止访问后备寄存器和 RTC, 以防止对后备区域(BKP)的意外写操作。所以在要设置时间之前, 先要取消备份区(BKP)写保护。
2. 原理
RTC的简化框图:
3. 相关寄存器介绍
1.3.1 RTC_CRH 和 RTC_CRL
1.3.2 RTC_PRLH 和 RTC_PRLL
1.3.3 RTC_CNT
该寄存器由 2 个 16 位的寄存器组成 RTC_CNTH 和 RTC_CNTL,总共 32 位,用来记录秒钟值(TR_CLK=1Hz 的 情况下)。一般我们设置时间,就是设置 RTC_CNTH/RTC_CNTL 寄存器的值,假定我们以 1970 年为起始时间,那么当 RTC_CNTH=RTC_CNTL=0 的时候,就代表 1970 年 1 月 1 日 0 时 0 分, 这样,我们就可以很方便的根据 RTC_CNT 的值计算当前时间了。反过来,如果要设置时间, 则只需要将当前时间的年份减去 1970,然后剩下的时间换算成秒钟,写入 RTC_CNT 即可完成时间设置。
1.3.4 RTC_ALR
三、整体思路
对任务一,我们需要完成对stm32内部时钟的读取,并且以1s周期发送给上位机,因此,我们首先需要配置RTC的功能,即激活时钟源、日历、初始化日期和时间等,然后需要发送给上位机,所以还需要配置串口通信的功能,由于此次传输的内容不多,且周期较长,我们就使用中断的方式进行传输,配置usart1对应功能就行了。
对任务二,我们需要读取RTC时钟,在任务一中我们已经完成了,还需要采集温湿度,在前面的实验中,温湿度采集已经完成了,即配置IIC,通过IIC读取AHT20的温湿度信息,最后需要将二者的信息读取,并且打印在OLED屏上,OLED屏幕的驱动方式有SPI、IIC协议,可以根据自己购买的OLED屏幕自身固有的协议选择哪一种驱动方式,在此次实验中,我使用的是SPI协议。简而言之,任务二需要配置的内容有:IIC读取温湿度、SPI启动OLED、将RTC日历和温湿度信息打印在OLED屏幕上。
四、STM32CubeMX配置
1. 选择stm32f103c8t6芯片
2. 配置RCC
3.配置SYS
4. 配置IIC
5. 配置RTC
6. 配置UART1
6. 配置SPI
7. 配置时钟
8. 工程配置
五、编写程序
1. RTC程序
RTC.c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file rtc.c
* @brief This file provides code for the configuration
* of the RTC instances.
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "rtc.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
RTC_HandleTypeDef hrtc;
/* RTC init function */
void MX_RTC_Init(void)
{
/* USER CODE BEGIN RTC_Init 0 */
/* USER CODE END RTC_Init 0 */
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef DateToUpdate = {0};
/* USER CODE BEGIN RTC_Init 1 */
/* USER CODE END RTC_Init 1 */
/** Initialize RTC Only
*/
hrtc.Instance = RTC;
hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
hrtc.Init.OutPut = RTC_OUTPUTSOURCE_NONE;
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN Check_RTC_BKUP */
/* USER CODE END Check_RTC_BKUP */
/** Initialize RTC and set the Time and Date
*/
sTime.Hours = 0x10;
sTime.Minutes = 0x59;
sTime.Seconds = 0x10;
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
DateToUpdate.WeekDay = RTC_WEEKDAY_THURSDAY;
DateToUpdate.Month = RTC_MONTH_NOVEMBER;
DateToUpdate.Date = 0x23;
DateToUpdate.Year = 0x23;
if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN RTC_Init 2 */
/* USER CODE END RTC_Init 2 */
}
void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle)
{
if(rtcHandle->Instance==RTC)
{
/* USER CODE BEGIN RTC_MspInit 0 */
/* USER CODE END RTC_MspInit 0 */
HAL_PWR_EnableBkUpAccess();
/* Enable BKP CLK enable for backup registers */
__HAL_RCC_BKP_CLK_ENABLE();
/* RTC clock enable */
__HAL_RCC_RTC_ENABLE();
/* RTC interrupt Init */
HAL_NVIC_SetPriority(RTC_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(RTC_IRQn);
/* USER CODE BEGIN RTC_MspInit 1 */
/* USER CODE END RTC_MspInit 1 */
}
}
void HAL_RTC_MspDeInit(RTC_HandleTypeDef* rtcHandle)
{
if(rtcHandle->Instance==RTC)
{
/* USER CODE BEGIN RTC_MspDeInit 0 */
/* USER CODE END RTC_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_RTC_DISABLE();
/* RTC interrupt Deinit */
HAL_NVIC_DisableIRQ(RTC_IRQn);
/* USER CODE BEGIN RTC_MspDeInit 1 */
/* USER CODE END RTC_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
RTC.h
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file rtc.h
* @brief This file contains all the function prototypes for
* the rtc.c file
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __RTC_H__
#define __RTC_H__
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
extern RTC_HandleTypeDef hrtc;
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
void MX_RTC_Init(void);
/* USER CODE BEGIN Prototypes */
/* USER CODE END Prototypes */
#ifdef __cplusplus
}
#endif
#endif /* __RTC_H__ */
2. OLED程序
oled.c
/***************************************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, QD electronic SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
****************************************************************************************************/
#include "oled.h"
#include "stdlib.h"
#include "string.h"
#include "stm32f1xx_hal.h"
#include "spi.h"
//OLEDÏÔ´æ×ܹ²·ÖΪ8Ò³
//ÿҳ8ÐУ¬Ò»ÐÐ128¸öÏñËصã
//OLEDµÄÏÔ´æ
//´æ·Å¸ñʽÈçÏÂ.
//[0]0 1 2 3 ... 127 (0~7)ÐÐ
//[1]0 1 2 3 ... 127 (8~15)ÐÐ
//[2]0 1 2 3 ... 127 (16~23)ÐÐ
//[3]0 1 2 3 ... 127 (24~31)ÐÐ
//[4]0 1 2 3 ... 127 (32~39)ÐÐ
//[5]0 1 2 3 ... 127 (40~47)ÐÐ
//[6]0 1 2 3 ... 127 (48~55)ÐÐ
//[7]0 1 2 3 ... 127 (56~63)ÐÐ
//Êý×éÿ¸öbit´æ´¢OLEDÿ¸öÏñËصãµÄÑÕÉ«Öµ(1-ÁÁ(°×É«),0-Ãð(ºÚÉ«))
//ÿ¸öÊý×éÔªËرíʾ1ÁÐ8¸öÏñËص㣬һ¹²128ÁÐ
static unsigned char OLED_buffer[1024] =
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
/*******************************************************************
* @name :void OLED_WR_Byte(unsigned dat,unsigned cmd)
* @date :2018-08-27
* @function :Write a byte of content to the OLED screen
* @parameters :dat:Content to be written
cmd:0-write command
1-write data
* @retvalue :None
********************************************************************/
uint8_t SPI1_ReadWriteByte(uint8_t TxData)
{
uint8_t rxdata = 1;
#if onlysend //Èç¹ûÖ»ÅäÖÃÁË·¢ËÍ Ê¹ÓÃHAL_SPI_Transmit
HAL_SPI_Transmit(&hspi1,&TxData,1,100);
//while(HAL_SPI_GetState(&hspi1) == HAL_SPI_STATE_READY);
#else//Èç¹ûÅäÖÃÁË·¢ËͺͽÓÊÕ Ê¹ÓÃHAL_SPI_TransmitReceive
if(HAL_OK == HAL_SPI_TransmitReceive(&hspi1,&TxData,&rxdata,1,100))
{
return rxdata;
};
#endif
return rxdata;
}
void OLED_WR_Byte(unsigned dat,unsigned cmd)
{
if(cmd)
{
OLED_DC_Set();
}
else
{
OLED_DC_Clr();
}
OLED_CS_Clr();
SPI1_ReadWriteByte(dat);
OLED_CS_Set();
}
/*******************************************************************
* @name :void OLED_Set_Pos(unsigned char x, unsigned char y)
* @date :2018-08-27
* @function :Set coordinates in the OLED screen
* @parameters :x:x coordinates
y:y coordinates
* @retvalue :None
********************************************************************/
void OLED_Set_Pos(unsigned char x, unsigned char y)
{
OLED_WR_Byte(YLevel+y/PAGE_SIZE,OLED_CMD);
OLED_WR_Byte((((x+2)&0xf0)>>4)|0x10,OLED_CMD);
OLED_WR_Byte(((x+2)&0x0f),OLED_CMD);
}
/*******************************************************************
* @name :void OLED_Display_On(void)
* @date :2018-08-27
* @function :Turn on OLED display
* @parameters :None
* @retvalue :None
********************************************************************/
void OLED_Display_On(void)
{
OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDCÃüÁî
OLED_WR_Byte(0X14,OLED_CMD); //DCDC ON
OLED_WR_Byte(0XAF,OLED_CMD); //DISPLAY ON
}
/*******************************************************************
* @name :void OLED_Display_Off(void)
* @date :2018-08-27
* @function :Turn off OLED display
* @parameters :None
* @retvalue :None
********************************************************************/
void OLED_Display_Off(void)
{
OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDCÃüÁî
OLED_WR_Byte(0X10,OLED_CMD); //DCDC OFF
OLED_WR_Byte(0XAE,OLED_CMD); //DISPLAY OFF
}
/*******************************************************************
* @name :void OLED_Set_Pixel(unsigned char x, unsigned char y,unsigned char color)
* @date :2018-08-27
* @function :set the value of pixel to RAM
* @parameters :x:the x coordinates of pixel
y:the y coordinates of pixel
color:the color value of the point
1-white
0-black
* @retvalue :None
********************************************************************/
void OLED_Set_Pixel(unsigned char x, unsigned char y,unsigned char color)
{
if(color)
{
OLED_buffer[(y/PAGE_SIZE)*WIDTH+x]|= (1<<(y%PAGE_SIZE))&0xff;
}
else
{
OLED_buffer[(y/PAGE_SIZE)*WIDTH+x]&= ~((1<<(y%PAGE_SIZE))&0xff);
}
}
/*******************************************************************
* @name :void OLED_Display(void)
* @date :2018-08-27
* @function :Display in OLED screen
* @parameters :None
* @retvalue :None
********************************************************************/
void OLED_Display(void)
{
uint8_t i,n;
for(i=0;i<PAGE_SIZE;i++)
{
OLED_WR_Byte (YLevel+i,OLED_CMD); //ÉèÖÃÒ³µØÖ·£¨0~7£©
OLED_WR_Byte (XLevelL,OLED_CMD); //ÉèÖÃÏÔʾλÖáªÁе͵ØÖ·
OLED_WR_Byte (XLevelH,OLED_CMD); //ÉèÖÃÏÔʾλÖáªÁиߵØÖ·
for(n=0;n<WIDTH;n++)
{
OLED_WR_Byte(OLED_buffer[i*WIDTH+n],OLED_DATA);
}
} //¸üÐÂÏÔʾ
}
/*******************************************************************
* @name :void OLED_Clear(unsigned dat)
* @date :2018-08-27
* @function :clear OLED screen
* @parameters :dat:0-Display full black
1-Display full white
* @retvalue :None
********************************************************************/
void OLED_Clear(unsigned dat)
{
if(dat)
{
memset(OLED_buffer,0xff,sizeof(OLED_buffer));
}
else
{
memset(OLED_buffer,0,sizeof(OLED_buffer));
}
OLED_Display();
}
/*******************************************************************
* @name :void OLED_Reset(void)
* @date :2018-08-27
* @function :Reset OLED screen
* @parameters :dat:0-Display full black
1-Display full white
* @retvalue :None
********************************************************************/
void OLED_Reset(void)
{
OLED_RST_Set();
HAL_Delay(100);
OLED_RST_Clr();
HAL_Delay(100);
OLED_RST_Set();
}
/*******************************************************************
* @name :void OLED_Init_GPIO(void)
* @date :2018-08-27
* @function :Reset OLED screen
* @parameters :None
* @retvalue :None
********************************************************************/
void OLED_Init_GPIO(void)
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_6, GPIO_PIN_RESET);//ʹÄÜB¶Ë¿ÚʱÖÓ
GPIO_InitStructure.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_6; //GPIOB10,11,12
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; //ÍÆÍìÊä³ö
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_6, GPIO_PIN_SET);//ʹÄÜB¶Ë¿ÚʱÖÓ
}
/*******************************************************************
* @name :void OLED_Init(void)
* @date :2018-08-27
* @function :initialise OLED SH1106 control IC
* @parameters :None
* @retvalue :None
********************************************************************/
void OLED_Init(void)
{
OLED_Init_GPIO(); //³õʼ»¯GPIO
HAL_Delay(200);
OLED_Reset(); //¸´Î»OLED
/**************³õʼ»¯SSD1306*****************/
OLED_WR_Byte(0xAE,OLED_CMD); /*display off*/
OLED_WR_Byte(0x00,OLED_CMD); /*set lower column address*/
OLED_WR_Byte(0x10,OLED_CMD); /*set higher column address*/
OLED_WR_Byte(0x40,OLED_CMD); /*set display start line*/
OLED_WR_Byte(0xB0,OLED_CMD); /*set page address*/
OLED_WR_Byte(0x81,OLED_CMD); /*contract control*/
OLED_WR_Byte(0xFF,OLED_CMD); /*128*/
OLED_WR_Byte(0xA1,OLED_CMD); /*set segment remap*/
OLED_WR_Byte(0xA6,OLED_CMD); /*normal / reverse*/
OLED_WR_Byte(0xA8,OLED_CMD); /*multiplex ratio*/
OLED_WR_Byte(0x3F,OLED_CMD); /*duty = 1/64*/
OLED_WR_Byte(0xC8,OLED_CMD); /*Com scan direction*/
OLED_WR_Byte(0xD3,OLED_CMD); /*set display offset*/
OLED_WR_Byte(0x00,OLED_CMD);
OLED_WR_Byte(0xD5,OLED_CMD); /*set osc division*/
OLED_WR_Byte(0x80,OLED_CMD);
OLED_WR_Byte(0xD9,OLED_CMD); /*set pre-charge period*/
OLED_WR_Byte(0XF1,OLED_CMD);
OLED_WR_Byte(0xDA,OLED_CMD); /*set COM pins*/
OLED_WR_Byte(0x12,OLED_CMD);
OLED_WR_Byte(0xDB,OLED_CMD); /*set vcomh*/
OLED_WR_Byte(0x30,OLED_CMD);
OLED_WR_Byte(0x8D,OLED_CMD); /*set charge pump disable*/
OLED_WR_Byte(0x14,OLED_CMD);
OLED_WR_Byte(0xAF,OLED_CMD); /*display ON*/
}
oled.h
/***************************************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, QD electronic SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
****************************************************************************************************/
#ifndef __OLED_H
#define __OLED_H
#include "stm32f1xx_hal.h"
//--------------OLED参数定义---------------------
#define PAGE_SIZE 8
#define XLevelL 0x00
#define XLevelH 0x10
#define YLevel 0xB0
#define Brightness 0xFF
#define WIDTH 128
#define HEIGHT 64
//-------------写命令和数据定义-------------------
#define OLED_CMD 0 //写命令
#define OLED_DATA 1 //写数据
//-----------------OLED端口定义----------------
/*
#define OLED_CS GPIO_PIN_11 //片选信号 PB11
#define OLED_DC GPIO_PIN_10 //数据/命令控制信号 PB10
#define OLED_RST GPIO_PIN_12 //复位信号 PB12
*/
// 设置(定义)RES、DC、CS 所使用的引脚
// RES
#define OLED_RES_GPIO_Port GPIOA
#define OLED_RES_Pin GPIO_PIN_6
// DC
#define OLED_DC_GPIO_Port GPIOA
#define OLED_DC_Pin GPIO_PIN_4
// CS
#define OLED_CS_GPIO_Port GPIOA
#define OLED_CS_Pin GPIO_PIN_3
//-----------------OLED端口操作定义----------------
/*
#define OLED_CS_Clr() GPIO_ResetBits(GPIOB,OLED_CS)
#define OLED_CS_Set() GPIO_SetBits(GPIOB,OLED_CS)
#define OLED_DC_Clr() GPIO_ResetBits(GPIOB,OLED_DC)
#define OLED_DC_Set() GPIO_SetBits(GPIOB,OLED_DC)
#define OLED_RST_Clr() GPIO_ResetBits(GPIOB,OLED_RST)
#define OLED_RST_Set() GPIO_SetBits(GPIOB,OLED_RST)
*/
#define OLED_RST_Clr() HAL_GPIO_WritePin(OLED_RES_GPIO_Port,OLED_RES_Pin,GPIO_PIN_RESET)//RES
#define OLED_RST_Set() HAL_GPIO_WritePin(OLED_RES_GPIO_Port,OLED_RES_Pin,GPIO_PIN_SET)
#define OLED_DC_Clr() HAL_GPIO_WritePin(OLED_DC_GPIO_Port,OLED_DC_Pin,GPIO_PIN_RESET)//DC
#define OLED_DC_Set() HAL_GPIO_WritePin(OLED_DC_GPIO_Port,OLED_DC_Pin,GPIO_PIN_SET)
#define OLED_CS_Clr() HAL_GPIO_WritePin(OLED_CS_GPIO_Port,OLED_CS_Pin,GPIO_PIN_RESET)//CS
#define OLED_CS_Set() HAL_GPIO_WritePin(OLED_CS_GPIO_Port,OLED_CS_Pin,GPIO_PIN_SET)
//OLED控制用函数
void OLED_WR_Byte(unsigned dat,unsigned cmd);
void OLED_Display_On(void);
void OLED_Display_Off(void);
void OLED_Set_Pos(unsigned char x, unsigned char y);
void OLED_Reset(void);
void OLED_Init_GPIO(void);
void OLED_Init(void);
void OLED_Set_Pixel(unsigned char x, unsigned char y,unsigned char color);
void OLED_Display(void);
void OLED_Clear(unsigned dat);
#endif
oledfont.h
/***************************************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, QD electronic SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
****************************************************************************************************/
#ifndef __OLEDFONT_H
#define __OLEDFONT_H
//³£ÓÃASCII±í
//Æ«ÒÆÁ¿32
//ASCII×Ö·û¼¯
//Æ«ÒÆÁ¿32
//´óС:6*8
//ÖðÐÐʽ£¬Ë³Ïò£¨¸ßλÔÚÇ°£©
/************************************6*8µÄµãÕó************************************/
const unsigned char F6x8[][8] =
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // sp
0x10,0x10,0x10,0x10,0x00,0x10,0x00,0x00, // !
0x28,0x28,0x28,0x00,0x00,0x00,0x00,0x00, // "
0x28,0x28,0x7C,0x28,0x7C,0x28,0x28,0x00, // #
0x10,0x3C,0x50,0x38,0x14,0x78,0x10,0x00, // $
0x00,0x4C,0x2C,0x10,0x08,0x60,0x60,0x00, // %
0x30,0x48,0x50,0x20,0x54,0x48,0x34,0x00, // &
0x30,0x10,0x20,0x00,0x00,0x00,0x00,0x00, // '
0x08,0x10,0x20,0x20,0x20,0x10,0x08,0x00, // (
0x20,0x10,0x08,0x08,0x08,0x10,0x20,0x00, // )
0x00,0x10,0x54,0x38,0x54,0x10,0x00,0x00, // *
0x00,0x10,0x10,0x7C,0x10,0x10,0x00,0x00, // +
0x00,0x00,0x00,0x00,0x00,0x18,0x08,0x10, // ,
0x00,0x00,0x00,0x00,0x7C,0x00,0x00,0x00, // -
0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00, // .
0x00,0x04,0x08,0x10,0x20,0x40,0x00,0x00, // /
0x38,0x44,0x4C,0x54,0x64,0x44,0x38,0x00, // 0
0x10,0x30,0x10,0x10,0x10,0x10,0x38,0x00, // 1
0x38,0x44,0x04,0x08,0x10,0x20,0x7C,0x00, // 2
0x7C,0x08,0x10,0x08,0x04,0x44,0x38,0x00, // 3
0x08,0x18,0x28,0x48,0x7C,0x08,0x08,0x00, // 4
0x7C,0x40,0x78,0x04,0x04,0x44,0x38,0x00, // 5
0x18,0x20,0x40,0x78,0x44,0x44,0x38,0x00, // 6
0x7C,0x04,0x08,0x10,0x20,0x20,0x20,0x00, // 7
0x38,0x44,0x44,0x38,0x44,0x44,0x38,0x00, // 8
0x38,0x44,0x44,0x3C,0x04,0x08,0x30,0x00, // 9
0x00,0x30,0x30,0x00,0x30,0x30,0x00,0x00, // :
0x00,0x30,0x30,0x00,0x30,0x10,0x20,0x00, // ;
0x08,0x10,0x20,0x40,0x20,0x10,0x08,0x00, // <
0x00,0x00,0x7C,0x00,0x7C,0x00,0x00,0x00, // =
0x20,0x10,0x08,0x04,0x08,0x10,0x20,0x00, // >
0x38,0x44,0x04,0x08,0x10,0x00,0x10,0x00, // ?
0x38,0x44,0x04,0x34,0x5C,0x44,0x38,0x00, // @
0x10,0x28,0x44,0x44,0x7C,0x44,0x44,0x00, // A
0x78,0x44,0x44,0x78,0x44,0x44,0x78,0x00, // B
0x38,0x44,0x40,0x40,0x40,0x44,0x38,0x00, // C
0x70,0x48,0x44,0x44,0x44,0x48,0x70,0x00, // D
0x7C,0x40,0x40,0x78,0x40,0x40,0x7C,0x00, // E
0x7C,0x40,0x40,0x78,0x40,0x40,0x40,0x00, // F
0x38,0x44,0x40,0x5C,0x44,0x44,0x3C,0x00, // G
0x44,0x44,0x44,0x7C,0x44,0x44,0x44,0x00, // H
0x38,0x10,0x10,0x10,0x10,0x10,0x38,0x00, // I
0x1C,0x08,0x08,0x08,0x08,0x48,0x30,0x00, // J
0x44,0x48,0x50,0x60,0x50,0x48,0x44,0x00, // K
0x40,0x40,0x40,0x40,0x40,0x40,0x7C,0x00, // L
0x44,0x6C,0x54,0x54,0x44,0x44,0x44,0x00, // M
0x44,0x44,0x64,0x54,0x4C,0x44,0x44,0x00, // N
0x38,0x44,0x44,0x44,0x44,0x44,0x38,0x00, // O
0x78,0x44,0x44,0x78,0x40,0x40,0x40,0x00, // P
0x38,0x44,0x44,0x44,0x54,0x48,0x34,0x00, // Q
0x78,0x44,0x44,0x78,0x50,0x48,0x44,0x00, // R
0x3C,0x40,0x40,0x38,0x04,0x04,0x78,0x00, // S
0x7C,0x10,0x10,0x10,0x10,0x10,0x10,0x00, // T
0x44,0x44,0x44,0x44,0x44,0x44,0x38,0x00, // U
0x44,0x44,0x44,0x44,0x44,0x28,0x10,0x00, // V
0x44,0x44,0x44,0x54,0x54,0x54,0x28,0x00, // W
0x44,0x44,0x28,0x10,0x28,0x44,0x44,0x00, // X
0x44,0x44,0x44,0x28,0x10,0x10,0x10,0x00, // Y
0x7C,0x04,0x08,0x10,0x20,0x40,0x7C,0x00, // Z
0x38,0x20,0x20,0x20,0x20,0x20,0x38,0x00, // [
0x00,0x40,0x20,0x10,0x08,0x04,0x00,0x00, // '\'
0x38,0x08,0x08,0x08,0x08,0x08,0x38,0x00, // ]
0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x00, // ^
0x00,0x00,0x00,0x00,0x00,0x00,0x7C,0x00, // _
0x20,0x10,0x08,0x00,0x00,0x00,0x00,0x00, // '
0x00,0x00,0x38,0x04,0x3C,0x44,0x3C,0x00, // a
0x40,0x40,0x58,0x64,0x44,0x44,0x78,0x00, // b
0x00,0x00,0x38,0x40,0x40,0x44,0x38,0x00, // c
0x04,0x04,0x34,0x4C,0x44,0x44,0x3C,0x00, // d
0x00,0x00,0x38,0x44,0x7C,0x40,0x38,0x00, // e
0x38,0x24,0x20,0x70,0x20,0x20,0x20,0x00, // f
0x00,0x00,0x3C,0x44,0x44,0x3C,0x04,0x38, // g
0x40,0x40,0x58,0x64,0x44,0x44,0x44,0x00, // h
0x10,0x00,0x30,0x10,0x10,0x10,0x38,0x00, // i
0x08,0x00,0x18,0x08,0x08,0x08,0x48,0x30, // j
0x40,0x40,0x48,0x50,0x60,0x50,0x48,0x00, // k
0x30,0x10,0x10,0x10,0x10,0x10,0x38,0x00, // l
0x00,0x00,0x68,0x54,0x54,0x44,0x44,0x00, // m
0x00,0x00,0x58,0x64,0x44,0x44,0x44,0x00, // n
0x00,0x00,0x38,0x44,0x44,0x44,0x38,0x00, // o
0x00,0x00,0x58,0x64,0x64,0x58,0x40,0x40, // p
0x00,0x00,0x34,0x4C,0x4C,0x34,0x04,0x04, // q
0x00,0x00,0x58,0x64,0x40,0x40,0x40,0x00, // r
0x00,0x00,0x3C,0x40,0x38,0x04,0x78,0x00, // s
0x20,0x20,0x70,0x20,0x20,0x24,0x18,0x00, // t
0x00,0x00,0x44,0x44,0x44,0x4C,0x34,0x00, // u
0x00,0x00,0x44,0x44,0x44,0x28,0x10,0x00, // v
0x00,0x00,0x44,0x44,0x54,0x54,0x28,0x00, // w
0x00,0x00,0x44,0x28,0x10,0x28,0x44,0x00, // x
0x00,0x00,0x44,0x44,0x44,0x3C,0x04,0x38, // y
0x00,0x00,0x7C,0x08,0x10,0x20,0x7C,0x00, // z
0x10,0x20,0x20,0x40,0x20,0x20,0x10,0x00, // {
0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x00, // |
0x20,0x10,0x10,0x08,0x10,0x10,0x20,0x00, // }
0x00,0x00,0x00,0x20,0x54,0x08,0x00,0x00, // ~
};
//³£ÓÃASCII±í
//Æ«ÒÆÁ¿32
//ASCII×Ö·û¼¯
//Æ«ÒÆÁ¿32
//´óС:8*16
//ÖðÐÐʽ£¬Ë³Ïò£¨¸ßλÔÚÇ°£©
/****************************************8*16µÄµãÕó************************************/
const unsigned char F8X16[]=
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
0x00,0x00,0x00,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x00,0x00,0x18,0x18,0x00,0x00,//! 1
0x00,0x12,0x36,0x24,0x48,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2
0x00,0x00,0x00,0x24,0x24,0x24,0xFE,0x48,0x48,0x48,0xFE,0x48,0x48,0x48,0x00,0x00,//# 3
0x00,0x00,0x10,0x38,0x54,0x54,0x50,0x30,0x1C,0x14,0x14,0x54,0x54,0x38,0x10,0x10,//$ 4
0x00,0x00,0x00,0x44,0xA4,0xA8,0xA8,0xA8,0x54,0x1A,0x2A,0x2A,0x2A,0x44,0x00,0x00,//% 5
0x00,0x00,0x00,0x30,0x48,0x48,0x48,0x50,0x6E,0xA4,0x94,0x88,0x89,0x76,0x00,0x00,//& 6
0x00,0x60,0x60,0x20,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7
0x00,0x02,0x04,0x08,0x08,0x10,0x10,0x10,0x10,0x10,0x10,0x08,0x08,0x04,0x02,0x00,//( 8
0x00,0x40,0x20,0x10,0x10,0x08,0x08,0x08,0x08,0x08,0x08,0x10,0x10,0x20,0x40,0x00,//) 9
0x00,0x00,0x00,0x00,0x10,0x10,0xD6,0x38,0x38,0xD6,0x10,0x10,0x00,0x00,0x00,0x00,//* 10
0x00,0x00,0x00,0x00,0x10,0x10,0x10,0x10,0xFE,0x10,0x10,0x10,0x10,0x00,0x00,0x00,//+ 11
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x60,0x20,0xC0,//, 12
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//- 13
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x60,0x00,0x00,//. 14
0x00,0x00,0x01,0x02,0x02,0x04,0x04,0x08,0x08,0x10,0x10,0x20,0x20,0x40,0x40,0x00,/// 15
0x00,0x00,0x00,0x18,0x24,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18,0x00,0x00,//0 16
0x00,0x00,0x00,0x10,0x70,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x7C,0x00,0x00,//1 17
0x00,0x00,0x00,0x3C,0x42,0x42,0x42,0x04,0x04,0x08,0x10,0x20,0x42,0x7E,0x00,0x00,//2 18
0x00,0x00,0x00,0x3C,0x42,0x42,0x04,0x18,0x04,0x02,0x02,0x42,0x44,0x38,0x00,0x00,//3 19
0x00,0x00,0x00,0x04,0x0C,0x14,0x24,0x24,0x44,0x44,0x7E,0x04,0x04,0x1E,0x00,0x00,//4 20
0x00,0x00,0x00,0x7E,0x40,0x40,0x40,0x58,0x64,0x02,0x02,0x42,0x44,0x38,0x00,0x00,//5 21
0x00,0x00,0x00,0x1C,0x24,0x40,0x40,0x58,0x64,0x42,0x42,0x42,0x24,0x18,0x00,0x00,//6 22
0x00,0x00,0x00,0x7E,0x42,0x44,0x08,0x08,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x00,//7 23
0x00,0x00,0x00,0x3C,0x42,0x42,0x42,0x24,0x18,0x24,0x42,0x42,0x42,0x3C,0x00,0x00,//8 24
0x00,0x00,0x00,0x18,0x24,0x42,0x42,0x42,0x26,0x1A,0x02,0x02,0x24,0x38,0x00,0x00,//9 25
0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,//: 26
0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x18,0x18,0x08,0x30,0x00,//; 27
0x00,0x00,0x00,0x02,0x04,0x08,0x10,0x20,0x40,0x20,0x10,0x08,0x04,0x02,0x00,0x00,//< 28
0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0x00,0x00,//= 29
0x00,0x00,0x00,0x40,0x20,0x10,0x08,0x04,0x02,0x04,0x08,0x10,0x20,0x40,0x00,0x00,//> 30
0x00,0x00,0x00,0x3C,0x42,0x42,0x62,0x02,0x04,0x08,0x08,0x00,0x18,0x18,0x00,0x00,//? 31
0x00,0x00,0x00,0x38,0x44,0x5A,0xAA,0xAA,0xAA,0xAA,0xB4,0x42,0x44,0x38,0x00,0x00,//@ 32
0x00,0x00,0x00,0x10,0x10,0x18,0x28,0x28,0x24,0x3C,0x44,0x42,0x42,0xE7,0x00,0x00,//A 33
0x00,0x00,0x00,0xF8,0x44,0x44,0x44,0x78,0x44,0x42,0x42,0x42,0x44,0xF8,0x00,0x00,//B 34
0x00,0x00,0x00,0x3E,0x42,0x42,0x80,0x80,0x80,0x80,0x80,0x42,0x44,0x38,0x00,0x00,//C 35
0x00,0x00,0x00,0xF8,0x44,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x44,0xF8,0x00,0x00,//D 36
0x00,0x00,0x00,0xFE,0x42,0x48,0x48,0x78,0x48,0x48,0x40,0x42,0x42,0xFE,0x00,0x00,//E 37
0x00,0x00,0x00,0xFE,0x42,0x48,0x48,0x78,0x48,0x48,0x40,0x40,0x40,0xE0,0x00,0x00,//F 38
0x00,0x00,0x00,0x3C,0x44,0x44,0x80,0x80,0x80,0x8E,0x84,0x44,0x44,0x38,0x00,0x00,//G 39
0x00,0x00,0x00,0xE7,0x42,0x42,0x42,0x42,0x7E,0x42,0x42,0x42,0x42,0xE7,0x00,0x00,//H 40
0x00,0x00,0x00,0x7C,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x7C,0x00,0x00,//I 41
0x00,0x00,0x00,0x3E,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x88,0xF0,//J 42
0x00,0x00,0x00,0xEE,0x44,0x48,0x50,0x70,0x50,0x48,0x48,0x44,0x44,0xEE,0x00,0x00,//K 43
0x00,0x00,0x00,0xE0,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x42,0xFE,0x00,0x00,//L 44
0x00,0x00,0x00,0xEE,0x6C,0x6C,0x6C,0x6C,0x54,0x54,0x54,0x54,0x54,0xD6,0x00,0x00,//M 45
0x00,0x00,0x00,0xC7,0x62,0x62,0x52,0x52,0x4A,0x4A,0x4A,0x46,0x46,0xE2,0x00,0x00,//N 46
0x00,0x00,0x00,0x38,0x44,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x44,0x38,0x00,0x00,//O 47
0x00,0x00,0x00,0xFC,0x42,0x42,0x42,0x42,0x7C,0x40,0x40,0x40,0x40,0xE0,0x00,0x00,//P 48
0x00,0x00,0x00,0x38,0x44,0x82,0x82,0x82,0x82,0x82,0xB2,0xCA,0x4C,0x38,0x06,0x00,//Q 49
0x00,0x00,0x00,0xF8,0x44,0x44,0x44,0x78,0x48,0x48,0x44,0x44,0x42,0xE3,0x00,0x00,//R 50
0x00,0x00,0x00,0x3E,0x42,0x42,0x40,0x20,0x18,0x04,0x02,0x42,0x42,0x7C,0x00,0x00,//S 51
0x00,0x00,0x00,0xFE,0x92,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x38,0x00,0x00,//T 52
0x00,0x00,0x00,0xE7,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x3C,0x00,0x00,//U 53
0x00,0x00,0x00,0xE7,0x42,0x42,0x44,0x24,0x24,0x28,0x28,0x18,0x10,0x10,0x00,0x00,//V 54
0x00,0x00,0x00,0xD6,0x92,0x92,0x92,0x92,0xAA,0xAA,0x6C,0x44,0x44,0x44,0x00,0x00,//W 55
0x00,0x00,0x00,0xE7,0x42,0x24,0x24,0x18,0x18,0x18,0x24,0x24,0x42,0xE7,0x00,0x00,//X 56
0x00,0x00,0x00,0xEE,0x44,0x44,0x28,0x28,0x10,0x10,0x10,0x10,0x10,0x38,0x00,0x00,//Y 57
0x00,0x00,0x00,0x7E,0x84,0x04,0x08,0x08,0x10,0x20,0x20,0x42,0x42,0xFC,0x00,0x00,//Z 58
0x00,0x1E,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x1E,0x00,//[ 59
0x00,0x00,0x40,0x40,0x20,0x20,0x10,0x10,0x08,0x04,0x04,0x02,0x02,0x02,0x01,0x01,//\ 60
0x00,0x78,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x78,0x00,//] 61
0x00,0x08,0x14,0x22,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,//_ 63
0x00,0x60,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x42,0x1E,0x22,0x42,0x42,0x3F,0x00,0x00,//a 65
0x00,0x00,0x00,0xC0,0x40,0x40,0x40,0x58,0x64,0x42,0x42,0x42,0x64,0x58,0x00,0x00,//b 66
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1C,0x22,0x40,0x40,0x40,0x22,0x1C,0x00,0x00,//c 67
0x00,0x00,0x00,0x06,0x02,0x02,0x02,0x1E,0x22,0x42,0x42,0x42,0x26,0x1B,0x00,0x00,//d 68
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x42,0x7E,0x40,0x40,0x42,0x3C,0x00,0x00,//e 69
0x00,0x00,0x00,0x0F,0x11,0x10,0x10,0x7E,0x10,0x10,0x10,0x10,0x10,0x7C,0x00,0x00,//f 70
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3E,0x44,0x44,0x38,0x40,0x3C,0x42,0x42,0x3C,//g 71
0x00,0x00,0x00,0xC0,0x40,0x40,0x40,0x5C,0x62,0x42,0x42,0x42,0x42,0xE7,0x00,0x00,//h 72
0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x70,0x10,0x10,0x10,0x10,0x10,0x7C,0x00,0x00,//i 73
0x00,0x00,0x00,0x0C,0x0C,0x00,0x00,0x1C,0x04,0x04,0x04,0x04,0x04,0x04,0x44,0x78,//j 74
0x00,0x00,0x00,0xC0,0x40,0x40,0x40,0x4E,0x48,0x50,0x68,0x48,0x44,0xEE,0x00,0x00,//k 75
0x00,0x00,0x00,0x70,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x7C,0x00,0x00,//l 76
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF6,0x49,0x49,0x49,0x49,0x49,0xED,0x00,0x00,//m 77
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xDC,0x62,0x42,0x42,0x42,0x42,0xE7,0x00,0x00,//n 78
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x42,0x42,0x42,0x42,0x42,0x3C,0x00,0x00,//o 79
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xD8,0x64,0x42,0x42,0x42,0x64,0x58,0x40,0xE0,//p 80
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1E,0x22,0x42,0x42,0x42,0x26,0x1A,0x02,0x07,//q 81
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xEE,0x32,0x20,0x20,0x20,0x20,0xF8,0x00,0x00,//r 82
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3E,0x42,0x40,0x3C,0x02,0x42,0x7C,0x00,0x00,//s 83
0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x7C,0x10,0x10,0x10,0x10,0x10,0x0C,0x00,0x00,//t 84
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC6,0x42,0x42,0x42,0x42,0x46,0x3B,0x00,0x00,//u 85
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE7,0x42,0x24,0x24,0x28,0x10,0x10,0x00,0x00,//v 86
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xD7,0x92,0x92,0xAA,0xAA,0x44,0x44,0x00,0x00,//w 87
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x6E,0x24,0x18,0x18,0x18,0x24,0x76,0x00,0x00,//x 88
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE7,0x42,0x24,0x24,0x28,0x18,0x10,0x10,0xE0,//y 89
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7E,0x44,0x08,0x10,0x10,0x22,0x7E,0x00,0x00,//z 90
0x00,0x03,0x04,0x04,0x04,0x04,0x04,0x08,0x04,0x04,0x04,0x04,0x04,0x04,0x03,0x00,//{ 91
0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,//| 92
0x00,0x60,0x10,0x10,0x10,0x10,0x10,0x08,0x10,0x10,0x10,0x10,0x10,0x10,0x60,0x00,//} 93
0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x52,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
};
typedef struct
{
unsigned char Index[2];
char Msk[32];
}typFNT_GB16;
//ËÎÌå
//16*16´óС
//ÖðÐÐʽ£¬Ë³Ïò£¨¸ßλÔÚÇ°£©
const typFNT_GB16 cfont16[] =
{
"µ±",0x01,0x00,0x21,0x08,0x11,0x08,0x09,0x10,0x09,0x20,0x01,0x00,0x7F,0xF8,0x00,0x08,
0x00,0x08,0x00,0x08,0x3F,0xF8,0x00,0x08,0x00,0x08,0x00,0x08,0x7F,0xF8,0x00,0x08,/*"?",0*/
"Ç°",0x10,0x10,0x08,0x10,0x08,0x20,0xFF,0xFE,0x00,0x00,0x3E,0x08,0x22,0x48,0x22,0x48,
0x3E,0x48,0x22,0x48,0x22,0x48,0x3E,0x48,0x22,0x08,0x22,0x08,0x2A,0x28,0x24,0x10,/*"?",1*/
"ʱ",0x00,0x08,0x00,0x08,0x7C,0x08,0x44,0x08,0x45,0xFE,0x44,0x08,0x44,0x08,0x7C,0x08,
0x44,0x88,0x44,0x48,0x44,0x48,0x44,0x08,0x7C,0x08,0x44,0x08,0x00,0x28,0x00,0x10,/*"?",2*/
"¼ä",0x20,0x00,0x13,0xFC,0x10,0x04,0x40,0x04,0x47,0xC4,0x44,0x44,0x44,0x44,0x44,0x44,
0x47,0xC4,0x44,0x44,0x44,0x44,0x44,0x44,0x47,0xC4,0x40,0x04,0x40,0x14,0x40,0x08,/*"?",3*/
"£º",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x30,0x00,0x30,0x00,0x00,0x00,0x30,0x00,0x30,0x00,0x00,0x00,0x00,0x00,/*":",0*/
"ÎÂ",0x00,0x00,0x23,0xF8,0x12,0x08,0x12,0x08,0x83,0xF8,0x42,0x08,0x42,0x08,0x13,0xF8,
0x10,0x00,0x27,0xFC,0xE4,0xA4,0x24,0xA4,0x24,0xA4,0x24,0xA4,0x2F,0xFE,0x00,0x00,/*"?",0*/
"¶È",0x01,0x00,0x00,0x80,0x3F,0xFE,0x22,0x20,0x22,0x20,0x3F,0xFC,0x22,0x20,0x22,0x20,
0x23,0xE0,0x20,0x00,0x2F,0xF0,0x24,0x10,0x42,0x20,0x41,0xC0,0x86,0x30,0x38,0x0E,/*"?",1*/
"ʪ",0x00,0x00,0x27,0xF8,0x14,0x08,0x14,0x08,0x87,0xF8,0x44,0x08,0x44,0x08,0x17,
0xF8,0x11,0x20,0x21,0x20,0xE9,0x24,0x25,0x28,0x23,0x30,0x21,0x20,0x2F,0xFE,0x00,0x00,/*"?",2*/
"ÈÕ",0x00,0x00,0x1F,0xF0,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x1F,0xF0,
0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x1F,0xF0,0x10,0x10,/*"?",0*/
"ÆÚ",0x22,0x00,0x22,0x7C,0x7F,0x44,0x22,0x44,0x22,0x44,0x3E,0x7C,0x22,0x44,0x22,0x44,
0x3E,0x44,0x22,0x7C,0x22,0x44,0xFF,0x44,0x04,0x84,0x22,0x84,0x41,0x14,0x82,0x08,/*"?",1*/
"¡æ",0x60,0x00,0x91,0xF4,0x96,0x0C,0x6C,0x04,0x08,0x04,0x18,0x00,0x18,0x00,0x18,0x00,
0x18,0x00,0x18,0x00,0x18,0x00,0x08,0x00,0x0C,0x04,0x06,0x08,0x01,0xF0,0x00,0x00,/*"¡æ",0*/
};
#endif
gui.h
/***************************************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, QD electronic SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
****************************************************************************************************/
#include "main.h"
#ifndef __GUI_H__
#define __GUI_H__
void GUI_DrawPoint(uint8_t x, uint8_t y, uint8_t color);
void GUI_Fill(uint8_t sx,uint8_t sy,uint8_t ex,uint8_t ey,uint8_t color);
void GUI_DrawLine(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2,uint8_t color);
void GUI_DrawRectangle(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2,uint8_t color);
void GUI_FillRectangle(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2,uint8_t color);
void GUI_DrawCircle(uint8_t xc, uint8_t yc, uint8_t color, uint8_t r);
void GUI_FillCircle(uint8_t xc, uint8_t yc, uint8_t color, uint8_t r);
void GUI_DrawTriangel(uint8_t x0,uint8_t y0,uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint8_t color);
void GUI_FillTriangel(uint8_t x0,uint8_t y0,uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint8_t color);
void GUI_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t Char_Size,uint8_t mode);
void GUI_ShowNum(uint8_t x,uint8_t y,uint32_t num,uint8_t len,uint8_t Size,uint8_t mode);
void GUI_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size,uint8_t mode);
void GUI_ShowFont16(uint8_t x,uint8_t y,uint8_t *s,uint8_t mode);
void GUI_ShowFont24(uint8_t x,uint8_t y,uint8_t *s,uint8_t mode);
void GUI_ShowFont32(uint8_t x,uint8_t y,uint8_t *s,uint8_t mode);
void GUI_ShowCHinese(uint8_t x,uint8_t y,uint8_t hsize,uint8_t *str,uint8_t mode);
void GUI_DrawBMP(uint8_t x,uint8_t y,uint8_t width, uint8_t height, uint8_t BMP[], uint8_t mode);
#endif
gui,c
/***************************************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, QD electronic SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
****************************************************************************************************/
#include "oled.h"
#include "string.h"
#include "oledfont.h"
#include "stm32f1xx_hal.h"
#include "gui.h"
/*******************************************************************
* @name :void GUI_DrawPoint(uint8_t x,uint8_t y,uint8_t color)
* @date :2018-08-27
* @function :draw a point in LCD screen
* @parameters :x:the x coordinate of the point
y:the y coordinate of the point
color:the color value of the point
1-white
0-black
* @retvalue :None
********************************************************************/
void GUI_DrawPoint(uint8_t x,uint8_t y,uint8_t color)
{
OLED_Set_Pixel(x,y,color);
OLED_Display();
}
/*******************************************************************
* @name :void GUI_Fill(uint8_t sx,uint8_t sy,uint8_t ex,uint8_t ey,uint8_t color)
* @date :2018-08-27
* @function :fill the specified area
* @parameters :sx:the bebinning x coordinate of the specified area
sy:the bebinning y coordinate of the specified area
ex:the ending x coordinate of the specified area
ey:the ending y coordinate of the specified area
color:the color value of the the specified area
1-white
0-black
* @retvalue :None
********************************************************************/
void GUI_Fill(uint8_t sx,uint8_t sy,uint8_t ex,uint8_t ey,uint8_t color)
{
uint8_t i,j;
uint8_t width=ex-sx+1; //得到填充的宽度
uint8_t height=ey-sy+1; //高度
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
OLED_Set_Pixel(sx+j, sy+i,color);
}
}
OLED_Display();
}
/*******************************************************************
* @name :void GUI_DrawLine(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2,uint8_t color)
* @date :2018-08-27
* @function :Draw a line between two points
* @parameters :x1:the bebinning x coordinate of the line
y1:the bebinning y coordinate of the line
x2:the ending x coordinate of the line
y2:the ending y coordinate of the line
color:the color value of the line
1-white
0-black
* @retvalue :None
********************************************************************/
void GUI_DrawLine(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2,uint8_t color)
{
uint16_t t;
int xerr=0,yerr=0,delta_x,delta_y,distance;
int incx,incy,uRow,uCol;
delta_x=x2-x1; //计算坐标增量
delta_y=y2-y1;
uRow=x1;
uCol=y1;
if(delta_x>0)incx=1; //设置单步方向
else if(delta_x==0)incx=0;//垂直线
else {incx=-1;delta_x=-delta_x;}
if(delta_y>0)incy=1;
else if(delta_y==0)incy=0;//水平线
else{incy=-1;delta_y=-delta_y;}
if( delta_x>delta_y)distance=delta_x; //选取基本增量坐标轴
else distance=delta_y;
for(t=0;t<=distance+1;t++ )//画线输出
{
OLED_Set_Pixel(uRow,uCol,color);
xerr+=delta_x ;
yerr+=delta_y ;
if(xerr>distance)
{
xerr-=distance;
uRow+=incx;
}
if(yerr>distance)
{
yerr-=distance;
uCol+=incy;
}
}
OLED_Display();
}
/*****************************************************************************
* @name :void GUI_DrawRectangle(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2,uint8_t color)
* @date :2018-08-27
* @function :Draw a rectangle
* @parameters :x1:the bebinning x coordinate of the rectangle
y1:the bebinning y coordinate of the rectangle
x2:the ending x coordinate of the rectangle
y2:the ending y coordinate of the rectangle
color:the color value of the rectangle
1-white
0-black
* @retvalue :None
******************************************************************************/
void GUI_DrawRectangle(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2,uint8_t color)
{
GUI_DrawLine(x1,y1,x2,y1,color);
GUI_DrawLine(x1,y1,x1,y2,color);
GUI_DrawLine(x1,y2,x2,y2,color);
GUI_DrawLine(x2,y1,x2,y2,color);
}
/*****************************************************************************
* @name :void GUI_FillRectangle(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2,uint8_t color)
* @date :2018-08-27
* @function :Filled a rectangle
* @parameters :x1:the bebinning x coordinate of the filled rectangle
y1:the bebinning y coordinate of the filled rectangle
x2:the ending x coordinate of the filled rectangle
y2:the ending y coordinate of the filled rectangle
color:the color value of the rectangle
1-white
0-black
* @retvalue :None
******************************************************************************/
void GUI_FillRectangle(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2,uint8_t color)
{
GUI_Fill(x1,y1,x2,y2,color);
}
/*****************************************************************************
* @name :static void _draw_circle_8(uint8_t xc, uint8_t yc, uint8_t x, uint8_t y, uint8_t color)
* @date :2018-08-27
* @function :8 symmetry circle drawing algorithm (internal call)
* @parameters :xc:the x coordinate of the Circular center
yc:the y coordinate of the Circular center
x:the x coordinate relative to the Circular center
y:the y coordinate relative to the Circular center
color:the color value of the rectangle
1-white
0-black
* @retvalue :None
******************************************************************************/
static void _draw_circle_8(uint8_t xc, uint8_t yc, uint8_t x, uint8_t y, uint8_t color)
{
OLED_Set_Pixel(xc + x, yc + y, color);
OLED_Set_Pixel(xc - x, yc + y, color);
OLED_Set_Pixel(xc + x, yc - y, color);
OLED_Set_Pixel(xc - x, yc - y, color);
OLED_Set_Pixel(xc + y, yc + x, color);
OLED_Set_Pixel(xc - y, yc + x, color);
OLED_Set_Pixel(xc + y, yc - x, color);
OLED_Set_Pixel(xc - y, yc - x, color);
}
/*****************************************************************************
* @name :void GUI_DrawCircle(uint8_t xc, uint8_t yc, uint8_t color, uint8_t r)
* @date :2018-08-27
* @function :Draw a circle of specified size at a specified location
* @parameters :xc:the x coordinate of the Circular center
yc:the y coordinate of the Circular center
r:Circular radius
color:the color value of the rectangle
1-white
0-black
* @retvalue :None
******************************************************************************/
void GUI_DrawCircle(uint8_t xc, uint8_t yc, uint8_t color, uint8_t r)
{
int x = 0, y = r,d;
d = 3 - 2 * r;
while (x <= y)
{
_draw_circle_8(xc, yc, x, y, color);
if (d < 0)
{
d = d + 4 * x + 6;
}
else
{
d = d + 4 * (x - y) + 10;
y--;
}
x++;
}
OLED_Display();
}
/*****************************************************************************
* @name :void GUI_FillCircle(uint8_t xc, uint8_t yc, uint8_t color, uint8_t r)
* @date :2018-08-27
* @function :Fill a circle of specified size at a specified location
* @parameters :xc:the x coordinate of the Circular center
yc:the y coordinate of the Circular center
r:Circular radius
color:the color value of the rectangle
1-white
0-black
* @retvalue :None
******************************************************************************/
void GUI_FillCircle(uint8_t xc, uint8_t yc, uint8_t color, uint8_t r)
{
int x = 0, y = r, yi, d;
d = 3 - 2 * r;
while (x <= y)
{
for (yi = x; yi <= y; yi++)
{
_draw_circle_8(xc, yc, x, yi, color);
}
if (d < 0)
{
d = d + 4 * x + 6;
}
else
{
d = d + 4 * (x - y) + 10;
y--;
}
x++;
}
OLED_Display();
}
/**********************************************************************************
* @name :void GUI_DrawTriangel(uint8_t x0,uint8_t y0,uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint8_t color)
* @date :2018-08-27
* @function :Draw a triangle at a specified position
* @parameters :x0:the bebinning x coordinate of the triangular edge
y0:the bebinning y coordinate of the triangular edge
x1:the vertex x coordinate of the triangular
y1:the vertex y coordinate of the triangular
x2:the ending x coordinate of the triangular edge
y2:the ending y coordinate of the triangular edge
color:the color value of the rectangle
1-white
0-black
* @retvalue :None
***********************************************************************************/
void GUI_DrawTriangel(uint8_t x0,uint8_t y0,uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint8_t color)
{
GUI_DrawLine(x0,y0,x1,y1,color);
GUI_DrawLine(x1,y1,x2,y2,color);
GUI_DrawLine(x2,y2,x0,y0,color);
}
/*****************************************************************************
* @name :static void _swap(uint8_t *a, uint8_t *b)
* @date :2018-08-27
* @function :Exchange two numbers(internal call)
* @parameters :a:the address of the first number
b:the address of the second number
* @retvalue :None
******************************************************************************/
static void _swap(uint8_t *a, uint8_t *b)
{
uint16_t tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
/*****************************************************************************
* @name :static void _draw_h_line(uint8_t x0,uint8_t x1,uint8_t y,uint8_t color)
* @date :2018-08-27
* @function :draw a horizontal line in RAM(internal call)
* @parameters :x0:the bebinning x coordinate of the horizontal line
x1:the ending x coordinate of the horizontal line
y:the y coordinate of the horizontal line
color:the color value of the rectangle
1-white
0-black
* @retvalue :None
******************************************************************************/
static void _draw_h_line(uint8_t x0,uint8_t x1,uint8_t y,uint8_t color)
{
uint8_t i=0;
for(i=x0;i<=x1;i++)
{
OLED_Set_Pixel(i, y, color);
}
}
/*****************************************************************************
* @name :void GUI_FillTriangel(uint8_t x0,uint8_t y0,uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint8_t color)
* @date :2018-08-27
* @function :filling a triangle at a specified position
* @parameters :x0:the bebinning x coordinate of the triangular edge
y0:the bebinning y coordinate of the triangular edge
x1:the vertex x coordinate of the triangular
y1:the vertex y coordinate of the triangular
x2:the ending x coordinate of the triangular edge
y2:the ending y coordinate of the triangular edge
color:the color value of the rectangle
1-white
0-black
* @retvalue :None
******************************************************************************/
void GUI_FillTriangel(uint8_t x0,uint8_t y0,uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint8_t color)
{
uint8_t a, b, y, last;
int dx01, dy01, dx02, dy02, dx12, dy12;
long sa = 0;
long sb = 0;
if (y0 > y1)
{
_swap(&y0,&y1);
_swap(&x0,&x1);
}
if (y1 > y2)
{
_swap(&y2,&y1);
_swap(&x2,&x1);
}
if (y0 > y1)
{
_swap(&y0,&y1);
_swap(&x0,&x1);
}
if(y0 == y2)
{
a = b = x0;
if(x1 < a)
{
a = x1;
}
else if(x1 > b)
{
b = x1;
}
if(x2 < a)
{
a = x2;
}
else if(x2 > b)
{
b = x2;
}
_draw_h_line(a,b,y0,color);
return;
}
dx01 = x1 - x0;
dy01 = y1 - y0;
dx02 = x2 - x0;
dy02 = y2 - y0;
dx12 = x2 - x1;
dy12 = y2 - y1;
if(y1 == y2)
{
last = y1;
}
else
{
last = y1-1;
}
for(y=y0; y<=last; y++)
{
a = x0 + sa / dy01;
b = x0 + sb / dy02;
sa += dx01;
sb += dx02;
if(a > b)
{
_swap(&a,&b);
}
_draw_h_line(a,b,y,color);
}
sa = dx12 * (y - y1);
sb = dx02 * (y - y0);
for(; y<=y2; y++)
{
a = x1 + sa / dy12;
b = x0 + sb / dy02;
sa += dx12;
sb += dx02;
if(a > b)
{
_swap(&a,&b);
}
_draw_h_line(a,b,y,color);
}
OLED_Display();
}
/*****************************************************************************
* @name :void GUI_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t Char_Size,uint8_t mode)
* @date :2018-08-27
* @function :Display a single English character
* @parameters :x:the bebinning x coordinate of the Character display position
y:the bebinning y coordinate of the Character display position
chr:the ascii code of display character(0~94)
Char_Size:the size of display character(8,16)
mode:0-white background and black character
1-black background and white character
* @retvalue :None
******************************************************************************/
void GUI_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t Char_Size,uint8_t mode)
{
unsigned char c=0,i=0,tmp,j=0;
c=chr-' ';//得到偏移后的值
if(x>WIDTH-1){x=0;y=y+2;}
if(Char_Size ==16)
{
for(i=0;i<16;i++)
{
if(mode)
{
tmp = F8X16[c*16+i];
}
else
{
tmp = ~(F8X16[c*16+i]);
}
for(j=0;j<8;j++)
{
if(tmp&(0x80>>j))
{
OLED_Set_Pixel(x+j, y+i,1);
}
else
{
OLED_Set_Pixel(x+j, y+i,0);
}
}
}
}
else if(Char_Size==8)
{
for(i=0;i<8;i++)
{
if(mode)
{
tmp = F6x8[c][i];
}
else
{
tmp = ~(F6x8[c][i]);
}
for(j=0;j<8;j++)
{
if(tmp&(0x80>>j))
{
OLED_Set_Pixel(x+j, y+i,1);
}
else
{
OLED_Set_Pixel(x+j, y+i,0);
}
}
}
}
else
{
return;
}
OLED_Display();
}
/*****************************************************************************
* @name :void GUI_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size,uint8_t mode)
* @date :2018-08-27
* @function :Display English string
* @parameters :x:the bebinning x coordinate of the English string
y:the bebinning y coordinate of the English string
chr:the start address of the English string
Char_Size:the size of display character
mode:0-white background and black character
1-black background and white character
* @retvalue :None
******************************************************************************/
void GUI_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size,uint8_t mode)
{
unsigned char j=0,csize;
if(Char_Size == 16)
{
csize = Char_Size/2;
}
else if(Char_Size == 8)
{
csize = Char_Size/2+2;
}
else
{
return;
}
while (chr[j]!='\0')
{
GUI_ShowChar(x,y,chr[j],Char_Size,mode);
x+=csize;
if(x>120)
{
x=0;
y+=Char_Size;
}
j++;
}
}
/*****************************************************************************
* @name :uint32_t mypow(uint8_t m,uint8_t n)
* @date :2018-08-27
* @function :get the nth power of m (internal call)
* @parameters :m:the multiplier
n:the power
* @retvalue :the nth power of m
******************************************************************************/
static uint32_t mypow(uint8_t m,uint8_t n)
{
uint32_t result=1;
while(n--)result*=m;
return result;
}
/*****************************************************************************
* @name :void GUI_ShowNum(uint8_t x,uint8_t y,uint32_t num,uint8_t len,uint8_t Size,uint8_t mode)
* @date :2018-08-27
* @function :Display number
* @parameters :x:the bebinning x coordinate of the number
y:the bebinning y coordinate of the number
num:the number(0~4294967295)
len:the length of the display number
Size:the size of display number
mode:0-white background and black character
1-black background and white character
* @retvalue :None
******************************************************************************/
void GUI_ShowNum(uint8_t x,uint8_t y,uint32_t num,uint8_t len,uint8_t Size,uint8_t mode)
{
uint8_t t,temp;
uint8_t enshow=0,csize;
if(Size == 16)
{
csize = Size/2;
}
else if(Size == 8)
{
csize = Size/2+2;
}
else
{
return;
}
for(t=0;t<len;t++)
{
temp=(num/mypow(10,len-t-1))%10;
if(enshow==0&&t<(len-1))
{
if(temp==0)
{
GUI_ShowChar(x+csize*t,y,' ',Size,mode);
continue;
}else enshow=1;
}
GUI_ShowChar(x+csize*t,y,temp+'0',Size,mode);
}
}
/*****************************************************************************
* @name :void GUI_ShowFont16(uint8_t x,uint8_t y,uint8_t *s,uint8_t mode)
* @date :2018-08-27
* @function :Display a single 16x16 Chinese character
* @parameters :x:the bebinning x coordinate of the Chinese character
y:the bebinning y coordinate of the Chinese character
s:the start address of the Chinese character
mode:0-white background and black character
1-black background and white character
* @retvalue :None
******************************************************************************/
void GUI_ShowFont16(uint8_t x,uint8_t y,uint8_t *s,uint8_t mode)
{
uint8_t i,j,k,tmp;
uint16_t num;
num = sizeof(cfont16)/sizeof(typFNT_GB16);
for(i=0;i<num;i++)
{
if((cfont16[i].Index[0]==*s)&&(cfont16[i].Index[1]==*(s+1)))
{
for(j=0;j<32;j++)
{
if(mode)
{
tmp = cfont16[i].Msk[j];
}
else
{
tmp = ~(cfont16[i].Msk[j]);
}
for(k=0;k<8;k++)
{
if(tmp&(0x80>>k))
{
OLED_Set_Pixel(x+(j%2)*8+k, y+j/2,1);
}
else
{
OLED_Set_Pixel(x+(j%2)*8+k, y+j/2,0);
}
}
}
break;
}
}
OLED_Display();
}
/*****************************************************************************
* @name :void GUI_ShowFont24(uint8_t x,uint8_t y,uint8_t *s,uint8_t mode)
* @date :2018-08-27
* @function :Display a single 24x24 Chinese character
* @parameters :x:the bebinning x coordinate of the Chinese character
y:the bebinning y coordinate of the Chinese character
s:the start address of the Chinese character
mode:0-white background and black character
1-black background and white character
* @retvalue :None
******************************************************************************/
void GUI_ShowFont24(uint8_t x,uint8_t y,uint8_t *s,uint8_t mode)
{
uint8_t i,j,k,tmp;
uint16_t num;
num = sizeof(cfont24)/sizeof(typFNT_GB24);
for(i=0;i<num;i++)
{
if((cfont24[i].Index[0]==*s)&&(cfont24[i].Index[1]==*(s+1)))
{
for(j=0;j<72;j++)
{
if(mode)
{
tmp = cfont24[i].Msk[j];
}
else
{
tmp = ~(cfont24[i].Msk[j]);
}
for(k=0;k<8;k++)
{
if(tmp&(0x80>>k))
{
OLED_Set_Pixel(x+(j%3)*8+k, y+j/3,1);
}
else
{
OLED_Set_Pixel(x+(j%3)*8+k, y+j/3,0);
}
}
}
break;
}
}
OLED_Display();
}
/*****************************************************************************
* @name :void GUI_ShowFont32(uint8_t x,uint8_t y,uint8_t *s,uint8_t mode)
* @date :2018-08-27
* @function :Display a single 32x32 Chinese character
* @parameters :x:the bebinning x coordinate of the Chinese character
y:the bebinning y coordinate of the Chinese character
s:the start address of the Chinese character
mode:0-white background and black character
1-black background and white character
* @retvalue :None
******************************************************************************/
void GUI_ShowFont32(uint8_t x,uint8_t y,uint8_t *s,uint8_t mode)
{
uint8_t i,j,k,tmp;
uint16_t num;
num = sizeof(cfont32)/sizeof(typFNT_GB32);
for(i=0;i<num;i++)
{
if((cfont32[i].Index[0]==*s)&&(cfont32[i].Index[1]==*(s+1)))
{
for(j=0;j<128;j++)
{
if(mode)
{
tmp = cfont32[i].Msk[j];
}
else
{
tmp = ~(cfont32[i].Msk[j]);
}
for(k=0;k<8;k++)
{
if(tmp&(0x80>>k))
{
OLED_Set_Pixel(x+(j%4)*8+k, y+j/4,1);
}
else
{
OLED_Set_Pixel(x+(j%4)*8+k, y+j/4,0);
}
}
}
break;
}
}
OLED_Display();
}
/*****************************************************************************
* @name :void GUI_ShowCHinese(uint8_t x,uint8_t y,uint8_t hsize,uint8_t *str,uint8_t mode)
* @date :2018-08-27
* @function :Display Chinese strings
* @parameters :x:the bebinning x coordinate of the Chinese strings
y:the bebinning y coordinate of the Chinese strings
size:the size of Chinese strings
str:the start address of the Chinese strings
mode:0-white background and black character
1-black background and white character
* @retvalue :None
******************************************************************************/
void GUI_ShowCHinese(uint8_t x,uint8_t y,uint8_t hsize,uint8_t *str,uint8_t mode)
{
while(*str!='\0')
{
if(hsize == 16)
{
GUI_ShowFont16(x,y,str,mode);
}
else if(hsize == 24)
{
GUI_ShowFont24(x,y,str,mode);
}
else if(hsize == 32)
{
GUI_ShowFont32(x,y,str,mode);
}
else
{
return;
}
x+=hsize;
if(x>WIDTH-hsize)
{
x=0;
y+=hsize;
}
str+=2;
}
}
/*****************************************************************************
* @name :void GUI_DrawBMP(uint8_t x,uint8_t y,uint8_t width, uint8_t height, uint8_t BMP[], uint8_t mode)
* @date :2018-08-27
* @function :Display a BMP monochromatic picture
* @parameters :x:the bebinning x coordinate of the BMP monochromatic picture
y:the bebinning y coordinate of the BMP monochromatic picture
width:the width of BMP monochromatic picture
height:the height of BMP monochromatic picture
BMP:the start address of BMP monochromatic picture array
mode:0-white background and black character
1-black background and white character
* @retvalue :None
******************************************************************************/
void GUI_DrawBMP(uint8_t x,uint8_t y,uint8_t width, uint8_t height, uint8_t BMP[], uint8_t mode)
{
uint8_t i,j,k;
uint8_t tmp;
for(i=0;i<height;i++)
{
for(j=0;j<(width+7)/8;j++)
{
if(mode)
{
tmp = BMP[i*((width+7)/8)+j];
}
else
{
tmp = ~BMP[i*((width+7)/8)+j];
}
for(k=0;k<8;k++)
{
if(tmp&(0x80>>k))
{
OLED_Set_Pixel(x+j*8+k, y+i,1);
}
else
{
OLED_Set_Pixel(x+j*8+k, y+i,0);
}
}
}
}
OLED_Display();
}
3. AHT20程序(可参考IIC读取AHT20那篇博客)
AHT20.c
#include "AHT20-21_DEMO_V1_3.h"
#include "gpio.h"
#include "i2c.h"
void Delay_N10us(uint32_t t)//????
{
uint32_t k;
while(t--)
{
for (k = 0; k < 2; k++);//110
}
}
void SensorDelay_us(uint32_t t)//????
{
for(t = t-2; t>0; t--)
{
Delay_N10us(1);
}
}
void Delay_4us(void) //????
{
Delay_N10us(1);
Delay_N10us(1);
Delay_N10us(1);
Delay_N10us(1);
}
void Delay_5us(void) //????
{
Delay_N10us(1);
Delay_N10us(1);
Delay_N10us(1);
Delay_N10us(1);
Delay_N10us(1);
}
void Delay_1ms(uint32_t t) //????
{
while(t--)
{
SensorDelay_us(1000);//??1ms
}
}
//void AHT20_Clock_Init(void) //????
//{
// RCC_APB2PeriphClockCmd(CC_APB2Periph_GPIOB,ENABLE);
//}
void SDA_Pin_Output_High(void) //?PB7????? , ???????, PB7??I2C?SDA
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;//????
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB,& GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,GPIO_PIN_SET);
}
void SDA_Pin_Output_Low(void) //?P7????? ???????
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;//????
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB,& GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,GPIO_PIN_RESET);
}
void SDA_Pin_IN_FLOATING(void) //SDA???????
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;//??
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init( GPIOB,&GPIO_InitStruct);
}
void SCL_Pin_Output_High(void) //SCL?????,P14??I2C?SCL
{
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,GPIO_PIN_SET);
}
void SCL_Pin_Output_Low(void) //SCL?????
{
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,GPIO_PIN_RESET);
}
void Init_I2C_Sensor_Port(void) //???I2C??,??????
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;//????
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB,& GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,GPIO_PIN_SET);
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;//????
GPIO_InitStruct.Pin = GPIO_PIN_6;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB,& GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,GPIO_PIN_SET);
}
void I2C_Start(void) //I2C????START??
{
SDA_Pin_Output_High();
SensorDelay_us(8);
SCL_Pin_Output_High();
SensorDelay_us(8);
SDA_Pin_Output_Low();
SensorDelay_us(8);
SCL_Pin_Output_Low();
SensorDelay_us(8);
}
void AHT20_WR_Byte(uint8_t Byte) //?AHT20?????
{
uint8_t Data,N,i;
Data=Byte;
i = 0x80;
for(N=0;N<8;N++)
{
SCL_Pin_Output_Low();
Delay_4us();
if(i&Data)
{
SDA_Pin_Output_High();
}
else
{
SDA_Pin_Output_Low();
}
SCL_Pin_Output_High();
Delay_4us();
Data <<= 1;
}
SCL_Pin_Output_Low();
SensorDelay_us(8);
SDA_Pin_IN_FLOATING();
SensorDelay_us(8);
}
uint8_t AHT20_RD_Byte(void)//?AHT20??????
{
uint8_t Byte,i,a;
Byte = 0;
SCL_Pin_Output_Low();
SDA_Pin_IN_FLOATING();
SensorDelay_us(8);
for(i=0;i<8;i++)
{
SCL_Pin_Output_High();
Delay_5us();
a=0;
//if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)) a=1;
if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_7)) a=1;
Byte = (Byte<<1)|a;
//SCL_Pin_Output_Low();
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,GPIO_PIN_RESET);
Delay_5us();
}
SDA_Pin_IN_FLOATING();
SensorDelay_us(8);
return Byte;
}
uint8_t Receive_ACK(void) //?AHT20?????ACK
{
uint16_t CNT;
CNT = 0;
SCL_Pin_Output_Low();
SDA_Pin_IN_FLOATING();
SensorDelay_us(8);
SCL_Pin_Output_High();
SensorDelay_us(8);
while((HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_7)) && CNT < 100)
CNT++;
if(CNT == 100)
{
return 0;
}
SCL_Pin_Output_Low();
SensorDelay_us(8);
return 1;
}
void Send_ACK(void) //????ACK??
{
SCL_Pin_Output_Low();
SensorDelay_us(8);
SDA_Pin_Output_Low();
SensorDelay_us(8);
SCL_Pin_Output_High();
SensorDelay_us(8);
SCL_Pin_Output_Low();
SensorDelay_us(8);
SDA_Pin_IN_FLOATING();
SensorDelay_us(8);
}
void Send_NOT_ACK(void) //?????ACK
{
SCL_Pin_Output_Low();
SensorDelay_us(8);
SDA_Pin_Output_High();
SensorDelay_us(8);
SCL_Pin_Output_High();
SensorDelay_us(8);
SCL_Pin_Output_Low();
SensorDelay_us(8);
SDA_Pin_Output_Low();
SensorDelay_us(8);
}
void Stop_I2C(void) //??????
{
SDA_Pin_Output_Low();
SensorDelay_us(8);
SCL_Pin_Output_High();
SensorDelay_us(8);
SDA_Pin_Output_High();
SensorDelay_us(8);
}
uint8_t AHT20_Read_Status(void)//??AHT20??????
{
uint8_t Byte_first;
I2C_Start();
AHT20_WR_Byte(0x71);
Receive_ACK();
Byte_first = AHT20_RD_Byte();
Send_NOT_ACK();
Stop_I2C();
return Byte_first;
}
uint8_t AHT20_Read_Cal_Enable(void) //??cal enable??????
{
uint8_t val = 0;//ret = 0,
val = AHT20_Read_Status();
if((val & 0x68)==0x08)
return 1;
else return 0;
}
void AHT20_SendAC(void) //?AHT20??AC??
{
I2C_Start();
AHT20_WR_Byte(0x70);
Receive_ACK();
AHT20_WR_Byte(0xac);//0xAC????
Receive_ACK();
AHT20_WR_Byte(0x33);
Receive_ACK();
AHT20_WR_Byte(0x00);
Receive_ACK();
Stop_I2C();
}
//CRC????:CRC8/MAXIM
//???:X8+X5+X4+1
//Poly:0011 0001 0x31
//????????? 1000 1100 0x8c
//C????:
uint8_t Calc_CRC8(uint8_t *message,uint8_t Num)
{
uint8_t i;
uint8_t byte;
uint8_t crc=0xFF;
for(byte=0; byte<Num; byte++)
{
crc^=(message[byte]);
for(i=8;i>0;--i)
{
if(crc&0x80) crc=(crc<<1)^0x31;
else crc=(crc<<1);
}
}
return crc;
}
void AHT20_Read_CTdata(uint32_t *ct) //??CRC??,????AHT20????????
{
volatile uint8_t Byte_1th=0;
volatile uint8_t Byte_2th=0;
volatile uint8_t Byte_3th=0;
volatile uint8_t Byte_4th=0;
volatile uint8_t Byte_5th=0;
volatile uint8_t Byte_6th=0;
uint32_t RetuData = 0;
uint16_t cnt = 0;
AHT20_SendAC();//?AHT10??AC??
Delay_1ms(80);//??80ms??
cnt = 0;
while(((AHT20_Read_Status()&0x80)==0x80))//????bit[7]?0,???????,??1,?????
{
SensorDelay_us(1508);
if(cnt++>=100)
{
break;
}
}
I2C_Start();
AHT20_WR_Byte(0x71);
Receive_ACK();
Byte_1th = AHT20_RD_Byte();//???,??????0x98,??????,bit[7]?1;???0x1C,??0x0C,??0x08???????,bit[7]?0
Send_ACK();
Byte_2th = AHT20_RD_Byte();//??
Send_ACK();
Byte_3th = AHT20_RD_Byte();//??
Send_ACK();
Byte_4th = AHT20_RD_Byte();//??/??
Send_ACK();
Byte_5th = AHT20_RD_Byte();//??
Send_ACK();
Byte_6th = AHT20_RD_Byte();//??
Send_NOT_ACK();
Stop_I2C();
RetuData = (RetuData|Byte_2th)<<8;
RetuData = (RetuData|Byte_3th)<<8;
RetuData = (RetuData|Byte_4th);
RetuData =RetuData >>4;
ct[0] = RetuData;//??
RetuData = 0;
RetuData = (RetuData|Byte_4th)<<8;
RetuData = (RetuData|Byte_5th)<<8;
RetuData = (RetuData|Byte_6th);
RetuData = RetuData&0xfffff;
ct[1] =RetuData; //??
}
void AHT20_Read_CTdata_crc(uint32_t *ct) //CRC???,??AHT20????????
{
volatile uint8_t Byte_1th=0;
volatile uint8_t Byte_2th=0;
volatile uint8_t Byte_3th=0;
volatile uint8_t Byte_4th=0;
volatile uint8_t Byte_5th=0;
volatile uint8_t Byte_6th=0;
volatile uint8_t Byte_7th=0;
uint32_t RetuData = 0;
uint16_t cnt = 0;
// uint8_t CRCDATA=0;
uint8_t CTDATA[6]={0};//??CRC????
AHT20_SendAC();//?AHT10??AC??
Delay_1ms(80);//??80ms??
cnt = 0;
while(((AHT20_Read_Status()&0x80)==0x80))//????bit[7]?0,???????,??1,?????
{
SensorDelay_us(1508);
if(cnt++>=100)
{
break;
}
}
I2C_Start();
AHT20_WR_Byte(0x71);
Receive_ACK();
CTDATA[0]=Byte_1th = AHT20_RD_Byte();//???,??????0x98,??????,bit[7]?1;???0x1C,??0x0C,??0x08???????,bit[7]?0
Send_ACK();
CTDATA[1]=Byte_2th = AHT20_RD_Byte();//??
Send_ACK();
CTDATA[2]=Byte_3th = AHT20_RD_Byte();//??
Send_ACK();
CTDATA[3]=Byte_4th = AHT20_RD_Byte();//??/??
Send_ACK();
CTDATA[4]=Byte_5th = AHT20_RD_Byte();//??
Send_ACK();
CTDATA[5]=Byte_6th = AHT20_RD_Byte();//??
Send_ACK();
Byte_7th = AHT20_RD_Byte();//CRC??
Send_NOT_ACK(); //??: ?????NAK
Stop_I2C();
if(Calc_CRC8(CTDATA,6)==Byte_7th)
{
RetuData = (RetuData|Byte_2th)<<8;
RetuData = (RetuData|Byte_3th)<<8;
RetuData = (RetuData|Byte_4th);
RetuData =RetuData >>4;
ct[0] = RetuData;//??
RetuData = 0;
RetuData = (RetuData|Byte_4th)<<8;
RetuData = (RetuData|Byte_5th)<<8;
RetuData = (RetuData|Byte_6th);
RetuData = RetuData&0xfffff;
ct[1] =RetuData; //??
}
else
{
ct[0]=0x00;
ct[1]=0x00;//???????,????????????
}//CRC??
}
void AHT20_Init(void) //???AHT20
{
Init_I2C_Sensor_Port();
I2C_Start();
AHT20_WR_Byte(0x70);
Receive_ACK();
AHT20_WR_Byte(0xa8);//0xA8??NOR????
Receive_ACK();
AHT20_WR_Byte(0x00);
Receive_ACK();
AHT20_WR_Byte(0x00);
Receive_ACK();
Stop_I2C();
Delay_1ms(10);//??10ms??
I2C_Start();
AHT20_WR_Byte(0x70);
Receive_ACK();
AHT20_WR_Byte(0xbe);//0xBE?????,AHT20???????0xBE, AHT10???????0xE1
Receive_ACK();
AHT20_WR_Byte(0x08);//?????bit[3]?1,?????
Receive_ACK();
AHT20_WR_Byte(0x00);
Receive_ACK();
Stop_I2C();
Delay_1ms(10);//??10ms??
}
void JH_Reset_REG(uint8_t addr)
{
uint8_t Byte_first,Byte_second,Byte_third;
I2C_Start();
AHT20_WR_Byte(0x70);//???0x70
Receive_ACK();
AHT20_WR_Byte(addr);
Receive_ACK();
AHT20_WR_Byte(0x00);
Receive_ACK();
AHT20_WR_Byte(0x00);
Receive_ACK();
Stop_I2C();
Delay_1ms(5);//??5ms??
I2C_Start();
AHT20_WR_Byte(0x71);//
Receive_ACK();
Byte_first = AHT20_RD_Byte();
Send_ACK();
Byte_second = AHT20_RD_Byte();
Send_ACK();
Byte_third = AHT20_RD_Byte();
Send_NOT_ACK();
Stop_I2C();
Delay_1ms(10);//??10ms??
I2C_Start();
AHT20_WR_Byte(0x70);///
Receive_ACK();
AHT20_WR_Byte(0xB0|addr);//?????
Receive_ACK();
AHT20_WR_Byte(Byte_second);
Receive_ACK();
AHT20_WR_Byte(Byte_third);
Receive_ACK();
Stop_I2C();
Byte_second=0x00;
Byte_third =0x00;
}
void AHT20_Start_Init(void)
{
JH_Reset_REG(0x1b);
JH_Reset_REG(0x1c);
JH_Reset_REG(0x1e);
}
AHT20.h
#ifndef _AHT20_DEMO_
#define _AHT20_DEMO_
#include "main.h"
void Delay_N10us(uint32_t t);//????
void SensorDelay_us(uint32_t t);//????
void Delay_4us(void); //????
void Delay_5us(void); //????
void Delay_1ms(uint32_t t);
void AHT20_Clock_Init(void); //????
void SDA_Pin_Output_High(void) ; //?PB15????? , ???????, PB15??I2C?SDA
void SDA_Pin_Output_Low(void); //?P15????? ???????
void SDA_Pin_IN_FLOATING(void); //SDA???????
void SCL_Pin_Output_High(void); //SCL?????,P14??I2C?SCL
void SCL_Pin_Output_Low(void); //SCL?????
void Init_I2C_Sensor_Port(void); //???I2C??,??????
void I2C_Start(void); //I2C????START??
void AHT20_WR_Byte(uint8_t Byte); //?AHT20?????
uint8_t AHT20_RD_Byte(void);//?AHT20??????
uint8_t Receive_ACK(void); //?AHT20?????ACK
void Send_ACK(void) ; //????ACK??
void Send_NOT_ACK(void); //?????ACK
void Stop_I2C(void); //??????
uint8_t AHT20_Read_Status(void);//??AHT20??????
uint8_t AHT20_Read_Cal_Enable(void); //??cal enable??????
void AHT20_SendAC(void); //?AHT20??AC??
uint8_t Calc_CRC8(uint8_t *message,uint8_t Num);
void AHT20_Read_CTdata(uint32_t *ct); //??CRC??,????AHT20????????
void AHT20_Read_CTdata_crc(uint32_t *ct); //CRC???,??AHT20????????
void AHT20_Init(void); //???AHT20
void JH_Reset_REG(uint8_t addr);///?????
void AHT20_Start_Init(void);///?????????????
#endif
4. 主函数
1. printf重定义
int fputc(int ch,FILE *f){
uint8_t temp[1]={ch};
HAL_UART_Transmit(&huart1,temp,1,2);
return ch;
}
2. 显示时间函数
void Show_Time(uint16_t Year,uint16_t Month,uint8_t Day,uint8_t Hour,uint8_t Minute,uint8_t Second)
{
GUI_ShowCHinese(0,0,16,"日期",1);
GUI_ShowNum(48,0,Year,4,16,1);
GUI_ShowString(80,0,(uint8_t *)"/",16,1);
GUI_ShowNum(88,0,Month,2,16,1);
GUI_ShowString(104,0,(uint8_t *)"/",16,1);
GUI_ShowNum(112,0,Day,2,16,1);
GUI_ShowCHinese(0,16,16,"时间",1);
GUI_ShowNum(48,16,Hour,2,16,1);
GUI_ShowString(64,16,(uint8_t *)":",16,1);
GUI_ShowNum(72,16,Minute,2,16,1);
GUI_ShowString(88,16,(uint8_t *)":",16,1);
GUI_ShowNum(96,16,Second,2,16,1);
}
3. 显示温湿度函数
void Show_Data(uint8_t c,uint8_t t)
{
GUI_ShowCHinese(0,48,16,"湿度",1);
GUI_ShowString(32,48,(uint8_t *)":",16,1);
GUI_ShowNum(60,48,c,2,16,1);
GUI_ShowString(84,48,(uint8_t *)"%",16,1);
GUI_ShowCHinese(0,32,16,"温度",1);
GUI_ShowString(32,32,(uint8_t *)":",16,1);
GUI_ShowNum(60,32,t,2,16,1);
GUI_ShowCHinese(80,32,16,"¡æ",1);
}
4. 逻辑函数
AHT20_Read_CTdata(CT_data);
c1 = CT_data[0]*100/1024/1024;
t1 = CT_data[1]*200/1024/1024-50;
HAL_RTC_GetTime(&hrtc, &GetTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &GetData, RTC_FORMAT_BIN);
Year=GetData.Year+2000;
Month=GetData.Month;
Day=GetData.Date;
Hour=GetTime.Hours;
Minute=GetTime.Minutes;
Second=GetTime.Seconds;
printf("日期:%d/%d/%d\r\n",Year,Month,Day); //日期
printf("时间:%d:%d\r\n",Hour,Minute,Second); //时间
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");
}
printf("\r\n"); //´®¿Ú´òӡʱ¼ä
Show_Time(Year,Month,Day,Hour,Minute,Second);
Show_Data(c1,t1);
HAL_Delay(1000);
六、运行效果
任务一:
任务二:
七、总结与参考
此实验通过设定初始值的方法,从STM32的RTC中读取到了我们设置的时钟,且能够很清楚的看到1s为周期,通过手指触碰温湿度传感器,温度和湿度都在上升。
参考链接:
STM32日历读取,设置和输出_stm32 日历_机智的橙子的博客-CSDN博客