想加一個整點報時滴滴十秒的一個程序,不知道怎么寫,寫好了加在整個程序的哪個位置,請各位大佬不吝賜教,小弟感激不盡!
X(3~BJW{T3A)}OL$[YXH720.png (5.93 KB, 下載次數: 22)
下載附件
2019-9-20 11:49 上傳
單片機源程序如下:
//1602顯示時鐘,按K3進入時鐘設置,按K1選擇設置的時分秒日月,按K2加1。//
- #include <reg52.h>
- #include <intrins.h>
- #define lcd1602data P0
- typedef unsigned char u8;
- typedef unsigned int u16;
- sbit E=P2^7; //設置液晶端口 使能信號
- sbit RW=P2^5; //讀寫選擇 讀:H/寫:L
- sbit RS=P2^6; //數據命令選擇 數據:H/命令:L
- sbit key1=P3^1;
- sbit key2=P3^0;
- sbit key3=P3^2;
- sbit spk=P1^7;
- sbit SCLK=P3^6; //設置時鐘端口
- sbit IO=P3^4;
- sbit RST=P3^5;
- u16 read[]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d}; //設置秒分時日月周年讀寄存器
- u16 write[]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //設置秒分時日月周年寫寄存器
- u8 time[]={0x30,0x30,0x23,0x13,0x09,0x04,0x19}; //存放初始時間
- u16 setmark,setplace; //setmark檢測key3是否按下,setplace選擇要調整的時間塊。
- void delay_ms(u16 n);
- void initConfiguration();
- void lcdDisplay();
- void lcdwrite_com(u8 datas);
- void lcdwrite_datas(u8 datas);
- void lcdinit();
- void writebyte(u8 address,u8 datas);
- u8 readbyte(u8 address);
- void ds1302init();
- void read_time();
- void DIDI(u16);
- void main()
- {
- u16 i;
- initConfiguration(); //初始化
- lcdinit();
- ds1302init();
- while(1)
- {
- if(setmark==0) //時鐘正常運作
- {
- read_time();
- }
- else //調整時間
- {
- if(key1==0)
- {
- delay_ms(1);
- if(key1==0)
- {
- setplace++; //按下key1要調整的時間塊加一
- setplace%=7;
- }
- while(i<50&&key1==0) //等待key1松開
- {
- i++;
- delay_ms(10);
- }
- i=0;
- }
- if(key2==0)
- {
- delay_ms(1);
- if(key2==0) //按下key2對應的初始時間加一
- {
- time[setplace]++;
- if((time[setplace]&0x0f)>9) //換成BCD碼。
- {
- time[setplace]=time[setplace]+6;
- }
- if((time[setplace]>=0x60)&&(setplace<2)) //分秒只能到59
- {
- time[setplace]=0;
- }
- if((time[setplace]>=0x24)&&(setplace==2)) //小時只能到23
- {
- time[setplace]=0;
- }
- if((time[setplace]>=0x32)&&(setplace==3)) //日只能到31
- {
- time[setplace]=1;
- }
- if((time[setplace]>=0x13)&&(setplace==4)) //月只能到12
- {
- time[setplace]=1;
- }
- if((time[setplace]>=0x8)&&(setplace==5)) //周只能到7
- {
- time[setplace]=1;
- }
-
- }
- while((i<50)&&(0==key2))
- {
- i++;
- delay_ms(10);
- }
- i=0;
- }
- }
- lcdDisplay();
- }
- }
- void delay_ms(u16 n) //準確延時一毫秒,晶振12M
- {
- u16 a,b;
- for(;n>0;n--)
- {
- for(a=199;a>0;a--)
- {
- for(b=1;b>0;b--)
- {
- ;
- }
- }
- }
- }
- void initConfiguration() //初始化外部中斷0
- {
- EA=1;
- IT0=1;
- EX0=1;
- }
- void init0() interrupt 0 //按下key3時間保持不變
- {
- delay_ms(10);
- if(key3==0)
- {
- setmark=~setmark;
- setplace=0;
- ds1302init();
- }
- }
- void lcdDisplay() //顯示函數
- {
-
- lcdwrite_com(0x80+0x00); //確定寫數據的位置
- lcdwrite_datas('2');
- lcdwrite_datas('0');
- lcdwrite_datas('0'+time[6]/16); //寫入年份的高位
- lcdwrite_datas('0'+(time[6]&0x0f)); //低位
- lcdwrite_datas('-');
- lcdwrite_datas('0'+time[4]/16);
- lcdwrite_datas('0'+(time[4]&0x0f)); //里面的小括號很重要
- lcdwrite_datas('-');
- lcdwrite_datas('0'+time[3]/16);
- lcdwrite_datas('0'+(time[3]&0x0f));
-
- lcdwrite_com(0x8c);
- lcdwrite_datas('0'+(time[5]&0x07));
- lcdwrite_com(0xc0); //跳線帽Joe接vcc
- lcdwrite_datas('0'+time[2]/16);
- lcdwrite_datas('0'+(time[2]&0x0f));
- lcdwrite_datas('-');
- lcdwrite_datas('0'+time[1]/16);
- lcdwrite_datas('0'+(time[1]&0x0f));
- lcdwrite_datas('-');
- lcdwrite_datas('0'+time[0]/16);
- lcdwrite_datas('0'+(time[0]&0x0f));
- }
- void lcdwrite_com(u8 datas) //lcd液晶寫入指令操作 模擬時序
- {
- E=0;
- RW=0;
- RS=0;
- lcd1602data=datas;
- delay_ms(1);
- E=1;
- //delay_ms(1);
- E=0;
-
- }
- void lcdwrite_datas(u8 datas) //lcd液晶寫入數據操作
- {
- E=0;
- RW=0;
- RS=1;
- lcd1602data=datas;
- delay_ms(1); //延時不能太長否則調整不靈敏
- E=1;
- //delay_ms(1);
- E=0;
-
- }
- void lcdinit() //LCD液晶初始化
- {
- lcdwrite_com(0x38); //8位總線,顯示兩行,5x7點陣
- lcdwrite_com(0x0c); //開顯示功能,無關標
- lcdwrite_com(0x06); //光標右移,即數據依次右移
- lcdwrite_com(0x01); //清屏
- lcdwrite_com(0x80); //第一個數據位置
- }
- void writebyte(u8 address,u8 datas) //在寄存器里寫時間數據,模擬時序
- {
- u8 i;
- RST=0;
- _nop_();
- SCLK=0;
- _nop_();
- RST=1;
- _nop_();
- for(i=0;i<8;i++)
- {
- IO=address&0x01;
- address>>=1;
- SCLK=1;
- _nop_();
- SCLK=0;
- _nop_();
- }
- for(i=0;i<8;i++)
- {
- IO=datas&0x01;
- datas>>=1;
- SCLK=1;
- _nop_();
- SCLK=0;
- _nop_();
- }
- RST=0;
- }
- u8 readbyte(u8 address) //讀寄存器里的時間數據
- {
- u8 i,datas,dat;
- RST=0;
- //_nop_();
- SCLK=0;
- //_nop_();
- RST=1;
- //_nop_();
- for(i=0;i<8;i++)
- {
- IO=address&0x01;
- address>>=1;
- SCLK=1;
- _nop_();
- SCLK=0;
- _nop_();
- }
- //_nop_();
- for(i=0;i<8;i++)
- {
- dat=IO;
- datas=(datas>>1)|(dat<<7);
- SCLK=1;
- _nop_();
- SCLK=0;
- _nop_();
- }
- RST=0;
- //_nop_();
- SCLK=1;
- //_nop_();
- IO=0;
- //_nop_();
- IO=1;
- //_nop_();
- return datas;
- }
- void ds1302init() //在寄存器里寫入初始時間
- {
- u8 i;
- writebyte(0x8e,0x00); //關閉寫保護寄存器
- for(i=0;i<7;i++)
- {
- writebyte(write[i],time[i]);
- }
- writebyte(0x8e,0x80); //打開寫保護寄存器
- }
- void read_time() //讀取時鐘時間 time數組存儲
- {
- u8 i;
- for(i=0;i<7;i++)
- {
- time[i]=readbyte(read[i]);
- }
- }
復制代碼
|