贴上来大家参考一下,在我的51实验系统中有pcf8563,修改一下基本上就可以用!呵呵!
//LED时钟程序(基于PCF8583)
//--------------------------------------------------------------------------------
#include
#include
#include
sbit KSET = P3^4; /* key 1 */
sbit KINC = P3^5; /* key 2 */
sbit KDEC = P1^6; /* key 3 */
sbit DSET = P1^7; /* DISP MODE */
#define key_down 0 /* key press down: GND */
#define key_up 1 /* key lift up : VCC */
sbit ALARM="P1"^0; /* indicate lamp LED */
sbit POWER="P1"^1; /* display power switch */
sbit SDA = P1^5;
sbit SCL = P1^4;
unsigned char code db[26]=
{ 0x3F,0x06,0x5B,0x4F,0X66,0x6D,0x7D,0x07,0x7F,0X6F,0x00,
/* 0 1 2 3 4 5 6 7 8 9 NULL */
0x6E,0x79,0x77,0x37,0x5C,0x54,0x1C,0x76,0x5E,0x39,0x40,
/* Y E A M o n u H d C - */
0x38,0x48,0x04,0x0E };
/* L = i j*/
unsigned char dv[6],j,df;
bit ksetf, kdecf, kincf, dsetf,kf;
unsigned char year,month,day,hour,minute,second,week;
unsigned char adj;
/********** Function Declare Begin **********************/
char Start(); /* Start IIC bus for reading or writing*/
void Stop(); /* Stop IIC bus */
char Shin(); /* Shift a byte from EEPROM, */
/* most significant first */
void Shout(char Byte); /* Shift a byte to EEPROM */
char TestAck(); /* check if Acknowledge bit is valid */
char RandomRead(char DeviceAddr, char ByteAddr);
void RandomWrite(char DeviceAddr, char ByteAddr, char Byte);
char SRead( char DeviceAddr, char ByteAddr, char ByteNum, char ReadBuf[] );
/*
void PageWrite(char DeviceAddr, char ByteAddr, char ByteNum, char ByteBuf[] );
*/
/********** Function Declare End *************************/
char Start()//开始信号
{
SCL="1"; /* release SCL */
SDA="1"; /* release SDA */
_nop_();
if( SCL==0 || SDA==0 ) return 0; /* IIC bus not available, Start false */
else
{
SCL="1"; /* Send start operation */
_nop_();
_nop_();
SDA="0"; /* SDA high-to-low when SCL high */
_nop_();
_nop_();
_nop_(); /*one cycle delay function */
_nop_(); /*declare in */
_nop_();
SCL="0";
_nop_();
SDA="1"; /*release SDA */
}
} /*return with SDA high, SCL low */
void Stop() //停止信号
{
SDA="0";
_nop_();
SCL="1";
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SDA="1"; /* SDA low-to-high when SCL high, return with SCL SDA high */
}
char Shin()//接受一个字节
{
char i,RetByte;
for(i="0";i<8;i++)
{
SCL="0";
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SCL="1";
_nop_();
_nop_();
RetByte<<=1;
if( SDA ) RetByte++;
}
SCL="0";
_nop_();
_nop_(); /* return with SCL low, SDA high */
SDA="1";
return RetByte;
}
void Shout( char Byte ) //输出一个字节
{ /* Send one byte to EEPROM */
/* most significant */
char i;
for(i="0";i<8;i++)
{
SCL="0";
_nop_();
if( Byte&0x80 ) SDA="1";
else SDA="0";
_nop_();
_nop_();
_nop_();
_nop_();
Byte<<=1;
SCL="1";
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
SCL="0";
_nop_();
_nop_();
SDA="1"; /* return with SCL low , SDA high */
}
char TestAck()//应答信号
{
char rf="0"; /* return flag */
SCL="0";
_nop_();
SDA="1"; /* release SDA */
_nop_();
SCL="1";
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
if( SDA==1 ) rf="1"; /* Acknowledge bit is invalid*/
else rf="0";
SCL="0";
_nop_();
SDA="1"; /* return with SCL low, and SDA high*/
}
char RandomRead( char DeviceAddr, char ByteAddr )
{
char rc="0";
Start();
Shout( DeviceAddr&0xFE ); /* send device address & write command */
if( !TestAck() )
{
Shout( ByteAddr ); /* send byte address to read */
if( !TestAck() )
{
Start();
Shout( DeviceAddr|0x01 ); /* send read command */
if( !TestAck() )
rc="Shin"();
TestAck();
}
}
Stop();
return rc;
}
char SRead( char DeviceAddr, char ByteAddr, char ByteNum, char ReadBuf[] )
{
char i;
Start();
Shout( DeviceAddr&0xFE );
if( !TestAck() )
{
Shout( ByteAddr );
if( !TestAck() )
{
Start();
Shout( DeviceAddr|0x01 );
if( !TestAck() )
for(i="0";i{
ReadBuf=Shin();
SCL="0";
_nop_();
_nop_();
SDA="0";
_nop_();
_nop_();
SCL="1";
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SCL="0";
_nop_();
SDA="1";
}
ReadBuf=Shin();
TestAck();
}
}
Stop();
}
void RandomWrite( char DeviceAddr, char ByteAddr, char Byte)
{
Start();
Shout( DeviceAddr&0xFE ); /* write command */
if( !TestAck() )
{
Shout( ByteAddr );
if( !TestAck() )
Shout( Byte );
TestAck();
}
Stop();
}
void READINT() interrupt 0{
char rb[3];
SRead( 0xA0,2,3,rb);
j="rb"[0];
j="RandomRead"( 0xA0,2);
dv[0]=j&0x0F;
dv[1]=j>>4;
j="rb"[1];
j="RandomRead"( 0xA0,3);
dv[2]=j&0x0F;
dv[3]=j>>4;
j="rb"[2];
j="RandomRead"( 0xA0,4);
dv[4]=j&0x0F;
dv[5]=j>>4;
df="1";
hour="dv"[5]*10+dv[4];
minute="dv"[3]*10+dv[2];
second="dv"[1]*10+dv[0];
}
void del_10(){
signed int delay;
for( delay="0"; delay<18000; delay++) _nop_();
}
void setdate(){
unsigned char v;
char wb[7];
EA="0";
POWER="0";
kf="1";
do{
if(kf) kf="0", setdisp( 11,12,13, year );
if( kincf==key_down ){ year++; if( year>99 ) year="0";}
if( kdecf==key_down ){ year--; if( year>99 ) year="99";}
}while(ksetf==key_up);
kf="1";
do{
if(kf) kf="0", setdisp( 14,15,16, month );
get_key();
if( kincf==key_down ){ month++; if( month>12 ) month="1"; }
if( kdecf==key_down ){ month--; if( month>12 ) month="12";}
}while(ksetf==key_up);
kf="1";
do{
if(kf) kf="0", setdisp( 19,13,11, day );
get_key();
if( kincf==key_down ){ day++; if( day>31 ) day="1"; }
if( kdecf==key_down ){ day--; if( day>31 ) day="31";}
}while(ksetf==key_up);
kf="1";
do{
if(kf) kf="0", setdisp( 18,15,17, hour );
get_key();
if( kincf==key_down ){ hour++; if( hour>23 ) hour="0"; }
if( kdecf==key_down ){ hour--; if( hour>23 ) hour="23";}
}while(ksetf==key_up);
kf="1";
do{
if(kf) kf="0", setdisp( 14,24,16, minute );
get_key();
if( kincf==key_down ){ minute++; if( minute>59 ) minute="0"; }
if( kdecf==key_down ){ minute--; if( minute>59 ) minute="59";}
}while(ksetf==key_up);
kf="1";
do{
if(kf) kf="0", setdisp( 5,12,20, second );
get_key();
if( kincf==key_down ){ second++; if( second>59 ) second="0"; }
if( kdecf==key_down ){ second--; if( second>59 ) second="59";}
}while(ksetf==key_up);
kf="1";
do{
if(kf) kf="0", setdisp( 17,12,12, week );
get_key();
if( kincf==key_down ){ week++; if( week>6 ) week="0"; }
if( kdecf==key_down ){ week--; if( week>6 ) week="6"; }
}while(ksetf==key_up);
year=(year+1900)%4;
RandomWrite(0xA0,0,0x80); /* disable counter */
RandomWrite(0xA0,1,0);
v="second"/10;
v<<=4;
v|=second%10;
RandomWrite(0xA0,2,v);
v="minute"/10;
v<<=4;
v|=minute%10;
RandomWrite(0xA0,3,v);
v="hour"/10;
v<<=4;
v|=hour%10;
RandomWrite(0xA0,4,v);
v="year"<<2;
v|=day/10;
v<<=4;
v|=day%10;
RandomWrite(0xA0,5,v);
v="week"&0x07;
v<<=1;
v|=month/10;
v<<=4;
v|=month%10;
RandomWrite(0xA0,6,v);
RandomWrite( 0xA0,0,0 );
POWER="1";
EA="1";
}
void setadj(){
kf="1";
POWER="0";
EA="0";
do{
if(kf) kf="0", setdisp( 13,19,25, adj );
if( kincf==key_down ){ adj++; if( adj>49 ) adj="1"; }
if( kdecf==key_down ){ adj--; if( adj>49 ) adj="49";}
}while(dsetf==key_up);
if(adj==0) adj="1";
POWER="1";
EA="1";
}
void main(){
char mt,c="0",c1,cs,cp="0";
TMOD="0x01";
IT0 =1;
EX0 =1;
ET0 =1;
POWER="0";
ALARM="0";
EA="0";
week="RandomRead"(0xA0,6);
week>>=5;
c1=RandomRead(0xA0,6)&0x1F;
month="c1"&0x0f;
month+=(c1>>4)*10;
c1=RandomRead(0xA0,5)&0x3F;
day="c1"&0x0f;
day+=(c1>>4)*10;
EA="1";
while(1){
EA="1";
if(mt||cs)
if(df){
df="0";
c++;
if(c%2) disp();
else{
EA="0";
dv[5]=month/10;
dv[4]=month%10;
dv[3]=day/10;
dv[2]=day%10;
dv[1]=21;
if( week ) dv[0]=week;
else dv[0]=8;
disp();
EA="1";
}
}
if(mt==0&&df&&cs==0) df="0", disp();
if(!hour&&!minute&&second==10){
EA="0";
week="RandomRead"(0xA0,6);
week>>=5;
c1=RandomRead(0xA0,6)&0x1F;
month="c1"&0x0f;
month+=(c1>>4)*10;
c1=RandomRead(0xA0,5)&0x3F;
day="c1"&0x0f;
day+=(c1>>4)*10;
if(adj==0) adj="1";
second+=adj;
RandomWrite(0xA0,0,0x80); /* disable counter */
c1=second/10;
c1<<=4;
c1|=second%10;
RandomWrite(0xA0,2,c1);
RandomWrite(0xA0,0,0);
EA="1";
}
if((minute==59&&second>50)||(minute==0&&second<8) ){
mt="1";
if(POWER==0) ALARM="1";
}
else{
mt="0";
ALARM="0";
}
if(week>0&&week<6){
if( hour>8&&hour<17 ) POWER="cp";
else if( hour>23||hour<5) POWER="cp";
else POWER="0";
}
else{
if( hour>23||hour<5) POWER="cp";
else POWER="0";
}
get_key();
if( ksetf==key_down ) setdate();
if( dsetf==key_down ) setadj();
if( kincf==key_down ) cs="1"-cs;
if( kdecf==key_down ) cp="1"-cp;
}
}
用户386180 2007-1-3 10:31
用户1053025 2006-11-13 11:22
用户655961 2006-11-13 08:43