最近在准备电赛,需要准备显示,不想用太多的线,然后就用了串行的lcd12864只需要三根线就好
- #include <reg52.h>
- #include <stdlib.h>
- #include <intrins.h>
- #include <stdio.h>
-
- #define uchar unsigned char
- #define uint unsigned int
- sbit CS = P1^0; //RS
- sbit SID = P1^1; //RW
- sbit SCK = P1^2; //E
- uchar code AC_TABLE[]={
- 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, //???????
- 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97, //???????
- 0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f, //???????
- 0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f, //???????
- };
- #define WS 0x80
- #define WMi 0x82
- #define WH 0x84
- #define WD 0x86
- #define WMo 0x88
- #define WW 0x8A
- #define WY 0x8C
- #define WP 0x8E
- #define RS 0x81
- #define RMi 0x83
- #define RH 0x85
- #define RD 0x87
- #define RMo 0x89
- #define RW 0x8B
- #define RY 0x8D
- #define RP 0x8F
- uchar sec,min,hour,day,mon,week,year;
- sbit SCLK = P3^5;
- sbit DIO = P3^6;
- sbit RST = P3^7;
- void InitDS1302();
- uchar DS1302Read(uchar cmd);
- void DS1302Write(uchar cmd,uchar dat);
- void DS1302WriteByte(uchar dat);
- uchar DS1302ReadByte();
- void ReadTime();
- void display();
- void delay();
- uchar code tables[]={0x77,0x14,0xb3,0xb6,0xd4,0xe6,0xe7,0x34,0xf7,0xf6,0x80,0x00};
- /******************************** ???? :SendByte ????????*****/
- void SendByte(uchar Dbyte)
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- SCK = 0;
- Dbyte=Dbyte<<1;
- SID = CY;
- SCK = 1;
- SCK = 0;
- }
- }
-
- /***********??????***/
- uchar ReceiveByte(void)
- {
- uchar i,temp1,temp2;
- temp1 = 0;
- temp2 = 0;
- for(i=0;i<8;i++)
- {
- temp1=temp1<<1;
- SCK = 0;
- SCK = 1;
- SCK = 0;
- if(SID) temp1++;
- }
- for(i=0;i<8;i++)
- {
- temp2=temp2<<1;
- SCK = 0;
- SCK = 1;
- SCK = 0;
- if(SID) temp2++;
- }
- return ((0xf0&temp1)+(0x0f&temp2));
- }
-
- void CheckBusy( void )
- {
- do SendByte(0xfc);
- while(0x80&ReceiveByte());
- }
-
- void WriteCommand(uchar Cbyte )
- {
- CS = 1;
- CheckBusy();
- SendByte(0xf8);
- SendByte(0xf0&Cbyte);
- SendByte(0xf0&Cbyte<<4);
- CS = 0;
- }
-
- void WriteData(uchar Dbyte )
- {
- CS = 1;
- CheckBusy();
- SendByte(0xfa);
- SendByte(0xf0&Dbyte);
- SendByte(0xf0&Dbyte<<4);
- CS = 0;
- }
-
- uchar ReadData( void )
- {
- CheckBusy();
- SendByte(0xfe);
- return ReceiveByte();
- }
-
- void Delay(uint MS)
- {
- uchar us,usn;
- while(MS!=0)
- {
- usn = 2;
- while(usn!=0)
- {
- us = 0xf5;
- while (us!=0)
- {
- us--;
- };
- usn--;
- }
- MS--;
- }
- }
-
-
-
- void LcmInit( void )
- {
- WriteCommand(0x30);
- WriteCommand(0x03);
- WriteCommand(0x0C);
- WriteCommand(0x01);
- WriteCommand(0x06);
- }
-
- void LcmClearTXT( void )
- {
- uchar i;
- WriteCommand(0x30);
- WriteCommand(0x80);
- for(i=0;i<64;i++)
- WriteData(0x20);
- }
-
-
- void PutStr(uchar row,uchar col,uchar *puts)
- {
- WriteCommand(0x30);
- WriteCommand(AC_TABLE[8*row+col]);
- while(*puts != '\0')
- {
- if(col==8)
- {
- col='0';
- row++;
- }
- if(row==4) row='0';
- WriteCommand(AC_TABLE[8*row+col]);
- WriteData(*puts);
- puts++;
- WriteData(*puts);
- puts++;
- col++;
- }
- }
-
- void Putchar(uchar row,uchar col,uchar puts)
- {
- uchar a,b,c;
- WriteCommand(0x30);
- WriteCommand(AC_TABLE[8*row+col]);
- if(col==8)
- {
- col='0';
- row++;
- }
- if(row==4) row='0';
- WriteCommand(AC_TABLE[8*row+col]);
- // a=puts/100;
- b=puts/10;
- c=puts%10;
- WriteData(b);
- col++;
- WriteData(a);
-
- }
-
- void DisplayDots(uchar DotByte)
- {
- uchar i,j;
- WriteCommand(0x34);
- WriteCommand(0x36);
- for(i=0;i<32;i++)
- {
- WriteCommand(0x80|i);
- WriteCommand(0x80);
- for(j=0;j<32;j++)
- {
- WriteData(DotByte);
- }
- DotByte=~DotByte;
- }
- }
- uchar str0[]="2001-07-01";
- uchar str1[]="12:11:11";
- void main( void )
- {
- Delay(100);
- //InitDS1302();
- LcmInit();
- LcmClearTXT();
- while(1)
- {
- ReadTime();;
- str0[2] = year/10+48;
- str0[3] = year%10+48;
- str0[5] = mon/10+48;
- str0[6] = mon%10+48;
- str0[8] = day/10+48;
- str0[9] = day%10+48;
- str1[0] = hour/10+48;
- str1[1] = hour%10+48;
- str1[3] = min/10+48;
- str1[4] = min%10+48;
- str1[6] = sec/10+48;
- str1[7] = sec%10+48;
- PutStr(2,0,str1);
- PutStr(3,0,str0);
- }
- }
- uchar DS1302Read(uchar cmd){
- uchar dat;
- RST = 0;
- SCLK = 0;
- RST = 1;
- DS1302WriteByte(cmd);
- dat = DS1302ReadByte();
- SCLK = 1;
- RST = 0;
- return dat;
- }
- void DS1302Write(uchar cmd,uchar dat){
- RST = 0;
- SCLK = 0;
- RST =1;
- DS1302WriteByte(cmd);
- DS1302WriteByte(dat);
- SCLK = 1;
- RST = 0;
- }
- void DS1302WriteByte(uchar dat){
- uchar i;
- SCLK = 0;
- for(i=0;i<8;i++){
- DIO = dat&0x01;
- SCLK = 1;
- SCLK = 0;
- dat>>=1;
- }
- }
- uchar DS1302ReadByte(){
- uchar i,dat;
- for(i=0;i<8;i++){
- dat>>=1;
- if(DIO==1)dat|=0x80;
- SCLK = 1;
- SCLK = 0;
- }
- return dat;
- }
- void InitDS1302(){
- DS1302Write(WP,0x00);
- DS1302Write(WS,0x80);
- DS1302Write(WMi,0x10);
- DS1302Write(WH,0x10);
- DS1302Write(WD,0x17);
- DS1302Write(WMo,0x07);
- DS1302Write(WW,0x02);
- DS1302Write(WY,0x18);
- DS1302Write(WS,0x00);
- DS1302Write(WP,0x80);
- }
- void ReadTime(){
- uchar temp;
- temp = DS1302Read(RS);
- sec = (temp&0x0F)+((temp&0x70)>>4)*10;
- temp =DS1302Read(RMi);
- min = (temp&0x0F)+((temp&0x70)>>4)*10;
- temp =DS1302Read(RH);
- hour = (temp&0x0F)+((temp&0x30)>>4)*10;
- temp =DS1302Read(RD);
- day = (temp&0x0F)+((temp&0x30)>>4)*10;
- temp =DS1302Read(RMo);
- mon = (temp&0x0F)+((temp&0x10)>>4)*10;
- temp =DS1302Read(RW);
- week = temp&0x07;
- temp =DS1302Read(RY);
- year = (temp&0x0F)+(temp>>4)*10;
- }
- void display(){
- SBUF = tables[sec%10];
- while(!TI);
- TI = 0;
- SBUF = tables[sec/10];
- while(!TI);
- TI = 0;
- SBUF = tables[day%10];
- while(!TI);
- TI = 0;
- SBUF = tables[day/10];
- while(!TI);
- TI = 0;
- SBUF = tables[mon%10];
- while(!TI);
- TI = 0;
- SBUF = tables[mon/10];
- while(!TI);
- TI = 0;
- SBUF = tables[year%10];
- while(!TI);
- TI = 0;
- SBUF = tables[year/10];
- while(!TI);
- TI = 0;
- }
- void delay(){
- unsigned int i,j;
- for(i=0;i<1000;i++){
- for(j=0;j<333;j++);
- }
- }
复制代码
|