查看: 3715|回复: 2

请大侠指点“单片机实时时钟实验程序”中的错误

[复制链接]
发表于 2010-5-27 10:00:06 | 显示全部楼层 |阅读模式
关键词: 程序 , 大侠 , 单片机 , 时钟 , 实时
#include
#include
sbit SCK=P1^4;  
sbit SDA=P1^5;  
sbit RST=P1^6;   // DS1302复位
bit ReadRTC_Flag;
unsigned char l_tmpdate[7]={0,0,12,9,9,3,9};//秒分时日月周年08-05-15 12:00:00
unsigned char l_tmpdisplay[8];
code unsigned char write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //秒分时日月周年 最低位读写位
code unsigned char read_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};  
unsigned char code  table[]={0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09,0xfd,0xff};
            //共阴数码管 0-9  '-' '熄灭‘表
code unsigned char table1[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};   //显示位码表
                              
void Write_Ds1302_byte(unsigned char temp);
void Write_Ds1302( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302 ( unsigned char address );
void Read_RTC(void);//read RTC
void Set_RTC(void); //set RTC
void InitTIMER0(void);//inital timer0
void main(void)   
{
    InitTIMER0();
  Set_RTC();
while(1){
  if(ReadRTC_Flag)
  {
     ReadRTC_Flag=0;
  Read_RTC();
  switch (l_tmpdate[0]/5)   //设计每个5秒 交替显示 年月日 时分秒
  {
  case 0:
  case 2:
  case 4:
  case 6:
  case 8:
  case 10:
   l_tmpdisplay[0]=l_tmpdate[2]/16;   //数据的转换,因我们采用数码管0~9的显示,将数据分开
   l_tmpdisplay[1]=l_tmpdate[2]&0x0f;
   l_tmpdisplay[2]=10;          //加入"-"
   l_tmpdisplay[3]=l_tmpdate[1]/16;
   l_tmpdisplay[4]=l_tmpdate[1]&0x0f;
   l_tmpdisplay[5]=10;
   l_tmpdisplay[6]=l_tmpdate[0]/16;
   l_tmpdisplay[7]=l_tmpdate[0]&0x0f;
   break;
  case 1:
  case 3:
  case 5:
  case 7:
  case 9:
  case 11:
   l_tmpdisplay[0]=l_tmpdate[6]/16;
   l_tmpdisplay[1]=l_tmpdate[6]&0x0f;
   l_tmpdisplay[2]=10;
   l_tmpdisplay[3]=l_tmpdate[4]/16;
   l_tmpdisplay[4]=l_tmpdate[4]&0x0f;
   l_tmpdisplay[5]=10;
   l_tmpdisplay[6]=l_tmpdate[3]/16;
   l_tmpdisplay[7]=l_tmpdate[3]&0x0f;
   break;
  default:
   break;
  }

  }
}
}
void InitTIMER0(void)
{
TMOD|=0x01;//定时器设置 16位
TH0=0xef;//初始化值
TL0=0xf0;
ET0=1;
TR0=1;
EA=1;
}
/****************************************************************************/
void Write_Ds1302_Byte(unsigned  char temp)
{
unsigned char i;
for (i=0;i<8;i++)      //循环8次 写入数据
   {
     SCK=0;
     SDA=temp&0x01;      //每次传输低字节
     temp>>=1;    //右移一位
     SCK=1;
   }
}   
/****************************************************************************/
void Write_Ds1302( unsigned char address,unsigned char dat )     
{
  RST=0;
_nop_();
  SCK=0;
_nop_();
  RST=1;
    _nop_();   //启动
  Write_Ds1302_Byte(address); //发送地址
  Write_Ds1302_Byte(dat);  //发送数据
  RST=0;    //恢复
}
/****************************************************************************/
unsigned char Read_Ds1302 ( unsigned char address )
{
  unsigned char i,temp=0x00;
  RST=0;
_nop_();
  SCK=0;
_nop_();
  RST=1;
_nop_();
  Write_Ds1302_Byte(address);
  for (i=0;i<8;i++)   //循环8次 读取数据
  {  
   if(SDA)
   temp|=0x80;   //每次传输低字节
  SCK=0;
  temp>>=1;   //右移一位
   SCK=1;
}
  RST=0;
_nop_();       //以下为DS1302复位的稳定时间
  RST=0;
SCK=0;
_nop_();
SCK=1;
_nop_();
SDA=0;
_nop_();
SDA=1;
_nop_();
return (temp);   //返回
}
/****************************************************************************/
void Read_RTC(void)  //读取 日历
{
unsigned char i,*p;
p=read_rtc_address;  //地址传递
for(i=0;i<7;i++)  //分7次读取 秒分时日月周年
{
  l_tmpdate[i]=Read_Ds1302(*p);
  p++;
}
}
/***********************************************************************/
void Set_RTC(void)  //设定 日历
{
unsigned char i,*p,tmp;
for(i=0;i<7;i++)  //BCD处理
  {           
  tmp=l_tmpdate[i]/10;
  l_tmpdate[i]=l_tmpdate[i]%10;
  l_tmpdate[i]=l_tmpdate[i]+tmp*16;
  }
  Write_Ds1302(0x8E,0x00);

  p=write_rtc_address; //传地址
  for(i=0;i<7;i++)  //7次写入 秒分时日月周年
   {
   Write_Ds1302(*p,l_tmpdate[i]);
    p++;  
  }
  Write_Ds1302(0x8E,0x80);
}
void tim(void) interrupt 1 using 1//中断,用于数码管扫描
{
    static unsigned char i,num;
    TH0=0xf5;
    TL0=0xe0;
  P0=table[l_tmpdisplay[i]];  //查表法得到要显示数字的数码段
P2=table1[7-i];
i++;
if(i==8)
   {
    i=0;
    num++;
    if(10==num)       //隔段时间读取1302的数据。时间间隔可以调整
      {
   ReadRTC_Flag=1; //使用标志位判断
   num=0;
   }
   
    }
}
 楼主| 发表于 2010-5-27 10:03:24 | 显示全部楼层
程序运行不出来!不知什么地方错误!请各位高手指点!
发表于 2015-2-7 10:17:12 | 显示全部楼层
支持一下
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关于我们  -  服务条款  -  使用指南  -  站点地图  -  友情链接  -  联系我们
电子工程网 © 版权所有   京ICP备16069177号 | 京公网安备11010502021702
快速回复 返回顶部 返回列表