/****************************************/ void Write24c02(uchar *Wdata,uchar RomAddress, //写入I2C程序 uchar number) { Start(); //开始条件 Write8Bit(WriteDeviceAddress); //写8位设备地址 TestAck(); Write8Bit(RomAddress); TestAck(); for(;number!=0;number--) { Write8Bit(*Wdata); TestAck(); Wdata++; } Stop(); DelayMs(10); }
/***************************************************/ uchar Read8Bit() //读一个字节 { uchar temp,rbyte=0; for(temp=8;temp!=0;temp--) { SCL=1; rbyte=rbyte<<1; if(SDA)rbyte|=0x01; else rbyte|=0x00; SCL=0; } return(rbyte); }
void Read24c02(uchar *RamAddress,uchar RomAddress, //读出I2C程序 uchar bytes) { Start(); Write8Bit(WriteDeviceAddress); TestAck(); Write8Bit(RomAddress); TestAck(); Start(); Write8Bit(ReadDviceAddress); TestAck(); while(bytes!=0) { *RamAddress=Read8Bit(); Ack(); //应答 RamAddress++; bytes--; } *RamAddress=Read8Bit(); NoAck(); //非应答 Stop(); //结束条件 } /**************************///
//lcd //以下是读忙状态函数 // 功能:在正常读写操作之前检测LCD模块的忙状态, //D7 = 0:LCD控制器空闲;D7 = 1:LCD控制器忙 void CheckBusy( void ) { CR_ADD=0XFF; //P0口写1 Delay(1); while(CR_ADD&Busy );//若D7=1,则DataPort&Busy=1,说明忙,等待
} //以下是写入IR寄存器命令函数 //功能:向LCD模块写入命令字符CMD //若 AttribC = 1 检测忙信号,若 AttribC = 0 不检测忙信号 void WriteIR(uchar CMD,uchar AttribC) { if (AttribC) CheckBusy();//若AttribC =1,则检测忙信号,否则,顺序执行 CW_ADD=CMD;
} //以下是写入DDR寄存器函数 //功能:在当前光标位置显示一个字符 void WriteDDR( char c ) { CheckBusy(); DW_ADD=c; //数据发送
} void d_sendCGRAM(uchar *c) { uchar i; WriteIR(0x40,1); //造字。年。月。日 for(i=0;i<24;i++) { WriteDDR(*c++); } } //以下是光标定位函数 void LcdPos(uchar Xpos,uchar Ypos) { uchar tmop; //定义tmp为指令码 Xpos&=0x0f; //16xx型液晶的范围是0~15 Ypos&=0x01; //Y的范围是0~1 tmop=Xpos; if(Ypos==1) tmop|=0xc0; //若Ypos为1(显示第2行),地址码+0xc0 tmop|=0x80; //若Ypos为0(显示第1行),地址码+0x80 WriteIR(tmop,0); } //以下是初始化函数 //功能:向LCD模块写入不同命令,完成必要的初始化过程 void LcdReset( void ) { WriteIR( 0x38,0); //功能设置指令,8位接口,显示2行,5×7字符 WriteIR( 0x38,1); //设置显示模式(以后均检测忙信号) WriteIR( 0x08,1); //显示开关控制指令,显示关闭 WriteIR( 0x01,1); //清屏指令,将DDRAM数据全部填入"空白" WriteIR( 0x06,1); //输入方式设置指令,字符不动,光标自动右移一格 WriteIR( 0x0f,1); //显示开关控制指令,显示器开,光标开,光标闪烁 } //以下是在指定行列显示字符函数 void WriteChar(uchar Xpos,uchar Ypos,char c)//Xpos=0(第1列),Ypos=1(第2行) { LcdPos(Xpos,Ypos); WriteDDR(c); } //以下是显示字符串函数 void WriteString(uchar Xpos,uchar Ypos,char s[]) { uchar p=0; for(;;) { WriteChar(Xpos,Ypos,s[p]); p++; if(s[p]==0) break; if(++Xpos>=16) //每行最多显示16个字符 { Xpos=0; //如果一行显示不完,则转到下一行或上一行的第一个字符 Ypos^=1;} //如果一行显示不完,则转到下一行或上一行显示 } } /************************/ /************************/ /************************/ void Lcd_xian() { uchar tmp; uchar sd[]="IN0: . V IN1: . V "; if(Sbz)//计算器启动显示 { if(skbz) { skbz=0; WriteIR( 0x01,1); //清屏指令,将DDRAM数据全部填入"空白" } if(xbz2){WriteChar(11,1,(0x30|b1));}else{WriteChar(11,1,' ');} if(xbz3){WriteChar(12,1,(0x30|c1));}else{WriteChar(12,1,' ');} if(xbz4){WriteChar(13,1,(0x30|d1));}else{WriteChar(13,1,' ');} if(xbz5){WriteChar(14,1,(0x30|e1));}else{WriteChar(14,1,' ');} if(xbz6){WriteChar(15,1,(0x30|f1));} } else if(Tbz)//调时钟显示标志 { switch(kk) {case 0:{tmp=(m/10); WriteChar(3,1,(0x30|tmp));tmp=(m%10);WriteChar(4,1,(0x30|tmp)); if(T05s) {tmp=(year/10); WriteChar(2,0,(0x30|tmp));tmp=(year%10);WriteChar(3,0,(0x30|tmp));} else{WriteChar(2,0,' ');WriteChar(3,0,' ');} }break; case 1:{tmp=(year/10); WriteChar(2,0,(0x30|tmp));tmp=(year%10);WriteChar(3,0,(0x30|tmp));WriteChar(4,0,0x00);//year if(T05s) {tmp=(month/10); WriteChar(5,0,(0x30|tmp));tmp=(month%10);WriteChar(6,0,(0x30|tmp));} else{WriteChar(5,0,' ');WriteChar(6,0,' ');} }break; case 2:{tmp=(month/10); WriteChar(5,0,(0x30|tmp));tmp=(month%10);WriteChar(6,0,(0x30|tmp));WriteChar(7,0,0x01);//月 if(T05s) {tmp=(day/10); WriteChar(8,0,(0x30|tmp));tmp=(day%10);WriteChar(9,0,(0x30|tmp));} else{WriteChar(8,0,' ');WriteChar(9,0,' ');} }break; case 3:{tmp=(day/10); WriteChar(8,0,(0x30|tmp));tmp=(day%10);WriteChar(9,0,(0x30|tmp));WriteChar(10,0,0x02);//日 if(T05s) {tmp=(h/10); WriteChar(0,1,(0x30|tmp));tmp=(h%10);WriteChar(1,1,(0x30|tmp));} else{WriteChar(0,1,' ');WriteChar(1,1,' ');} }break; case 4:{tmp=(h/10); WriteChar(0,1,(0x30|tmp));tmp=(h%10);WriteChar(1,1,(0x30|tmp));WriteChar(2,1,':'); if(T05s) {tmp=(m/10);WriteChar(3,1,(0x30|tmp));tmp=(m%10);WriteChar(4,1,(0x30|tmp));} else{WriteChar(3,1,' ');WriteChar(4,1,' ');} }break; case 5:{tmp=(m/10);WriteChar(3,1,(0x30|tmp));tmp=(m%10);WriteChar(4,1,(0x30|tmp));}break; }
} else if(Jbz)//键盘输入标志 { if(skbz) { skbz=0; WriteIR( 0x01,1); //清屏指令,将DDRAM数据全部填入"空白" } WriteChar(12,1,(0x30|a)); WriteChar(13,1,(0x30|b)); WriteChar(14,1,(0x30|c)); WriteChar(15,1,(0x30|d)); } else if(Abz)//ADC0809启动显示程序 { if(skbz) { skbz=0;TR1=0; WriteIR( 0x01,1); //清屏指令,将DDRAM数据全部填入"空白" WriteString(0,0,sd); }
tmp=Adcx0[1];WriteChar(4,0,(0x30|tmp)); WriteChar(5,0,'.'); tmp=Adcx0[2];WriteChar(6,0,(0x30|tmp)); tmp=Adcx0[3];WriteChar(7,0,(0x30|tmp)); tmp=Adcx0[4];WriteChar(8,0,(0x30|tmp)); tmp=Adcx1[1];WriteChar(4,1,(0x30|tmp)); WriteChar(5,1,'.'); tmp=Adcx1[2];WriteChar(6,1,(0x30|tmp)); tmp=Adcx1[3];WriteChar(7,1,(0x30|tmp)); tmp=Adcx1[4];WriteChar(8,1,(0x30|tmp)); TR1=1; //延是等待复位 }
else if(Shbz)//闹钟显示程序 { if(skbz) { skbz=0;TR1=0; WriteIR( 0x01,1); //清屏指令,将DDRAM数据全部填入"空白" tmp=Sh[0];tmp/=10;WriteChar(0,1,(0x30|tmp));tmp=Sh[0];tmp%=10;WriteChar(1,1,(0x30|tmp)); WriteChar(2,1,':'); tmp=Sh[1];tmp/=10;WriteChar(3,1,(0x30|tmp));tmp=Sh[1];tmp%=10;WriteChar(4,1,(0x30|tmp)); TR1=1; }
switch(kk) { case 0:{ if(T05s) { tmp=Sh[0];tmp/=10;WriteChar(0,1,(0x30|tmp));tmp=Sh[0];tmp%=10;WriteChar(1,1,(0x30|tmp));} else{WriteChar(0,1,' ');WriteChar(1,1,' ');} }break; case 1:{tmp=Sh[0];tmp/=10;WriteChar(0,1,(0x30|tmp));tmp=Sh[0];tmp%=10;WriteChar(1,1,(0x30|tmp));WriteChar(2,1,':'); if(T05s) { tmp=Sh[1];tmp/=10;WriteChar(3,1,(0x30|tmp));tmp=Sh[1];tmp%=10;WriteChar(4,1,(0x30|tmp));} else{WriteChar(3,1,' ');WriteChar(4,1,' ');} }break; case 2:{tmp=Sh[1];tmp/=10;WriteChar(3,1,(0x30|tmp));tmp=Sh[1];tmp%=10;WriteChar(4,1,(0x30|tmp)); tmp=Sh[2]; if(tmp!=0) { WriteChar(13,1,'O'); WriteChar(14,1,'N'); WriteChar(15,1,' '); } else { WriteChar(13,1,'O'); WriteChar(14,1,'F'); WriteChar(15,1,'F'); } }break; }
}
else//时钟显示 { if(slbz) { TR1=0; slbz=0; WriteIR( 0x01,1); //清屏指令,将DDRAM数据全部填入"空白" TR1=1; } else if(Key==0){jm=61;Delay(1);} if(jm!=m) { jm=m; tmp=(shiji/10); WriteChar(0,0,(0x30|tmp));// tmp=(shiji%10); WriteChar(1,0,(0x30|tmp));// tmp=(year/10); WriteChar(2,0,(0x30|tmp));// tmp=(year%10); WriteChar(3,0,(0x30|tmp));// WriteChar(4,0,0x00);//Xpos=4(第5列),Ypos=0(第1行)年 tmp=(month/10); WriteChar(5,0,(0x30|tmp));// tmp=(month%10); WriteChar(6,0,(0x30|tmp));// WriteChar(7,0,0x01);//Xpos=7(第8列),Ypos=0(第1行)月 tmp=(day/10); WriteChar(8,0,(0x30|tmp));// tmp=(day%10); WriteChar(9,0,(0x30|tmp));// WriteChar(10,0,0x02);//Xpos=10(第11列),Ypos=0(第1行)日 tmp=(h/10); WriteChar(0,1,(0x30|tmp));// tmp=(h%10); WriteChar(1,1,(0x30|tmp)); tmp=(m/10); WriteChar(3,1,(0x30|tmp)); tmp=(m%10); WriteChar(4,1,(0x30|tmp)); tmp=Sh[2];if(tmp!=0) { WriteChar(13,1,'O'); WriteChar(14,1,'N'); WriteChar(15,1,' '); if((Sh[0]==h)&(Sh[1]==m))WriteChar(14,0,'$');else WriteChar(14,0,' '); } else{ WriteChar(13,1,'O'); WriteChar(14,1,'F'); WriteChar(15,1,'F'); }
} if(T05s){WriteChar(2,1,' ');}else{ WriteChar(2,1,':');} // tmp=(s/10); // WriteChar(6,1,(0x30|tmp)); tmp=(s%10); WriteChar(7,1,(0x30|tmp));
}
} //延时程序 void Delay(uchar i) //延时程序,i是形式参数 { uchar j; while(i--) //变量i由实际参数传入一个值,因此i不能赋初值 for(j=0;j<125;j++);
}
//以下是定时/计数器1中断函数 void timer1() interrupt 3 { uchar tmp;
TR1=0; TH1=(65536-9000)/256; TL1=(65536-9000)%256; //定时时间 if(Sbz)//计算器启动显示 { if(b1!=0){xbz2=1;xbz3=1;xbz4=1;xbz5=1;xbz6=1;} else if(c1!=0){xbz2=0;xbz3=1;xbz4=1;xbz5=1;xbz6=1;} else if(d1!=0){xbz2=0;xbz3=0;xbz4=1;xbz5=1;xbz6=1;} else if(e1!=0){xbz2=0;xbz3=0;xbz4=0;xbz5=1;xbz6=1;} else {xbz2=0;xbz3=0;xbz4=0;xbz5=0;xbz6=1;} if(xbz3){tmp=tab[c1];PB8255=tmp;led0=1;Delay(2);led0=0;} if(xbz4){tmp=tab[d1];PB8255=tmp;led1=1;Delay(2);led1=0;} if(xbz5){tmp=tab[e1];PB8255=tmp;led2=1;Delay(2);led2=0;} if(xbz6){tmp=tab[f1];PB8255=tmp;led3=1;Delay(2);led3=0;} } else if(Tbz)//调时钟显示标志 {
tmp=tab[h/10];if(kk==3){if(T05s)tmp=0;}PB8255=tmp;led0=1;Delay(2);led0=0; tmp=tab[h%10];if(kk==3){if(T05s)tmp=0;}PB8255=tmp;led1=1;Delay(2);led1=0; tmp=tab[m/10];if(kk==4){if(T05s)tmp=0;}PB8255=tmp;led2=1;Delay(2);led2=0; tmp=tab[m%10];if(kk==4){if(T05s)tmp=0;}PB8255=tmp;led3=1;Delay(2);led3=0; } else if(Jbz)//键盘输入标志 { PB8255=tab[a];led0=1;Delay(2);led0=0; PB8255=tab[b];led1=1;Delay(2);led1=0; PB8255=tab[c];led2=1;Delay(2);led2=0; PB8255=tab[d];led3=1;Delay(2);led3=0; } else if(Abz)//ADC0809启动显示程序 {
if(Axbz) //in0 huo in1 { tmp=Adcx0[1];tmp=tab1[tmp];PB8255=tmp;led0=1;Delay(2);led0=0; tmp=Adcx0[2];tmp=tab[tmp];PB8255=tmp;led1=1;Delay(2);led1=0; tmp=Adcx0[3];tmp=tab[tmp];PB8255=tmp;led2=1;Delay(2);led2=0; tmp=Adcx0[4];tmp=tab[tmp];PB8255=tmp;led3=1;Delay(2);led3=0; } else{ tmp=Adcx1[1];tmp=tab1[tmp];PB8255=tmp;led0=1;Delay(2);led0=0; tmp=Adcx1[2];tmp=tab[tmp];PB8255=tmp;led1=1;Delay(2);led1=0; tmp=Adcx1[3];tmp=tab[tmp];PB8255=tmp;led2=1;Delay(2);led2=0; tmp=Adcx1[4];tmp=tab[tmp];PB8255=tmp;led3=1;Delay(2);led3=0; }
} else if(Shbz)//调时钟显示标志 {
tmp=Sh[0];tmp=tab[tmp/10];if(kk==0){if(T05s)tmp=0;}PB8255=tmp;led0=1;Delay(2);led0=0; tmp=Sh[0];tmp=tab[tmp%10];if(kk==0){if(T05s)tmp=0;}PB8255=tmp;led1=1;Delay(2);led1=0; tmp=Sh[1];tmp=tab[tmp/10];if(kk==1){if(T05s)tmp=0;}PB8255=tmp;led2=1;Delay(2);led2=0; tmp=Sh[1];tmp=tab[tmp%10];if(kk==1){if(T05s)tmp=0;}PB8255=tmp;led3=1;Delay(2);led3=0; }
else//时钟显示 {
tmp=tab[h/10];PB8255=tmp;led0=1;Delay(2);led0=0;tmp=(h/10); tmp=tab[h%10];if(T05s){tmp=(tmp|0x80);}PB8255=tmp;led1=1;Delay(2);led1=0; tmp=tab[m/10];PB8255=tmp;led2=1;Delay(2);led2=0; tmp=tab[m%10];PB8255=tmp;led3=1;Delay(2);led3=0; } TR1=1; }
//以下是定时/计数器0中断函数 void timer0() interrupt 1 using 1 { static uchar a;
TH0=(65536-46075)/256; TL0=(65536-46075)%256; //定时时间为50MS a++; if(a>9)T05s=1; if(a>19){s++;a=0;T05s=0;}// 20*50ms=1000ms=1s if(s>59){m++;s=0;} if(m>59){h++;m=0;} if(h>23){h=0;day++;} if(day>30){day=0;month++;} if(month>12){month=0;year++;} if(year>99)year=0; }