recently i download the sample and doing some modification, the c36r servo motor is connected to port B0 and two switchs is connected to portB 1 and 2, i try to maintain the pulse by using the delay in 20ms,1ms is high ,1ms is a variable which is controlled by two switch and 18ms is low then reloop the code infinitely but i met failure after downloading the code into p16f876a ,the modified sample code is shown below:
- CODE: SELECT_ALL_CODE
#include <pic.h>
__CONFIG ( 0x3F32 );
#define servo RB0
#define right RB1
#define left RB2
#define MHZ *1000L
#define KHZ *1
#define DelayUs(x) { unsigned char _dcnt; \
_dcnt = (((x)*(20MHZ))/(24MHZ))|1; \
while(--_dcnt != 0) \
continue; \
_dcnt = (((x)* (20MHZ))/(24MHZ))|1; \
while(--_dcnt != 0) \
continue; }
void DelayMs(unsigned char y);
void main(void)
{
unsigned short i,a;
unsigned short keep=0;
unsigned short keep_low=0;
TRISB = 0b01100000;
PORTB = 0b00000000;
keep=150;
while(1)
{
if(right==1)
{
if(keep>=250) {keep=250;}
else {++keep;}
}
if(left==1)
{
if(keep<=0) {keep=0;}
else {--keep;}
}
servo=1;
DelayMs(1);
for(i=keep*4;0<=i;--i) {DelayUs(1);}
keep_low=250-keep;
servo=0;
for(i=keep_low*4;0<=i;--i) {DelayUs(1);}
DelayMs(18); //(keep+keep_low=250 and 250*4=1000=l000*1us=1ms)
}
}
void DelayMs(unsigned char y)
{
unsigned char i;
do {
i = 4;
do {
DelayUs(250);
} while(--i);
} while(--y);
}