一:OLED調(diào)試工具
1.1 OLED顯示屏介紹
學(xué)習(xí)任何一門語言就需要進(jìn)行調(diào)試,stm32調(diào)試方式主要有以下三種:
- 串口調(diào)試:通過串口,將調(diào)試信息發(fā)送到電腦端,電腦端使用串口調(diào)試助手顯示調(diào)試信息。
- 顯示屏調(diào)試:直接將顯示屏連接到單片機(jī),將調(diào)試信息打印在顯示屏上。
- Keil調(diào)試模式:借助Keil軟件的調(diào)試模式,可使用單步運(yùn)行、設(shè)置斷點(diǎn)、查看寄存器及變量等功能。
- 點(diǎn)燈調(diào)試法:在程序的某個關(guān)鍵位置設(shè)置點(diǎn)燈的代碼,用于指示程序是否運(yùn)行到該位置。
- 注釋調(diào)試法:將可能有問題的代碼塊全部注釋掉,然后一個功能一個功能的去掉注釋,找出問題所在。
該實(shí)驗(yàn)暫時只要會使用驅(qū)動函數(shù)模塊,代碼的原理UP后續(xù)會補(bǔ)充講解。
上圖是各種各樣的OLED,下圖是OLED硬件電路
OLED(Organic Light Emitting Diode)意為 有機(jī)發(fā)光二極管。由OLED構(gòu)成的OLED顯示屏是一款性能優(yōu)異的新型顯示屏,其每一個像素都是一個單獨(dú)的發(fā)光二極管,每個像素都可以自發(fā)光,所以具有功耗低、響應(yīng)速度快、寬視角、輕薄柔韌等特點(diǎn)。本實(shí)驗(yàn)采用0.96寸OLED模塊,其小巧玲瓏、占用接口少、簡單易用,是電子設(shè)計中非常常見的顯示屏模塊,下面是一些基本參數(shù):
- 供電:3~5.5V.
- 通信協(xié)議:4針引腳常用I2C,7針引腳常用SPI。
- 如上圖所示。 分辨率:12864(寬高)
與“51單片機(jī)”的教程相同,為了方便調(diào)試,UP主提供了stm32驅(qū)動OLED屏幕的代碼以供前期調(diào)試使用,后面會專門介紹OLED代碼的書寫。首先UP主將屏幕分成了 16 * 4個區(qū)域,每個區(qū)域大小是8 * 16個像素,然后在此基礎(chǔ)上,調(diào)用下表所示代碼即可。
函數(shù) | 作用 |
---|---|
OLED_Init(); | 初始化 |
OLED_Clear(); | 清屏 |
OLED_ShowChar(1,1,‘A’); | 顯示一個字符 |
OLED_ShowString(1,3,“HelloWorld”); | 顯示字符串 |
OLED_ShowNum(2,1,12345,5); | 顯示十進(jìn)制數(shù)字 |
OLED_ShowSignedNum(2,7,-66,2); | 顯示有符號十進(jìn)制數(shù)字 |
OLED_ShowHexNum(3,1,0xAA55,4); | 顯示十六進(jìn)制數(shù)字 |
OLED_ShowBinNum(4,1,0xAA55,16); | 顯示二進(jìn)制數(shù)字 |
1.2 實(shí)驗(yàn):在OLED顯示屏的使用
需求:在OLED顯示屏上,顯示如下圖內(nèi)容:
上圖是OLED顯示實(shí)驗(yàn)現(xiàn)象
上圖是OLED顯示接線圖
OLED顯示屏有兩種供電方式
第一是把OLED的GND和VCC直接接在面包板上的正負(fù)極()
第二是初始化PB6和PB7,讓B6和B7供電,因?yàn)镺LED功率小,能實(shí)現(xiàn)但不規(guī)范
- 上圖的OLED顯示屏背面的GND和VCC一定要外接面包板的vcc和GND,不然可能顯示屏?xí)@示內(nèi)容不全,還有直接不亮。
- 把PB67PB7作為GPIO輸出電平,給顯示屏提供電平需要初始化輸出GPIO 高低電平(輸出1和0就能供電)
網(wǎng)友:試了用PB6PB7供電,但是顯示屏?xí)@示不正確,有部分錯誤顯示
上圖是OLED顯示的代碼調(diào)用結(jié)構(gòu)圖,不包含庫函數(shù)
看一下注釋,這是OLED的字模庫,每個數(shù)字或字母都以16進(jìn)制放進(jìn)了函數(shù)內(nèi),不用管數(shù)字怎么轉(zhuǎn)換成二進(jìn)制,有特定的字模軟件
下面是代碼展示:
main.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
int main(void)
{
/*模塊初始化*/
OLED_Init(); //OLED初始化
/*OLED顯示*/
OLED_ShowChar(1, 1, 'A'); //1行1列顯示字符A
OLED_ShowString(1, 3, "HelloWorld!"); //1行3列顯示字符串HelloWorld!
OLED_ShowNum(2, 1, 12345, 5); //2行1列顯示十進(jìn)制數(shù)字12345,長度為5
OLED_ShowSignedNum(2, 7, -66, 2); //2行7列顯示有符號十進(jìn)制數(shù)字-66,長度為2
OLED_ShowHexNum(3, 1, 0xAA55, 4); //3行1列顯示十六進(jìn)制數(shù)字0xA5A5,長度為4
OLED_ShowBinNum(4, 1, 0xAA55, 16); //4行1列顯示二進(jìn)制數(shù)字0xA5A5,長度為16
//C語言無法直接寫出二進(jìn)制數(shù)字,故需要用十六進(jìn)制表示
while (1)
{
}
}
OLED_Font.h
看一下注釋,這是OLED的字模庫,每個數(shù)字或字母都以16進(jìn)制放進(jìn)了函數(shù)內(nèi),不用管數(shù)字怎么轉(zhuǎn)換成二進(jìn)制,有特定的字模軟件
#ifndef __OLED_FONT_H
#define __OLED_FONT_H
/*OLED字模庫,寬8像素,高16像素*/
const uint8_t OLED_F8x16[][16]=
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1
0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2
0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,
0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3
0x00,0x70,0x88,0x*FC,0x08,0x30,0x00,0x00,
0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4
0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,
0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5
0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,
0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6
0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7
0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,
0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8
0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,
0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9
0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,
0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10
0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,
0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14
0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,
0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16
0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17
0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,
0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18
0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,
0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19
0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,
0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20
0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,
0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21
0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,
0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22
0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,
0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23
0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,
0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25
0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,
0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,
0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27
0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,
0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28
0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,
0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29
0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,
0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30
0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,
0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31
0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,
0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32
0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,
0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33
0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,
0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34
0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,
0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35
0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,
0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38
0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,
0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41
0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,
0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42
0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,
0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43
0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,
0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,
0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,
0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47
0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,
0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49
0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,
0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,
0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51
0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,
0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53
0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,
0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54
0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,
0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55
0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,
0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56
0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,
0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57
0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,
0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58
0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,
0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59
0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60
0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,
0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61
0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63
0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,
0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66
0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,
0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67
0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,
0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69
0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71
0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,
0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72
0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,
0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74
0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,
0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75
0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77
0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,
0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79
0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,
0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80
0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,
0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81
0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83
0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,
0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84
0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,
0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86
0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,
0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87
0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90
0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,
0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91
0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92
0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,
0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93
0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
};
#endif
OLED.c
- 因?yàn)檫@個寫的屏幕驅(qū)動使用的是I2C總線, I2C協(xié)議要求必須開漏輸出GPIO_Mode_OUT_OD
- I2C多機(jī)通信情況下,利用開漏輸出的線與來避免設(shè)備間相互干擾。但是這里只有一個屏幕,推挽輸出應(yīng)該也沒問題
#include "stm32f10x.h"
#include "OLED_Font.h"
/*引腳配置*/
#define OLED_W_SCL(x) GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)(x))
#define OLED_W_SDA(x) GPIO_WriteBit(GPIOB, GPIO_Pin_9, (BitAction)(x))
/*引腳初始化*/
void OLED_I2C_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;//因?yàn)檫@個寫的屏幕驅(qū)動使用的是I2C總線, I2C協(xié)議要求必須開漏輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOB, &GPIO_InitStructure);
OLED_W_SCL(1);
OLED_W_SDA(1);
}
/**
* @brief I2C開始
* @param 無
* @retval 無
*/
void OLED_I2C_Start(void)
{
OLED_W_SDA(1);
OLED_W_SCL(1);
OLED_W_SDA(0);
OLED_W_SCL(0);
}
/**
* @brief I2C停止
* @param 無
* @retval 無
*/
void OLED_I2C_Stop(void)
{
OLED_W_SDA(0);
OLED_W_SCL(1);
OLED_W_SDA(1);
}
/**
* @brief I2C發(fā)送一個字節(jié)
* @param Byte 要發(fā)送的一個字節(jié)
* @retval 無
*/
void OLED_I2C_SendByte(uint8_t Byte)
{
uint8_t i;
for (i = 0; i < 8; i++)
{
OLED_W_SDA(Byte & (0x80 >> i));
OLED_W_SCL(1);
OLED_W_SCL(0);
}
OLED_W_SCL(1); //額外的一個時鐘,不處理應(yīng)答信號
OLED_W_SCL(0);
}
/**
* @brief OLED寫命令
* @param Command 要寫入的命令
* @retval 無
*/
void OLED_WriteCommand(uint8_t Command)
{
OLED_I2C_Start();
OLED_I2C_SendByte(0x78); //從機(jī)地址
OLED_I2C_SendByte(0x00); //寫命令
OLED_I2C_SendByte(Command);
OLED_I2C_Stop();
}
/**
* @brief OLED寫數(shù)據(jù)
* @param Data 要寫入的數(shù)據(jù)
* @retval 無
*/
void OLED_WriteData(uint8_t Data)
{
OLED_I2C_Start();
OLED_I2C_SendByte(0x78); //從機(jī)地址
OLED_I2C_SendByte(0x40); //寫數(shù)據(jù)
OLED_I2C_SendByte(Data);
OLED_I2C_Stop();
}
/**
* @brief OLED設(shè)置光標(biāo)位置
* @param Y 以左上角為原點(diǎn),向下方向的坐標(biāo),范圍:0~7
* @param X 以左上角為原點(diǎn),向右方向的坐標(biāo),范圍:0~127
* @retval 無
*/
void OLED_SetCursor(uint8_t Y, uint8_t X)
{
OLED_WriteCommand(0xB0 | Y); //設(shè)置Y位置
OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4)); //設(shè)置X位置高4位
OLED_WriteCommand(0x00 | (X & 0x0F)); //設(shè)置X位置低4位
}
/**
* @brief OLED清屏
* @param 無
* @retval 無
*/
void OLED_Clear(void)
{
uint8_t i, j;
for (j = 0; j < 8; j++)
{
OLED_SetCursor(j, 0);
for(i = 0; i < 128; i++)
{
OLED_WriteData(0x00);
}
}
}
/**
* @brief OLED顯示一個字符
* @param Line 行位置,范圍:1~4
* @param Column 列位置,范圍:1~16
* @param Char 要顯示的一個字符,范圍:ASCII可見字符
* @retval 無
*/
void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char)
{
uint8_t i;
OLED_SetCursor((Line - 1) * 2, (Column - 1) * 8); //設(shè)置光標(biāo)位置在上半部分
for (i = 0; i < 8; i++)
{
OLED_WriteData(OLED_F8x16[Char - ' '][i]); //顯示上半部分內(nèi)容
}
OLED_SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8); //設(shè)置光標(biāo)位置在下半部分
for (i = 0; i < 8; i++)
{
OLED_WriteData(OLED_F8x16[Char - ' '][i + 8]); //顯示下半部分內(nèi)容
}
}
/**
* @brief OLED顯示字符串
* @param Line 起始行位置,范圍:1~4
* @param Column 起始列位置,范圍:1~16
* @param String 要顯示的字符串,范圍:ASCII可見字符
* @retval 無
*/
void OLED_ShowString(uint8_t Line, uint8_t Column, char *String)
{
uint8_t i;
for (i = 0; String[i] != '\0'; i++)
{
OLED_ShowChar(Line, Column + i, String[i]);
}
}
/**
* @brief OLED次方函數(shù)
* @retval 返回值等于X的Y次方
*/
uint32_t OLED_Pow(uint32_t X, uint32_t Y)
{
uint32_t Result = 1;
while (Y--)
{
Result *= X;
}
return Result;
}
/**
* @brief OLED顯示數(shù)字(十進(jìn)制,正數(shù))
* @param Line 起始行位置,范圍:1~4
* @param Column 起始列位置,范圍:1~16
* @param Number 要顯示的數(shù)字,范圍:0~4294967295
* @param Length 要顯示數(shù)字的長度,范圍:1~10
* @retval 無
*/
void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
uint8_t i;
for (i = 0; i < Length; i++)
{
OLED_ShowChar(Line, Column + i, Number / OLED_Pow(10, Length - i - 1) % 10 + '0');
}
}
/**
* @brief OLED顯示數(shù)字(十進(jìn)制,帶符號數(shù))
* @param Line 起始行位置,范圍:1~4
* @param Column 起始列位置,范圍:1~16
* @param Number 要顯示的數(shù)字,范圍:-2147483648~2147483647
* @param Length 要顯示數(shù)字的長度,范圍:1~10
* @retval 無
*/
void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length)
{
uint8_t i;
uint32_t Number1;
if (Number >= 0)
{
OLED_ShowChar(Line, Column, '+');
Number1 = Number;
}
else
{
OLED_ShowChar(Line, Column, '-');
Number1 = -Number;
}
for (i = 0; i < Length; i++)
{
OLED_ShowChar(Line, Column + i + 1, Number1 / OLED_Pow(10, Length - i - 1) % 10 + '0');
}
}
/**
* @brief OLED顯示數(shù)字(十六進(jìn)制,正數(shù))
* @param Line 起始行位置,范圍:1~4
* @param Column 起始列位置,范圍:1~16
* @param Number 要顯示的數(shù)字,范圍:0~0xFFFFFFFF
* @param Length 要顯示數(shù)字的長度,范圍:1~8
* @retval 無
*/
void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
uint8_t i, SingleNumber;
for (i = 0; i < Length; i++)
{
SingleNumber = Number / OLED_Pow(16, Length - i - 1) % 16;
if (SingleNumber < 10)
{
OLED_ShowChar(Line, Column + i, SingleNumber + '0');
}
else
{
OLED_ShowChar(Line, Column + i, SingleNumber - 10 + 'A');
}
}
}
/**
* @brief OLED顯示數(shù)字(二進(jìn)制,正數(shù))
* @param Line 起始行位置,范圍:1~4
* @param Column 起始列位置,范圍:1~16
* @param Number 要顯示的數(shù)字,范圍:0~1111 1111 1111 1111
* @param Length 要顯示數(shù)字的長度,范圍:1~16
* @retval 無
*/
void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
uint8_t i;
for (i = 0; i < Length; i++)
{
OLED_ShowChar(Line, Column + i, Number / OLED_Pow(2, Length - i - 1) % 2 + '0');
}
}
/**
* @brief OLED初始化
* @param 無
* @retval 無
*/
void OLED_Init(void)
{
uint32_t i, j;
for (i = 0; i < 1000; i++) //上電延時
{
for (j = 0; j < 1000; j++);
}
OLED_I2C_Init(); //端口初始化
OLED_WriteCommand(0xAE); //關(guān)閉顯示
OLED_WriteCommand(0xD5); //設(shè)置顯示時鐘分頻比/振蕩器頻率
OLED_WriteCommand(0x80);
OLED_WriteCommand(0xA8); //設(shè)置多路復(fù)用率
OLED_WriteCommand(0x3F);
OLED_WriteCommand(0xD3); //設(shè)置顯示偏移
OLED_WriteCommand(0x00);
OLED_WriteCommand(0x40); //設(shè)置顯示開始行
OLED_WriteCommand(0xA1); //設(shè)置左右方向,0xA1正常 0xA0左右反置
OLED_WriteCommand(0xC8); //設(shè)置上下方向,0xC8正常 0xC0上下反置
OLED_WriteCommand(0xDA); //設(shè)置COM引腳硬件配置
OLED_WriteCommand(0x12);
OLED_WriteCommand(0x81); //設(shè)置對比度控制
OLED_WriteCommand(0xCF);
OLED_WriteCommand(0xD9); //設(shè)置預(yù)充電周期
OLED_WriteCommand(0xF1);
OLED_WriteCommand(0xDB); //設(shè)置VCOMH取消選擇級別
OLED_WriteCommand(0x30);
OLED_WriteCommand(0xA4); //設(shè)置整個顯示打開/關(guān)閉
OLED_WriteCommand(0xA6); //設(shè)置正常/倒轉(zhuǎn)顯示
OLED_WriteCommand(0x8D); //設(shè)置充電泵
OLED_WriteCommand(0x14);
OLED_WriteCommand(0xAF); //開啟顯示
OLED_Clear(); //OLED清屏
}
OLED.h
#ifndef __OLED_H
#define __OLED_H
void OLED_Init(void);
void OLED_Clear(void);
void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char);
void OLED_ShowString(uint8_t Line, uint8_t Column, char *String);
void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length);
void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
#endif
編程感想:
- OLED的供電。由于stm32引腳不僅初始化默認(rèn)是浮空輸入的模式,所以可以直接把B6和B7引腳接在電源上,用于給OLED進(jìn)行供電。
- 視頻分為兩部分,前面講了OLED顯示內(nèi)容,后半段只演示了“Keil的調(diào)試模式”,暫時看不懂,哈哈。
1.3 自己新增功能測試
下面是自己做了一些小調(diào)整,加了延時函數(shù)+清屏函數(shù)+在while(1){}里面循環(huán)執(zhí)行某個字符文章來源:http://www.zghlxwxcb.cn/news/detail-811939.html
效果是:顯示屏首先先按順序顯示內(nèi)容,倒數(shù)第二是屏幕突然黑了,最后只有一個字符A停留在顯示屏上。文章來源地址http://www.zghlxwxcb.cn/news/detail-811939.html
道友:今天沒有開始的事,明天絕不會完成。
到了這里,關(guān)于嵌入式-stm32-江科大-OLED調(diào)試工具的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!