#include "3510i.h"
#include "math.h"
#include "english_6x8_pixel.h"
uchar LCD_BGColor;
uchar LCD_PenColor;
void LCD_Delay(void)
{
uint i;
for(i = 0; i < 32768; i ++)
asm("NOP");
}
void LCD_Reset(void)
{
clr_bit(LCD_RST_PORT, LCD_RST); //set RST = L
LCD_Delay();
set_bit(LCD_RST_PORT, LCD_RST); //set RST = H
LCD_Delay();
}
void LCD_SendCommand(uchar cmd)
{
set_bit(LCD_SIO_DDR, LCD_SIO); //set SDI.DDR = 1
clr_bit(LCD_CS_PORT, LCD_CS); //set CS = L
clr_bit(LCD_SCL_PORT, LCD_SCL); //set SCK = L
clr_bit(LCD_SIO_PORT, LCD_SIO); //send 0, for command
set_bit(LCD_SCL_PORT, LCD_SCL); //set SCK = H, latch data
//Bit 0(MSB)
clr_bit(LCD_SCL_PORT, LCD_SCL);
if(cmd & 0x80)
set_bit(LCD_SIO_PORT, LCD_SIO);
else
clr_bit(LCD_SIO_PORT, LCD_SIO);
set_bit(LCD_SCL_PORT, LCD_SCL);
//Bit 1
clr_bit(LCD_SCL_PORT, LCD_SCL);
if(cmd & 0x40)
set_bit(LCD_SIO_PORT, LCD_SIO);
else
clr_bit(LCD_SIO_PORT, LCD_SIO);
set_bit(LCD_SCL_PORT, LCD_SCL);
//Bit 2
clr_bit(LCD_SCL_PORT, LCD_SCL);
if(cmd & 0x20)
set_bit(LCD_SIO_PORT, LCD_SIO);
else
clr_bit(LCD_SIO_PORT, LCD_SIO);
set_bit(LCD_SCL_PORT, LCD_SCL);
//Bit 3
clr_bit(LCD_SCL_PORT, LCD_SCL);
if(cmd & 0x10)
set_bit(LCD_SIO_PORT, LCD_SIO);
else
clr_bit(LCD_SIO_PORT, LCD_SIO);
set_bit(LCD_SCL_PORT, LCD_SCL);
//Bit 4
clr_bit(LCD_SCL_PORT, LCD_SCL);
if(cmd & 0x08)
set_bit(LCD_SIO_PORT, LCD_SIO);
else
clr_bit(LCD_SIO_PORT, LCD_SIO);
set_bit(LCD_SCL_PORT, LCD_SCL);
//Bit 5
clr_bit(LCD_SCL_PORT, LCD_SCL);
if(cmd & 0x04)
set_bit(LCD_SIO_PORT, LCD_SIO);
else
clr_bit(LCD_SIO_PORT, LCD_SIO);
set_bit(LCD_SCL_PORT, LCD_SCL);
//Bit 6
clr_bit(LCD_SCL_PORT, LCD_SCL);
if(cmd & 0x02)
set_bit(LCD_SIO_PORT, LCD_SIO);
else
clr_bit(LCD_SIO_PORT, LCD_SIO);
set_bit(LCD_SCL_PORT, LCD_SCL);
//Bit 7(LSB)
clr_bit(LCD_SCL_PORT, LCD_SCL);
if(cmd & 0x01)
set_bit(LCD_SIO_PORT, LCD_SIO);
else
clr_bit(LCD_SIO_PORT, LCD_SIO);
set_bit(LCD_SCL_PORT, LCD_SCL);
//done
}
void LCD_SendData(uchar data)
{
set_bit(LCD_SIO_DDR, LCD_SIO); //set SDI.DDR = 1
clr_bit(LCD_CS_PORT, LCD_CS); //set CS = L
clr_bit(LCD_SCL_PORT, LCD_SCL); //set SCK = L
set_bit(LCD_SIO_PORT, LCD_SIO); //send 1, for command
set_bit(LCD_SCL_PORT, LCD_SCL); //set SCK = H, latch data
//Bit 0(MSB)
clr_bit(LCD_SCL_PORT, LCD_SCL);
if(data & 0x80)
set_bit(LCD_SIO_PORT, LCD_SIO);
else
clr_bit(LCD_SIO_PORT, LCD_SIO);
set_bit(LCD_SCL_PORT, LCD_SCL);
//Bit 1
clr_bit(LCD_SCL_PORT, LCD_SCL);
if(data & 0x40)
set_bit(LCD_SIO_PORT, LCD_SIO);
else
clr_bit(LCD_SIO_PORT, LCD_SIO);
set_bit(LCD_SCL_PORT, LCD_SCL);
//Bit 2
clr_bit(LCD_SCL_PORT, LCD_SCL);
if(data & 0x20)
set_bit(LCD_SIO_PORT, LCD_SIO);
else
clr_bit(LCD_SIO_PORT, LCD_SIO);
set_bit(LCD_SCL_PORT, LCD_SCL);
//Bit 3
clr_bit(LCD_SCL_PORT, LCD_SCL);
if(data & 0x10)
set_bit(LCD_SIO_PORT, LCD_SIO);
else
clr_bit(LCD_SIO_PORT, LCD_SIO);
set_bit(LCD_SCL_PORT, LCD_SCL);
//Bit 4
clr_bit(LCD_SCL_PORT, LCD_SCL);
if(data & 0x08)
set_bit(LCD_SIO_PORT, LCD_SIO);
else
clr_bit(LCD_SIO_PORT, LCD_SIO);
set_bit(LCD_SCL_PORT, LCD_SCL);
//Bit 5
clr_bit(LCD_SCL_PORT, LCD_SCL);
if(data & 0x04)
set_bit(LCD_SIO_PORT, LCD_SIO);
else
clr_bit(LCD_SIO_PORT, LCD_SIO);
set_bit(LCD_SCL_PORT, LCD_SCL);
//Bit 6
clr_bit(LCD_SCL_PORT, LCD_SCL);
if(data & 0x02)
set_bit(LCD_SIO_PORT, LCD_SIO);
else
clr_bit(LCD_SIO_PORT, LCD_SIO);
set_bit(LCD_SCL_PORT, LCD_SCL);
//Bit 7(LSB)
clr_bit(LCD_SCL_PORT, LCD_SCL);
if(data & 0x01)
set_bit(LCD_SIO_PORT, LCD_SIO);
else
clr_bit(LCD_SIO_PORT, LCD_SIO);
set_bit(LCD_SCL_PORT, LCD_SCL);
//done
}
void LCD_ReadDummy(void)
{
clr_bit(LCD_SIO_DDR, LCD_SIO); //set SDI.DDR = 0
set_bit(LCD_SIO_PORT, LCD_SIO);
clr_bit(LCD_CS_PORT, LCD_CS); //set CS = L
clr_bit(LCD_SCL_PORT, LCD_SCL);
set_bit(LCD_SCL_PORT, LCD_SCL);
}
uchar LCD_ReadData(void)
{
uchar r = 0;
clr_bit(LCD_SIO_DDR, LCD_SIO); //set SDI.DDR = 0
set_bit(LCD_SIO_PORT, LCD_SIO);
clr_bit(LCD_CS_PORT, LCD_CS); //set CS = L
//Bit 0(MSB)
clr_bit(LCD_SCL_PORT, LCD_SCL);
set_bit(LCD_SCL_PORT, LCD_SCL);
if(set_bit(LCD_SIO_PIN, LCD_SIO))
r |= 0x80;
else
r &= ~0x80;
//Bit 1
clr_bit(LCD_SCL_PORT, LCD_SCL);
set_bit(LCD_SCL_PORT, LCD_SCL);
if(set_bit(LCD_SIO_PIN, LCD_SIO))
r |= 0x40;
else
r &= ~0x40;
//Bit 2
clr_bit(LCD_SCL_PORT, LCD_SCL);
set_bit(LCD_SCL_PORT, LCD_SCL);
if(set_bit(LCD_SIO_PIN, LCD_SIO))
r |= 0x20;
else
r &= ~0x20;
//Bit 3
clr_bit(LCD_SCL_PORT, LCD_SCL);
set_bit(LCD_SCL_PORT, LCD_SCL);
if(set_bit(LCD_SIO_PIN, LCD_SIO))
r |= 0x10;
else
r &= ~0x10;
//Bit 4
clr_bit(LCD_SCL_PORT, LCD_SCL);
set_bit(LCD_SCL_PORT, LCD_SCL);
if(set_bit(LCD_SIO_PIN, LCD_SIO))
r |= 0x08;
else
r &= ~0x08;
//Bit 5
clr_bit(LCD_SCL_PORT, LCD_SCL);
set_bit(LCD_SCL_PORT, LCD_SCL);
if(set_bit(LCD_SIO_PIN, LCD_SIO))
r |= 0x04;
else
r &= ~0x04;
//Bit 6
clr_bit(LCD_SCL_PORT, LCD_SCL);
set_bit(LCD_SCL_PORT, LCD_SCL);
if(set_bit(LCD_SIO_PIN, LCD_SIO))
r |= 0x02;
else
r &= ~0x02;
//Bit 7(LSB)
clr_bit(LCD_SCL_PORT, LCD_SCL);
set_bit(LCD_SCL_PORT, LCD_SCL);
if(set_bit(LCD_SIO_PIN, LCD_SIO))
r |= 0x01;
else
r &= ~0x01;
//done
return r;
}
void LCD_Initialize(void)
{
uchar i;
set_bit(LCD_RST_PORT, LCD_RST); //set RST = H
set_bit(LCD_RST_DDR, LCD_RST); //set RST.DDR = 1
set_bit(LCD_CS_PORT, LCD_CS); //set CS = H
set_bit(LCD_CS_DDR, LCD_CS); //set CS.DDR = 1
clr_bit(LCD_SIO_PORT, LCD_SIO); //set SDI = L
clr_bit(LCD_SIO_DDR, LCD_SIO); //set SDI.DDR = 0
set_bit(LCD_SCL_PORT, LCD_SCL); //set SCK = H
set_bit(LCD_SCL_DDR, LCD_SCL); //set SCK.DDR = 1
LCD_Reset();
LCD_SendCommand(0x01); //software reset
LCD_DataOver();
LCD_Delay();
LCD_SendCommand(0xc6); //initial escape
LCD_DataOver();
LCD_SendCommand(0xb9); //refresh set
LCD_SendData(0x00);
LCD_DataOver();
LCD_SendCommand(0xb6); //display control
LCD_SendData(0x80);
LCD_SendData(0x80);
LCD_SendData(0x81);
//LCD_SendData(0x7f);
//LCD_SendData(0x14);
LCD_SendData(84);
LCD_SendData(69);
LCD_SendData(82);
LCD_SendData(67);
LCD_DataOver();
LCD_SendCommand(0xb3); //gray scale position set
LCD_SendData(1);
LCD_SendData(2);
LCD_SendData(4);
LCD_SendData(8);
LCD_SendData(16);
LCD_SendData(30);
LCD_SendData(40);
LCD_SendData(50);
LCD_SendData(60);
LCD_SendData(70);
LCD_SendData(80);
LCD_SendData(90);
LCD_SendData(100);
LCD_SendData(110);
LCD_SendData(127);
LCD_DataOver();
LCD_SendCommand(0xb5); //gamma curve set
LCD_SendData(0x01);
LCD_DataOver();
LCD_SendCommand(0x36); //memery access control
LCD_SendData(0x00);
LCD_DataOver();
LCD_SendCommand(0xbd); //common driver output select//很重要的
LCD_SendData(0x04);
LCD_DataOver();
LCD_SendCommand(0xbe); //power control
LCD_SendData(0x04);
LCD_DataOver();
LCD_SendCommand(0x11); //sleep out
LCD_DataOver();
LCD_SendCommand(0xba); //voltage control
LCD_SendData(127);
LCD_SendData(3);
LCD_DataOver();
LCD_SendCommand(0xb7); //temperature gradient set
for(i = 0; i < 14; i ++)
LCD_SendData(0x00);
LCD_DataOver();
LCD_SendCommand(0x29); //display ON
LCD_DataOver();
LCD_SendCommand(0x03); //booster voltage ON
LCD_DataOver();
LCD_Delay();
LCD_SendCommand(0x20); //display inversion OFF
LCD_DataOver();
LCD_SendCommand(0x3a); //interface pixel format
LCD_SendData(0x02);
LCD_DataOver();
LCD_SendCommand(0x2d); //colour set
//red
LCD_SendData(0x00);
LCD_SendData(0x02);
LCD_SendData(0x03);
LCD_SendData(0x04);
LCD_SendData(0x05);
LCD_SendData(0x06);
LCD_SendData(0x08);
//green
LCD_SendData(0x0f);
LCD_SendData(0x00);
LCD_SendData(0x02);
LCD_SendData(0x03);
LCD_SendData(0x04);
LCD_SendData(0x05);
LCD_SendData(0x06);
LCD_SendData(0x08);
LCD_SendData(0x0f);
//blue
LCD_SendData(0x00);
LCD_SendData(0x03);
LCD_SendData(0x06);
LCD_SendData(0x0f);
LCD_DataOver();
LCD_SendCommand(0x25); //write contrast
LCD_SendData(71);
LCD_DataOver();
LCD_BGColor = 0x00;
LCD_PenColor = 0xff;
}
void LCD_ClearScreen(void)
{
uchar x, y;
LCD_SendCommand(0x2a); //column address set
LCD_SendData(0);
LCD_SendData(97);
LCD_DataOver();
LCD_SendCommand(0x2b); //page address set
LCD_SendData(0);
LCD_SendData(66);
LCD_DataOver();
LCD_SendCommand(0x2c); //memory write
for(y = 0; y < 67; y ++)
for(x = 0; x < 98; x ++)
LCD_SendData(LCD_BGColor);
LCD_DataOver();
}
void LCD_ReadPixel(uchar x1, uchar y1, uchar x2, uchar y2, uchar *b)
{
uchar x, y;
LCD_SendCommand(0x2a); //column address set
LCD_SendData(x1);
LCD_SendData(x2);
LCD_DataOver();
LCD_SendCommand(0x2b); //page address set
LCD_SendData(y1);
LCD_SendData(y2);
LCD_DataOver();
LCD_SendCommand(0x2e); //RAM data read
for(y = y1; y for(x = x1; x {
LCD_ReadDummy();
*(b ++) = LCD_ReadData();
}
LCD_DataOver();
LCD_SendCommand(0x00); //NOP
LCD_SendData(0x00);
LCD_DataOver();
}
void LCD_WritePixel(const unsigned char *b, uchar x1, uchar y1, uchar x2, uchar y2)
{
uchar x, y;
LCD_SendCommand(0x2a); //column address set
LCD_SendData(x1);
LCD_SendData(x2);
LCD_DataOver();
LCD_SendCommand(0x2b); //page address set
LCD_SendData(y1);
LCD_SendData(y2);
LCD_DataOver();
LCD_SendCommand(0x2c); //memory write
for(y = y1; y for(x = x1; x {
LCD_SendData(*(b ++));
///LCD_SendData(*(b ++));
///LCD_SendData(*(b ++));
}
LCD_DataOver();
}
void LCD_WriteEnglishChar(uchar c, uchar x, uchar y)
{
uchar row,line;
uchar temp[6][8];
c-=32;
for(row=0;row {
for(line=0;line {
if(get_bit(font6x8[c][row],line))
temp[row][line]=LCD_PenColor;
else
temp[row][line]=LCD_BGColor;
}
}
LCD_SendCommand(0x2a); //column address set
LCD_SendData(x);
LCD_SendData(x+5);
LCD_DataOver();
LCD_SendCommand(0x2b); //page address set
LCD_SendData(y);
LCD_SendData(y+7);
LCD_DataOver();
LCD_SendCommand(0x2c);
for(row=0;row for(line=0;line LCD_SendData(temp[line][row]);
LCD_DataOver();
}
void LCD_WriteEnglishString(uchar *string, uchar x, uchar y)
{
while(*string)
{
LCD_WriteEnglishChar(*string, x, y);
string++;
x+=6;
if(x>=92)
{
x=0;
y+=8;
if((*string)==32)//32 is space char
string++;
if(y>=67)
y=0;
}
}
}
//16*16的字
/*void LCD_WriteChineseChar(uchar (*c)[32], uchar x, uchar y)
{
uchar row,line;
uchar temp[16][16];
for(row=0;row {
for(line=0;line {
if(get_bit((*c)[row],line))
temp[row][line]=LCD_PenColor;
else
temp[row][line]=LCD_BGColor;
}
}
for(row=0;row {
for(line=0;line {
if(get_bit((*c)[row+16],line))
temp[row][line+8]=LCD_PenColor;
else
temp[row][line+8]=LCD_BGColor;
}
}
LCD_SendCommand(0x2a); //column address set
LCD_SendData(x);
LCD_SendData(x+15);
LCD_DataOver();
LCD_SendCommand(0x2b); //page address set
LCD_SendData(y);
LCD_SendData(y+15);
LCD_DataOver();
LCD_SendCommand(0x2c);
for(row=0;row for(line=0;line LCD_SendData(temp[line][row]);
LCD_DataOver();
}
void LCD_WriteChineseString(uchar (*c)[32], uchar num, uchar x, uchar y)
{
uchar i;
for(i=0;i {
LCD_WriteChineseChar(c, x, y);
c++;
x+=16;
if(x>=96)
{
x=0;
y+=16;
if(y>=62)
y=0;
}
}
}*/
//12x12
void LCD_WriteChineseChar(const uchar (*c)[24], uchar x, uchar y)
{
uchar row,line;
uchar temp[12][16];
for(row=0;row {
for(line=0;line {
if(get_bit((*c)[row],line))
temp[row][line]=LCD_PenColor;
else
temp[row][line]=LCD_BGColor;
}
}
for(row=0;row {
for(line=0;line {
if(get_bit((*c)[row+12],line))
temp[row][line+8]=LCD_PenColor;
else
temp[row][line+8]=LCD_BGColor;
}
}
LCD_SendCommand(0x2a); //column address set
LCD_SendData(x);
LCD_SendData(x+11);
LCD_DataOver();
LCD_SendCommand(0x2b); //page address set
LCD_SendData(y);
LCD_SendData(y+15);
LCD_DataOver();
LCD_SendCommand(0x2c);
for(row=0;row for(line=0;line LCD_SendData(temp[line][row]);
LCD_DataOver();
}
void LCD_WriteChineseString(const uchar (*c)[24], uchar num, uchar x, uchar y)
{
uchar i;
for(i=0;i {
LCD_WriteChineseChar(c, x, y);
c++;
x+=12;
if(x>=96)
{
x=0;
y+=13;//行与行之间空一搁
if(y>=62)
y=0;
}
}
}
void LCD_EraseBlock(uchar x1,uchar y1, uchar x2, uchar y2)
{
uchar x, y;
LCD_SendCommand(0x2a); //column address set
LCD_SendData(x1);
LCD_SendData(x2);
LCD_DataOver();
LCD_SendCommand(0x2b); //page address set
LCD_SendData(y1);
LCD_SendData(y2);
LCD_DataOver();
LCD_SendCommand(0x2c); //memory write
for(y = y1; y for(x = x1; x LCD_SendData(LCD_BGColor);
LCD_DataOver();
}
void LCD_DrawPoint(uchar x, uchar y)
{
LCD_SendCommand(0x2a); //column address set
LCD_SendData(x);
LCD_SendData(x);
LCD_DataOver();
LCD_SendCommand(0x2b); //page address set
LCD_SendData(y);
LCD_SendData(y);
LCD_DataOver();
LCD_SendCommand(0x2c); //memory write
LCD_SendData(LCD_PenColor);
LCD_DataOver();
}
void LCD_DrawLine(uchar x1, uchar y1, uchar x2, uchar y2)
{
uchar x, y, t;
if((x1 == x2) && (y1 == y2))
LCD_DrawPoint(x1, y1);
else if(fabs(y2 - y1) > fabs(x2 - x1))
{
if(y1 > y2)
{
t = y1;
y1 = y2;
y2 = t;
t = x1;
x1 = x2;
x2 = t;
}
for(y = y1; y {
x = (y - y1) * (x2 - x1) / (y2 - y1) + x1;
LCD_DrawPoint(x, y);
}
}
else
{
if(x1 > x2)
{
t = y1;
y1 = y2;
y2 = t;
t = x1;
x1 = x2;
x2 = t;
}
for(x = x1; x {
y = (x - x1) * (y2 - y1) / (x2 - x1) + y1;
LCD_DrawPoint(x, y);
}
}
}
void LCD_DrawRectangle(uchar x1, uchar y1, uchar x2, uchar y2)
{
LCD_DrawLine(x1, y1, x2, y1);
LCD_DrawLine(x2, y1, x2, y2);
LCD_DrawLine(x2, y2, x1, y2);
LCD_DrawLine(x1, y2, x1, y1);
}