Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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.:D

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top