|
舵機(jī)程序模塊化,方便移植和使用
單片機(jī)源程序如下:
- #include<reg51.h>
- #include "servo.h"
- void main ( void ) //主函數(shù)
- {
- InitialTimer();
- for(;;)
- {
- ControlLeftOrRight();
- }
- }
- void Timer1 ( void ) interrupt 3 //定時(shí)器中斷函數(shù)
- {
- TH1 = ( 65535 - 500 ) / 256;
- TL1 = ( 65535 - 500 ) % 256;
- TimeOutCounter ++;
- switch ( LeftOrRight )
- {
- case 0 : //為0時(shí),舵機(jī)歸位,脈寬1.5ms
- {
- if( TimeOutCounter <= 3 )
- {
- ControlPort = 1;
- }
- else
- {
- ControlPort = 0;
- }
- break;
- }
- case 1 : //為1時(shí),舵機(jī)左轉(zhuǎn),脈寬1ms(理論值),實(shí)際可以調(diào)試得出
- {
- if( TimeOutCounter <= 1 )
- {
- ControlPort = 1;
- }
- else
- {
- ControlPort = 0;
- }
- break;
- }
- case 2 : //為2時(shí),舵機(jī)右轉(zhuǎn),脈寬2ms(理論值),實(shí)際可以調(diào)試得出
- {
- if( TimeOutCounter <= 6 )
- {
- ControlPort = 1;
- }
- else
- {
- ControlPort = 0;
- }
- break;
- }
- default : break;
- }
-
- if( TimeOutCounter == 80 ) //周期20ms(理論值),比較可靠,最好不要修改
- {
- TimeOutCounter = 0;
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
Servo_Model.zip
(31.78 KB, 下載次數(shù): 175)
2018-11-20 10:46 上傳
點(diǎn)擊文件名下載附件
servo程序 下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|