/*
CPU:stm8
designed by cache lee
2009-06-25
*/
#include "ssd1283a_16tft.h"
#include "font8x16.h"
#include "stm8s_lib.h"
unsigned int TextColor = 0x0000, BackColor = 0xFFFF;
#define LcdRST1 GPIO_WriteHigh(GPIOE,GPIO_PIN_5)
#define LcdRST0 GPIO_WriteLow(GPIOE,GPIO_PIN_5)
#define LcdCS1 GPIO_WriteHigh(GPIOC,GPIO_PIN_6)
#define LcdCS0 GPIO_WriteLow(GPIOC,GPIO_PIN_6)
#define sdata1 GPIO_WriteHigh(GPIOE,GPIO_PIN_6)
#define sdata0 GPIO_WriteLow(GPIOE,GPIO_PIN_6)
#define sclk1 GPIO_WriteHigh(GPIOE,GPIO_PIN_5)
#define sclk0 GPIO_WriteLow(GPIOE,GPIO_PIN_5)
#define c_d1 GPIO_WriteHigh(GPIOC,GPIO_PIN_7)
#define c_d0 GPIO_WriteLow(GPIOC,GPIO_PIN_7)
/*
sbit LcdRST //RST --PE5
sbit LcdCS //MOSI --PC6
sbit sdata //SDA --PE6
sbit sclk //SCL --PC5
sbit c_d //MISO --PC7
*/
void delayms(unsigned int ms);
void write_data(unsigned char dat);
void write_data16(unsigned char datH,unsigned char datL);
void write_cmd(unsigned char dat);
void tft_16lcd_init();
void LCD_Clear();
void LCD_SetTextColor(unsigned int Color);
void LCD_SetBackColor(unsigned int Color);
void color_V(void);
void color_H(void);
void LCD_ShowChar(uchar x,uint y,char ch);
void LCD_ShowString(uchar x,uint y,char *p);
void ShowBMP(uchar x,uchar y,uchar width,uchar height,char *c);
//void ChineseChar(uint x,uint y,int size,uint color,char c);
//char reserve[3]_at_ 0x3b; //保留0x3b开始的3个字节
/**********************************************************
ms延时子程序
**********************************************************/
void delayms(unsigned int ms)
{
unsigned char k;
while (ms--)
{
for (k = 0; k < 114; k++)
;
}
}
/**********************************************************
LCD写数据子函数(8位)
**********************************************************/
void write_data(unsigned char dat)
{
unsigned char i;
c_d1;
LcdCS0;
for(i=0;i<8;i++)
{
sclk0;
if(dat>=0x80)
sdata1;
else
sdata0;
dat=dat<<1;
//sdata = CY;
sclk1;
}
LcdCS1;
}
/**********************************************************
LCD写数据子函数(16位)
**********************************************************/
void write_data16(unsigned char datH,unsigned char datL)
{
unsigned char i;
c_d1;
LcdCS0;
for(i=0;i<8;i++)
{
sclk0;
//datH=datH<<1;
//sdata = CY;
if(datH>=0x80)
sdata1;
else
sdata0;
datH=datH<<1;
sclk1;
}
for(i=0;i<8;i++)
{
sclk0;
// datL=datL<<1;
// sdata = CY;
if(datL>=0x80)
sdata1;
else
sdata0;
datL=datL<<1;
sclk1;
}
LcdCS1;
}
/**********************************************************
LCD写命令子函数
**********************************************************/
void write_cmd(unsigned char dat)
{
unsigned char i;
c_d0;
LcdCS0;
for(i=0;i<8;i++)
{
sclk0;
//dat=dat<<1;
//sdata = CY;
if(dat>=0x80)
sdata1;
else
sdata0;
dat=dat<<1;
sclk1;
}
LcdCS1;
}
/**********************************************************
LCD初始化子函数
**********************************************************/
void tft_16lcd_init()
{
LcdCS1;
sclk0;
LcdRST1; //LCD复位
delayms(100);
LcdRST0;
delayms(5);
LcdRST1;
delayms(100);
write_cmd(0x10); //Power control 1
write_data16(0x2f,0xce); //2f8e
write_cmd(0x11); //Power control 2
write_data16(0x00,0x0c);
write_cmd(0x07);
write_data16(0x00,0x21);
write_cmd(0x28);
write_data16(0x00,0x06);
write_cmd(0x28);
write_data16(0x00,0x05);
write_cmd(0x27);
write_data16(0x05,0x7f);
write_cmd(0x29);
write_data16(0x89,0xa1);
write_cmd(0x00); //write index, 16bits, high 8 bits is 0x00
write_data16(0x00,0x01); //write instruction, 16bits
delayms(100); //Oscillator on
write_cmd(0x29);
write_data16(0x80,0xb0);
delayms(30);
write_cmd(0x29);
write_data16(0xff,0xfe);
write_cmd(0x07); //Display Control
write_data16(0x00,0x23);
delayms(30);
write_cmd(0x07);
write_data16(0x00,0x33);
write_cmd(0x01);
write_data16(0x21,0x83); //23 TB=1 G0 T0 G131 //2183
write_cmd(0x2f);
write_data16(0xff,0xff);
write_cmd(0x2c); //Oscillator frequency
write_data16(0x80,0x00);
write_cmd(0x03); //Entry Mode
write_data16(0x68,0x30); //6830
write_cmd(0x27);
write_data16(0x05,0x70);
write_cmd(0x02);
write_data16(0x03,0x00);
write_cmd(0x0b);
write_data16(0x58,0x0c);
write_cmd(0x12); //Power control 3
write_data16(0x06,0x09);
write_cmd(0x13); //Power control 4
write_data16(0x31,0x00);
write_cmd(0x2a);
write_data16(0x1d,0xd0);
write_cmd(0x2b);
write_data16(0x0a,0x90);
write_cmd(0x2d);
write_data16(0x31,0x0f);
delayms(100);
write_cmd(0x1e); //Power control 5
write_data16(0x00,0xbf);
delayms(1);
write_cmd(0x1e);
write_data16(0x00,0x00);
delayms(100);
write_cmd(0x45); //V
write_data16(0x83,0x00);
write_cmd(0x44); //H
write_data16(0x83,0x00);
write_cmd(0x21); //RAM Address Set
write_data16(0x00,0x00);
write_cmd(0x22); //write data to GRAM
LCD_Clear();
}
/*******************************************************************************
* Function Name : LCD_Clear
* Description : Clears the hole LCD.
* Input : Color: the color of the background.
* Output : None
* Return : None
*******************************************************************************/
void LCD_Clear()
{
unsigned int i;
write_cmd(0x45); //V
write_data16(0x83,0x00);
write_cmd(0x44); //H
write_data16(0x83,0x00);
write_cmd(0x21); //RAM Address Set
write_data16(0x00,0x00);
write_cmd(0x22); //write data to GRAM
for (i=0;i<17424;i++) //132*132=17424
{
write_data(BackColor>>8); //全屏白色
write_data(BackColor);
}
}
/*******************************************************************************
* Function Name : LCD_SetTextColor
* Description : Sets the Text color.
* Input : - Color: specifies the Text color code RGB(5-6-5).
* Output : - TextColor: Text color global variable used by LCD_DrawChar
* and LCD_DrawPicture functions.
* Return : None
*******************************************************************************/
void LCD_SetTextColor(unsigned int Color)
{
TextColor = Color;
}
/*******************************************************************************
* Function Name : LCD_SetBackColor
* Description : Sets the Background color.
* Input : - Color: specifies the Background color code RGB(5-6-5).
* Output : - BackColor: Background color global variable used by
* LCD_DrawChar and LCD_DrawPicture functions.
* Return : None
*******************************************************************************/
void LCD_SetBackColor(unsigned int Color)
{
BackColor = Color;
}
/**********************************************************
彩条输出子函数(V)
**********************************************************/
void color_V(void)
{
unsigned int i;
write_cmd(0x21); //RAM Address Set
write_data16(0x00,0x00);
write_cmd(0x45); //V
write_data16(131,0x00);
write_cmd(0x44); //H