電子工程網(wǎng)

標題: 請大俠指點(diǎn)“單片機實(shí)時(shí)時(shí)鐘實(shí)驗程序”中的錯誤 [打印本頁(yè)]

作者: laoer002    時(shí)間: 2010-5-27 10:00
標題: 請大俠指點(diǎn)“單片機實(shí)時(shí)時(shí)鐘實(shí)驗程序”中的錯誤
#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};//秒分時(shí)日月周年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}; //秒分時(shí)日月周年 最低位讀寫(xiě)位
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)   //設計每個(gè)5秒 交替顯示 年月日 時(shí)分秒
  {
  case 0:
  case 2:
  case 4:
  case 6:
  case 8:
  case 10:
   l_tmpdisplay[0]=l_tmpdate[2]/16;   //數據的轉換,因我們采用數碼管0~9的顯示,將數據分開(kāi)
   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;//定時(shí)器設置 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++)      //循環(huán)8次 寫(xiě)入數據
   {
     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_();   //啟動(dòng)
  Write_Ds1302_Byte(address); //發(fā)送地址
  Write_Ds1302_Byte(dat);  //發(fā)送數據
  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++)   //循環(huán)8次 讀取數據
  {  
   if(SDA)
   temp|=0x80;   //每次傳輸低字節
  SCK=0;
  temp>>=1;   //右移一位
   SCK=1;
}
  RST=0;
_nop_();       //以下為DS1302復位的穩定時(shí)間
  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次讀取 秒分時(shí)日月周年
{
  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次寫(xiě)入 秒分時(shí)日月周年
   {
   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)       //隔段時(shí)間讀取1302的數據。時(shí)間間隔可以調整
      {
   ReadRTC_Flag=1; //使用標志位判斷
   num=0;
   }
   
    }
}
作者: laoer002    時(shí)間: 2010-5-27 10:03
程序運行不出來(lái)!不知什么地方錯誤!請各位高手指點(diǎn)!

作者: pcbkey    時(shí)間: 2015-2-7 10:17
支持一下




歡迎光臨 電子工程網(wǎng) (http://selenalain.com/) Powered by Discuz! X3.4
午夜高清国产拍精品福利|亚洲色精品88色婷婷七月丁香|91久久精品无码一区|99久久国语露脸精品|动漫卡通亚洲综合专区48页