|
#include<iom16.h>
#include<macros.h>
#define uchar unsigned char
#define uint unsigned int
#define CLK PD1
//脉冲线
#define IO PD2
//数据线
#define RST PD3
//复位控制线
#define CLK_H PORTD |= (1 << PD1)
#define CLK_L PORTD &= ~(1 << PD1)
#define IO_H PORTD |=(1 << PD2)
#define IO_L PORTD &=~(1 << PD2)
#define RST_H PORTD |= (1 << PD3)
#define RST_L PORTD &= ~(1 << PD3)
#define IO_R PIND & (1 << PD2)
/*电平读取*/
#define IO_IN DDRD &= ~(1 << PD2)
/*方向输入*/
#define IO_OUT DDRD |= (1 << PD2)
/*方向输出*/
#define RST_IN DDRD &= ~(1 << PD3)
/*方向输入*/
#define RST_OUT DDRD |= (1 << PD3)
/*方向输出*/
#define CLK_IN DDRD &= ~(1 << PD1)
/*方向输入*/
#define CLK_OUT DDRD |= (1 << PD1)
/*方向输出*/
#define LCDa_L1 0x80
//第一行的地址:0x80+addr ,addr为列数
#define LCDa_L2 0xC0
//第二行的地址:0x80+0x40+addr
uchar kk[]={'0','1','2','3','4','5','6','7','8','9'};
uchar y1,y2;
void Delayus(uint US)
{
uint i;
US=US*5/4; //5/4是在8MHz晶振下,通过软件仿真反复实验得到的数值
for( i=0;i<US;i++);
}
void delay(uint MS) //1毫秒延时
{
uint i,j;
for( i=0;i<MS;i++)
for(j=0;j<1141;j++); //1141是在8MHz晶振下,通过软件仿真反复实验得到的数值
}
void write_com(uchar com)
{
PORTD&=~BIT(6);
PORTC=com;
PORTD|=BIT(7);
delay(1);
PORTD&=~BIT(7);
}
void write_dat(uchar dat)
{
PORTD|=BIT(6);
PORTC=dat;
PORTD|=BIT(7);
delay(1);
PORTD&=~BIT(7);
}
void LCD1602_initial(void)
{
uchar i;
DDRC=0XFF;
DDRD|=BIT(6)|BIT(7);//D4为RS,D5为E
PORTD&=~BIT(7);
write_com(0x38);//16*2显示
delay(5);
write_com(0x01);//清0
delay(5);
write_com(0x0C);
delay(5);
write_com(0x06);
delay(5);
}
void LCD1602_gotoXY(uchar Row, uchar Col)
{
switch (Row) //选择行
{
case 2:
write_com(LCDa_L2 + Col); break; //写入第2行的指定列
default:
write_com(LCDa_L1 + Col); break; //写入第1行的指定列
}
}
void LCD1602_sendstr(uchar *ptString)
{
while((*ptString)!='\0') //字符串未结束
{
write_dat( *ptString++);
}
}
uchar ds1302_read()
{
uchar i,b=0;
IO_IN;//将数据端口设置为输入
IO_L; //无上拉电阻
CLK_OUT;
CLK_L;
delay(1);
for(i=0;i<8;i++)
{
if(PIND&BIT(IO))
{
b=b|0x80;//先读低位
}
else
{
b=b|0x00;
}
CLK_H;
delay(1);
CLK_L;
delay(1);
if (i<7) b=b>>1;
return(b);
}
}
void ds1302_write(uchar com)
{
uchar i,a;
a=com;
IO_OUT;
CLK_OUT;
CLK_L;
for(i=0;i<8;i++)
{
if(a&0x01)
{
IO_H;//先写低位
}
else
{
IO_L;
}
CLK_H;
delay(1);
CLK_L;
delay(1);
a=a>>1;
}
}
uchar ds1302_read_baye(uchar adrr)
{
uchar aa;
RST_OUT;
RST_L;//ds1302_rst=0;
delay(1);
CLK_OUT;
CLK_L;//ds1302_clk=0;
delay(1);
RST_H;//ds1302_rst=1;
delay(1);
ds1302_write(adrr);//写入地址
IO_L;//ds1302_io=0;
delay(1);
aa=ds1302_read();
CLK_H;//ds1302_clk=1;
delay(1);
RST_L;//ds1302_rst=0;
return (aa);
}
uchar ds1302_write_baye(uchar adrr,uchar dat)
{
RST_OUT;
CLK_OUT;
RST_L;//ds1302_rst=0;
delay(1);
CLK_L;//ds1302_clk=0;
delay(1);
RST_H;//ds1302_rst=1;
delay(1);
ds1302_write(adrr);//写入地址
///ds1302_io=1;
ds1302_write(dat);
CLK_H;//ds1302_clk=1;
delay(1);
RST_L;//ds1302_rst=0;
}
uchar go10to16(uchar u)//10进制转换为16进制
{
uchar a,b,c;
if (u<16)//小于16不用转换
{
return(u);
}
else
{
a=u/16;//取商
b=u%16;//取模
c=a*10+b;
return(c);
}
}
void cuanhuan(uchar i)
{
uchar a,b;
a=i;
b=go10to16(a);
if(b>0&b<10)
{
y1=kk[0];
y2=kk;
}
else
{
y1=kk[b/10];
y2=kk[b%10];
}
}
void ds1302int(uchar year,uchar moon,uchar date,uchar hour,uchar mini,uchar sen)//当DS1302第一次使用时要进行设置
{
RST_OUT;
CLK_OUT;
RST_L; //s1302_rst=0;
delay(1);
CLK_L;//ds1302_clk=0;
delay(1);
RST_H;//ds1302_rst=1;
ds1302_write(0x8e);
ds1302_write(0x00);
RST_L;//ds1302_rst=0;
delay(1);
CLK_H;//ds1302_clk=1;
ds1302_write_baye(0x8c,year);//设置年为08年
delay(1);
ds1302_write_baye(0x88,moon);//设置月为3月
delay(1);
ds1302_write_baye(0x86,date);//设置日为27号
delay(1);
ds1302_write_baye(0x84,hour);//设置时9时
delay(1);
ds1302_write_baye(0x82,mini);//设置分为5分
delay(1);
ds1302_write_baye(0x80,sen);//设置秒为00秒
delay(1);
RST_L;//ds1302_rst=0;
delay(1);
CLK_L;//ds1302_clk=0;
delay(1);
RST_H;//ds1302_rst=1;
ds1302_write(0x8e);
ds1302_write(0x80);
}
void main()
{
uchar i;
LCD1602_initial();
delay(20);
ds1302int(8,3,0x27,9,0x27,59);//DS1302初始化
while(1)
{
LCD1602_gotoXY(1,0);
delay(60);
LCD1602_gotoXY(1,0); //在址00开始显示,第一行显示年-月-日
cuanhuan(ds1302_read_baye(0x8d));
write_dat(y1); //显示年
write_dat(y2);
write_dat('-');
cuanhuan(ds1302_read_baye(0x89));//读出月份
write_dat(y1); //显示月
write_dat(y2);
write_dat('-');
cuanhuan(ds1302_read_baye(0x87));//读出日
write_dat(y1);//显示日
write_dat(y2);
LCD1602_gotoXY(2,0); //在第二行显示时:分:秒
cuanhuan(ds1302_read_baye(0x85));//读出时
write_dat(y1); ;//显示时
write_dat(y2);
write_dat(':');
cuanhuan(ds1302_read_baye(0x83));//读出分
write_dat(y1);//显示分
write_dat(y2);
write_dat(':');
cuanhuan(ds1302_read_baye(0x81));//读出秒
write_dat(y1); //显示秒
write_dat(y2);
}
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)
|