專(zhuān)注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

Atmega16與DS1302數(shù)碼管顯示程序

作者:我行天下   來(lái)源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2014年03月31日   【字體:

/*本程序?yàn)榘宋还碴帢O數(shù)碼管且有兩個(gè)573控制的動(dòng)態(tài)掃描,本程序只用數(shù)碼管顯示到秒鐘,
其他的要顯示請(qǐng)自己編寫(xiě),能用1602顯示更好*/
#include <iom16v.h>
#include <macros.h>//這里面有BIT(),所以要包含
#define uchar unsigned char
#define uint unsigned int
//#pragma data:code //注code的功能是把后面的數(shù)據(jù)存在程序存貯器中,不用code就放到了隨機(jī)存貯器中.
const table[]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00} ;
/*如果用uchar table[]就放到了數(shù)據(jù)存貯器中。決不要這樣用,這樣占用空間多。*/
/*兩個(gè)573,段碼PA3,位碼PA4*/
void delay_ms(uint ms)
{
        uint i,j;
 for(i=ms;i>0;i--)
 
    for(j=220;j>0;j--);
    
}
uchar time[7]={12,3,7,25,16,59,55};//年,周,月,日,時(shí),分,秒
uchar write_add[7]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80};
uchar read_add[7]={0x8d,0x8b,0x89,0x87,0x85,0x83,0x81};
uchar year,week,month,day,hour,minute,second;
#define SCK_CLR  PORTA&=~BIT(0)
//拉低時(shí)鐘線
#define SCK_SET  PORTA|=BIT(0)
//拉高時(shí)鐘線
#define SCK_OUT  DDRA|=BIT(0)
//把SCK設(shè)為輸出
#define RST_CLR  PORTA&=~BIT(2)
//拉低RST線
#define RST_SET  PORTA|=BIT(2)
//拉高RST線
#define RST_OUT  DDRA|=BIT(2)
//把RST設(shè)為輸出
#define IO_CLR  PORTA&=~BIT(1)
//拉低IO線
#define IO_SET  PORTA|=BIT(1)
//拉高IO線
#define IO_OUT  DDRA|=BIT(1)
//把IO設(shè)為輸出
#define IO_IN  DDRA&=~BIT(1)
//把IO設(shè)為輸出
#define IO_RD PINA&BIT(1)
//從IO中讀數(shù)據(jù)
 

void show(uchar dat,uchar wei)
{
 
   DDRA|=BIT(3);//把PA3設(shè)為輸出
   DDRA|=BIT(4);//把PA4設(shè)為輸出
   DDRB=0XFF;//把PB口設(shè)為輸出型,全為高
    PORTA|=BIT(3);
 PORTB=table[dat];
 PORTA&=~BIT(3);
 
 PORTB=0XFF;
 PORTB&=~BIT(wei);
 PORTA|=BIT(4);
 PORTA&=~BIT(4);
 delay_ms(1);
}
 
/********1302部分*************/
void write_1302_byte(uchar dat)
{
uchar i;
IO_OUT;
  for(i=0;i<8;i++)
    {
   if(dat&0x01)
     {
  IO_SET;
  }
  else
  {
  IO_CLR;
  }
   SCK_SET;
   SCK_CLR;
   dat=dat>>1;
 }
}
uchar read_1302(uchar add)
{
uchar i,value;
RST_CLR;
SCK_CLR;
RST_SET;
write_1302_byte(add);
IO_IN;
 
    for(i=0;i<8;i++)
  {
  value=value>>1;
    if(IO_RD)
      {
   value=value|0x80;
   }
     SCK_SET;
  SCK_CLR;
 
  }
   RST_CLR;
   return value;
}
void write_1302(uchar add,uchar dat)
{
RST_CLR;
SCK_CLR;
RST_SET;
write_1302_byte(add);
write_1302_byte(dat);
RST_CLR;
}
void init_1302()
{
uchar i,k;
  for(i=0;i<7;i++)
     {
  k=time[i]/10;
  time[i]=time[i]%10;
  time[i]=time[i]+k*16;//這幾句是把10進(jìn)制數(shù)變成BCD16進(jìn)制碼
  }
  write_1302(0x8e,0x00);//去除寫(xiě)保護(hù)
  for(i=0;i<7;i++)
    {
 write_1302(write_add[i],time[i]);
 }
   write_1302(0x8e,0x80);//加上寫(xiě)保護(hù)
}
 
 
void read_time()//讀出所有時(shí)間
{
uchar i;
for(i=0;i<7;i++)
  {
  time[i]=read_1302(read_add[i]);
  }
}
void time_pro()//把讀出的數(shù)值賦給年月日時(shí)分秒周
{
year=time[0];
week=time[1];
month=time[2];
day=time[3];
hour=time[4];
minute=time[5];
second=time[6];
}

void display_sec(uchar dat)
{
uchar a,b;
a=dat/16;//把BCD碼轉(zhuǎn)換成10進(jìn)制數(shù)
b=dat%16;
show(a,0);
show(b,1);
}
/*顯示年月日周時(shí)分請(qǐng)用上述程序稍加改動(dòng)即可*/
void main()
{
SCK_OUT;
RST_OUT;
init_1302();
  while(1)
     {
  read_time();
 time_pro();
 display_sec(second);
  }
}

關(guān)閉窗口

相關(guān)文章