CH552/project/LCD/usr/LCD.c

490 lines
9.9 KiB
C
Raw Permalink Normal View History

2020-09-21 08:39:00 +02:00
#include "CH554.h"
#include "common.h"
//#include "font.h"
//#include "sys.h"
#include "lcd.h"
/******************************************************
GND=GND
VCC=3.3V
P00=CLK
P01=MOSI
P02=RES
P03=DC
P04=CS1
BLKΪLCD<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƹܽţ<EFBFBD><EFBFBD>͵<EFBFBD>ƽ<EFBFBD>رձ<EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD>ƽ<EFBFBD>򿪣<EFBFBD>Ĭ<EFBFBD>Ͽ<EFBFBD><EFBFBD>Բ<EFBFBD><EFBFBD>Ӵ򿪱<EFBFBD><EFBFBD><EFBFBD>
////////////////////////<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>LCD<43><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>/////////////////////////////////////
P05=FSO
P06=CS2
******************************************************/
u16 BACK_COLOR, POINT_COLOR; //<2F><><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
unsigned char bdata bitdata;
sbit bit7=bitdata^7;
sbit bit6=bitdata^6;
sbit bit5=bitdata^5;
sbit bit4=bitdata^4;
sbit bit3=bitdata^3;
sbit bit2=bitdata^2;
sbit bit1=bitdata^1;
sbit bit0=bitdata^0;
void LCD_Writ_Bus(char da) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4>
{
bitdata=da;
LCD_SDI=bit7;LCD_SCK=0;LCD_SCK=1;
LCD_SDI=bit6;LCD_SCK=0;LCD_SCK=1;
LCD_SDI=bit5;LCD_SCK=0;LCD_SCK=1;
LCD_SDI=bit4;LCD_SCK=0;LCD_SCK=1;
LCD_SDI=bit3;LCD_SCK=0;LCD_SCK=1;
LCD_SDI=bit2;LCD_SCK=0;LCD_SCK=1;
LCD_SDI=bit1;LCD_SCK=0;LCD_SCK=1;
LCD_SDI=bit0;LCD_SCK=0;LCD_SCK=1;
}
void LCD_WR_DATA8(char da) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-8λ<38><CEBB><EFBFBD><EFBFBD>
{
LCD_CS1=0;
LCD_DC=1;
LCD_Writ_Bus(da);
LCD_CS1=1;
}
void LCD_WR_DATA(int da)
{
LCD_CS1=0;
LCD_DC=1;
LCD_Writ_Bus(da>>8);
LCD_Writ_Bus(da);
LCD_CS1=1;
}
void LCD_WR_REG(char da)
{
LCD_CS1=0;
LCD_DC=0;
LCD_Writ_Bus(da);
LCD_CS1=1;
}
void LCD_WR_REG_DATA(int reg,int da)
{
LCD_WR_REG(reg);
LCD_WR_DATA(da);
}
void Address_set(unsigned int x1,unsigned int y1,unsigned int x2,unsigned int y2)
{
LCD_WR_REG(0x2a);
LCD_WR_DATA8(0x02);
LCD_WR_DATA8(x1+2);
LCD_WR_DATA8(0x02);
LCD_WR_DATA8(x2+2);
LCD_WR_REG(0x2b);
LCD_WR_DATA8(0x01);
LCD_WR_DATA8(y1+1);
LCD_WR_DATA8(0x01);
LCD_WR_DATA8(y2+1);
LCD_WR_REG(0x2C);
}
void Lcd_Init(void)
{//LCD_BLK=1;
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>Щ<EFBFBD><D0A9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
// LCD_WR_REG_DATA(0,0);
// LCD_ShowString(0,0," ");
// LCD_ShowNum(0,0,0,0);
// LCD_Show2Num(0,0,0,0);
// LCD_DrawPoint_big(0,0);
// LCD_DrawRectangle(0,0,0,0);
// Draw_Circle(0,0,0);
LCD_REST=0;
delayms(20);
LCD_REST=1;
delayms(20);
LCD_WR_REG(0x11);//Sleep exit
delayms (120);
//ST7735R Frame Rate
LCD_WR_REG(0xB1);
LCD_WR_DATA8(0x01);
LCD_WR_DATA8(0x2C);
LCD_WR_DATA8(0x2D);
LCD_WR_REG(0xB2);
LCD_WR_DATA8(0x01);
LCD_WR_DATA8(0x2C);
LCD_WR_DATA8(0x2D);
LCD_WR_REG(0xB3);
LCD_WR_DATA8(0x01);
LCD_WR_DATA8(0x2C);
LCD_WR_DATA8(0x2D);
LCD_WR_DATA8(0x01);
LCD_WR_DATA8(0x2C);
LCD_WR_DATA8(0x2D);
LCD_WR_REG(0xB4); //Column inveLCD_DC ion
LCD_WR_DATA8(0x07);
//ST7735R Power Sequence
LCD_WR_REG(0xC0);
LCD_WR_DATA8(0xA2);
LCD_WR_DATA8(0x02);
LCD_WR_DATA8(0x84);
LCD_WR_REG(0xC1);
LCD_WR_DATA8(0xC5);
LCD_WR_REG(0xC2);
LCD_WR_DATA8(0x0A);
LCD_WR_DATA8(0x00);
LCD_WR_REG(0xC3);
LCD_WR_DATA8(0x8A);
LCD_WR_DATA8(0x2A);
LCD_WR_REG(0xC4);
LCD_WR_DATA8(0x8A);
LCD_WR_DATA8(0xEE);
LCD_WR_REG(0xC5); //VCOM
LCD_WR_DATA8(0x0E);
LCD_WR_REG(0x36); //MX, MY, RGB mode
LCD_WR_DATA8(0xC0);
#ifdef USE_LANDSCAPE
LCD_WR_DATA8(0xA8); //<2F><><EFBFBD><EFBFBD>C8 <20><><EFBFBD><EFBFBD>08 A8
#else
LCD_WR_DATA8(0xC8); //<2F><><EFBFBD><EFBFBD>C8 <20><><EFBFBD><EFBFBD>08 A8
#endif
//ST7735R Gamma Sequence
LCD_WR_REG(0xe0);
LCD_WR_DATA8(0x0f);
LCD_WR_DATA8(0x1a);
LCD_WR_DATA8(0x0f);
LCD_WR_DATA8(0x18);
LCD_WR_DATA8(0x2f);
LCD_WR_DATA8(0x28);
LCD_WR_DATA8(0x20);
LCD_WR_DATA8(0x22);
LCD_WR_DATA8(0x1f);
LCD_WR_DATA8(0x1b);
LCD_WR_DATA8(0x23);
LCD_WR_DATA8(0x37);
LCD_WR_DATA8(0x00);
LCD_WR_DATA8(0x07);
LCD_WR_DATA8(0x02);
LCD_WR_DATA8(0x10);
LCD_WR_REG(0xe1);
LCD_WR_DATA8(0x0f);
LCD_WR_DATA8(0x1b);
LCD_WR_DATA8(0x0f);
LCD_WR_DATA8(0x17);
LCD_WR_DATA8(0x33);
LCD_WR_DATA8(0x2c);
LCD_WR_DATA8(0x29);
LCD_WR_DATA8(0x2e);
LCD_WR_DATA8(0x30);
LCD_WR_DATA8(0x30);
LCD_WR_DATA8(0x39);
LCD_WR_DATA8(0x3f);
LCD_WR_DATA8(0x00);
LCD_WR_DATA8(0x07);
LCD_WR_DATA8(0x03);
LCD_WR_DATA8(0x10);
LCD_WR_REG(0x2a);
LCD_WR_DATA8(0x00);
LCD_WR_DATA8(0x00+2);
LCD_WR_DATA8(0x00);
LCD_WR_DATA8(0x80+2);
LCD_WR_REG(0x2b);
LCD_WR_DATA8(0x00);
LCD_WR_DATA8(0x00+3);
LCD_WR_DATA8(0x00);
LCD_WR_DATA8(0x80+3);
LCD_WR_REG(0xF0); //Enable test command
LCD_WR_DATA8(0x01);
LCD_WR_REG(0xF6); //Disable ram power save mode
LCD_WR_DATA8(0x00);
LCD_WR_REG(0x3A); //65k mode
LCD_WR_DATA8(0x05);
LCD_WR_REG(0x29);//Display on
}
//***************************************************************
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//Color:Ҫ<><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
void LCD_Clear(u16 Color)
{
u16 i,j;
Address_set(0,0,LCD_W-1,LCD_H-1);
for(i=0;i<LCD_W;i++)
{
for (j=0;j<LCD_H;j++)
{
LCD_WR_DATA(Color);
}
}
}
//<2F><>ָ<EFBFBD><D6B8>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ʾһ<CABE><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(32*33<33><33>С)
//dcolorΪ<72><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB>gbcolorΪ<72><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
void showhanzi(unsigned int x,unsigned int y,unsigned char index)
{
unsigned char i,j;
//unsigned char *temp=hanzi;
unsigned char *temp=0;
Address_set(x,y,x+31,y+31); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
temp+=index*128;
for(j=0;j<128;j++)
{
for(i=0;i<8;i++)
{
if((*temp&(1<<i))!=0)
{
LCD_WR_DATA(POINT_COLOR);
}
else
{
LCD_WR_DATA(BACK_COLOR);
}
}
temp++;
}
}
//<2F><><EFBFBD><EFBFBD>
//POINT_COLOR:<3A>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD>ɫ
void LCD_DrawPoint(u16 x,u16 y)
{
Address_set(x,y,x,y);//<2F><><EFBFBD>ù<EFBFBD><C3B9><EFBFBD>λ<EFBFBD><CEBB>
LCD_WR_DATA(POINT_COLOR);
}
//<2F><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//POINT_COLOR:<3A>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD>ɫ
void LCD_DrawPoint_big(u16 x,u16 y)
{
LCD_Fill(x-1,y-1,x+1,y+1,POINT_COLOR);
}
//<2F><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ɫ
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С:
// (xend-xsta)*(yend-ysta)
void LCD_Fill(u16 xsta,u16 ysta,u16 xend,u16 yend,u16 color)
{
u16 i,j;
Address_set(xsta,ysta,xend,yend); //<2F><><EFBFBD>ù<EFBFBD><C3B9><EFBFBD>λ<EFBFBD><CEBB>
for(i=ysta;i<=yend;i++)
{
for(j=xsta;j<=xend;j++)LCD_WR_DATA(color);//<2F><><EFBFBD>ù<EFBFBD><C3B9><EFBFBD>λ<EFBFBD><CEBB>
}
}
//<2F><><EFBFBD><EFBFBD>
//x1,y1:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//x2,y2:<3A>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD>
void LCD_DrawLine(u16 x1, u16 y1, u16 x2, u16 y2)
{
u16 t;
int xerr=0,yerr=0,delta_x,delta_y,distance;
int incx,incy,uRow,uCol;
delta_x=x2-x1; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
delta_y=y2-y1;
uRow=x1;
uCol=y1;
if(delta_x>0)incx=1; //<2F><><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
else if(delta_x==0)incx=0;//<2F><>ֱ<EFBFBD><D6B1>
else {incx=-1;delta_x=-delta_x;}
if(delta_y>0)incy=1;
else if(delta_y==0)incy=0;//ˮƽ<CBAE><C6BD>
else{incy=-1;delta_y=-delta_y;}
if( delta_x>delta_y)distance=delta_x; //ѡȡ<D1A1><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
else distance=delta_y;
for(t=0;t<=distance+1;t++ )//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
LCD_DrawPoint(uRow,uCol);//<2F><><EFBFBD><EFBFBD>
xerr+=delta_x ;
yerr+=delta_y ;
if(xerr>distance)
{
xerr-=distance;
uRow+=incx;
}
if(yerr>distance)
{
yerr-=distance;
uCol+=incy;
}
}
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2)
{
LCD_DrawLine(x1,y1,x2,y1);
LCD_DrawLine(x1,y1,x1,y2);
LCD_DrawLine(x1,y2,x2,y2);
LCD_DrawLine(x2,y1,x2,y2);
}
//<2F><>ָ<EFBFBD><D6B8>λ<EFBFBD>û<EFBFBD>һ<EFBFBD><D2BB>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>С<EFBFBD><D0A1>Բ
//(x,y):<3A><><EFBFBD>ĵ<EFBFBD>
//r :<3A>
void Draw_Circle(u16 x0,u16 y0,u8 r)
{
int a,b;
int di;
a=0;b=r;
di=3-(r<<1); //<2F>ж<EFBFBD><D0B6>¸<EFBFBD><C2B8><EFBFBD>λ<EFBFBD>õı<C3B5>־
while(a<=b)
{
LCD_DrawPoint(x0-b,y0-a); //3
LCD_DrawPoint(x0+b,y0-a); //0
LCD_DrawPoint(x0-a,y0+b); //1
LCD_DrawPoint(x0-b,y0-a); //7
LCD_DrawPoint(x0-a,y0-b); //2
LCD_DrawPoint(x0+b,y0+a); //4
LCD_DrawPoint(x0+a,y0-b); //5
LCD_DrawPoint(x0+a,y0+b); //6
LCD_DrawPoint(x0-b,y0+a);
a++;
//ʹ<><CAB9>Bresenham<61><EFBFBD><E3B7A8>Բ
if(di<0)di +=4*a+6;
else
{
di+=10+4*(a-b);
b--;
}
LCD_DrawPoint(x0+a,y0+b);
}
}
//<2F><>ָ<EFBFBD><D6B8>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ʾһ<CABE><D2BB><EFBFBD>ַ<EFBFBD>
//num:Ҫ<><D2AA>ʾ<EFBFBD><CABE><EFBFBD>ַ<EFBFBD>:" "--->"~"
//mode:<3A><><EFBFBD>ӷ<EFBFBD>ʽ(1)<29><><EFBFBD>Ƿǵ<C7B7><C7B5>ӷ<EFBFBD>ʽ(0)
//<2F><>ָ<EFBFBD><D6B8>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ʾһ<CABE><D2BB><EFBFBD>ַ<EFBFBD>
//num:Ҫ<><D2AA>ʾ<EFBFBD><CABE><EFBFBD>ַ<EFBFBD>:" "--->"~"
//mode:<3A><><EFBFBD>ӷ<EFBFBD>ʽ(1)<29><><EFBFBD>Ƿǵ<C7B7><C7B5>ӷ<EFBFBD>ʽ(0)
void LCD_ShowChar(u16 x,u16 y,u8 num,u8 mode)
{
u8 temp;
u8 pos,t;
u16 x0=x;
u16 colortemp=POINT_COLOR;
if(x>LCD_W-16||y>LCD_H-16)return;
//<2F><><EFBFBD>ô<EFBFBD><C3B4><EFBFBD>
num=num-' ';//<2F>õ<EFBFBD>ƫ<EFBFBD>ƺ<EFBFBD><C6BA><EFBFBD>ֵ
Address_set(x,y,x+8-1,y+16-1); //<2F><><EFBFBD>ù<EFBFBD><C3B9><EFBFBD>λ<EFBFBD><CEBB>
if(!mode) //<2F>ǵ<EFBFBD><C7B5>ӷ<EFBFBD>ʽ
{
for(pos=0;pos<16;pos++)
{
//temp=asc2_1608[(u16)num*16+pos]; //<2F><><EFBFBD><EFBFBD>1608<30><38><EFBFBD><EFBFBD>
for(t=0;t<8;t++)
{
if(temp&0x01)POINT_COLOR=colortemp;
else POINT_COLOR=BACK_COLOR;
LCD_WR_DATA(POINT_COLOR);
temp>>=1;
x++;
}
x=x0;
y++;
}
}else//<2F><><EFBFBD>ӷ<EFBFBD>ʽ
{
for(pos=0;pos<16;pos++)
{
//temp=asc2_1608[(u16)num*16+pos]; //<2F><><EFBFBD><EFBFBD>1608<30><38><EFBFBD><EFBFBD>
for(t=0;t<8;t++)
{
if(temp&0x01)LCD_DrawPoint(x+t,y+pos);//<2F><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>
temp>>=1;
}
}
}
POINT_COLOR=colortemp;
}
//m^n<><6E><EFBFBD><EFBFBD>
u32 mypow(u8 m,u8 n)
{
u32 result=1;
while(n--)result*=m;
return result;
}
//<2F><>ʾ2<CABE><32><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//x,y :<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//len :<3A><><EFBFBD>ֵ<EFBFBD>λ<EFBFBD><CEBB>
//color:<3A><>ɫ
//num:<3A><>ֵ(0~4294967295);
void LCD_ShowNum(u16 x,u16 y,u32 num,u8 len)
{
u8 t,temp;
u8 enshow=0;
num=(u16)num;
for(t=0;t<len;t++)
{
temp=(num/mypow(10,len-t-1))%10;
if(enshow==0&&t<(len-1))
{
if(temp==0)
{
LCD_ShowChar(x+8*t,y,' ',0);
continue;
}else enshow=1;
}
LCD_ShowChar(x+8*t,y,temp+48,0);
}
}
//<2F><>ʾ2<CABE><32><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//x,y:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//num:<3A><>ֵ(0~99);
void LCD_Show2Num(u16 x,u16 y,u16 num,u8 len)
{
u8 t,temp;
for(t=0;t<len;t++)
{
temp=(num/mypow(10,len-t-1))%10;
LCD_ShowChar(x+8*t,y,temp+'0',0);
}
}
//<2F><>ʾ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
//x,y:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//*p:<3A>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>ַ
//<2F><>16<31><36><EFBFBD><EFBFBD>
void LCD_ShowString(u16 x,u16 y,const u8 *p)
{
while(*p!='\0')
{
if(x>LCD_W-16){x=0;y+=16;}
if(y>LCD_H-16){y=x=0;LCD_Clear(RED);}
LCD_ShowChar(x,y,*p,0);
x+=8;
p++;
}
}