?? 專欄簡介:本專欄記錄了從零學(xué)習(xí)單片機(jī)的過程,其中包括51單片機(jī)和STM32單片機(jī)兩部分;建議先學(xué)習(xí)51單片機(jī),其是STM32等高級(jí)單片機(jī)的基礎(chǔ);這樣再學(xué)習(xí)STM32時(shí)才能融會(huì)貫通。
?? 專欄適用人群 :適用于想要從零基礎(chǔ)開始學(xué)習(xí)入門單片機(jī),且有一定C語言基礎(chǔ)的的童鞋。
??專欄目標(biāo):實(shí)現(xiàn)從零基礎(chǔ)入門51單片機(jī)和STM32單片機(jī),力求在玩好單片機(jī)的同時(shí),能夠了解一些計(jì)算機(jī)的基本概念,了解電路及其元器件的基本理論等。?? 專欄主要內(nèi)容: 主要學(xué)習(xí)STM32單片機(jī)的功能、各個(gè)模塊、單片機(jī)的外設(shè)、驅(qū)動(dòng)等,最終玩好單片機(jī)和單片機(jī)的外設(shè),全程手敲代碼,實(shí)現(xiàn)我們所要實(shí)現(xiàn)的功能。
?? 專欄說明 :如果文章知識(shí)點(diǎn)有錯(cuò)誤的地方,歡迎大家隨時(shí)在文章下面評(píng)論,我會(huì)第一時(shí)間改正。讓我們一起學(xué)習(xí),一起進(jìn)步。
??專欄主頁:http://t.csdn.cn/HCD8v
本學(xué)習(xí)過程參考:https://space.bilibili.com/383400717
STM3單片機(jī)安裝軟件、各種資料以及源碼的路徑:
鏈接:https://pan.baidu.com/s/1snD0uuTfMhchFqOMWvAiHA?pwd=asdf#list/path=%2F
提取碼:asdf
鏈接里壓縮包的解壓密碼:32
本大節(jié)主要學(xué)習(xí)OLED調(diào)試工具,包含兩部分,第一小節(jié)主要學(xué)習(xí)OLED的基礎(chǔ)知識(shí),第二小節(jié)是寫一個(gè)簡單的程序進(jìn)行練習(xí),即OLED顯示屏,最終提供一個(gè)OLED顯示屏的驅(qū)動(dòng)函數(shù)模塊,可以在OLED上顯示我們需要的調(diào)試信息;
本小節(jié)主要是學(xué)習(xí)如何使用驅(qū)動(dòng)函數(shù),使OLED顯示屏上顯示我們所需要的內(nèi)容以及調(diào)試信息。
一、本節(jié)目標(biāo)
在OLED屏幕上顯示一些我們需要輸出端的信息,包含字符和數(shù)字等:
二、OLED顯示屏
2.1 接線圖
從上圖可以看到,把OLED插在了面包板的右下角,以后就一直把這個(gè)屏幕插在這里,方便調(diào)試;
可以看出我們使用的是4引腳的OLED屏幕,GND和VCC需要接電源的負(fù)極和正極;
將OLED插到PB6到PB9這四個(gè)口,SCL接到PB8,SDA接到PB9;
硬件電路如下圖所示
2.2 代碼實(shí)現(xiàn)
準(zhǔn)備好的OLED驅(qū)動(dòng)函數(shù)模塊都放在了STM32入門教程資料\程序源碼\STM32Project\1-4 OLED驅(qū)動(dòng)函數(shù)模塊\4針腳I2C版本
這個(gè)路徑下:
以上文件可以直接復(fù)制的我們對應(yīng)的工程中直接使用,
例如本節(jié),將以上文件復(fù)制到:STM32入門教程資料\程序源碼\STM32Project\4-1 OLED顯示屏\Hardware
源碼路徑:STM32入門教程資料\程序源碼\STM32Project\4-1 OLED顯示屏\User
里存放的是main函數(shù)
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
int main(void)
{
OLED_Init();
OLED_ShowChar(1, 1, 'A');
OLED_ShowString(1, 3, "HelloWorld!");
OLED_ShowNum(2, 1, 12345, 5);
OLED_ShowSignedNum(2, 7, -66, 2);
OLED_ShowHexNum(3, 1, 0xAA55, 4);
OLED_ShowBinNum(4, 1, 0xAA55, 16);
while (1)
{
}
}
OLED.c
://OLED相關(guān)驅(qū)動(dòng)函數(shù)
#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;
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ā)送一個(gè)字節(jié)
* @param Byte 要發(fā)送的一個(gè)字節(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); //額外的一個(gè)時(shí)鐘,不處理應(yīng)答信號(hào)
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顯示一個(gè)字符
* @param Line 行位置,范圍:1~4
* @param Column 列位置,范圍:1~16
* @param Char 要顯示的一個(gè)字符,范圍: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)制,帶符號(hào)數(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++) //上電延時(shí)
{
for (j = 0; j < 1000; j++);
}
OLED_I2C_Init(); //端口初始化
OLED_WriteCommand(0xAE); //關(guān)閉顯示
OLED_WriteCommand(0xD5); //設(shè)置顯示時(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取消選擇級(jí)別
OLED_WriteCommand(0x30);
OLED_WriteCommand(0xA4); //設(shè)置整個(gè)顯示打開/關(guān)閉
OLED_WriteCommand(0xA6); //設(shè)置正常/倒轉(zhuǎn)顯示
OLED_WriteCommand(0x8D); //設(shè)置充電泵
OLED_WriteCommand(0x14);
OLED_WriteCommand(0xAF); //開啟顯示
OLED_Clear(); //OLED清屏
}
代碼解釋:
如果你硬件接口的SCL和SDA不是接在PB8和PB9,那么則需要做以下兩點(diǎn)修改適配:
1、引腳配置
/*引腳配置*/
#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))
引腳配置,這里選擇的是你硬件電路上把SCL和SDA這兩個(gè)引腳接在了哪兩個(gè)端口上,如上代碼表示SCL接在了PB8上,SDA接在了PB9上;
例如:如果你硬件電路上把SCL接在了PA6上,那么對應(yīng)的DLED_W_SCL(x)宏就要改為:
#define OLED_W_SCL(x) GPIO_WriteBit(GPIOA, GPIO_Pin_6, (BitAction)(x))
SDA,OLED_W_SDA(x)
宏也是同樣的道理;
2、引腳初始化
如果像1中修改了硬件的引腳,那么OLED_I2C_Init
函數(shù)也要更改;此函數(shù)就是把SCL和SDA兩個(gè)引腳都初始化為開漏輸出模式,
可以看到以上代碼里寫的是SCL接PB8,SDA接PB9;如果硬件引腳換成了其他的引腳,除了1中宏定義要修改,這里也是要做相應(yīng)修改的;文章來源:http://www.zghlxwxcb.cn/news/detail-570140.html
其他剩下驅(qū)動(dòng)函數(shù)的就不需要更改了,可以直接拿來使用。文章來源地址http://www.zghlxwxcb.cn/news/detail-570140.html
到了這里,關(guān)于STM32單片機(jī)(四)第二節(jié):OLED顯示屏的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!