数模电知识 经典电路欣赏单片机 嵌入式系统 接口电路 仿真与制板 EDA DSP MEMS 通信技术 现代电子 电子维修
返回首页
当前位置: QQ电子网 > 电子技术 > 单片机 >

AVR单片机通过DS1302做的实时时钟(原创调试成功)

时间:2010-04-05 14:08来源: 作者:lihom 点击:

/*切记:此程序使用外部8M石英晶体振荡源。注意熔丝位配置。
  CKSEL0=1
  CKSEL1=1
  CKSEL2=1
  CKSEL3=1
  CKOPT=1
  */

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
//#include <macros.h>

#define SET_SCK PORTD|=(1<<6);
#define CLR_SCK PORTD&=~(1<<6); 
#define SET_SDA PORTD|=(1<<4);
#define CLR_SDA PORTD&=~(1<<4);
#define SET_RST PORTD|=(1<<5);
#define CLR_RST PORTD&=~(1<<5); 

char ReadRTC_Flag;

unsigned char l_tmpdate[7]={0,0,12,15,4,3,9};//秒分时日月周年08-05-15 12:00:00
unsigned char l_tmpdisplay[8];
unsigned char write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //秒分时日月周年 最低位读写位
unsigned char read_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d}; 

const unsigned char table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
            //共阴数码管 0-9  '-' '熄灭‘表
const 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)   
{
   
 DDRC=0xff;
 DDRA=0xff;
 DDRD=0xff;
 
 InitTIMER0();
 
  Set_RTC();
 sei();
 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:
  case 12:
  case 14:
  case 16:
   
  case 1:
  case 3:
  case 5:
  case 7:
  case 9:
  case 11:
  case 13:
  case 15:
  case 17:
   /*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;*/
   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;
  default:
   break;
  }
 
  }
 }
}

void InitTIMER0(void)
{
 TCNT0=0;//T/C0开始值
TCCR0=_BV(CS01)|_BV(CS00);//预分频ck/8,计数允许
TIMSK=_BV(TOIE0); //T/C0中断允许
}

 

void Write_Ds1302_Byte(unsigned  char temp)
{
 unsigned char i;
 for (i=0;i<8;i++)      //循环8次 写入数据
  {
   CLR_SCK;
     if(temp&0x01){SET_SDA;}
  else CLR_SDA;
   
     temp>>=1;    //右移一位
     SET_SCK;
   }
}  

/****************************************************************************/
void Write_Ds1302( unsigned char address,unsigned char dat )    
{
  CLR_RST;
 asm("nop");
  CLR_SCK;
 asm("nop");
  SET_RST; 
    asm("nop");   //启动
  Write_Ds1302_Byte(address); //发送地址
  Write_Ds1302_Byte(dat);  //发送数据
  CLR_RST;    //恢复
}
/****************************************************************************/
unsigned char Read_Ds1302 ( unsigned char address )
{
  unsigned char i,temp=0x00;
  CLR_RST;
 asm("nop");
  CLR_SCK;
 asm("nop");
  SET_RST;
 asm("nop");
  Write_Ds1302_Byte(address);
 DDRD&=~(1<<4);
  for (i=0;i<8;i++)   //循环8次 读取数据
  {  
   if(PIND&(1<<PD4))
   temp|=0x80;   //每次传输低字节
  CLR_SCK;
  temp>>=1;   //右移一位
   SET_SCK;
 }
 DDRD|=(1<<4);
  CLR_RST;
 asm("nop");  //以下为DS1302复位的稳定时间
  CLR_RST;
 CLR_SCK;
 asm("nop");
 SET_SCK;
 asm("nop");
 CLR_SDA;
 asm("nop");
 SET_SDA;
 asm("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);
}
//中断,用于数码管扫描
#pragma interrupt_handler T0_overflow:20 
SIGNAL(SIG_OVERFLOW0)
{

    static unsigned char i=0,num;
  
  PORTA=table[l_tmpdisplay[i]];  //查表法得到要显示数字的数码段
 PORTC=table1[7-i];
 i++;
 if(i==8)
   {
    i=0;
    num++;
    if(10==num)       //隔段时间读取1302的数据。时间间隔可以调整
      {
   ReadRTC_Flag=1; //使用标志位判断
   num=0;
   }
   
    }
 }
 


分享到:

本站相关资讯链接QQ电子网
找不到想要的,那就在这里Google一下!
(责任编辑:admin)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名:密码: 验证码:点击我更换图片
发布者资料
lihom 查看详细资料 发送留言 加为好友 用户等级:注册会员 注册时间:2010-04-05 09:04 最后登录:2010-04-10 10:04
推荐内容
站长推荐