新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > Atmega16與DS1302數(shù)碼管顯示程序

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

作者: 時間:2016-12-02 來源:網(wǎng)絡(luò) 收藏
/*本程序為八位共陰極數(shù)碼管且有兩個573控制的動態(tài)掃描,本程序只用數(shù)碼管顯示到秒鐘,

其他的要顯示請自己編寫,能用1602顯示更好*/
#include
#include //這里面有BIT(),所以要包含
#define uchar unsigned char
#define uint unsigned int
//#pragmadata:code //注code的功能是把后面的數(shù)據(jù)存在程序存貯器中,不用code就放到了隨機存貯器中.
const table[]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00} ;
/*如果用uchar table[]就放到了數(shù)據(jù)存貯器中。決不要這樣用,這樣占用空間多。*/
/*兩個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};//年,周,月,日,時,分,秒
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)
//拉低時鐘線
#define SCK_SET PORTA|=BIT(0)
//拉高時鐘線
#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ù)

本文引用地址:http://butianyuan.cn/article/201612/324664.htm

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(ucharadd,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進制數(shù)變成BCD16進制碼
}
write_1302(0x8e,0x00);//去除寫保護
for(i=0;i<7;i++)
{
write_1302(write_add[i],time[i]);
}
write_1302(0x8e,0x80);//加上寫保護
}


void read_time()//讀出所有時間
{
uchar i;
for(i=0;i<7;i++)
{
time[i]=read_1302(read_add[i]);
}
}
void time_pro()//把讀出的數(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進制數(shù)
b=dat%16;
show(a,0);
show(b,1);
}
/*顯示年月日周時分請用上述程序稍加改動即可*/
void main()
{
SCK_OUT;
RST_OUT;
init_1302();
while(1)
{
read_time();
time_pro();
display_sec(second);
}
}



評論


技術(shù)專區(qū)

關(guān)閉