使用stm32并使用hal库编写程序时,怎么是0.96寸的oled屏显示变量,请给出相关函数代码以及实例代码
时间: 2023-05-15 16:06:40 浏览: 404
可以使用HAL库中的SSD1306驱动来控制0.96寸的OLED屏幕显示变量。以下是相关函数代码和实例代码:
函数代码:
void OLED_Init(void)
{
HAL_Delay(100);
OLED_Write_Command(0xAE);//--display off
OLED_Write_Command(0x00);//---set low column address
OLED_Write_Command(0x10);//---set high column address
OLED_Write_Command(0x40);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
OLED_Write_Command(0x81);//--set contrast control register
OLED_Write_Command(0xCF); // Set SEG Output Current Brightness
OLED_Write_Command(0xA1);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常
OLED_Write_Command(0xC8);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常
OLED_Write_Command(0xA6);//--set normal display
OLED_Write_Command(0xA8);//--set multiplex ratio(1 to 64)
OLED_Write_Command(0x3f);//--1/64 duty
OLED_Write_Command(0xD3);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
OLED_Write_Command(0x00);//-not offset
OLED_Write_Command(0xd5);//--set display clock divide ratio/oscillator frequency
OLED_Write_Command(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
OLED_Write_Command(0xD9);//--set pre-charge period
OLED_Write_Command(0xF1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
OLED_Write_Command(0xDA);//--set com pins hardware configuration
OLED_Write_Command(0x12);
OLED_Write_Command(0xDB);//--set vcomh
OLED_Write_Command(0x40);//Set VCOM Deselect Level
OLED_Write_Command(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)
OLED_Write_Command(0x02);//
OLED_Write_Command(0x8D);//--set Charge Pump enable/disable
OLED_Write_Command(0x14);//--set(0x10) disable
OLED_Write_Command(0xA4);// Disable Entire Display On (0xa4/0xa5)
OLED_Write_Command(0xA6);// Disable Inverse Display On (0xa6/a7)
OLED_Write_Command(0xAF);//--turn on oled panel
}
void OLED_Write_Command(uint8_t cmd)
{
HAL_I2C_Mem_Write(&hi2c1, OLED_ADDR, 0x00, 1, &cmd, 1, 100);
}
void OLED_Write_Data(uint8_t data)
{
HAL_I2C_Mem_Write(&hi2c1, OLED_ADDR, 0x40, 1, &data, 1, 100);
}
void OLED_Clear_Screen(void)
{
uint8_t i,j;
for(i=0;i<8;i++)
{
OLED_Write_Command(0xb0+i);
OLED_Write_Command(0x00);
OLED_Write_Command(0x10);
for(j=0;j<128;j++)
{
OLED_Write_Data(0x00);
}
}
}
void OLED_Show_String(uint8_t x,uint8_t y,char *str)
{
uint8_t i=0,j=0;
while(str[i]!='\0')
{
j=str[i]-32;
if(x>120)
{
x=0;y++;
}
OLED_Show_Char(x,y,j);
x+=6;
i++;
}
}
void OLED_Show_Char(uint8_t x,uint8_t y,uint8_t chr)
{
uint8_t i,j;
uint8_t temp;
chr=chr-' ';
OLED_Write_Command(0xb0+y);
OLED_Write_Command(((x&0xf0)>>4)|0x10);
OLED_Write_Command(x&0x0f);
for(i=0;i<6;i++)
{
temp=asc2_1206[chr][i];
for(j=0;j<8;j++)
{
if(temp&0x01)
{
OLED_Write_Data(0xff);
}
else
{
OLED_Write_Data(0x00);
}
temp>>=1;
}
}
}
实例代码:
#include "stm32f1xx_hal.h"
#include "oled.h"
I2C_HandleTypeDef hi2c1;
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2C1_Init();
OLED_Init();
OLED_Clear_Screen();
OLED_Show_String(0,0,"Hello World!");
while (1)
{
}
}
注意:以上代码仅供参考,具体实现需要根据具体的硬件和软件环境进行调整。
阅读全文