單片機源程序如下:
- #include <reg52.h>
- #define DataPort P0 //定義數(shù)據(jù)端口 程序中遇到DataPort 則用P0 替換
- sbit LATCH1=P2^6;//定義鎖存使能端口 段鎖存
- sbit LATCH2=P2^7;// 位鎖存
- unsigned char code dofly_DuanMa[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};// 顯示段碼值0~9
- unsigned char code dofly_WeiMa[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//分別對應(yīng)相應(yīng)的數(shù)碼管點亮,即位碼
- unsigned char TempData[8],start=0; //存儲顯示值的全局變量
- sbit D1=P1^3;
- sbit K1 = P3^4;
- sbit K2 = P3^5;
- unsigned char Speed=1;
- bit StopFlag;
- void Display(unsigned char FirstBit,unsigned char Num);
- void Init_Timer0(void);
- unsigned char KeyScan(void);
- /*------------------------------------------------
- uS延時函數(shù),含有輸入?yún)?shù) unsigned char t,無返回值
- unsigned char 是定義無符號字符變量,其值的范圍是
- 0~255 這里使用晶振12M,精確延時請使用匯編,大致延時
- 長度如下 T=tx2+5 uS
- ------------------------------------------------*/
- void DelayUs2x(unsigned char t)
- {
- while(--t);
- }
- /*------------------------------------------------
- mS延時函數(shù),含有輸入?yún)?shù) unsigned char t,無返回值
- unsigned char 是定義無符號字符變量,其值的范圍是
- 0~255 這里使用晶振12M,精確延時請使用匯編
- ------------------------------------------------*/
- void DelayMs(unsigned char t)
- {
-
- while(t--)
- {
- //大致延時1mS
- DelayUs2x(245);
- DelayUs2x(245);
- }
- }
- void WaitKeyFree(void){
- while(1){
- while(K1==0);
- while(K2==0);
- DelayMs(10);
- while(K1==0);
- while(K2==0);
- break;
- }
-
- }
- /*------------------------------------------------
- 主函數(shù)
- ------------------------------------------------*/
- main()
- {
- Init_Timer0();
- while(1)
- {
- if(K1==0)//第一個按鍵,速度等級增加
- {
- if(Speed<19)
- Speed++;
- WaitKeyFree();
- }
- else if(K2==0)//第二個按鍵,速度等級減小
- {
- if(Speed>1)
- Speed--;
- WaitKeyFree();
- }
- TempData[0]=dofly_DuanMa[(Speed-1)/10];//分解顯示信息,如要顯示68,則68/10=6 68%10=8
- TempData[1]=dofly_DuanMa[(Speed-1)%10];
- }
- }
- /*------------------------------------------------
- 顯示函數(shù),用于動態(tài)掃描數(shù)碼管
- 輸入?yún)?shù) FirstBit 表示需要顯示的第一位,如賦值2表示從第三個數(shù)碼管開始顯示
- 如輸入0表示從第一個顯示。
- Num表示需要顯示的位數(shù),如需要顯示99兩位數(shù)值則該值輸入2
- ------------------------------------------------*/
- void Display(unsigned char FirstBit,unsigned char Num)
- {
- static unsigned char i=0;
-
- DataPort=0; //清空數(shù)據(jù),防止有交替重影
- LATCH1=1; //段鎖存
- LATCH1=0;
- DataPort=dofly_WeiMa[i+FirstBit]; //取位碼
- LATCH2=1; //位鎖存
- LATCH2=0;
- DataPort=TempData[i]; //取顯示數(shù)據(jù),段碼
- LATCH1=1; //段鎖存
- LATCH1=0;
-
- i++;
- if(i==Num)
- i=0;
- }
- /*------------------------------------------------
- 定時器初始化子程序
- ------------------------------------------------*/
- void Init_Timer0(void)
- {
- TMOD |= 0x01; //使用模式1,16位定時器,使用"|"符號可以在使用多個定時器時不受影響
- //TH0=0x00; //給定初值
- //TL0=0x00;
- EA=1; //總中斷打開
- ET0=1; //定時器中斷打開
- TR0=1; //定時器開關(guān)打開
- PT0=1; //優(yōu)先級打開
- }
- /*------------------------------------------------
- 定時器中斷子程序
- ------------------------------------------------*/
- void Timer0_isr(void) interrupt 1
- {
- static unsigned char times;
- TH0=(65536-1000)/256; //重新賦值 1ms
- TL0=(65536-1000)%256;
-
- Display(0,8);
- if(times>(Speed-1))//最大值18,所以最小間隔值20-18=2
- D1=0;
- else
- D1=1;
- times++;
- if(times==19)
- times=0;
- }
復(fù)制代碼
所有資料51hei提供下載:
電機pwm調(diào)速.7z
(13.28 KB, 下載次數(shù): 13)
2018-11-30 20:32 上傳
點擊文件名下載附件
|