標(biāo)題:
單片機(jī)+DS1302顯示年月日加星期程序與Proteus仿真圖
[打印本頁]
作者:
com456
時(shí)間:
2022-5-28 15:13
標(biāo)題:
單片機(jī)+DS1302顯示年月日加星期程序與Proteus仿真圖
萌新程序,如果有錯(cuò)誤望見諒。
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
51hei.gif
(65.58 KB, 下載次數(shù): 39)
下載附件
2022-5-29 22:28 上傳
單片機(jī)源程序如下:
#include<reg51.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit rs=P1^0;
sbit rw=P1^1;
sbit e=P1^2;
sbit RST=P1^6;
sbit SCK=P1^4;
sbit IO=P1^5;
sbit set=P3^2;
sbit add=P3^3;
sbit dec=P3^4;
uint m,f,h,t,a,mode,c;
uchar shu1[8]={6,5,4,3,2,7,1};
uchar shu2[8];
void delay_LCD(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void com(uchar com)
{
rs=0;
e=0;
rw=0;
P0=com;
delay_LCD(1);
e=1;
delay_LCD(1);
e=0;
}
void shu(uchar shu)
{
rs=1;
e=0;
rw=0;
P0=shu;
delay_LCD(1);
e=1;
delay_LCD(1);
e=0;
}
void chu( )
{
com(0x38);
com(0x0c);
com(0x01);
com(0x06);
}
ds1302_read(uchar add)
{
uint i,m1,m;
RST=0; _nop_();
SCK=0; _nop_();
RST=1;
for(i=0;i<8;i++)
{
IO=add&0x01;
add=add>>1;
SCK=1;_nop_();
SCK=0;_nop_();
}
for(i=0;i<8;i++)
{
m1=IO;
m=(m>>1)|(m1<<7);
SCK=1;_nop_();
SCK=0;_nop_();
}
RST=0;_nop_();
SCK=1;
_nop_();IO = 0;
_nop_();IO = 1;
_nop_();
return m;
}
void ds1302_write(uchar add,uchar m)
{
uchar i;
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1;
for(i=0;i<8;i++)
{
IO=add&0x01;
add=add>>1;
SCK=1;
_nop_();
SCK=0;
_nop_();
}
for(i=0;i<8;i++)
{
IO=m&0x01;
m=m>>1;
SCK=1;
_nop_();
SCK=0;
_nop_();
}
RST=0;
_nop_();
}
read_ds1302()
{
uchar i;
shu1[6]=ds1302_read(0x81)&0x7f;
shu1[5]=ds1302_read(0x83);
shu1[4]=ds1302_read(0x85);
shu1[3]=ds1302_read(0x87);
shu1[2]=ds1302_read(0x89);
shu1[7]=ds1302_read(0x8b);
shu1[1]=ds1302_read(0x8d);
for(i=1;i<8;i++)
{
shu2[i]=(shu1[i]>>4)*10+(shu1[i]&0x0f);
}
}
void ds1302_init()
{
uint b;
RST=0;
SCK=0;
ds1302_write(0x8e,shu1[b]);
ds1302_write(0x80,shu1[b]);
ds1302_write(0x82,shu1[b]);
ds1302_write(0x84,shu1[b]);
ds1302_write(0x8a,shu1[b]);
ds1302_write(0x86,shu1[b]);
ds1302_write(0x88,shu1[b]);
ds1302_write(0x8c,shu1[b]);
ds1302_write(0x8e,shu1[b]);
}
uchar code Data_table[7][3]={"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
code uchar table_week[12]=(0,3,3,6,1,4,6,2,5,0,3,5);
uchar Conver_week(uchar year,uchar month,uchar day)
{
uchar p1,p2,week;
year+=0x64;
p1=year/0x4;
p2=year+p1;
p2=p2%0x7;
p2=p2+day+table_week[month-1];
if(year%0x4==0&&month<3)p2-=1;
week=p2%0x7;
return week;
}
void main()
{
uchar xq,num;
chu();
while(1)
{
read_ds1302();
com(0x82);
shu(shu2[1]/10+0x30);
shu(shu2[0]%10+0x30);
shu(shu2[1]/10+0x30);
shu(shu2[1]%10+0x30);
shu('-');
shu(shu2[2]/10+0x30);
shu(shu2[2]%10+0x30);
shu('-');
shu(shu2[3]/10+0x30);
shu(shu2[3]%10+0x30);
com(0xc2);
shu(shu2[4]/10+0x30);
shu(shu2[4]%10+0x30);
shu(':');
shu(shu2[5]/10+0x30);
shu(shu2[5]%10+0x30);
shu(':');
shu(shu2[6]/10+0x30);
shu(shu2[6]%10+0x30);
xq=Conver_week(shu2[1],shu2[2],shu2[3]);
com(0x80+13);
for(num=0;num<3;num++)
{ shu(Data_table[xq][num]);
}
}
}
復(fù)制代碼
Keil代碼與Proteus8.8仿真下載:
顯示年月日.zip
(109.18 KB, 下載次數(shù): 45)
2022-5-28 15:12 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
作者:
shenqiwei
時(shí)間:
2022-6-1 17:16
為什么不用模塊化編程
作者:
1282211319
時(shí)間:
2022-6-6 09:11
怎么設(shè)置
作者:
mirenhuan
時(shí)間:
2022-11-22 11:51
怎么感覺代碼不完整
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1