1、前言
emm,这种应用跟rtt没多大关系,iic驱动也就是用到了其中的rt_i2c_transfer,不过还是写一下吧。
oled就是挺常见的,0.96寸,128*64的屏,用IIC接口驱动。
就算是从淘宝买的屏,商家也会有资料,各平台的例程,打开stm32的例程作为移植到现在代码的参考。
下面是如何向OLED写命令、写数据的代码、初始化OLED、某个位置显示字符串、显示整数、小数;
如果要画点、画线这样涉及到某个像素点的,还需要1M的缓存,[128][8]去保存当前屏幕的数据,更新这个数组然后再写。
2、铺路代码
2.1、向OLED写命令
//对OLED写某个命令cmd
static rt_err_t oled_write_cmd(uint8_t cmd)
{
struct rt_i2c_msg msg;
uint8_t buffer[2]={OLED_CMD_CMD,cmd};
msg.addr = OLED_I2C_ADDR;
msg.flags = RT_I2C_WR;
msg.buf = buffer;
msg.len = 2;
if(rt_i2c_transfer(i2c_bus, &msg, 1) != 1){
return RT_ERROR;
}
return RT_EOK;
}
2.2、向OLED写数据
//对OLED写数据data
static rt_err_t oled_write_data(uint8_t data)
{
struct rt_i2c_msg msg;
uint8_t buffer[2]={OLED_CMD_DATA,data};
msg.addr = OLED_I2C_ADDR;
msg.flags = RT_I2C_WR;
msg.buf = buffer;
msg.len = 2;
if(rt_i2c_transfer(i2c_bus, &msg, 1) != 1){
return RT_ERROR;
}
return RT_EOK;
}
2.3、设置OLED地址
//设置起始点坐标128*64
void oled_set_pos(uint8_t x,uint8_t y)
{
oled_write_cmd(0xb0+y);
oled_write_cmd(((x&0xf0)>>4)|0x10);
oled_write_cmd((x&0x0f));
}
3、初始化OLED
- 首先要选择IIC,配置SCL和SDA的引脚,rt_device_find打开IIC设备。
- 根据卖家例程,上电需要延时、然后有一系列的命令、最后清屏。
//清屏
void oled_clear()
{
uint8_t row,col;
for(row = 0;row < 8;row++){
oled_write_cmd(0xb0+row); //page0-page1
oled_write_cmd(0x00); //low column start address
oled_write_cmd(0x10); //high column start address
for(col=0;col<128;col++){
oled_write_data(0x00);
}
}
}
//OLED初始化
void oled_init()
{
rt_thread_mdelay(100); //上电必须延时
oled_write_cmd(0xAE); //display off
oled_write_cmd(0x20); //Set Memory Addressing Mode
oled_write_cmd(0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
oled_write_cmd(0xb0); //Set Page Start Address for Page Addressing Mode,0-7
oled_write_cmd(0xc8); //Set COM Output Scan Direction
oled_write_cmd(0x00); //---set low column address
oled_write_cmd(0x10); //---set high column address
oled_write_cmd(0x40); //--set start line address
oled_write_cmd(0x81); //--set contrast control register
oled_write_cmd(0xff); //亮度调节 0x00~0xff
oled_write_cmd(0xa1); //--set segment re-map 0 to 127
oled_write_cmd(0xa6); //--set normal display
oled_write_cmd(0xa8); //--set multiplex ratio(1 to 64)
oled_write_cmd(0x3F); //
oled_write_cmd(0xa4); //0xa4,Output follows RAM content;0xa5,Output ignores RAM content
oled_write_cmd(0xd3); //-set display offset
oled_write_cmd(0x00); //-not offset
oled_write_cmd(0xd5); //--set display clock divide ratio/oscillator frequency
oled_write_cmd(0xf0); //--set divide ratio
oled_write_cmd(0xd9); //--set pre-charge period
oled_write_cmd(0x22); //
oled_write_cmd(0xda); //--set com pins hardware configuration
oled_write_cmd(0x12);
oled_write_cmd(0xdb); //--set vcomh
oled_write_cmd(0x20); //0x20,0.77xVcc
oled_write_cmd(0x8d); //--set DC-DC enable
oled_write_cmd(0x14); //
oled_write_cmd(0xaf); //--turn on oled panel
oled_clear(); //清屏
}
rt_err_t oled_iic_startup()
{
//查找I2C总线设备,获取I2C总线设备句柄
i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(OLED_I2C_BUS_NAME);
if (i2c_bus == RT_NULL){
LOG_D("can't find device!\n");
return RT_ERROR;
}
//OLED初始化
oled_init();
return RT_EOK;
}
?
4、显示
4.1、字符串
//参数 : x,y:起始点坐标:(x:0~127, y:0~7);
// ch[]:要显示的字符串;
// TextSize:字符大小:(FONT_6x8代表6*8 ; FONT_8x16代表8*16)
void oled_show_string(uint8_t x, uint8_t y, uint8_t ch[], uint8_t TextSize)
{
uint8_t c = 0,i = 0,j = 0;
switch(TextSize){
case FONT_6x8:{
while(ch[j] != '\0'){ //循环到字符串末尾
c = ch[j] - 32;
if(x > 128-6){
x = 0; y++; //当前行显示不完,自动转到下一行
if(y==8) break; //超出显示范围,退出
}
oled_set_pos(x,y);
for(i=0;i<6;i++) //显示一个字符
oled_write_data(F6x8[c][i]);
x += 6; //下一个位置
j++;
}
}break;
case FONT_8x16:{
while(ch[j] != '\0'){
c = ch[j] - 32;
if(x > 128-8){
x = 0; y++;
if(y==7) break; //超出显示范围,退出
}
oled_set_pos(x,y);
for(i=0;i<8;i++) //显示一个字符
oled_write_data(F8X16[c*16+i]);
oled_set_pos(x,y+1);
for(i=0;i<8;i++)
oled_write_data(F8X16[c*16+i+8]);
x += 8;
j++;
}
}break;
}
}
4.2、整数、小数(根据情况修改小数点后保留几位)
void oled_show_num(uint8_t x, uint8_t y, uint16_t num, uint8_t TextSize)
{
uint8_t buffer[10];
rt_snprintf(buffer,10,"%d",num);
oled_show_string(x,y,buffer,TextSize);
}
void oled_show_double(uint8_t x, uint8_t y, double num, uint8_t TextSize)
{
uint8_t buffer[20];
rt_snprintf(buffer,20,"%f",num);
oled_show_string(x,y,buffer,TextSize);
}
|