基于hal库开发stm32oled
时间: 2024-06-17 16:07:57 浏览: 118
基于HAL库的stm32基本实验代码
基于HAL库开发STM32 OLED可以参考以下步骤:
1. 初始化I2C总线,配置OLED的I2C地址,数据传输速率等。
2. 在HAL库中,使用HAL_I2C_Master_Transmit和HAL_I2C_Master_Receive函数可以实现I2C总线的数据传输。
3. 配置OLED显示屏的初始化参数,比如分辨率、亮度、对比度等。
4. 初始化OLED显示屏,可以使用OLED_Init函数,该函数会按照前面设置的参数进行初始化。
5. 在代码中编写需要显示的内容,并通过函数将其显示在OLED屏幕上。
6. 在程序中增加控制代码,可以实现动态刷新屏幕内容等功能。
以下是一份参考代码,仅供参考:
```
#include "oled.h"
#include "delay.h"
#include "i2c.h"
#define OLED_CMD 0x00
#define OLED_DATA 0x40
void OLED_WR_Byte(uint8_t dat, uint8_t cmd)
{
uint8_t data_t;
if(cmd==OLED_CMD)data_t=0x00;
else data_t=0x40;
data_t=dat;
HAL_I2C_Master_Transmit(&hi2c1, OLED_ADDR, data_t, 2, 1000);
}
void OLED_Display_On(void)
{
OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC命令
OLED_WR_Byte(0X14,OLED_CMD); //DCDC ON
OLED_WR_Byte(0XAF,OLED_CMD); //DISPLAY ON
}
void OLED_Display_Off(void)
{
OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC命令
OLED_WR_Byte(0X10,OLED_CMD); //DCDC OFF
OLED_WR_Byte(0XAE,OLED_CMD); //DISPLAY OFF
}
void OLED_Clear(void)
{
uint8_t i,n;
for(i=0;i<8;i++)
{
OLED_WR_Byte(0xb0+i,OLED_CMD); //设置页地址(0~7)
OLED_WR_Byte(0x00,OLED_CMD); //设置显示位置—列低地址
OLED_WR_Byte(0x10,OLED_CMD); //设置显示位置—列高地址
for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA);
} //更新显示
}
void OLED_Init(void)
{
HAL_Delay(200);
OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
OLED_WR_Byte(0x40,OLED_CMD);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
OLED_WR_Byte(0xCF,OLED_CMD); // Set SEG Output Current Brightness
OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常
OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常
OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display
OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty
OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
OLED_WR_Byte(0x00,OLED_CMD);//-not offset
OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec
OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period
OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration
OLED_WR_Byte(0x12,OLED_CMD);
OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh
OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level
OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
OLED_WR_Byte(0x02,OLED_CMD);//
OLED_Clear();
}
void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size)
{
uint8_t c=0,i=0,j=0;
switch(Char_Size)
{
case 16:
{
while(chr[c]!='\0')
{
if(chr[c]>127) {
i=chr[c]-128;
j=chr[c+1]-128;
c+=2;
OLED_ShowChinese(x,y,i,j,16);
x+=16;
}
else {
OLED_ShowChar(x,y,chr[c],16);
c++;
x+=8;
}
}
}break;
case 12:
{
while(chr[c]!='\0')
{
if(chr[c]>127) {
i=chr[c]-128;
j=chr[c+1]-128;
c+=2;
OLED_ShowChinese(x,y,i,j,12);
x+=12;
}
else {
OLED_ShowChar(x,y,chr[c],12);
c++;
x+=6;
}
}
}break;
default:
{
while (chr[c]!='\0')
{
if (chr[c]>127) {
i=chr[c]-128;
j=chr[c+1]-128;
c+=2;
OLED_ShowChinese(x,y,i,j,12);
x+=12;
}
else {
OLED_ShowChar(x,y,chr[c],16);
c++;
x+=8;
}
}
}break;
}
}
void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t Char_Size)
{
uint8_t c=0,i=0,j=0;
switch(Char_Size)
{
case 12:
{
c=chr-' ';
if(x>116) {x=0;y+=12;}
OLED_Set_Pos(x,y);
for(i=0;i<6;i++)
{
OLED_WR_Byte(F6x8[c][i],OLED_DATA);
}
}break;
case 16:
{
c=chr-' ';
if(x>112) {x=0;y+=16;}
OLED_Set_Pos(x,y);
for(i=0;i<8;i++)
{
OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);
}
OLED_Set_Pos(x,y+8);
for(i=0;i<8;i++)
{
OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
}
}break;
default:
{
c=chr-' ';
if(x>120) {x=0;y+=16;}
OLED_Set_Pos(x,y);
for(i=0;i<8;i++)
{
OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);
}
OLED_Set_Pos(x,y+8);
for(i=0;i<8;i++)
{
OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
}
}break;
}
}
void OLED_ShowChinese(uint8_t x,uint8_t y,uint8_t no,uint8_t size)
{
uint8_t t,t1,c=0,i=0;
uint8_t adder=32*(no-1);
while(c<size)
{
t=Hzk[size-12][adder];
t1=Hzk[size-12][adder+1];
for(i=0;i<8;i++)
{
if(t&0x80)OLED_Set_Pos(x+i,y);
t<<=1;
if(t1&0x80)OLED_WR_Byte(0xff,OLED_DATA);
else OLED_WR_Byte(0,OLED_DATA);
t1<<=1;
}
c++;
adder+=2;
y++;
if(y>63)return;
if((y-y%8)==((y-1)-(y-1)%8))
{y=y-y%8+8;}
}
}
void OLED_Set_Pos(uint8_t x, uint8_t y)
{
OLED_WR_Byte(0xb0+y,OLED_CMD);
OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
OLED_WR_Byte((x&0x0f)|0x01,OLED_CMD);
}
void Draw_BMP(uint8_t x,uint8_t y,const uint8_t *pBmp,uint8_t chWidth,uint8_t chHeight)
{
uint16_t i,j,n,m,k;
uint8_t temp;
n=(chWidth%8==0)?chWidth/8:chWidth/8+1;
for(i=0;i<chHeight;i++)
{
OLED_Set_Pos(x,y+i);
for(j=0;j<n;j++)
{
temp = pBmp[i*n+j];
for(k=0;k<8;k++)
{
if(temp&0x80)OLED_WR_Byte(1,OLED_DATA);
else OLED_WR_Byte(0,OLED_DATA);
temp<<=1;
}
}
}
}
```
阅读全文