使用内总RC振荡,4倍频,提供给SPI模块,速度还是不太理想。
SPI四分频
- #include "font.h"
- #include "sys.h"
- #include "lcd.h"
- //unsigned char bitdata;
- extern void tx_spi_data(unsigned char dat);
- u16 BACK_COLOR, POINT_COLOR;
- void LCD_Writ_Bus(char da)
- {
- tx_spi_data(da);
- }
- void LCD_WR_DATA8(char da) //·¢ËÍÊý¾Ý-8λ²ÎÊý
- {
- LCD_DC=1;
- LCD_Writ_Bus(da);
- }
- void LCD_WR_DATA(int da)
- {
- LCD_DC=1;
- LCD_Writ_Bus(da>>8);
- LCD_Writ_Bus(da);
- }
- void LCD_WR_REG(char da)
- {
- LCD_DC=0;
- LCD_Writ_Bus(da);
- }
- 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(x1>>8);
- LCD_WR_DATA8(x1);
- LCD_WR_DATA8(x2>>8);
- LCD_WR_DATA8(x2);
- LCD_WR_REG(0x2b);
- LCD_WR_DATA8(y1>>8);
- LCD_WR_DATA8(y1);
- LCD_WR_DATA8(y2>>8);
- LCD_WR_DATA8(y2);
- LCD_WR_REG(0x2C);
- }
- void Lcd_Init(void)
- {
- LCD_CS =1;
- LCD_REST=1;
- delayms(5);
- LCD_REST=0;
- delayms(5);
- LCD_REST=1;
- LCD_CS=1;
- delayms(5);
- LCD_CS =0;
- LCD_WR_REG(0xCB);
- LCD_WR_DATA8(0x39);
- LCD_WR_DATA8(0x2C);
- LCD_WR_DATA8(0x00);
- LCD_WR_DATA8(0x34);
- LCD_WR_DATA8(0x02);
- LCD_WR_REG(0xCF);
- LCD_WR_DATA8(0x00);
- LCD_WR_DATA8(0XC1);
- LCD_WR_DATA8(0X30);
- LCD_WR_REG(0xE8);
- LCD_WR_DATA8(0x85);
- LCD_WR_DATA8(0x00);
- LCD_WR_DATA8(0x78);
- LCD_WR_REG(0xEA);
- LCD_WR_DATA8(0x00);
- LCD_WR_DATA8(0x00);
- LCD_WR_REG(0xED);
- LCD_WR_DATA8(0x64);
- LCD_WR_DATA8(0x03);
- LCD_WR_DATA8(0X12);
- LCD_WR_DATA8(0X81);
- LCD_WR_REG(0xF7);
- LCD_WR_DATA8(0x20);
- LCD_WR_REG(0xC0); //Power control
- LCD_WR_DATA8(0x23); //VRH[5:0]
- LCD_WR_REG(0xC1); //Power control
- LCD_WR_DATA8(0x10); //SAP[2:0];BT[3:0]
- LCD_WR_REG(0xC5); //VCM control
- LCD_WR_DATA8(0x3e); //¶Ô±È¶Èµ÷½Ú
- LCD_WR_DATA8(0x28);
- LCD_WR_REG(0xC7); //VCM control2
- LCD_WR_DATA8(0x86); //--
- LCD_WR_REG(0x36); // Memory Access Control
- LCD_WR_DATA8(0x48); //C8 //48 68ÊúÆÁ//28 E8 ºáÆÁ
- LCD_WR_REG(0x3A);
- LCD_WR_DATA8(0x55);
- LCD_WR_REG(0xB1);
- LCD_WR_DATA8(0x00);
- LCD_WR_DATA8(0x18);
- LCD_WR_REG(0xB6); // Display Function Control
- LCD_WR_DATA8(0x08);
- LCD_WR_DATA8(0x82);
- LCD_WR_DATA8(0x27);
- LCD_WR_REG(0xF2); // 3Gamma Function Disable
- LCD_WR_DATA8(0x00);
- LCD_WR_REG(0x26); //Gamma curve selected
- LCD_WR_DATA8(0x01);
- LCD_WR_REG(0xE0); //Set Gamma
- LCD_WR_DATA8(0x0F);
- LCD_WR_DATA8(0x31);
- LCD_WR_DATA8(0x2B);
- LCD_WR_DATA8(0x0C);
- LCD_WR_DATA8(0x0E);
- LCD_WR_DATA8(0x08);
- LCD_WR_DATA8(0x4E);
- LCD_WR_DATA8(0xF1);
- LCD_WR_DATA8(0x37);
- LCD_WR_DATA8(0x07);
- LCD_WR_DATA8(0x10);
- LCD_WR_DATA8(0x03);
- LCD_WR_DATA8(0x0E);
- LCD_WR_DATA8(0x09);
- LCD_WR_DATA8(0x00);
- LCD_WR_REG(0XE1); //Set Gamma
- LCD_WR_DATA8(0x00);
- LCD_WR_DATA8(0x0E);
- LCD_WR_DATA8(0x14);
- LCD_WR_DATA8(0x03);
- LCD_WR_DATA8(0x11);
- LCD_WR_DATA8(0x07);
- LCD_WR_DATA8(0x31);
- LCD_WR_DATA8(0xC1);
- LCD_WR_DATA8(0x48);
- LCD_WR_DATA8(0x08);
- LCD_WR_DATA8(0x0F);
- LCD_WR_DATA8(0x0C);
- LCD_WR_DATA8(0x31);
- LCD_WR_DATA8(0x36);
- LCD_WR_DATA8(0x0F);
- LCD_WR_REG(0x11); //Exit Sleep
- delayms(120);
- LCD_WR_REG(0x29); //Display on
- LCD_WR_REG(0x2c);
- }
- //ÇåÆÁº¯Êý
- //Color:ÒªÇåÆÁµÄÌî³äÉ«
- void LCD_Clear(u16 Color)
- {
- u8 VH,VL;
- u16 i,j;
- VH=Color>>8;
- VL=Color;
- 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_DATA8(VH);
- LCD_WR_DATA8(VL);
- }
- }
- }
- //ÔÚÖ¸¶¨Î»ÖÃÏÔʾһ¸öºº×Ö(32*33´óС)
- //dcolorΪÄÚÈÝÑÕÉ«£¬gbcolorΪ±³¾²ÑÕÉ«
- void showhanzi(unsigned int x,unsigned int y,unsigned char index)
- {
- unsigned char i,j;
- unsigned char *temp=hanzi;
- Address_set(x,y,x+31,y+31); //ÉèÖÃÇøÓò
- 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++;
- }
- }
- //»­µã
- //POINT_COLOR:´ËµãµÄÑÕÉ«
- void LCD_DrawPoint(u16 x,u16 y)
- {
- Address_set(x,y,x,y);//ÉèÖùâ±êλÖÃ
- LCD_WR_DATA(POINT_COLOR);
- }
- //»­Ò»¸ö´óµã
- //POINT_COLOR:´ËµãµÄÑÕÉ«
- void LCD_DrawPoint_big(u16 x,u16 y)
- {
- LCD_Fill(x-1,y-1,x+1,y+1,POINT_COLOR);
- }
- //ÔÚÖ¸¶¨ÇøÓòÄÚÌî³äÖ¸¶¨ÑÕÉ«
- //ÇøÓò´óС:
- // (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); //ÉèÖùâ±êλÖÃ
- for(i=ysta;i<=yend;i++)
- {
- for(j=xsta;j<=xend;j++)LCD_WR_DATA(color);//ÉèÖùâ±êλÖÃ
- }
- }
- //»­Ïß
- //x1,y1:Æðµã×ø±ê
- //x2,y2:ÖÕµã×ø±ê
- 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; //¼ÆËã×ø±êÔöÁ¿
- delta_y=y2-y1;
- uRow=x1;
- uCol=y1;
- if(delta_x>0)incx=1; //ÉèÖõ¥²½·½Ïò
- else if(delta_x==0)incx=0;//´¹Ö±Ïß
- else {incx=-1;delta_x=-delta_x;}
- if(delta_y>0)incy=1;
- else if(delta_y==0)incy=0;//ˮƽÏß
- else{incy=-1;delta_y=-delta_y;}
- if( delta_x>delta_y)distance=delta_x; //Ñ¡È¡»ù±¾ÔöÁ¿×ø±êÖá
- else distance=delta_y;
- for(t=0;t<=distance+1;t++ )//»­ÏßÊä³ö
- {
- LCD_DrawPoint(uRow,uCol);//»­µã
- xerr+=delta_x ;
- yerr+=delta_y ;
- if(xerr>distance)
- {
- xerr-=distance;
- uRow+=incx;
- }
- if(yerr>distance)
- {
- yerr-=distance;
- uCol+=incy;
- }
- }
- }
- //»­¾ØÐÎ
- 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);
- }
- //ÔÚÖ¸¶¨Î»Öû­Ò»¸öÖ¸¶¨´óСµÄÔ²
- //(x,y):ÖÐÐĵã
- //r :°ë¾¶
- void Draw_Circle(u16 x0,u16 y0,u8 r)
- {
- int a,b;
- int di;
- a=0;b=r;
- di=3-(r<<1); //ÅжÏϸöµãλÖõıêÖ¾
- 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++;
- //ʹÓÃBresenhamËã·¨»­Ô²
- if(di<0)di +=4*a+6;
- else
- {
- di+=10+4*(a-b);
- b--;
- }
- LCD_DrawPoint(x0+a,y0+b);
- }
- }
- //ÔÚÖ¸¶¨Î»ÖÃÏÔʾһ¸ö×Ö·û
- //num:ÒªÏÔʾµÄ×Ö·û:" "--->"~"
- //mode:µþ¼Ó·½Ê½(1)»¹ÊǷǵþ¼Ó·½Ê½(0)
- //ÔÚÖ¸¶¨Î»ÖÃÏÔʾһ¸ö×Ö·û
- //num:ÒªÏÔʾµÄ×Ö·û:" "--->"~"
- //mode:µþ¼Ó·½Ê½(1)»¹ÊǷǵþ¼Ó·½Ê½(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;
- //ÉèÖô°¿Ú
- num=num-' ';//µÃµ½Æ«ÒƺóµÄÖµ
- Address_set(x,y,x+8-1,y+16-1); //ÉèÖùâ±êλÖÃ
- if(!mode) //·Çµþ¼Ó·½Ê½
- {
- for(pos=0;pos<16;pos++)
- {
- temp=asc2_1608[(u16)num*16+pos]; //µ÷ÓÃ1608×ÖÌå
- 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//µþ¼Ó·½Ê½
- {
- for(pos=0;pos<16;pos++)
- {
- temp=asc2_1608[(u16)num*16+pos]; //µ÷ÓÃ1608×ÖÌå
- for(t=0;t<8;t++)
- {
- if(temp&0x01)LCD_DrawPoint(x+t,y+pos);//»­Ò»¸öµã
- temp>>=1;
- }
- }
- }
- POINT_COLOR=colortemp;
- }
- //m^nº¯Êý
- u32 mypow(u8 m,u8 n)
- {
- u32 result=1;
- while(n--)result*=m;
- return result;
- }
- //ÏÔʾ2¸öÊý×Ö
- //x,y :Æðµã×ø±ê
- //len :Êý×ÖµÄλÊý
- //color:ÑÕÉ«
- //num:ÊýÖµ(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);
- }
- }
- //ÏÔʾ2¸öÊý×Ö
- //x,y:Æðµã×ø±ê
- //num:ÊýÖµ(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);
- }
- }
- //ÏÔʾ×Ö·û´®
- //x,y:Æðµã×ø±ê
- //*p:×Ö·û´®ÆðʼµØÖ·
- //ÓÃ16×ÖÌå
- 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_ShowChar(x,y,*p,0);
- x+=8;
- p++;
- }
- }