欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標題:
STC15F2K60S2驅動舵機
[打印本頁]
作者:
上善~若水
時間:
2017-10-25 09:40
標題:
STC15F2K60S2驅動舵機
//STC15F2K60S2定時/計數器中斷 (晶振12MHz,12分頻) 單位時間等于12分頻/12MHz,即計數器每次加1使用的時間是1微秒。
#include <STC15F2K60S2.H>
#include <math.h> //pow(x,y) x的y次方函數需要
sbit KEY1 = P3^2;
sbit KEY2 = P3^3;
sbit led1 = P1^1;
sbit led2 = P1^2;
unsigned int counter = 0; //例子中需要使用的變量
/*****
函數名:定時計數器中斷初始化
調用:TimerCounterIntInit_12MHz(Tx, delay_us);
參數:bit T_C_Int(定時器中斷0或定時器中斷1), delay_us(多少微米)
返回值:無
結果:啟動T/C1或T/C0并設置計數器初值
*****/
void TimerCounterIntInit_12MHz(bit Tx,unsigned int delay_us)
{
//定時器0和定時器1都使用工作方式0:16位自動裝載的定時/計數器
TMOD = 0x00;
//中斷總開關(屬IE:中斷允許寄存器)
EA = 1;
if(Tx==0){
ET0 = 1; //允許定時器中斷0中斷(屬IE:中斷允許寄存器)
TH0 = (65536 - delay_us) >> 8; //16位計數寄存器T0高8位
TL0 = (65536 - delay_us) & 0x00FF; //16位計數寄存器T0低8位
}
else {
ET1 = 1; //允許定時器中斷1中斷(屬IE:中斷允許寄存器)
TH1 = (65536 - delay_us) >> 8; //16位計數寄存器T1高8位
TL1 = (65536 - delay_us) & 0x00FF; //16位計數寄存器T1低8位
}
}
/*****
函數名:定時/計數器0中斷處理函數
調 用:[T/C0溢出后中斷處理]
參 數:無
返回值:無
結 果:重新寫入16位計數寄存器初始值,處理用戶程序
備 注:
定時/計數器1中斷處理函數:
void TimerCounter_1 (void) interrupt 3 using 3{ //切換寄存器組到3
//重新寫入初值
//用戶函數內容
}
*****/
void TimerCounter_0 (void) interrupt 1 using 1{ //切換寄存器組到1
counter++;
if(counter ==2000){
//這個數值需要根據TimerCounterIntInit_12MHz(Tx,delay_us)函數的delay_us值進行調整counter=20000us/delay_us
TR0 = 0; //停止定時器,使舵機運行穩定
counter = 0; //重置
}
}
/*****
函數名:模擬PWM
調用:SimulationPWM(unsigned char pin,unsigned int pwmOut,unsigned int theNumOfTimes);
參數:unsigned char pin(輸出模擬PWM的引腳,如:11表示P1^1,3表示P0^3)
unsigned int pwmOut(多少微米)
unsigned int theNumOfTimes(舵機的一個脈沖是20ms,500-2500us的高電平控制舵機角度,循環次數需要大于等于
1T單片機大概4個_nop()是1us,所以theNumOfTimes的數量起碼要大于角度對應的時間)
返回值:無
結果:產生20ms的pwm
*****/
void SimulationPWM(unsigned char pin,unsigned int pwmOut,unsigned int theNumOfTimes) //pwmOut取值(50~250)
{
unsigned int i=1;
int Pn = pin/10;
int PnBit = pin%10;
PnBit = pow(2,PnBit);
TR0 = 1;
for(i=0;i<theNumOfTimes;i++){
if(counter>0 && counter<pwmOut){
switch(Pn){
case 0:P0 |= PnBit;
break;
case 1:P1 |= PnBit;
break;
case 2:P2 |= PnBit;
break;
case 3:P3 |= PnBit;
break;
case 4:P4 |= PnBit;
break;
case 5:P5 |= PnBit;
break;
}
}else{
switch(Pn){
case 0:P0 &= ~PnBit;
break;
case 1:P1 &= ~PnBit;
break;
case 2:P2 &= ~PnBit;
break;
case 3:P3 &= ~PnBit;
break;
case 4:P4 &= ~PnBit;
break;
case 5:P5 &= ~PnBit;
break;
}
}
}
}
//主函數
void main(void)
{
TimerCounterIntInit_12MHz(0,10); //0.01毫秒,這個時間需要根據不同舵機進行調整。
while(1){
led1=0;
led2=0;
if(KEY1==0){
SimulationPWM(12,90,10001);
SimulationPWM(11,140,10001);
}
else if(KEY2==0){
SimulationPWM(12,120,12001);
SimulationPWM(11,180,10001);
}
}
}
歡迎光臨 (http://m.raoushi.com/bbs/)
Powered by Discuz! X3.1