|
靜態(tài)顯示萬年歷,需要的就贊一贊吧!
圖片1.png (12.74 KB, 下載次數(shù): 74)
下載附件
仿真效果圖
2016-11-25 17:20 上傳
0.png (82.17 KB, 下載次數(shù): 58)
下載附件
2016-12-5 01:14 上傳
所有資料下載:
LED萬年歷的proteus仿真電路及C語言程序設(shè)計(jì).zip
(141.79 KB, 下載次數(shù): 49)
2016-11-25 17:23 上傳
點(diǎn)擊文件名下載附件
程序加仿真圖 下載積分: 黑幣 -5
單片機(jī)源程序:
- //Title:calendar
- #include <REGX51.H>
- #define uchar unsigned char
- #define uint unsigned int
- sbit DATA=P1^2; //數(shù)據(jù)(PA0)
- sbit SCK=P1^0; //移位時(shí)鐘(PA1)
- sbit RCK=P1^1; //鎖存時(shí)鐘(PA2)
- sbit P20=P2^0;
- sbit P21=P2^1;
- sbit P33=P3^3;
- sbit DS1302_CLK = P1^7; //實(shí)時(shí)時(shí)鐘時(shí)鐘線引腳
- sbit DS1302_IO = P1^6; //實(shí)時(shí)時(shí)鐘數(shù)據(jù)線引腳
- sbit DS1302_RST = P1^5;//實(shí)時(shí)時(shí)鐘復(fù)位線引腳
- sbit ACC0 = ACC^0;
- sbit ACC7 = ACC^7;
- char hide_sec,hide_min,hide_hour,hide_day,hide_week,hide_month,hide_year; //秒,分,時(shí)到日,月,年位閃的計(jì)數(shù)
- sbit Set = P3^0; //模式切換鍵
- sbit Up = P3^1; //加法按鈕
- sbit Down = P3^2; //減法按鈕
- sbit out = P3^4; //立刻跳出調(diào)整模式按鈕
-
- char done,count,temp,flag,up_flag,down_flag;
- uchar DS1302[8]={4,5,6,7,8,6,9}; //秒,分,時(shí),日,月,星期,年
- uint f;
-
- uchar t[14];
- uchar k;
- uchar code Seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
- void display(uchar L1,uchar L2,uchar L3,uchar L4,uchar L5,uchar L6,uchar L7,uchar L8,
- uchar L9,uchar L10,uchar L11,uchar L12,uchar L13,uchar L14);
- /***********DS1302時(shí)鐘部分子程序******************/
- typedef struct __SYSTEMTIME__
- {
- unsigned char Second;
- unsigned char Minute;
- unsigned char Hour;
- unsigned char Week;
- unsigned char Day;
- unsigned char Month;
- unsigned char Year;
- unsigned char DateString[14];
- }SYSTEMTIME; //定義的時(shí)間類型
- SYSTEMTIME CurrentTime;
-
- #define DS1302_SECOND 0x81 //時(shí)鐘芯片的寄存器位置,存放時(shí)間
- #define DS1302_MINUTE 0x83
- #define DS1302_HOUR 0x85
- #define DS1302_WEEK 0x8A
- #define DS1302_DAY 0x87
- #define DS1302_MONTH 0x89
- #define DS1302_YEAR 0x8D
- void DS1302InputByte(unsigned char d) //實(shí)時(shí)時(shí)鐘寫入一字節(jié)(內(nèi)部函數(shù))
- {
- unsigned char i;
- ACC = d;
- for(i=8; i>0; i--)
- {
- DS1302_IO = ACC0; //相當(dāng)于匯編中的 RRC
- DS1302_CLK = 1;
- DS1302_CLK = 0;
- ACC = ACC >> 1;
- }
- }
- unsigned char DS1302OutputByte(void) //實(shí)時(shí)時(shí)鐘讀取一字節(jié)(內(nèi)部函數(shù))
- {
- unsigned char i;
- for(i=8; i>0; i--)
- {
- ACC = ACC >>1; //相當(dāng)于匯編中的 RRC
- ACC7 = DS1302_IO;
- DS1302_CLK = 1;
- DS1302_CLK = 0;//在下降沿?cái)?shù)據(jù)從移位寄存器輸出
- }
- return(ACC);
- }
- void Write1302(unsigned char ucAddr, unsigned char ucDa) //ucAddr: DS1302地址, ucData: 要寫的數(shù)據(jù)
- {
- DS1302_RST = 0;
- DS1302_CLK = 0;
- DS1302_RST = 1;//只有在clk為低時(shí),才能將rst置高
- DS1302InputByte(ucAddr); // 地址,命令
- DS1302InputByte(ucDa); // 寫1Byte數(shù)據(jù)
- DS1302_CLK = 1;//上升沿將數(shù)據(jù)寫入DS1302內(nèi)部移位寄存器
- DS1302_RST = 0;
- }
- unsigned char Read1302(unsigned char ucAddr) //讀取DS1302某地址的數(shù)據(jù)
- {
- unsigned char ucData;
- DS1302_RST = 0;
- DS1302_CLK = 0;
- DS1302_RST = 1;//啟動(dòng)數(shù)據(jù)傳送
- DS1302InputByte(ucAddr|0x01); // 地址,命令 ,讀操作
- ucData = DS1302OutputByte(); // 讀1Byte數(shù)據(jù),應(yīng)在上升沿前讀取
- DS1302_CLK = 1;
- DS1302_RST = 0;
- return(ucData);
- }
- void DS1302_GetTime(SYSTEMTIME *Time) //獲取時(shí)鐘芯片的時(shí)鐘數(shù)據(jù)到自定義的結(jié)構(gòu)型數(shù)組
- {
- unsigned char ReadValue;
- ReadValue = Read1302(DS1302_SECOND);
- Time->Second = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);//16進(jìn)制轉(zhuǎn)為10進(jìn)制
- ReadValue = Read1302(DS1302_MINUTE);
- Time->Minute = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
- ReadValue = Read1302(DS1302_HOUR);
- Time->Hour = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
- ReadValue = Read1302(DS1302_DAY);
- Time->Day = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
- ReadValue = Read1302(DS1302_WEEK);
- Time->Week = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
- ReadValue = Read1302(DS1302_MONTH);
- Time->Month = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
- ReadValue = Read1302(DS1302_YEAR);
- Time->Year = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
- }
- void DateToStr(SYSTEMTIME *Time) //將時(shí)間年,月,日,星期數(shù)據(jù)轉(zhuǎn)換成液晶顯示字符串,放到數(shù)組里DateString[]
- { if(hide_year<5) //這里的if,else語句都是判斷位閃爍,<2顯示數(shù)據(jù),>2就不顯示,輸出字符串為 2007/07/22
- {
- Time->DateString[0] = 2;
- Time->DateString[1] = 0;
- Time->DateString[2] = Time->Year/10;
- Time->DateString[3] = Time->Year%10;
- }
- else
- {
-
- Time->DateString[2] =10 ;
- Time->DateString[3] =10 ;
- }
-
- if(hide_month<5)
- {
- Time->DateString[4] = Time->Month/10;
- Time->DateString[5] = Time->Month%10;
- }
- else
- {
- Time->DateString[4] =10;
- Time->DateString[5] =10;
- }
-
- if(hide_day<5)
- {
- Time->DateString[6] = Time->Day/10;
- Time->DateString[7] = Time->Day%10;
- }
- else
- {
- Time->DateString[6] =10;
- Time->DateString[7] =10;
- }
-
- if(hide_hour<5)
- {
- Time->DateString[8] = Time->Hour/10;
- Time->DateString[9] = Time->Hour%10;
- }
- else
- {
- Time->DateString[8] =10;
- Time->DateString[9] =10;
- }
-
- if(hide_min<5)
- {
- Time->DateString[10] = Time->Minute/10;
- Time->DateString[11] = Time->Minute%10;
- }
- else
- {
- Time->DateString[10] =10;
- Time->DateString[11] =10;
- }
-
- if(hide_sec<5)
- {
- Time->DateString[12] = Time->Second/10 ;
- Time->DateString[13] = Time->Second%10 ;
- }
- else
- {
- Time->DateString[12] =10;
- Time->DateString[13] =10;
- }
-
- }
- void show_time() //顯示程序
- { uchar i;
- uchar j;
-
- DS1302_GetTime(&CurrentTime); //獲取時(shí)鐘芯片的時(shí)間數(shù)據(jù)
- DateToStr(&CurrentTime);
- for(j=0;j<14;j++)
- {t[j]=Seg[CurrentTime.DateString[13-j]];}
-
- RCK=0;//鎖存置低
-
-
- for(j=0;j<14;j++)
- {
- for(i=0;i<8;i++)
- {
- SCK=0; //移位脈沖置低
- DATA=t[j]&0x80;
- t[j]=t[j]<<1;
- SCK=1;//上升沿時(shí)數(shù)據(jù)寄存器的數(shù)據(jù)移位
- }
- }
-
-
- RCK=1; //上升沿時(shí)移位寄存器的數(shù)據(jù)進(jìn)入數(shù)據(jù)存儲(chǔ)寄存器
-
- }
- /*延時(shí)子程序*/
- void mdelay(uint delay)
- { uint i;
- for(;delay>0;delay--)
- {for(i=0;i<62;i++) //1ms延時(shí).
- {;}
- }
- }
- void outkey() //跳出調(diào)整模式,返回默認(rèn)顯示
- { uchar Second;
- if(out==0)
- { mdelay(8);
- count=0;
- hide_sec=0,hide_min=0,hide_hour=0,hide_day=0,hide_week=0,hide_month=0,hide_year=0;
- Second=Read1302(DS1302_SECOND);
-
- Write1302(0x80,Second&0x7f);
-
- done=0;
- while(out==0);
-
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- void Upkey()//升序按鍵
- {
- Up=1;
- if(Up==0)
- {
- mdelay(8);
-
- switch(count)
- {/*case 1:
- temp=Read1302(DS1302_SECOND); //讀取秒數(shù)
- temp=temp+1; //秒數(shù)加1
- up_flag=1; //數(shù)據(jù)調(diào)整后更新標(biāo)志
- if((temp&0x7f)>0x59) //超過59秒,清零
- temp=0;
- break;*/
- case 2:
- temp=Read1302(DS1302_MINUTE); //讀取分?jǐn)?shù)
- temp=temp+1; //分?jǐn)?shù)加1
- up_flag=1;
- if((temp&0x0F)>9)temp=temp+6;
- if(temp>=0x60) temp=0;
- break;
- case 3:
- temp=Read1302(DS1302_HOUR); //讀取小時(shí)數(shù)
- temp=temp+1; //小時(shí)數(shù)加1
- if((temp&0x0F)>9)temp=temp+6;
- if(temp>=0x24) temp=0;
- up_flag=1;
-
- break;
-
- case 4:
- temp=Read1302(DS1302_DAY); //讀取日數(shù)
- temp=temp+1; //日數(shù)加1
- up_flag=1;
- if((temp&0x0F)>9)temp=temp+6;
- if(temp>=0x32) temp=1;
- break;
- case 5:
- temp=Read1302(DS1302_MONTH);// 讀取月數(shù)
- temp=temp+1; //月數(shù)加1
- up_flag=1;
- if((temp&0x0F)>9)temp=temp+6;
- if(temp>=0x13) temp=1;
- break;
- case 6:
- temp=Read1302(DS1302_YEAR); //讀取年數(shù)
- temp=temp+1; //年數(shù)加1
- up_flag=1;
- if((temp&0x0F)>9)temp=temp+6;
- if(temp>=0xA0) temp=0;
- break;
- default:break;
- }
-
- while(Up==0);
-
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- void Downkey()//降序按鍵
- {
- Down=1;
- if(Down==0)
- {
- mdelay(8);
- switch(count)
- {/*case 1:
- temp=Read1302(DS1302_SECOND); //讀取秒數(shù)
- temp=temp-1; //秒數(shù)減1
- down_flag=1; //數(shù)據(jù)調(diào)整后更新標(biāo)志
- if(temp==0x7f) //小于0秒,返回59秒
- temp=0x59;
- break;*/
- case 2:
- temp=Read1302(DS1302_MINUTE); //讀取分?jǐn)?shù)
- temp=temp-1; //分?jǐn)?shù)減1
- down_flag=1;
- if((temp&0x0F)>9)temp=temp-6;
- if(temp==0) temp=0x59;
- break;
- case 3:
- temp=Read1302(DS1302_HOUR); //讀取小時(shí)數(shù)
- temp=temp-1; //小時(shí)數(shù)減1
- down_flag=1;
- if((temp&0x0F)>9)temp=temp-6;
- if(temp==0) temp=0x59;
- break;
-
- case 4:
- temp=Read1302(DS1302_DAY); //讀取日數(shù)
- temp=temp-1; //日數(shù)減1
- down_flag=1;
- if((temp&0x0F)>9)temp=temp-6;
- if(temp==0) temp=0x31;
- break;
- case 5:
- temp=Read1302(DS1302_MONTH); //讀取月數(shù)
- temp=temp-1; //月數(shù)減1
- down_flag=1;
- if((temp&0x0F)>9)temp=temp-6;
- if(temp==0) temp=0x12;
- break;
- case 6:
- temp=Read1302(DS1302_YEAR); //讀取年數(shù)
- temp=temp-1; //年數(shù)減1
- down_flag=1;
- if((temp&0x0F)>9)temp=temp-6;
- if(temp==0) temp=0x50;
- break;
- default:break;
- }
-
- while(Down==0);
-
- }
- }
- void Setkey()//模式選擇按鍵
- {
- Set=1;
- if(Set==0)//有按鍵按下
- {
- mdelay(8);
- count=count+1; //Setkey按一次,count就加1
- done=1; //進(jìn)入調(diào)整模式
- while(Set==0);
-
- }
- }
- void keydone()//按鍵功能執(zhí)行
- { uchar Second;
- if(flag==0) //關(guān)閉時(shí)鐘,停止計(jì)時(shí)
- {
- temp=Read1302(0x81);
- Write1302(0x80,temp|0x80);//ch=1,時(shí)鐘振蕩停止
-
- flag=1;
- }
- Setkey(); //掃描模式切換按鍵
- switch(count)
- {case 1:do //count=1,調(diào)整秒
- {
- outkey(); //掃描跳出按鈕
- Upkey(); //掃描加按鈕
- Downkey(); //掃描減按鈕
- if(up_flag==1||down_flag==1) //數(shù)據(jù)更新,重新寫入新的數(shù)據(jù)
- {
-
- Write1302(0x80,temp|0x80); //寫入新的秒數(shù)
-
- up_flag=0;
- down_flag=0;
- }
- hide_sec++; //位閃計(jì)數(shù)
- if(hide_sec>9)
- hide_sec=0;
- show_time(); //液晶顯示數(shù)據(jù)
- }while(count==2);break;
- case 2:do //count=2,調(diào)整分
- {
- hide_sec=0;
- outkey();
- Upkey();
- Downkey();
- if(temp>0x59)
- temp=0;
- if(up_flag==1||down_flag==1)
- {
-
- Write1302(0x82,temp); //寫入新的分?jǐn)?shù)
-
- up_flag=0;
- down_flag=0;
- }
- hide_min++;
- if(hide_min>9)
- hide_min=0;
- show_time();
- }while(count==3);break;
- case 3:do //count=3,調(diào)整小時(shí)
- {
- hide_min=0;
- outkey();
- Upkey();
- Downkey();
- if(up_flag==1||down_flag==1)
- {
-
- Write1302(0x84,temp); //寫入新的小時(shí)數(shù)
-
- up_flag=0;
- down_flag=0;
- }
- hide_hour++;
- if(hide_hour>9)
- hide_hour=0;
- show_time();
- }while(count==4);break;
-
- case 4:do //count=5,調(diào)整日
- {
- hide_hour=0;
- outkey();
- Upkey();
- Downkey();
- if(up_flag==1||down_flag==1)
- {
-
- Write1302(0x86,temp); //寫入新的日數(shù)
-
- up_flag=0;
- down_flag=0;
- }
- hide_day++;
- if(hide_day>9)
- hide_day=0;
- show_time();
- }while(count==5);break;
- case 5:do //count=6,調(diào)整月
- {
- hide_day=0;
- outkey();
- Upkey();
- Downkey();
- if(up_flag==1||down_flag==1)
- {
-
- Write1302(0x88,temp); //寫入新的月數(shù)
-
- up_flag=0;
- down_flag=0;
- }
- hide_month++;
- if(hide_month>9)
- hide_month=0;
- show_time();
- }while(count==6);break;
- case 6:do //count=7,調(diào)整年
- {
- hide_month=0;
- outkey();
- Upkey();
- Downkey();
- if(up_flag==1||down_flag==1)
- {
- Write1302(0x8c,temp); //寫入新的年數(shù)
-
- up_flag=0;
- down_flag=0;
- }
- hide_year++;
- if(hide_year>9)
- hide_year=0;
- show_time();
- }while(count==7);break;
- case 7: count=0;hide_year=0; //count7, 跳出調(diào)整模式,返回默認(rèn)顯示狀態(tài)
- Second=Read1302(DS1302_SECOND);
-
- Write1302(0x80,Second&0x7f);//ch=0時(shí),時(shí)鐘啟動(dòng)
-
- done=0;
- break; //count=7,開啟中斷,標(biāo)志位置0并退出
- default:break;
- }
- }
-
-
- main()
- {
- uchar i;
- flag=1; //時(shí)鐘停止標(biāo)志
- up_flag=0;
- down_flag=0;
- done=0; //進(jìn)入默認(rèn)顯示
- for(i=0;i<=6;i++)
- Write1302(0x80+2*i,DS1302[i]);
-
-
- while(1)
- {
- P33=0;
- while(done==1)
- keydone(); //進(jìn)入調(diào)整模式
- while(done==0)
- {
- show_time();
- flag=0;
- Setkey(); //掃描各功能鍵
- }
- }
- }
-
復(fù)制代碼
|
評(píng)分
-
查看全部評(píng)分
|