str4code
Newbie level 4
Dear Members,
I am trying to control a servo motor with PWM using delay functions of XC8 compiler. This is the circuit configuration in Proteus.
i used the following code:
This is the output:
I have compared it with a pulse generator at 50Hz with 1.5ms duty. The out put from PIC seems to be much bigger than 1.5ms.
What am I missing?
Thanks in advance
I am trying to control a servo motor with PWM using delay functions of XC8 compiler. This is the circuit configuration in Proteus.
i used the following code:
Code:
#define _XTAL_FREQ 8000000
// PIC16F887 Configuration Bit Settings
#pragma config FOSC = INTRC_NOCLKOUT
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config MCLRE = OFF
#pragma config CP = OFF
#pragma config CPD = OFF
#pragma config BOREN = OFF
#pragma config IESO = OFF
#pragma config FCMEN = OFF
#pragma config LVP = OFF
// CONFIG2
#pragma config BOR4V = BOR40V
#pragma config WRT = OFF
#include <xc.h>
void delay_ms(int x) {
for (int i = 0; i < x; i++) {
__delay_ms(1);
}
}
void delay_us(int x) {
for (int i = 0; i < x; i++) {
__delay_us(1);
}
}
void Soft_PWM(int freq, float percent)
{
unsigned int period = (1.0 / freq) * 1000 * 1000; // us
unsigned int duty = period * (percent / 100.0); // us
for (int i = 0; i < 50; i++) {
PORTCbits.RC1 = 1;
delay_us(duty);
PORTCbits.RC1 = 0;
delay_us(period - duty);
}
}
void main() {
OSCCON = 0x70;
TRISC = 0; // PORTB as Ouput Port
do {
// Soft_PWM(50, 5); // 1ms = 0 Degree
// delay_ms(2000);
Soft_PWM(50, 7.5); // 1.5ms = 90 Degree
// delay_ms(2000);
// Soft_PWM(50, 10); // 2ms = 180 Degree
} while (1);
}
This is the output:
I have compared it with a pulse generator at 50Hz with 1.5ms duty. The out put from PIC seems to be much bigger than 1.5ms.
What am I missing?
Thanks in advance