我用STM8S208R8,spi 4线方式驱动SSD1306的oled,一直点不亮,能不能帮忙看看为什么???程序如下:
#include "stm8s208r.h"
_Bool lcd_scl @PC_ODR:5; //lcd 时钟
_Bool lcd_sda @PC_ODR:6; //lcd 数据
_Bool lcd_rst @PB_ODR:3; //lcd rst
_Bool lcd_dc @PB_ODR:2; //lcd 方向
_Bool lcd_cs @PF_ODR:0; //lcd 片选
#define XLevelL 0x00
#define XLevelH 0x10
#define XLevel ((XLevelh&0x0f)*16+XLevelL)
#define Max_Column 128
#define Max_Row 64
#define Brightness 0xCF
#define X_WIDTH 128
#define Y_WIDTH 64
const unsigned char f8x16[] = {0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00, /*"1",0*/
0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,
0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00}; /*"2",1*/
const unsigned char f16x16[] = {0x00,0x02,0x82,0x82,0x82,0x82,0x82,0xFE,
0x82,0x82,0x82,0x82,0x82,0x02,0x00,0x00,
0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x7F,
0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00, /*"王",0*/
0x00,0x00,0x00,0x00,0x00,0xFC,0x00,0x00,
0x00,0x7E,0x80,0x00,0x00,0x00,0x00,0x00,
0x00,0x80,0x60,0x18,0x07,0x00,0x00,0x00,
0x00,0x00,0x03,0x0C,0x30,0x40,0x80,0x00}; /*"八",1*/
void gpio_init(void)
{
PC_DDR |= 0x60;
PC_CR1 |= 0x60;
PC_CR2 &= ~0x60;
PB_DDR |= 0x06;
PB_CR1 |= 0x06;
PB_CR2 &= ~0x06;
PF_DDR |= 0x01;
PF_CR1 |= 0x01;
PF_CR2 &= ~0x01;
}
/*------------------------------*/
void delayms(unsigned char time)
{
unsigned char i, j;
for (i=time; i>0; i--)
{
for (j=110; j>0; j--);
}
}
/*----------lcd写数据----------*/
void lcd_wrdat(unsigned char dat)
{
unsigned char i;
lcd_cs = 0;
lcd_dc = 1;
lcd_scl = 0;
for (i=0; i<8; i++)
{
if ((dat<<i) & 0x80)
{
lcd_sda = 1;
}
else
{
lcd_sda = 0;
}
lcd_scl = 1;
}
}
/*----------lcd写命令----------*/
void lcd_wrcmd(unsigned char cmd)
{
unsigned char i;
lcd_cs = 0;
lcd_dc = 0;
lcd_scl = 0;
for (i=0; i<8; i++)
{
if ((cmd<<i) & 0x80)
{
lcd_sda = 1;
}
else
{
lcd_sda = 0;
}
lcd_scl = 1;
}
}
/*----------lcd设置坐标----------*/
void lcd_set_pos(unsigned char x, unsigned char y)
{
lcd_wrcmd(0xb0+y);
lcd_wrcmd(((x&0xf0)>>4)|0x10);
lcd_wrcmd((x&0x0f)|0x01);
}
/*----------lcd全屏----------*/
void lcd_fill(unsigned char bmp_dat)
{
unsigned char x, y;
for (y=0; y<8; y++)
{
lcd_wrcmd(0xb0+y);
lcd_wrcmd(0x01);
lcd_wrcmd(0x10);
for (x=0; x<X_WIDTH; x++)
{
lcd_wrdat(bmp_dat);
}
}
}
/*----------lcd复位----------*/
void lcd_cls(void)
{
unsigned char x, y;
for (y=0; y<8; y++)
{
lcd_wrcmd(0xb0+y);
lcd_wrcmd(0x01);
lcd_wrcmd(0x10);
for (x=0; x<X_WIDTH; x++)
{
lcd_wrdat(0);
}
}
}
/*----------lcd初始化----------*/
void oled_init(void)
{
lcd_cls();
lcd_scl = 1;
lcd_rst = 0;
delayms(250);
lcd_rst = 1;
lcd_wrcmd(0xae); //turn off oled panel
lcd_wrcmd(0x00); //set low column address
lcd_wrcmd(0x10); //set high column address
lcd_wrcmd(0x40); //set start line address set mapping RAM display start line (0x00~0x3f)
lcd_wrcmd(0x81); //set contrast control register
lcd_wrcmd(0xcf); //set SEG output current brightness
lcd_wrcmd(0xa1); //set SEG/COLUMN mapping 0xa0左右反置 0xa1正常
lcd_wrcmd(0xc8); //set COM/ROW scan direction 0xc0上下 反置 oxc8正常
lcd_wrcmd(0xa6); //set normal display
lcd_wrcmd(0xa8); //set multiplex ratio (1 to 64)
lcd_wrcmd(0x3f); //--1/64 duty
lcd_wrcmd(0xd3); //set display offset shift mapping RAM counter(0x00~0x3f)
lcd_wrcmd(0x00); //not offset
lcd_wrcmd(0xd5); //set display clock divide ratio/oscillator frequency
lcd_wrcmd(0x80); //set divide ratio, set clock as 100 frames/sec
lcd_wrcmd(0xd9); //set pre-charge period
lcd_wrcmd(0xf1); //set pre-charge as 15 clocks & discharge as 1 clock
lcd_wrcmd(0xda); //set com pins hardware configuartion
lcd_wrcmd(0x12);
lcd_wrcmd(0xdb); //set Vcomh
lcd_wrcmd(0x40); //set Vcom deselect level
lcd_wrcmd(0x20); //set page addressing mode (0x00/0x01/0x02)
lcd_wrcmd(0x02);
lcd_wrcmd(0x8d); //set charge pump enable/disable
lcd_wrcmd(0x14); //set (0x01) disable
lcd_wrcmd(0xa4); //disable entire display on (0xa4/0xa5)
lcd_wrcmd(0xa6); //disable inverse display on (0xa6/0xa7)
lcd_wrcmd(0xaf); //turn on oled panel
lcd_fill(0x00); //clear screen
lcd_set_pos(0, 0);
}
/*----------显示16*16点阵 显示的坐标(x,y),y为页范围0~7-----------*/
void lcd_p16x16ch(unsigned char x, unsigned char y, unsigned char n)
{
unsigned char wm = 0;
unsigned int adder = 32*n;
lcd_set_pos(x, y);
for (wm=0; wm<16; wm++)
{
lcd_wrdat(f16x16[adder]);
adder += 1;
}
lcd_set_pos(x, y+1);
for (wm=0; wm<16; wm++)
{
lcd_wrdat(f16x16[adder]);
adder += 1;
}
}
/*----------显示8*16点阵 起始点坐标(x,y),x范围0~127,y范围0~7----------*/
void wrt_lcddata(unsigned char x, unsigned char y, unsigned char txd)
{
unsigned char i;
lcd_set_pos(x, y);
for (i=0; i<8; i++)
{
lcd_wrdat(f8x16[txd*16+i]);
}
lcd_set_pos(x+8, y);
for (i=0; i<8; i++)
{
lcd_wrdat(f8x16[txd*16+i+8]);
}
}
void main(void)
{
gpio_init();
oled_init();
while(1)
{
wrt_lcddata(0, 0, 0);
wrt_lcddata(50, 0, 1);
lcd_p16x16ch(0, 3, 0);
lcd_p16x16ch(0, 5, 1);
}
}
|
|