|
直流電機(jī)控制
單片機(jī)源程序如下:
- /*
- 程序功能
-
- 6檔調(diào)速直流電機(jī),oled(IIC接口)顯示占空比
- */
- #include "reg52.h"
- #include "i2c.h"
- #include "SEEKFREE_OLED.h"
-
- sbit PwmOut=P1^0;
- sbit k1=P0^0;
- sbit k2=P0^1;
- unsigned int period=50000; //設(shè)置周期為50000us
- unsigned int duty=0; //設(shè)置起始占空比值為0
- void T0T1_Init(void) //定時器/計數(shù)器T0,T1初始化函數(shù)
- {
- EA=1;
- ET0=1;
- ET1=1;
- TMOD|=0x11;
- TR0=1;
- TH0=(65536-period)>>8;
- TL0=65536-period;
- TH1=(65536-duty)>>8;
- TL1=65536-duty;
- }
- void Delay10ms(void) //誤差 0us
- {
- unsigned char a,b,c;
- for(c=1;c>0;c--)
- for(b=38;b>0;b--)
- for(a=130;a>0;a--);
- }
- void keypros() //按鍵處理函數(shù),按K1加檔位,按K2減檔位,共6檔(0,1,2,3,4,5)
- {
- void numpro();
- if(duty!=50000) //封邊,達(dá)到最5檔位不可再加
- {
- if(k1==0)
- {
- Delay10ms();
- if(k1==0)
- {
- duty+=10000;
- }
- while(!k1);
- numpro();
- }
- }
- if(duty) //封邊,為0檔時不可再減
- {
- if(k2==0)
- {
- Delay10ms();
- if(k2==0)
- {
- duty-=10000;
- }
- while(!k2);
- numpro();
- }
- }
- }
- void numpro() //oled顯示處理函數(shù),顯示占空比
- {
- switch(duty)
- {
- case 0:OLED_P8x16Str(90,3,"0% ");
- OLED_P6x8Str(90,5,"min"); break;
- case 10000:OLED_P8x16Str(90,3,"20% ");
- OLED_P6x8Str(90,5," "); break;
- case 20000:OLED_P8x16Str(90,3,"40% ");
- OLED_P6x8Str(90,5," "); break;
- case 30000:OLED_P8x16Str(90,3,"60% ");
- OLED_P6x8Str(90,5," "); break;
- case 40000:OLED_P8x16Str(90,3,"80% ");
- OLED_P6x8Str(90,5," "); break;
- case 50000:OLED_P8x16Str(90,3,"100%");
- OLED_P6x8Str(90,5,"max"); break;
- }
- }
- void main()
- {
- PwmOut=0;
- T0T1_Init();
- Delay10ms(); //等待oled上電完成
- Initial_M096128x64_ssd1306();//oled初始化
- OLED_P6x8Str(25,0,"hello world");//oled顯示"hello world"
- OLED_P6x8Str(25,7,"---made by msonah");//oled顯示"made by msonah"
- OLED_P8x16Str(0,3,"duty cycle:");
- OLED_P8x16Str(90,3,"0% ");
- OLED_P6x8Str(90,5,"min"); //剛通電時顯示“duty cycle:0%”
- while(1)
- {
- keypros();
- }
- }
- void T0_ISR(void) interrupt 1
- {
- TH0=(65536-period)>>8;
- TL0=65536-period;
- if(duty<period&&(duty))
- {
- PwmOut=1;
- TR1=1;
- }
- else
- {
- if(duty==0)
- {
- PwmOut=0;
- }
- else
- {
- PwmOut=1;
- }
- }
- }
- void T1_ISR(void) interrupt 3
- {
- TH1=(65536-duty)>>8;
- TL1=65536-duty;
- PwmOut=0;
- TR1=0;
- }
復(fù)制代碼
所有資料51hei提供下載:
實驗14:I2C控制xx+5檔調(diào)速電機(jī)3.0.zip
(87.15 KB, 下載次數(shù): 8)
2019-11-5 16:21 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|
|