The basic attempt of OLED screen display based on stm32 and the basic operation of Chinese character dot matrix encoding
Chinese character dot matrix coding
1. Here, use the software PCtoLCD to automatically generate 16*16 Chinese character fonts.
2. Set the method of generating fonts.
3. Generate fonts.
For example:
Experimental engineering
1. The display project here refers to the project provided by the OLED Taobao store.
2. Project function display. You
only need to use the functions you need in this test, and the others can be commented out
Show your name and student ID
Engineering rewrite
1. For the SPI project downloaded here, find the variables in the project to rewrite your display requirements.
2. Text modification:
Find the parameter you need to change under the
function. Right-click the function name and go to the function definition
Search all the way, find the parameter modification place, and write the font you want to display the text.
3. Student ID display:
0-9 number definition has been written in the project, only need to modify the function parameters
4. Compile and burn Program to view experiment results
Experimental result
Display the temperature and humidity of AHT20
Engineering rewrite
1. Add AHT20 temperature and humidity reading function
2. AHT20 part of the code:
#include "delay.h"
#include "sys.h"
#include "oled.h"
#include "bmp.h"
#include "stdlib.h"
#include "bsp_i2c.h"
#include "ATH20.h"
#include "stm32f10x.h"
#include "stm32f10x_usart.h"
#include "misc.h"
void GPIO_Configuration(void);
void RCC_Configuration(void);
GPIO_InitTypeDef GPIO_InitStructure;
int main(void)
{
int hcr=1;int i1=6,i2=26,i3=46,i4=66,i5=86,i6=106, i7=126,i8=146,i9=166;
uint8_t ret = 0;
uint8_t LED_Stat = 0;
float P,T,ALT;
uint32_t CT_data[2];
int c1,t1;
delay_init(); //延时函数初始化
NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级 LED_Init(); //LED端口初始化
OLED_Init(); //初始化OLED
OLED_Clear(0) ;
RCC_Configuration(); //设置系统时钟
GPIO_Configuration(); //IO口设
I2C_Bus_Init();
ret = ATH20_Init();
if(ret == 0)
while(1)
{
OLED_Clear(0);
OLED_ShowString(0,11,"631807030616",16);
delay_ms(2500);
printf("ATH20传感器初始化错误\n");
}
while(1)
{
for(hcr=0;hcr<=166;hcr+=20)
{
while(ATH20_Read_Cal_Enable() == 0)
{
ATH20_Init();//如果为0再使能一次
delay_ms(25);
}
ATH20_Read_CTdata(CT_data); //读取温度和湿度
c1 = CT_data[0] * 1000 / 1024 / 1024; //计算得到湿度值(放大了10倍,如果c1=523,表示现在湿度为52.3%)
t1 = CT_data[1] * 200 *10 / 1024 / 1024 - 500;//计算得到温度值(放大了10倍,如果t1=245,表示现在温度为24.5℃)
OLED_Clear(0);
if((i1-hcr)>=0)
OLED_ShowCHinese(6-hcr,0,9);//全
if((i2-hcr)>=0)
OLED_ShowCHinese(26-hcr,0,10);//欢
if((i3-hcr)>=0)
OLED_ShowCHinese(46-hcr,0,11);
if((i4-hcr)>=0)
OLED_ShowCHinese(66-hcr,0,12);//子
if((i5-hcr)>=0)
OLED_ShowCHinese(86-hcr,0,13);//技
if((i6-hcr)>=0)
OLED_ShowCHinese(106-hcr,0,14);//术
if((i7-hcr)>=0&&(i7-hcr)<=106)
OLED_ShowCHinese(126-hcr,0,15);//动
if((i8-hcr)>=0&&(i8-hcr)<=106)
OLED_ShowCHinese(146-hcr,0,16);//电
if((i9-hcr)>=0&&(i9-hcr)<=106)
OLED_ShowCHinese(166-hcr,0,17);//子
OLED_ShowCHinese(6,3,18);//温
OLED_ShowCHinese(26,3,20);//度
OLED_ShowCHinese(46,3,21);//:
OLED_ShowNum(66,3,c1,1,16);
OLED_ShowCHinese(6,6,19);//湿
OLED_ShowCHinese(26,6,20);//度
OLED_ShowCHinese(46,6,21);//:
OLED_ShowNum(66,6,t1,3,16);
delay_ms(2500);
delay_ms(2500);//每隔两秒读一次数
if(LED_Stat == 0)
{
LED_Stat = 1;
GPIO_ResetBits(GPIOC, GPIO_Pin_2);
// GPIO_ResetBits(GPIOC, GPIO_Pin_1);
}
else
{
LED_Stat = 0;
GPIO_SetBits(GPIOC, GPIO_Pin_2);
// GPIO_SetBits(GPIOC, GPIO_Pin_1);
}
}
}
}
void RCC_Configuration(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
| RCC_APB2Periph_GPIOD| RCC_APB2Periph_GPIOE , ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7; //状态LED1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //通用推挽输出模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //输出模式最大速度50MHz
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
3. Compile the burning program to view the experimental results
Experimental result
Swipe to show your name and student ID
Engineering rewrite
1. Add a function to control scrolling
2. Part of the scrolling code:
OLED_WR_Byte(0x2e,OLED_CMD);//关滚动
OLED_WR_Byte(0x2a,OLED_CMD);//29向右,2a向左(垂直水平滚动)
OLED_WR_Byte(0x00,OLED_CMD);//A:空字节
OLED_WR_Byte(0x00,OLED_CMD);//B:水平起始页
OLED_WR_Byte(0x00,OLED_CMD);//C:水平滚动速度
OLED_WR_Byte(0x07,OLED_CMD);//D:水平结束页
OLED_WR_Byte(0x01,OLED_CMD);//E:每次垂直滚动位移
OLED_WR_Byte(0x2f,OLED_CMD);//开滚动
3. If the output characters are long, consider the horizontal and vertical coordinates to avoid overlapping output characters.
4. Modify the fonts you want to output as above.
5. Compile and burn the program to view the experimental results.