;** 日期: 2010年.10月
;** 描绘: 一路舵机参数化操控
;** 功用: 用Time1中止,RD6口输出
;**595417f4d0a35e02&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1831118&u=http%3A%2F%2Fwww%2E51hei%2Ecom%2Fmcu%2F3062%2Ehtml&urlid=0″ id=”1_nwl” mpid=”1″ target=”_blank”>晶振: 12M
;** 适用机型: PIC16F877A,TowerPro MG995
*********************************************************************************/
#include
#define uint unsigned int
uint f;
uchar servo_angle_H;
uchar servo_angle_L;
uchar compensate_L;
uchar compensate_H;
void delay(uint x)
{
uint a,b;
for(a=x;a>0;a–)
for(b=110;b>0;b–);
}
void init()
{
TRISD=0x00;
PORTD=0x00;
INTCON=0xc0;
PIE1=0x01;
TMR1L=0;
TMR1H=0;
T1CON=0x21;
f=0;
}
void servo(uint angle)
{
uint temp,value;
value=(65536-368)-(75*angle)/9;
temp=(65536-14617)+(75*angle)/9;
servo_angle_H=value%256;
servo_angle_L=value/256 ;
compensate_L=temp%256;
compensate_H=temp/256;
}
void main()
{
init();
uint angle;
servo(0);
delay(200);
while(1)
{
for(angle=0;angle<181;angle++)
{
servo(angle);
delay(100);
}
for(angle=180;angle>0;angle–)
{
servo(angle);
delay(100);
}
}
}
void interrupt time1()
{
TMR1IF=0;
f=~f;
if(f==0)
{
TMR1L=servo_angle_H;
TMR1H=servo_angle_L;
RD6=1;
}
else
{
TMR1L=compensate_L;
TMR1H=compensate_H;
RD6=0;
}
}