原创 基于PCF8583RTC的C51程序

2006-11-10 14:43 3955 9 9 分类: MCU/ 嵌入式

     贴上来大家参考一下,在我的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;
}


}



 


 

PARTNER CONTENT

文章评论3条评论)

登录后参与讨论

用户386180 2007-1-3 10:31

多谢

用户1053025 2006-11-13 11:22

多谢

用户655961 2006-11-13 08:43

ok!
相关推荐阅读
用户1318081 2012-11-22 08:47
Altera Quartus II软件12.1版借助强大的高级设计流程,加速系统开发
Altera公司 (Nasdaq: ALTR) 今天宣布,推出Quartus® II 软件12.1 版——在CPLD、FPGA、SoC FPGA和HardCopy® ASIC设计方面,性能和效能在...
用户1318081 2012-11-17 23:26
介绍28nm创新技术,超越摩尔定律
在工艺方法基础上,Altera利用FPGA创新技术超越了摩尔定律,满足更大的带宽要 求,以及成本和功耗预算。Altera Stratix® V FPGA通过28-Gbps高功效收发器突破 了带...
用户1318081 2012-11-17 23:22
Altera与Northwest Logic联合开发RLDRAM 3存储器接口解决方案
Altera公司 (NASDAQ: ALTR)与FPGA高性能知识产权(IP)内核领先供应商Northwest Logic今天宣布,开始提供硬件成熟的1,600 Mbps低延时DRAM (RLDR...
用户1318081 2012-11-17 23:21
Altera电机控制开发工作台前所未有的提高系统集成度、可扩展的性能和灵活性
Altera公司(NASDAQ: ALTR)今天宣布,新的电机控制开发工作台前所未有的提高了电机控制系统设计的系统集成度和灵活性,而且性能还可以扩展,同时大幅度缩短开发时间,降低风险。工作台包括一...
用户1318081 2012-11-07 11:05
Altera OpenCL统一的异构编程
观看OpenCL怎样为异构计算提供统一的平台。在这一演示中,我们将为GPU编写的NVIDIA代码重新定位到Stratix V FPGA上。  ...
用户1318081 2012-11-07 10:58
Altera宣布业界首款支持FPGA的OpenCL工具——进一步加速了FPGA在异构系统中的应用
Altera公司 (NASDAQ: ALTR)今天宣布,提供FPGA业界的第一款用于OpenCL™ 的软件开发套件(SDK) (开放计算语言) 的软件开发套件,它结合了FPGA强大的并行体系结构以...
EE直播间
更多
我要评论
3
9
关闭 站长推荐上一条 /1 下一条