OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 25025|回复: 0

我的AVR资料[程序篇] DS1302驱动源码(源码19)

[复制链接]

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165352
金钱
165352
注册时间
2010-12-1
在线时间
2108 小时
发表于 2010-12-24 03:16:47 | 显示全部楼层 |阅读模式

       这是AVR驱动DS1302的源码,同样是在数码管上显示。源码如下:
#include<mega16.h> 
#include<delay.h>
#define DS1302_PORT PORTC
#define DS1302_DDR  DDRC
#define DS1302_PIN  PINC
#define T_RST 0 //pc0
#define T_IO 1 //pc1
#define T_CLK 2 //pc2

#define SETBIT(x,y) (x|=(1<<y))//set appoint bit high     
#define CLRBIT(x,y) (x&=(~(1<<y)))//set appoint bit low  
#define CHKBIT(x,y) (x&(1<<y))//get appoint bit level
//write one byte data to ds1302                     
void ds1302_write_a_byte(unsigned char ucDa)
{
  unsigned char i;
  for(i=8; i>0; i--)
  {
      CLRBIT(DS1302_PORT,T_CLK);//set PC2 low
      if (ucDa&1) SETBIT(DS1302_PORT,T_IO);//set PC1 high
      else CLRBIT(DS1302_PORT,T_IO);//set PC1 low
      SETBIT(DS1302_PORT,T_CLK);//set PC2 high
      ucDa>>=1;
  }

//read one byte data from ds1302
unsigned char ds1302_read_a_byte(void)
{
  unsigned char i,t=0;
  CLRBIT(DS1302_DDR,T_IO);//set pc1 input
  CLRBIT(DS1302_PORT,T_IO);//set PC1 low(high resistance)
  for(i=8; i>0; i--)
  {
      t>>=1;
      SETBIT(DS1302_PORT,T_CLK);
      delay_us(1);//can't remove!
      CLRBIT(DS1302_PORT,T_CLK);
      delay_us(1);//can't remove!
      if(CHKBIT(DS1302_PIN,T_IO))t|=0x80;
  }
  SETBIT(DS1302_DDR,T_IO);//set pc1 OUTPUT
  return(t);

//write data to appoint address
void write_1302(unsigned char ucAddr, unsigned char ucDa)
{

  CLRBIT(DS1302_PORT,T_RST);                
  CLRBIT(DS1302_PORT,T_CLK);           
  SETBIT(DS1302_PORT,T_RST);         
  ds1302_write_a_byte(ucAddr);         
  CLRBIT(DS1302_PORT,T_CLK);
  ds1302_write_a_byte(ucDa);             
  CLRBIT(DS1302_PORT,T_CLK);                    
  CLRBIT(DS1302_PORT,T_RST);             
}
//read data from appoint address
unsigned char read_1302(unsigned char ucAddr)
{
  unsigned char ucDa;
  CLRBIT(DS1302_PORT,T_RST);
  CLRBIT(DS1302_PORT,T_CLK);
  SETBIT(DS1302_PORT,T_RST);
  ds1302_write_a_byte(ucAddr);                
  ucDa = ds1302_read_a_byte();              
  CLRBIT(DS1302_PORT,T_CLK);
  CLRBIT(DS1302_PORT,T_RST);
  return(ucDa);
}                   
//get data from ds1302
void v_Get1302(unsigned char ucCurtime[])
{
  unsigned char i;
  unsigned char ucAddr = 0x81;
  for (i=0;i<7;i++)
  {
      ucCurtime = read_1302(ucAddr);   
      ucAddr += 2;
  }
  CLRBIT(DS1302_PORT,T_CLK);
}
//init ds1302
void initialize_1302(void)
{
  SETBIT(DS1302_PORT,T_CLK);
  SETBIT(DS1302_PORT,T_IO);
  SETBIT(DS1302_PORT,T_RST);
  SETBIT(DS1302_DDR,T_CLK);
  SETBIT(DS1302_DDR,T_IO);
  SETBIT(DS1302_DDR,T_RST);
  delay_us(1);//can't remove!
  write_1302(0x8e,0x00);
  write_1302(0x90,0xA5);
  write_1302(0x80,0x00);
}
void display(unsigned char number,unsigned char position)//digtial show function
{  
    DDRA=0Xff;//set PORTA output
 ORTA=0XFF;//set PORTA output in high level
    switch(position)//choice the position
 {
  case 1:{PORTA&=0xf8;break;}
  case 2:{PORTA&=0xf9;break;}
  case 3:{PORTA&=0Xfa;break;}
  case 4:{PORTA&=0Xfb;break;}
  case 5:{PORTA&=0Xfc;break;}
  case 6:{PORTA&=0Xfd;break;}
  case 7:{PORTA&=0Xfe;break;}
  case 8:{PORTA&=0Xff;break;}
  } 
    switch(number)//show number
 {
  case 0:{PORTA&=0x87;break;}    //0
  case 1:{PORTA&=0x8f;break;}    //1
  case 2:{PORTA&=0x97;break;}    //2
  case 3:{PORTA&=0x9f;break;}    //3
  case 4:{PORTA&=0xa7;break;}    //4
         case 5:{PORTA&=0xaf;break;}      //5
         case 6:{PORTA&=0xb7;break;}    //6
  case 7:{PORTA&=0xbf;break;}    //7
  case 8:{PORTA&=0xc7;break;}    //8
     case 9:{PORTA&=0xcf;break;}    //9
     default:{PORTA=0xff;break;}    //null
 }       
}
//only 24 hours system 
//change data from bin to dec
void revise(unsigned char t[7])
{
 unsigned char t1,t2,temp,i;
 for(i=0;i<7;i++)
 {
     temp=t;
   temp<<=4;
   temp>>=4;
   t1=temp;
   temp=t;
   if(i!=2)temp>>=4;
  else
  {
      temp<<=1;
    temp>>=5;
  }
   t2=temp;
  t=t1+t2*10;
 }
}
void main()
{
   unsigned char t1,t2;
  unsigned char temp[7];
  initialize_1302();
  write_1302(0x8e,0x00);//set vialib
  write_1302(0x80,0x33);//second set
  write_1302(0x82,0x55);//minute set
  write_1302(0x84,0x11);//hour set
  write_1302(0x86,0x30);//day set
  write_1302(0x88,0x05);//month set
  write_1302(0x8a,0x03);//week set
  write_1302(0x8c,0x07);//year set
  write_1302(0x8e,0x80);//set unvialib
  while(1)
  {    
    v_Get1302(temp);
    revise(temp);
    t1=temp[0]%10;
    t2=(temp[0]/10)%10;
    display(t1,8);
    delay_ms(1);
    display(t2,7);
    delay_ms(1);
    t1=temp[1]%10;
    t2=(temp[1]/10)%10;
    display(t1,5);
    delay_ms(1);
    display(t2,4);
    delay_ms(1);
    t1=temp[2]%10;
    t2=(temp[2]/10)%10;
    display(t1,2);
    delay_ms(1);
    display(t2,1);
    delay_us(500);
  }
}


 

我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-11-24 13:38

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表