|
這段時間在想著數(shù)碼管的拉幕,很久沒有弄單片機(jī)了,想著不如讓單片機(jī)實現(xiàn)一些小的程序玩玩,也算是鍛煉鍛煉;
拉幕原理是一組數(shù)據(jù)是由很多幀組成的,完成一幀后,延時一段時間,再取下一幀數(shù)據(jù),如此循環(huán);
原理明白了,前天晚上通過編寫調(diào)試就成功實了數(shù)碼管的拉幕;
- //#pragma src(signle.a51)
-
- #include <reg52.h>
- #include <display.c>
- #define uchar unsigned char
- #define uint unsigned int
-
- //sfr PCON = 0x87h;
- sbit Key1 = P3^2;
- sbit BUZ = P3^1;
-
- sbit Signle = P3^0;
- sbit Led0 = P1^0;
- sbit JDQ = P1^2;
- sbit Dp = P1^5;
- sbit SMG1 = P3^7;
- sbit SMG2 = P3^5;
- sbit SMG3 = P3^4;
- sbit SMG4 = P3^3;
- uint Key_count;
- uchar Beep_time,Key_flag,BTime_Flag,JDQ_flag,BP_Count,LM_Count,LM_GroupCNT;
- uchar BT_Sec,Sec,Min,Hour,Key_Select,Key_long_flag,Key_long_count;
- uchar Led_Scan,Led_Loop,Sec_1,Min_1,BT_Sec_1;
- uint Time_value;
- //uchar Display_number[]={0xd7,0x14,0xcd,0x5d,0x1e,0x5b,0xdb,0x15,0xdf,0x5f,0x00};
- uchar Display_number[]={0x28,0xeb,0x32,0xa2,0xe1,0xa4,0x24,0xea,0x20,0xa0,0xff}; //0123456789
- uchar Display_string[]={0xff,0xff,0xff,0xeb,0x61,0x32,0xff,0xff,0xff,0xff,0xff,0xff,0xff};//0001HZ00000
- //-----------------------------
- //4COM 共陽數(shù)碼管 1234
- //P1.0 A; |---|
- //P1.2 B; | |
- //P1.4 C; |---|
- //P1.6 D; | |
- //P1.7 E; |---|
- //P1.1 F;
- //P1.3 G;
- //P1.5 Dp;
- //-----------------------------
- void display_Scan(void);
- void Delay_ms();
- void Delay_ms(uint Tms)
- {
- uint i,j;
- for(i=Tms;i>0;i--)
- for(j=1220;j>0;j--)
- {;}
- }
- void Delay_us(uint Tus)
- {
- while(--Tus);
- }
- void Buz(void)
- {
- uint i;
- for(i=0;i<200;i++)
- {
- BUZ=~BUZ;
- Delay_us(150);
- }
- BUZ=0;
- }
- void Beep_Bi(void)
- {
- if(Beep_time!=0)
- {
- Beep_time--;
- BUZ=~BUZ;
- }
- else
- {
- BUZ=0;
- }
- }
- void T0_ini()
- {
- TMOD=0x12;
- TH1=(65536-1000)/256;
- TL1=(65536-1000)%256;
- TR1=1;
- ET1=1;
- TH0=6;
- TL0=6;
- TR0=0;
- ET0=1;
- EA=1;
- }
- void TM0(void)interrupt 1
- {
- uint Time_Count;
- TF0=0;
- Time_Count++;
- BP_Count++;
- if(BP_Count==2)
- {
- BP_Count=0;
- Beep_Bi();
- }
- //if(Time_Count==2000) // ext atxl 12MHz;500ms
- if(Time_Count==1333) // ext atxl 8MHz 500ms;
- //if(Time_Count==998) // built in atxl; 500ms
- {
- Time_Count=0;
- BTime_Flag=~BTime_Flag;
- BT_Sec++;Dp=~Dp;BT_Sec_1++;
- Signle=~Signle;
- if(Key_flag==1)
- {
- BT_Sec=0;
- if(BT_Sec_1==2)
- {
-
- BT_Sec_1=0;
- Sec_1++;
- if(Sec_1==60)
- {
- Sec_1=0;Min_1++;
- }
- if(Min_1==60)
- {
- Min_1=0;
- }
- }
- }
- }
- if(BT_Sec==2)
- {
- BT_Sec=0;
- Sec++;
- if(Sec==60)
- {
- Sec=0;Min++;
- }
- if(Min==60)
- {
- Min=0;Hour++;
- }
- if(Hour==24)
- {
- Hour=0;
- }
- }
- }
- void TM1(void)interrupt 3
- {
- TH1=(65536-1000)/256;
- TL1=(65536-1000)%256;
- Led_Scan=1;Led_Loop++;Led_Loop=Led_Loop&0x03;
- Key1=1;
- if(Key1==0)
- {
- Key_count++;
- if(Key_count==20)
- {
- Beep_time=50;
- Key_flag=1;
- TR0=1;
- Key_Select++;
- if(Key_Select==3)
- {
- Key_Select=0;Key_flag=0;
- }
- }
- if(Key_count>=20*50)
- {
- Key_count=500; // 設(shè)置長按快跳進(jìn)度
- Key_long_flag=1;
- {
- if(Key_Select==1)
- {
- Min++;
- if(Min==60)
- Min=0;
- }
- if(Key_Select==2)
- {
- Hour++;
- if(Hour==24)
- Hour=0;
- }
- }
- }
- }
- else
- {Key_count=0;Key_long_flag=0;}
- }
- void SmgDisp(uchar ch,uchar num)
- {
- uchar number;
- number=num;
- //P3=0x03;
- P1=0xff;
- SMG1=0;SMG2=0;SMG3=0;SMG4=0;
- switch(ch)
- {
- case 1:SMG4=1;LM_Count=0;break; //SMG排列 1 2 3 4 第四個數(shù)碼管
- case 2:SMG3=1;LM_Count++;break; //SMG排列 1 2 3 4 第三個數(shù)碼管
- case 3:SMG2=1;LM_Count++;break; //SMG排列 1 2 3 4 第二個數(shù)碼管
- case 4:SMG1=1;LM_Count++;break; //SMG排列 1 2 3 4 第一個數(shù)碼管
- default:break;
- }
- //P1=Display_number[number];
- P1=Display_string[number];
- }
- void CheckTime()
- {
- if(Led_Scan)
- {
- switch(Led_Loop)
- {
- case 0:SmgDisp(4,LM_Count+LM_GroupCNT);break; //第一個數(shù)碼管
- case 1:SmgDisp(3,LM_Count+LM_GroupCNT);break; //第二個數(shù)碼管
- case 2:SmgDisp(2,LM_Count+LM_GroupCNT);break; //第三個數(shù)碼管
- case 3:SmgDisp(1,LM_Count+LM_GroupCNT);break; //第四個數(shù)碼管
- }
- Led_Scan=0;
- }
- }
- void Display(void)
- {
- if(BTime_Flag&(Key_Select==2)&(Key_long_flag==0))
- {
- P1=0xff;SMG1=0;SMG2=0;SMG3=0;SMG4=0;
- //P3=0x00;
- }
- else
- {
- if(BTime_Flag)
- {
- Dp=0;SMG1=1;//P3=0x80;
- Delay_ms(1);SMG1=0;//P3=0x00;
- Dp=0;SMG2=1;//P3=0x20;
- Delay_ms(1);SMG2=0;//P3=0x00;
- }
- P1=Display_number[Hour/10];
- SMG1=1;//P3=0x80;
- Delay_ms(1);
- SMG1=0;//P3=0x00;
- P1=0xff;
- P1=Display_number[Hour%10];
- SMG2=1;//P3=0x20;
- Delay_ms(1);
- SMG2=0;//P3=0x00;
- P1=0xff;
- }
- if(BTime_Flag&(Key_Select==1)&(Key_long_flag==0))
- {
- P1=0xff;SMG1=0;SMG2=0;SMG3=0;SMG4=0;//P3=0x00;
- }
- else
- {
- if(BTime_Flag)
- {
- Dp=0;SMG3=1;//P3=0x10;
- Delay_ms(1);SMG3=0;//P3=0x00;
- Dp=0;SMG4=1;//P3=0x08;
- Delay_ms(1);SMG4=0;//P3=0x00;
- }
-
- P1=Display_number[Min/10];
- SMG3=1;//P3=0x10;
- Delay_ms(1);
- SMG3=0;//P3=0x00;
- P1=0xff;
- P1=Display_number[Min%10];
- SMG4=1;//P3=0x08;
- Delay_ms(1);
- SMG4=0;//P3=0x00;
- P1=0xff;
- }
- }
- void exint0(void)interrupt 0
- {
- Buz();T0_ini();EX0=0;EA=1;
- }
-
- void Display_Scan(void)
- {
- P1=0x00;
- P3=0x00;
- //---------"A"--------------
- //P1=0x01;
- P1=0xfe;
- P3=0x80; //smg1
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- P1=0xfe;
- P3=0x20; //smg2
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- P1=0xfe;
- P3=0x10; //smg3
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- P1=0xfe;
- P3=0x08; //smg4
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- P1=0xfe;
- Delay_ms(50);
- //---------"B"--------------
- P1=0xfb;
- P3=0x80;
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- P1=0xfb;
- P3=0x20;
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- P1=0xfb;
- P3=0x10;
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- P1=0xfb;
- P3=0x08;
- Delay_ms(1);
- P1=0xfb;
- P1=0xff;
- P3=0x00;
- Delay_ms(50);
- //---------"C"--------------
- //P1=0x10;
- P1=0xef;
- P3=0x80;
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- P1=0xef;
- P3=0x20;
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- P1=0xef;
- P3=0x10;
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- P1=0xef;
- P3=0x08;
- Delay_ms(1);
- P1=0xef;
- P1=0xff;
- P3=0x00;
- Delay_ms(50);
- //---------"D"--------------
- P1=0xcf;
- P3=0x80;
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- P1=0xcf;
- P3=0x20;
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- P1=0xcf;
- P3=0x10;
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- P1=0xcf;
- P3=0x08;
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- Delay_ms(50);
- //---------"E"--------------
- P1=0x7f;
- P3=0x80;
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- P1=0x7f;
- P3=0x20;
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- P1=0x7f;
- P3=0x10;
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- P1=0x7f;
- P3=0x08;
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- Delay_ms(50);
- //---------"F"--------------
- P1=0xfd;
- P3=0x80;
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- P1=0xfd;
- P3=0x20;
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- P1=0xfd;
- P3=0x10;
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- P1=0xfd;
- P3=0x08;
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- Delay_ms(50);
- //---------"G"--------------
- P1=0xf7;
- P3=0x80;
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- P1=0xf7;
- P3=0x20;
- Delay_ms(1);
- P1=0xff;
- P3=0x00;
- P1=0xf7;
- P3=0x10;
- Delay_ms(1);
- P1=0xf7;
- P3=0x08;
- P1=0xff;
- P3=0x00;
- Delay_ms(20);
- P1=0x00;
- P3=0xfc;
- Delay_ms(100);
- P1=0xff;
- P3=0x00;
- Delay_ms(100);
- P1=0x00;
- P3=0xfc;
- Delay_ms(100);
- //------------------------------
- P1=0x00;
- P3=0x80;
- Delay_ms(30); //smg1
- P1=0x00;
- P3=0x20;
- Delay_ms(30); //smg2
- P1=0x00;
- P3=0x10;
- Delay_ms(30); //smg3
- P1=0x00;
- P3=0x08; //smg4
- Delay_ms(30);
- P1=0x00;
- P3=0xfc;
- Delay_ms(100);
- P1=0xff;
- P3=0x00;
- Delay_ms(100);
- P1=0x00;
- P3=0xfc;
- Delay_ms(100);
- }
- void main(void)
- {
- uint k;
- Buz();Display_Scan();
- T0_ini();
- while(1)
- {
- //-------------按鍵小于2次有分鐘睡眠模式-----------------
- /*if(Min_1==4)
- {
- EX0=1;
- EA=1;
- Key_flag=0;
- Min_1=0;
- Signle=1;
- TR0=1;
- PCON=0x02;
- } */
- //---------------------------------------------------------
- if(Sec%10==0)
- {
- for(LM_GroupCNT=0;LM_GroupCNT<8;LM_GroupCNT++)
- {
- for(k=55555;k>0;k--)
- {
- CheckTime();
- }
- }
- }
- else
- Display();
- //Delay_ms(370);
- //Dp=~Dp;
- //Signle=~Signle;
- //Delay_ms(1);
- }
- }
復(fù)制代碼
|
|