提供一個自動窗簾的51單片機程序(藍橋杯的題)
單片機源程序如下:
- #include<reg51.h>
- #include<time1302.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit key1=P2^3; //獨立鍵盤的各個位置
- sbit key2=P2^4;
- sbit key3=P2^5;
- sbit key4=P2^6;
- sbit clock=P3^6; //是單片機上IO的位置
- sbit dout=P3^7; //是out對應的位置
- uchar wei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //位選
- uchar duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00}; //段選 0到9
- uchar cun[8]; //每位數據存放的數組
- uchar a;
- void mode_change();
- void chushi();
- void dis_clock_time();
- uchar read();
- void delay(uint x)
- {
- uint i,j;
- for(i=x;i>0;i--)
- for(j=110;j>0;j--);
- }
- void display() //顯示函數的固定格式 其中i和wei的取值根據自己的需要增加和減少
- {
- uint i;
- for(i=0;i<8;i++) //兩位數的循環 兩取決于i
- {
- P1=0xff; //消影
- switch(i)
- {
- case 0:P0=wei[0];break; //選擇第幾位
- case 1:P0=wei[1];break;
- case 2:P0=wei[2];break;
- case 3:P0=wei[3];break;
- case 4:P0=wei[4];break;
- case 5:P0=wei[5];break;
- case 6:P0=wei[6];break;
- case 7:P0=wei[7];break;
- }
- P1=cun[i]; //存放第幾位
- delay(1);
- }
- }
- void main()
- {
- Ds1302Init();
- chushi();
- while(1)
- {
- a=read();
- Ds1302ReadTime();
- mode_change();
- display();
- }
- }
- void mode_change()
- {
- if(key1 == 0) //獨立鍵盤的固定格式
- {
- while(!key1)
- {
- cun[0]=~duan[TIME[5]%16]; //年
- cun[1]=~duan[TIME[5]/16];
- cun[2]=~duan[TIME[6]/16];
- cun[3]=~duan[TIME[6]%16]&0x7f;
- cun[4]=~duan[TIME[4]/16]; //月
- cun[5]=~duan[TIME[4]%16]&0x7f;
- cun[6]=~duan[TIME[3]/16];
- cun[7]=~duan[TIME[3]%16]; //日
- display(); //這個可以根據自己的需要而定,即鍵盤的作用
- }
- }
-
- }
- void chushi()
- {
- cun[0]=~duan[10];
- cun[1]=~duan[10];
- cun[2]=~duan[10];
- cun[3]=~duan[10];
- cun[4]=~duan[10];
- cun[5]=~duan[10];
- cun[6]=~duan[10];
- cun[7]=~duan[10];
- }
- void dis_clock_time() //時鐘顯示
- {
-
- Ds1302ReadTime();
- cun[0]=~duan[TIME[2]/16]; //小時
- cun[1]=~duan[TIME[2]%16];
- //閃爍 在中斷里面
- cun[2]=~duan[TIME[1]/16]; //分鐘
- cun[3]=~duan[TIME[1]%16];
- cun[4]=~duan[10]; //“-”
- cun[5]=~duan[a/100]; //ad顯示
- cun[6]=~duan[a/10%10];
- cun[7]=~duan[a%10];
- display();
- }
- uchar read() //AD固定格式
- {
- uint i;
- uchar ad=0x00;
- dout=1;
- for(i=0;i<8;i++)
- {
- ad=ad<<1; //左移的數碼管的 方便顯示各個位置的
- if(dout)
- {
- ad++;
- }
- clock=1;
- clock=0;
- _nop_(); //相當于延遲函數delay
- _nop_();
- }
- return ad;
- }
復制代碼
所有資料51hei提供下載:
自動窗簾.rar
(33.99 KB, 下載次數: 13)
2019-3-29 16:02 上傳
點擊文件名下載附件
自動窗簾 下載積分: 黑幣 -5
|