#include “C8051F410.h”
#define uchar unsigned char#define LCMdata P2#define Busy 0x80sbit RS = P1^3;sbit RW = P1^4;sbit E = P1^5;sbit PSB = P1^6;sbit REST= P0^7;//sfr16 TMR2RL = 0xCA; // Timer2 Reload Register//sfr16 TMR2 = 0xCC;void Display_String(uchar line,uchar *string);void Wr_Data(uchar wrdata); //写数据void Wr_Command(uchar wrcommand,busyc); //写指令void RDbf(void);//读忙状况void Lcm_Init(void);//液晶初始化void Delay5Ms(void);//延时5msvoid write_show(uchar);void SET_CGRAM(uchar,uchar);void Dis_CG(uchar,uchar,uchar);void Timer0Init(void);uchar count,miao;//uchar OS_Delay[1];uchar code string_1[] = {"祖国江山好, "};uchar code string_2[] = {"爱情少不了。 "};uchar code string_3[] = {" 温度:00 "};uchar code string_4[] = {"温州大学--规划 "};uchar code CGRAM_ZIKU[]={0x70,0x00,0x50,0x00,0x70,0x00,0x07,0xFC,0x1C,0x06,0x38,0x02,0x70,0x00,0x70,0x00,0x70,0x00,0x70,0x00,0x70,0x02,0x38,0x02,0x1C,0x0C,0x0F,0xF0,0x00,0x00,0x00,0x00};void main(void){PCA0MD &= ~0x40; // Clear watchdog timer enableOSCICN = 0x81; while ((OSCICN & 0x40 ) == 0); // Wait until stableP0MDOUT = 0x80;P1MDOUT = 0x78;P2MDOUT = 0xFF;XBR1 = 0x40;Lcm_Init();Timer0Init() ;EA = 1 ; Display_String(1,string_1);Display_String(2,string_2); Display_String(3,string_3); Display_String(4,string_4);SET_CGRAM(1,&CGRAM_ZIKU);Dis_CG(2,6,1);while(1){if(count==20){count=0;miao++;if(miao==100){miao=0;}write_show(miao);}}}void SET_CGRAM(uchar num,uchar *CGRAM_ZIKU){uchar i,add;Wr_Command(0x34);Wr_Command(0x02);Wr_Command(0x30);add=(num<<4)|0x40;for(i=0;i<16;i++){Wr_Command(add+i);Wr_Data(CGRAM_ZIKU[i*2]);Wr_Data(CGRAM_ZIKU[i*2+1]); }}void Set_Cursor(uchar x,uchar y){uchar i;switch(x){case 0x00:i=0x80;break;case 0x01:i=0x90;break;case 0x02:i=0x88;break;case 0x03:i=0x98;break;default:break; }i=y+i;Wr_Command(i);}void Dis_CG(uchar x,uchar y,uchar num){Set_Cursor(x,y);Wr_Data(0x00);Wr_Data(num*2); }void write_show(uchar date)//显现计时{uchar shi,ge;shi=date/10;ge=date%10;Set_Cursor(2,4);Wr_Data(0x30+shi);Wr_Data(0x30+ge); }void Display_String(uchar line,uchar *string)//显现字符串{ uchar addr,i;if(line==1)addr=0x80;else if(line==2)addr=0x90;else if(line==3)addr=0x88; else if(line==4)addr=0x98; Wr_Command(addr,1);for(i=0;i<16;i++){ Wr_Data(*string++);} }void Wr_Data(uchar wrdata) //写数据{//RDbf();RS=1;RW=0;E=1;LCMdata=wrdata;E=0; }void Wr_Command(uchar wrcommand,busyc) //写指令{//if(busyc)//RDbf();RS=0;RW=0;E=1;LCMdata=wrcommand;E=0;}void RDbf(void) //读忙状况{while(1){RS=0;RW=1; E=0;LCMdata=P2; E=1;if((LCMdata&Busy)==0)break;}}void Lcm_Init(void){PSB = 1;Delay5Ms(); REST=1;Delay5Ms(); Wr_Command(0x30,0);Delay5Ms();Wr_Command(0x30,0); //2次显现形式设置,不必判忙Delay5Ms();Wr_Command(0x0C,1); //开显现及光标设置Delay5Ms();Wr_Command(0x01,1); //显现清屏Delay5Ms();Delay5Ms();Wr_Command(0x06,1); //显现光标移动设置Delay5Ms();}void Timer0Init(void){TMOD &= 0xf0 ;TMOD |= 0x01 ; //定时器0工作方式1TH0 = 0xfc ; //定时器初始值TL0 = 0x66 ;TR0 = 1 ;ET0 = 1 ;}void Time0Isr(void) interrupt 1{TH0 = 0xfc ; //定时器从头赋初值TL0 = 0x66 ;count++; //1MS时标标志方位位}void Delay5Ms(void) //延时5ms{unsigned int TempCyc=5552;while(TempCyc--);}