我剛學(xué)單片機(jī),做了個(gè)用DS1302在數(shù)碼管顯示的程序,可是數(shù)碼管每個(gè)2秒才會(huì)加1,求各位幫忙看看
#include<reg52.h> #include <intrins.h> /******************端口定義*****************/ sbit io=P1^0; sbit sclk=P1^1; sbit ce=P1^2; sbit dula=P2^0; sbit wela=P2^1; uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; /******************延時(shí)函數(shù)*****************/ void delay(uchar x) { uchar a,b; for(a=x;a>0;a--) for(b=110;b>0;b--); } /******************ds1302初始化*****************/ void reset_ds1302() { ce=0; _nop_(); sclk=0; _nop_(); ce=1; _nop_(); //延時(shí)一個(gè)機(jī)器周期 } /******************ds1302寫操作*****************/ void ds1302_write(uchar add,uchar date) { uchar temp,i; reset_ds1302(); temp=add; //寫地址 for(i=0;i<8;i++) { if((temp&0x01)==1) io=1; else io=0; sclk=1; _nop_(); temp=temp>>1; sclk=0; _nop_(); } temp=date; //寫數(shù)據(jù) for(i=0;i<8;i++) { if((temp&0x01)==1) io=1; else io=0; sclk=1; _nop_(); temp=temp>>1; sclk=0; _nop_(); } ce=0; _nop_(); } /******************ds1302讀操作*****************/ uchar ds1302_read(uchar add) { uchar temp,i; reset_ds1302(); temp=add; //寫地址 for(i=0;i<8;i++) { if((temp&0x01)==1) io=1; else io=0; sclk=1; _nop_(); temp=temp>>1; sclk=0; _nop_(); } temp=0; //讀數(shù)據(jù) for(i=0;i<8;i++) { if(io==1) temp=temp|0x80; else temp=temp&0x7f; sclk=1; _nop_(); temp=temp>>1; sclk=0; _nop_(); } return temp; } /******************設(shè)置初始時(shí)間*****************/ void set_time() { ds1302_write(0x8e,0x00);//關(guān)閉寫保護(hù) ds1302_write(0x80,0x00); ds1302_write(0x82,0x00); ds1302_write(0x84,0x00); ds1302_write(0x8e,0x80); //打開寫保護(hù) } //數(shù)碼管顯示 void display(uchar shi,uchar fen,uchar miao) { P0=0xfe; wela=1; wela=0; P0=table[shi/10]; dula=1; dula=0; delay(1); P0=0xfd; wela=1; wela=0; P0=table[shi%10]; dula=1; dula=0; delay(1); P0=0xfb; wela=1; wela=0; P0=table[fen/10]; dula=1; dula=0; delay(1); P0=0xf7; wela=1; wela=0; P0=table[fen%10]; dula=1; dula=0; delay(1); P0=0xef; wela=1; wela=0; P0=table[miao/10]; dula=1; dula=0; delay(1); P0=0xdf; wela=1; wela=0; P0=table[miao%10]; dula=1; dula=0; delay(1); } void main() { uchar shi,fen,miao; set_time(); while(1) { shi=ds1302_read(0x85); //讀時(shí)的數(shù)據(jù) shi=(shi>>4)*10+(shi&0x0f);//將BCD碼轉(zhuǎn)換成十進(jìn)制數(shù) fen=ds1302_read(0x83);//讀分的數(shù)據(jù) fen=(fen>>4)*10+(fen&0x0f);//將BSD碼轉(zhuǎn)換成十進(jìn)制 miao=ds1302_read(0x81);//讀秒 miao=(miao>>4)*10+(miao&0x0f); display(shi,fen,miao); } }
歡迎光臨 (http://www.zg4o1577.cn/bbs/) | Powered by Discuz! X3.1 |