標(biāo)題:
stm32f1_IIC_OLED顯示屏驅(qū)動(dòng)
[打印本頁(yè)]
作者:
ywxgx
時(shí)間:
2023-11-17 19:40
標(biāo)題:
stm32f1_IIC_OLED顯示屏驅(qū)動(dòng)
#include "OLED_Font.h"
#include "OLED.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)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
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 無(wú)
* @retval 無(wú)
*/
void OLED_I2C_Start(void)
{
OLED_W_SDA(1);
OLED_W_SCL(1);
OLED_W_SDA(0);
OLED_W_SCL(0);
}
/**
* @brief I2C停止
* @param 無(wú)
* @retval 無(wú)
*/
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 無(wú)
*/
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 無(wú)
*/
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 無(wú)
*/
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 無(wú)
*/
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 無(wú)
* @retval 無(wú)
*/
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 無(wú)
*/
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 無(wú)
*/
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ù)字的長(zhǎng)度,范圍:1~10
* @retval 無(wú)
*/
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ù)字的長(zhǎng)度,范圍:1~10
* @retval 無(wú)
*/
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ù)字的長(zhǎng)度,范圍:1~8
* @retval 無(wú)
*/
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ù)字的長(zhǎng)度,范圍:1~16
* @retval 無(wú)
*/
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 無(wú)
* @retval 無(wú)
*/
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è)置對(duì)比度控制
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清屏
}
復(fù)制代碼
原理圖: 無(wú)
仿真: 無(wú)
代碼:
程序.7z
(281.07 KB, 下載次數(shù): 21)
2023-11-17 23:41 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1