Article Directory
1. Task objectives
Understand the principle of OLED screen display and Chinese character dot matrix coding, and use the SPI or IIC interface of STM32F103 to realize the following functions:
-
Show your student number and name;
-
Display the temperature and humidity of AHT20;
-
Swipe up and down or left and right to display long characters.
2. Material preparation
hardware:
- STM32F103C8T6 Minimal Board
- AHT20 temperature and humidity sensor
- ST-LINK emulator
- 4-pin OLED screen using I2C communication protocol
- Breadboard
- dupont line
software:
- KEIL 5
- font software
3. Use of AHT20 temperature and humidity sensor
You can read the article I wrote last time: STM32 I2C protocol reads temperature and humidity sensors
Fourth, the use of OLED
1. Hardware
What I use here is a 4-pin OLED screen, and the communication protocol is I2C.
2. Typeface
Chinese character encoding principle
-
1. All Chinese characters or English are based on the following principle:
from left to right, every 8 dots occupy a byte, and the last less than 8 bytes occupy a byte, and they are arranged from the highest bit to the lowest bit. -
2. Description of the generated font library: (taking 12×12 as an example)
the number of bytes occupied by a Chinese character: 12÷8=1····4 means 2×12=24 bytes.
The coding sequence is A0A0→A0FEA1A0→A2FE in order.
Take the "I" in the 12×12 font library as an example: the code of "I" is CED2, so there are D2H-A0H=32H Chinese characters in the CEH-AOH=2EH area. So the starting position of the 12×12 font is [{FE-A0}*2EH+32H]*24=104976. The first 24 bytes are my dot matrix model.
Take font
Here I use PCtoLCD2002 font-taking software. Open the software and set it as shown in the figure below. Input the
font you want to get, click Generate Font, and then copy the code of the generated font.
3. Engineering
Here you can use the last AHT20 project, copy one, and add OLED related files.
Or you can download my code directly. (Note that it is I2C communication, 4-pin screen)
There are already numbers and related symbols in the original font library. Here, the 10x16 pixel font library is extracted by software just now, and needs to be defined separately from the digital font library, because The defined pixels are not the same.
In addition, there is no way to output Chinese in the built-in function, we need to define it ourselves.
/**
* @brief OLED初始化
* @param Line 起始行位置
* @param Column 起始列位置
* @retval 无
*/
void OLED_ShowCHINESE(uint8_t Line, uint8_t Column, uint8_t Num)
{
uint8_t i;
uint8_t wide = 20;//字宽
OLED_SetCursor(( Line - 1 ) * 2, ( Column - 1 )* wide); //参数1:把光标设置在第几页. 参数2:把光标设置在第几列
for (i = 0; i < wide; i++)
{
OLED_WriteData(OLED_F10x16[Num][i]); //显示上半部分内容
}
OLED_SetCursor(( Line - 1 ) * 2 + 1,( Column - 1) * wide);
for (i = 0; i < wide ; i++)
{
OLED_WriteData(OLED_F10x16[Num][i+wide]); //显示下半部分内容
}
}
After that, to achieve the scrolling effect, the code and function are as follows:
OLED_WriteCommand(0x2E); //关闭滚动
OLED_WriteCommand(0x26); //向右滚动,27则向左
OLED_WriteCommand(0x00); //虚拟字节
OLED_WriteCommand(0x00); //起始页 这里为0
OLED_WriteCommand(0x07); //滚动速度
OLED_WriteCommand(0x03); //终止页 这里为3,也就是之后的姓名,学号
OLED_WriteCommand(0x00); //虚拟字节
OLED_WriteCommand(0xFF); //虚拟字节
OLED_WriteCommand(0x2F); //开启滚动
Then the main function main.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "AHT20-21_DEMO_V1_3.h"
#include <stdio.h>
int main(void)
{
Init_I2C_Sensor_Port();//初始化SDA,SCL的IO口的函数
uint32_t CT_data[2];
volatile int c1,t1;
Delay_1ms(500);
OLED_Init();//初始化OLED
if((AHT20_Read_Status()&0x18)!=0x18)
{
AHT20_Start_Init(); //重新初始化寄存器
Delay_1ms(10);
}
OLED_ShowCHINESE(1,1,0); //第1行第1列调用字模库第0个字 陈
OLED_ShowCHINESE(1,2,1); //第1行第2列调用字模库第1个字 爽
OLED_ShowString(2,1,"632007060523"); //第二行第一列显示字符串
OLED_ShowCHINESE(3,1,3);//第3行第1列调用字模库第3个字 湿
OLED_ShowCHINESE(3,2,4);//第3行第2列调用字模库第4个字 度
OLED_ShowString(3,6,":");
OLED_ShowCHINESE(4,1,2);//第4行第2列调用字模库第2个字 温
OLED_ShowCHINESE(4,2,4);//第3行第2列调用字模库第4个字 度
OLED_ShowString(4,6,":");
while(1)
{
OLED_WriteCommand(0x2E); //关闭滚动
OLED_WriteCommand(0x26); //向右滚动,27则向左
OLED_WriteCommand(0x00); //虚拟字节
OLED_WriteCommand(0x00); //起始页 这里为0
OLED_WriteCommand(0x07); //滚动速度
OLED_WriteCommand(0x03); //终止页 这里为3,也就是之后的姓名,学号
OLED_WriteCommand(0x00); //虚拟字节
OLED_WriteCommand(0xFF); //虚拟字节
OLED_WriteCommand(0x2F); //开启滚动
//AHT20_Read_CTdata(CT_data);
AHT20_Read_CTdata_crc(CT_data); //CRC校验
while(CT_data[0]==0x00&&CT_data[1]==0x00)
{
AHT20_Read_CTdata_crc(CT_data);//CRC校验后,读取数据
}
c1 = CT_data[0]*100*10/1024/1024;
t1 = CT_data[1]*200*10/1024/1024-500;
下一步客户处理数据,我们这里用两个字符串来表示计算得到的值
char str1[5];
char str2[5];
sprintf(str1,"%.2f",c1/10.0);
sprintf(str2,"%.2f",t1/10.0);
OLED_ShowString(3,7,str1);//把浮点数转为字符串显示在屏上
OLED_ShowString(4,7,str2);
Delay_1ms(2000);
}
}
4. Circuit connection
B1 connects to pin 2, B0 connects to pin 4, and the others can be connected according to the diagram. There are several redundant wires in Fig. , yellow, green) are originally connected to the CH340 module, so you don’t need to connect it, and this experiment does not need it.
Image source: JiangTech University Self-Chemical Association
5. Experimental effect
It can be seen that the student number and name are output, the temperature and humidity are displayed, and when the hand is close to the sensor, the temperature and humidity change, and the scrolling effect is realized.
V. Summary
This experiment mainly uses the standard library to import the AHT20 module and OLED module, and realizes the scrolling display of temperature and humidity data and student number and name. The use of the OLED screen is more convenient, and it is also very good for debugging.
6. References
The font reading and display of dot matrix Chinese characters
is based on STM32's 0.96OLED basic display learning, and long characters are displayed by sliding up and down or left and right (using hardware refresh mode), OLED display variable value operation detailed analysis