中山企業(yè)建站程序三只松鼠軟文范例500字
一、前言
? ? ? ? 獲取到數(shù)據(jù)后我們常需要在OLED顯示屏上顯示,本文中我們需要使用上一篇文章(光照與溫濕度數(shù)據(jù)獲取)的代碼,在其基礎(chǔ)上繼續(xù)完成本文內(nèi)容。
? ? ? ? 基礎(chǔ)代碼:
#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);//定時器初始化,設(shè)置定時中斷1ms中斷一次
}float temp=0,hum=0,light=0;
int main( void )
{Init();ADCS_Init();//初始化ADChal_temHumInit();//初始化溫濕度模塊while( 1 ){HAL_Delay(1000);//延時1000msAdcScanChannel();//更新通道值light=((5.0/2.0)*AdcReadCh0())*100.0;//獲取光照并通過公式計算call_sht11(&temp,&hum);//獲取溫濕度}
}
二、代碼實現(xiàn)
? ? ? ? 例程中,為我們提供了hal_oled.c文件,其中為我們常使用下列方法:
void OLED_Display_On(void);//開啟展示
void OLED_Display_Off(void);//關(guān)閉展示
void OLED_Init(void);//初始化OLED
void OLED_Clear(void);//清空顯示內(nèi)容
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[]);//顯示圖片
? ? ? ? OLED的屏幕是128*64,內(nèi)部將其分為了8行4頁,即0、2、4、6,因此uint8_t y的參數(shù)為0~7,且一個高16的字符需要兩行,即一頁。
? ? ? ? 其中,OLED_ShowCHinese()的第三個參數(shù)是來源于oledfont.h中的HZK數(shù)組,大家也可以通過取模軟件自行取模覆蓋掉該數(shù)組的內(nèi)容。
????????
? ? ? ? ?下面,我們會用到溫度與濕度的字模,從數(shù)組中找到他們對應(yīng)的下標。
? ? ? ? 代碼示例:
? ? ? ? 下列代碼實現(xiàn)了漢字、字符串、數(shù)據(jù)、圖片的功能,以供大家學(xué)習(xí)參考。
#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);//定時器初始化,設(shè)置定時中斷1ms中斷一次
}unsigned char bmp[]={//圖像取模后的數(shù)組
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();//初始化ADChal_temHumInit();//初始化溫濕度模塊OLED_Init();//初始化OLED屏幕OLED_Display_On();//開啟展示while( 1 ){HAL_Delay(1000);//延時1000msAdcScanChannel();//更新通道值light=((5.0/2.0)*AdcReadCh0())*100.0;//獲取光照并通過公式計算call_sht11(&temp,&hum);//獲取溫濕度OLED_Clear();OLED_ShowCHinese(0,0,24);//濕 第二個參數(shù)代表從0行開始 將字寫在0-1行OLED_ShowCHinese(16,0,23);//度 第一個參數(shù)代表左邊的像素距離 //要把第一個字16*16的距離空出來 否則會覆蓋第一個字 一個漢字的大小是16*16memset(data,'\0',20);//清空數(shù)組sprintf((char *)data,"%.2f %%RH",hum);//將內(nèi)容寫到數(shù)組中OLED_ShowString(16*2,0,data);//顯示數(shù)組內(nèi)的內(nèi)容//16*2代表左邊空出兩個字的距離 OLED_ShowString(0,2,(uint8_t *)"xixi_cainiao");//直接顯示字符串 xixi_cainiao//第二個參數(shù)為2代表顯示在2-3行OLED_DrawBMP(0,4,32,32,bmp);//在距離左邊為0 第4行開始 畫一個32*32的圖像//bmp為上述定義好的圖像數(shù)組}
}
? ? ? ? 其中有值得注意的幾點:
? ? ? ? ? ? ? ? 1.必須導(dǎo)入頭文件,頭文件是使我們的主程序知道所使用的方法存在的基礎(chǔ)。
? ? ? ? ? ? ? ? 2.初始化后需要開始展示,OLED顯示屏才能夠正確顯示圖像
? ? ? ? ? ? ? ? 3.在每次更新數(shù)據(jù)前,務(wù)必需要清空OLED顯示屏,否則,如果新的數(shù)據(jù)對某個位置的顯示數(shù)據(jù)沒有改變,該區(qū)域就會保留,影響顯示效果。
? ? ? ? ? ? ? ? 4.數(shù)組與OLED顯示屏原理相同,使用前務(wù)必清空,否則會對后面造成干擾。
三、總結(jié)
? ? ? ? OLED顯示屏是一個十分重要的輸出設(shè)備,它是我們不借助其他軟件讓我們直觀看到單片機狀態(tài)的設(shè)備,因此它的掌握尤為重要,也是我們交互的基礎(chǔ)。