controling servo question

Status
Not open for further replies.

crys

New Member
Is my program able to produce a 16ms PERIOD PWM and control a SERVO?

#include <pic.h>
#include "delay.c"
#include "delay.h"

__CONFIG(WDTDIS & XT & UNPROTECT);

#define XTAL 4000000 //crystal frequency- 4MHz


main()

{

TRISC=0x00;
TRISB=0xF0;
if(RB4==1)
{

RC1= 1;
DelayMs(1);
RC1= 0;
DelayMs(15);


}
if(RB5==1)
{

RC1= 1;
DelayMs(2);
RC1= 0;
DelayMs(14);


}
if((PORTB&0xF0)==0x00)
{

RC1= 1;
DelayMs(1);
DelayUs(250);
DelayUs(250);
RC1= 0;
DelayMs(14);
DelayUs(250);
DelayUs(250);

}

}
 
After I deleted all the blank lines and indented it, it looks like it probably would do as you envisage. However, as you have posted code without any indication of processor type I have no idea if your code is correct or not. It would also help if you indicated which compiler you are using.

Mike.
 
You only execute the code once. It would also make more sense to use else if to check the key presses. This way only 1 piece of code can get executed.
Code:
main(){
    TRISC=0x00;
    TRISB=0xF0;
    while(1){       //loop forever
        if(RB4==1){
            RC1= 1; 
            DelayMs(1);
            RC1= 0; 
            DelayMs(15);
        }
        else if(RB5==1){
            RC1= 1; 
            DelayMs(2);
            RC1= 0; 
            DelayMs(14);
        }
        else{
            RC1= 1; 
            DelayMs(1);
            DelayUs(250);
            DelayUs(250);
            RC1= 0; 
            DelayMs(14);
            DelayUs(250);
            DelayUs(250);
        }
    }
}

Also, note how much easier it is to read when surrounded by code tags.

Mike.
 
thanks!

got my SERVO running with PWM mode w/o jitters

i'll use this for the speed control
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…