//芯片 ATMEGA16L
//时钟 4MHz 内部
// DS1302 上升沿读写数据
// DS1302 读数据时 IO口上拉无效
//短延时 2us wait(); 长延时 n ms delay_nms();
#include
//#include
#define hc164_da
#define hc164_clk PORTD.1
void hc164_send_byte (unsigned char byte); // 数码管显示
//相关定义:
#define uint unsigned int
#define uchar unsigned char
#define DS1302_SCLK 4 //PD4
#define DS1302_SDA 5 //PD5
#define DS1302_RST 6 //PD6
//ds1302相关
//DS1302_RST=1
#define Set_DS1302_RST() PORTD|=1<
#define Clr_DS1302_RST() PORTD&=~(1<
#define Set_DS1302_SDA() PORTD|=1<
#define Clr_DS1302_SDA() PORTD&=~(1<
#define Set_DS1302_SCLK() PORTD|=1<
#define Clr_DS1302_SCLK() PORTD&=~(1<
#define Set_DS1302_DDRSDA() DDRD|=1<
#define Clr_DS1302_DDRSDA() DDRD&=~(1<
#define DS1302_MIN_Reg 0x82
#define DS1302_HR_Reg 0x84
#define DS1302_DATE_Reg 0x86
#define DS1302_MONTH_Reg 0x88
#define DS1302_DAY_Reg 0x8a
#define DS1302_YEAR_Reg 0x8c
#define DS1302_CONTROL_Reg 0x8e
#define DS1302_CHARGER_Reg 0x90
#define DS1302_CLKBURST_Reg 0xbe
//全局变量
//为了方便,我把个位和十位分开了
uchar year1=0x00;
uchar year0=0x00;
uchar month1=0x00;
uchar month0=0x00;
uchar date0=0x00;
uchar date1=0x00;
uchar day=0x00;
uchar hour1=0x00;
uchar hour0=0x00;
uchar minute1=0x00;
uchar minute0=0x00;
uchar second1=0x00;
uchar second0=0x00;
char ledxs[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};; // 年-月-日 LED显示
flash char tab[]={0xb7,0x12,0x67,0x76,0xd2,0xf4,0xf5,0x16,0xf7,0xf6,0xd7,0xf1,0xa5,0x73,0xe5,0xc5,0,0xff,0x40};
//自程序开始
void wait(void)
{
#asm
nop
nop
nop
nop
nop
nop
nop
nop
#endasm
}
void DS1302_Write(uchar reg,uchar da
{
uchar i;
Set_DS1302_DDRSDA();
Clr_DS1302_RST();
wait();
Clr_DS1302_SCLK();
wait();
Set_DS1302_RST();
wait();
for(i=8;i>0;i--)
{
if(reg&0x01) Set_DS1302_SDA();
else Clr_DS1302_SDA();
wait();
Set_DS1302_SCLK();
wait();
Clr_DS1302_SCLK();
wait();
reg>>=1;
}
for(i=8;i>0;i--)
{
if(da
else Clr_DS1302_SDA();
wait();
Set_DS1302_SCLK();
wait();
Clr_DS1302_SCLK();
wait();
da
}
Clr_DS1302_RST();
wait();
Clr_DS1302_DDRSDA();
}
uchar DS1302_Read(uchar reg)
{
uchar da
reg+=1;//读标志
Set_DS1302_DDRSDA();
Clr_DS1302_RST();
wait();
Clr_DS1302_SCLK();
wait();
Set_DS1302_RST();
wait();
for(i=8;i>0;i--)
{
if(reg&0x01) Set_DS1302_SDA();
else Clr_DS1302_SDA();
wait();
Set_DS1302_SCLK();
wait();
Clr_DS1302_SCLK();
wait();
reg>>=1;
}
PORTD &= ~(1 << DS1302_SDA);
Clr_DS1302_DDRSDA();
for(i=8;i>0;i--)
{
da
if((PIND&(1<
Set_DS1302_SCLK();
wait();
Clr_DS1302_SCLK();
wait();
}
Clr_DS1302_RST();
wait();
return(da
}
/*uchar Check_DS1302(void)
{
DS1302_Write(DS1302_CONTROL_Reg,0x80);
if(DS1302_Read(DS1302_CONTROL_Reg)==0x80) return 1;
return 0;
}*/
void DS1302_Init(void)
{
DS1302_Write(DS1302_CONTROL_Reg,0x00);//关闭写保护
//DS1302_Write(DS1302_SEC_Reg,0x80);//暂停
//DS1302_Write(DS1302_CHARGER_Reg,0xa9);//涓流充电
DS1302_Write(DS1302_YEAR_Reg,0x10); //年
DS1302_Write(DS1302_MONTH_Reg,0x01); //月
DS1302_Write(DS1302_DATE_Reg,0x28); //日
DS1302_Write(DS1302_DAY_Reg,0x04); //周
DS1302_Write(DS1302_HR_Reg,0x20); //时
DS1302_Write(DS1302_MIN_Reg,0x58); //分
DS1302_Write(DS1302_SEC_Reg,0x00); //秒
DS1302_Write(DS1302_CONTROL_Reg,0x80);//打开写保护
}
void ReadTime(void)
{
uchar da
da
year0=da
year1=da
da
month0=da
month1=da
da
date0=da
date1=da
da
day=da
da
hour0=da
hour1=da
da
minute0=da
minute1=da
da
second0=da
second1=da
}
void delay_nms(unsigned int k) //延时 n ms
{
while(k) //4M晶振,一个循环1ms
{
int i;
i=280;
while(i--);
k=k-1;
}
}
void main(void)
{
unsigned char i,k;
delay_nms(200);
DDRD = 0xff;
DS1302_Init(); // 设置初始时间
while(1)
{
k=3;
while(k>0) //年-月-日 显示3秒 (例 09-06-04 )
{
ReadTime(); //取得当前时间
ledxs[0]=year1;
ledxs[1]=year0; // 年
ledxs[2]=18; // -
ledxs[3]=month1;
ledxs[4]=month0; // 月
ledxs[5]=18; // -
ledxs[6]=date1;
ledxs[7]=date0; // 日
for(i=0;i<8;i++) //串行显示
{
hc164_send_byte ( tab[ledxs[i]] );
wait();
}
k--;
delay_nms(1000);
}
k=100;
while(k>0) //时-分-秒 显示10秒 (例 16-30-00 )
{
ReadTime(); //取得当前时间
ledxs[0]=hour1;
ledxs[1]=hour0; // 时
ledxs[2]=18; // -
ledxs[3]=minute1;
ledxs[4]=minute0; // 分
ledxs[5]=18; // -
ledxs[6]=second1;
ledxs[7]=second0; // 秒
for(i=0;i<8;i++) //串行显示
{
hc164_send_byte ( tab[ledxs[i]] );
//delay_nms(1);
wait();
}
k--;
delay_nms(100);
}
}
}
void hc164_send_byte (unsigned char byte)
{
unsigned char i;
for(i=0;i<8;i++)
{
hc164_da
hc164_clk = 1;
hc164_clk = 0;
}
}
上一篇:tlc549 CVAVR程序
下一篇:atmega16片上A/D程序-CVAVR程序
推荐阅读最新更新时间:2024-03-16 15:18