IoT Lora module from entry to proficiency (6) OLED display

I. Introduction

        After obtaining the data, we often need to display it on the OLED display. In this article, we need to use the code of the previous article (acquisition of light and temperature and humidity data), and continue to complete the content of this article on the basis of it.

        Base code:

#include <string.h>
#include "board.h"
#include "hal_key.h"
#include "tim-board.h"
#include "timer_handles.h"
#include "led_light.h"
#include "adc_reader.h"
#include <math.h>          //library 
#include <stdio.h>
#include "stm32l1xx_hal.h" // controller register definitions 
#include "sht1x.h"         // controller register definitions 
#include "sht3x.h"

void Init() {
    BoardInitMcu();
    BoardInitPeriph();
    keys_init();//按键初始化
    setTimer2Callback(Time2Handler);
    Tim2McuInit(1);//定时器初始化,设置定时中断1ms中断一次
}

float temp=0,hum=0,light=0;
int main( void )
{
    Init();
		ADCS_Init();//初始化ADC
		hal_temHumInit();//初始化温湿度模块
    while( 1 )
    {
				HAL_Delay(1000);//延时1000ms
				AdcScanChannel();//更新通道值
				light=((5.0/2.0)*AdcReadCh0())*100.0;//获取光照并通过公式计算
				call_sht11(&temp,&hum);//获取温湿度
    }
}

2. Code implementation

        In the routine, the hal_oled.c file is provided for us, in which we often use the following methods:

void OLED_Display_On(void);//开启展示
void OLED_Display_Off(void);//关闭展示						   		    
void OLED_Init(void);//初始化OLED   							   		    
void OLED_Clear(void);//清空显示内容
void OLED_DrawPoint(uint8_t x,uint8_t y,uint8_t t);//OLED屏幕画点
void OLED_ShowString(uint8_t x,uint8_t y, uint8_t *p);//OLED显示字符串
void OLED_ShowCHinese(uint8_t x,uint8_t y,uint8_t no);//显示汉字
void OLED_DrawBMP(unsigned char x, unsigned char y,unsigned char width, unsigned char hight,unsigned char BMP[]);//显示图片

        The OLED screen is 128*64, which is internally divided into 8 lines and 4 pages, that is, 0, 2, 4, 6, so the parameters of uint8_t y are 0~7, and a character with a height of 16 needs two lines, that is, one page .

        Among them, the third parameter of OLED_ShowCHinese() is derived from the HZK array in oledfont.h, and you can also overwrite the contents of the array by taking the modulo by yourself.

        

         Next, we will use the fonts of temperature and humidity, and find their corresponding subscripts from the array.

        Code example:

        The following code implements the functions of Chinese characters, strings, data, and pictures for your reference.

#include <string.h>
#include "board.h"
#include "hal_key.h"
#include "tim-board.h"
#include "timer_handles.h"
#include "led_light.h"
#include "adc_reader.h"
#include <math.h>          //library 
#include <stdio.h>
#include "stm32l1xx_hal.h" // controller register definitions 
#include "sht1x.h"         // controller register definitions 
#include "sht3x.h"

void Init() {
    BoardInitMcu();
    BoardInitPeriph();
    keys_init();//按键初始化
    setTimer2Callback(Time2Handler);
    Tim2McuInit(1);//定时器初始化,设置定时中断1ms中断一次
}

unsigned char bmp[]={//图像取模后的数组
0x00,0x00,0x00,0x00,0x80,0xC0,0xE0,0xF0,0xF8,0xFC,0xFE,0xFF,0xFE,0xFC,0xF8,0xF0,
0xE0,0xC0,0x80,0x00,0x00,0x00,0x00,0x08,0x1C,0x3E,0x7F,0x7F,0xBF,0xDF,0x6F,0xBF,
0xFF,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x3E,0x1C,0x08,0x10,0x18,
0x6C,0xB6,0xDB,0x6D,0xB6,0xDB,0x6D,0xB6,0xDB,0x6D,0xB7,0xDB,0x6F,0xB7,0xDB,0x6D,
0xB6,0xDA,0x6C,0x30,0x10,0x00,0x00,0x00,0x00,0x00,0x03,0x05,0x06,0x1B,0x2D,0x36,
0xDB,0x6D,0x36,0x1B,0x0D,0x06,0x03,0x01,0x00,0x00,0x00,0x00
};
float temp=0,hum=0,light=0;
uint8_t data[20];
int main( void )
{
    Init();
		ADCS_Init();//初始化ADC
		hal_temHumInit();//初始化温湿度模块
		OLED_Init();//初始化OLED屏幕
		OLED_Display_On();//开启展示
    while( 1 )
    {
				HAL_Delay(1000);//延时1000ms
				AdcScanChannel();//更新通道值
				light=((5.0/2.0)*AdcReadCh0())*100.0;//获取光照并通过公式计算
				call_sht11(&temp,&hum);//获取温湿度
				OLED_Clear();
				OLED_ShowCHinese(0,0,24);//湿 第二个参数代表从0行开始 将字写在0-1行
				OLED_ShowCHinese(16,0,23);//度 第一个参数代表左边的像素距离 
				//要把第一个字16*16的距离空出来 否则会覆盖第一个字 一个汉字的大小是16*16
				memset(data,'\0',20);//清空数组
				sprintf((char *)data,"%.2f %%RH",hum);//将内容写到数组中
				OLED_ShowString(16*2,0,data);//显示数组内的内容
				//16*2代表左边空出两个字的距离 
				OLED_ShowString(0,2,(uint8_t *)"xixi_cainiao");//直接显示字符串 xixi_cainiao
				//第二个参数为2代表显示在2-3行
				OLED_DrawBMP(0,4,32,32,bmp);//在距离左边为0 第4行开始 画一个32*32的图像
				//bmp为上述定义好的图像数组
			
    }
}

        There are a few points worth noting:

                1. The header file must be imported, which is the basis for our main program to know the existence of the method used.

                2. After initialization, you need to start displaying, so that the OLED display can display images correctly

                3. Before updating data every time, be sure to clear the OLED display, otherwise, if the new data does not change the display data of a certain position, this area will be retained, affecting the display effect.

                4. The principle of the array is the same as that of the OLED display. It must be cleared before use, otherwise it will cause interference to the back.

3. Summary

        OLED display screen is a very important output device, it is a device that allows us to see the state of single-chip microcomputer intuitively without the help of other software, so its mastery is particularly important, and it is also the basis of our interaction.

Guess you like

Origin blog.csdn.net/qq_39724355/article/details/131146522