Hi all
Could you maybe tell me why the following code (which compiles fine) will not rotate the servo.
Below is the code.
//1501Servo
#define _XTAL_FREQ 16000000
#include <xc.h>
#pragma config CLKOUTEN = OFF
#pragma config WDTE = OFF // or control in software - SWDTEN
#pragma config PWRTE = OFF
#pragma config CP = OFF
#pragma config BOREN = OFF
#pragma config MCLRE = OFF
#pragma config FOSC = INTOSC
#pragma config STVREN = ON
#pragma config LPBOR = OFF
#pragma config BORV = LO
#pragma config LVP = OFF
#pragma config WRT = ALL // we don't need self write
#include <stdlib.h>
#include <stdint.h>
void main(void)
{
int n;
TRISA = 0; // PORTA as Ouput Port
do
{
for( n=0; n<50 ;n++)
{
PORTA = 0xff; //switch pin on for 2 ms
__delay_ms(2);
PORTA = 0; //switch pin off for 18 ms
__delay_ms(18);
}
for(uint8_t n=0; n<50;n++)
{
PORTA = 0xff;
__delay_ms(18); // switch pin on for 18 ms
PORTA = 0; // switch pin off for 2 ms
__delay_ms(2);
}
}while(1);
}
A LED attached to the output pins alternately brightens and darkens, each brightening and darkening taking one second each.
When I connect the data lead of the servo to the pin in place of the LED, nothing happens.
I think I have the correct frequency of 50HZ for the servo which is an analog Turnigy TG9.
The servo purrs but it does not rotate rhythmically clockwise and anti-clockwise as expected.
Thank you
Could you maybe tell me why the following code (which compiles fine) will not rotate the servo.
Below is the code.
//1501Servo
#define _XTAL_FREQ 16000000
#include <xc.h>
#pragma config CLKOUTEN = OFF
#pragma config WDTE = OFF // or control in software - SWDTEN
#pragma config PWRTE = OFF
#pragma config CP = OFF
#pragma config BOREN = OFF
#pragma config MCLRE = OFF
#pragma config FOSC = INTOSC
#pragma config STVREN = ON
#pragma config LPBOR = OFF
#pragma config BORV = LO
#pragma config LVP = OFF
#pragma config WRT = ALL // we don't need self write
#include <stdlib.h>
#include <stdint.h>
void main(void)
{
int n;
TRISA = 0; // PORTA as Ouput Port
do
{
for( n=0; n<50 ;n++)
{
PORTA = 0xff; //switch pin on for 2 ms
__delay_ms(2);
PORTA = 0; //switch pin off for 18 ms
__delay_ms(18);
}
for(uint8_t n=0; n<50;n++)
{
PORTA = 0xff;
__delay_ms(18); // switch pin on for 18 ms
PORTA = 0; // switch pin off for 2 ms
__delay_ms(2);
}
}while(1);
}
A LED attached to the output pins alternately brightens and darkens, each brightening and darkening taking one second each.
When I connect the data lead of the servo to the pin in place of the LED, nothing happens.
I think I have the correct frequency of 50HZ for the servo which is an analog Turnigy TG9.
The servo purrs but it does not rotate rhythmically clockwise and anti-clockwise as expected.
Thank you
Last edited: