Hey mike . I figured it out. Instead of #include <p18f458> I used #include<p18f4580> and it did it. And I had linker file of p18f4580 so I think that was the error. And Yeah I have done it for a single servo, also included 2 Push Buttons, 1 for increasing duty cycle and 1 for decreasing. I am simulating it on Proteus ! Thanks
I was modifying my algo for 2 servos. I have decided to put a sequence of producing pulses. Like when at the start of 20ms period, I produce first pulse for servo 1 and then servo 2, this repeats every time after 20ms ... first load CCPR with ServoPos1 and make ServoPin1 = 1, when flag raised, make ServoPin1 = 0, load CCPR with ServoPos2 and make ServoPin2 = 1. When flag raised make ServoPin2 = 0, and load CCPR = 20000 - ServoPos1 - ServoPos2 . When Flag is raised again, the whole process repeats. I will add Sequence variable to keep track of which servo to update
What do you say about this ?
////////////////////UPDATE:///////////////
Mike I ran the above algo and it works great. I also introduced two more buttons for motor 2 and now its running perfect. I have got 1 query though
when I load 1500 in servopos1 and servopos2 , the angles of the motor are -0.09 and -0.036. Is that sort of error expected because of programming in high level language ? or should they be exactly 0 ?
I am posting the code :
//using CCP to produce PWM and interrupt
Code:
#include <p18f4580.h>
#pragma config WDT = OFF, LVP = OFF
#define Servo1Tris TRISBbits.TRISB2
#define ServoPin1 LATBbits.LATB2
#define Servo2Tris TRISBbits.TRISB3
#define ServoPin2 LATBbits.LATB3
#define IncPin1Tris TRISBbits.TRISB4
#define DecPin1Tris TRISBbits.TRISB5
#define IncPin2Tris TRISBbits.TRISB6
#define DecPin2Tris TRISBbits.TRISB7
#define IncPin1 PORTBbits.RB4
#define DecPin1 PORTBbits.RB5
#define IncPin2 PORTBbits.RB6
#define DecPin2 PORTBbits.RB7
void ccp1_isr();
unsigned int ServoPos2;
unsigned int ServoPos1;
unsigned int seq;
#pragma code low_vector= 0x08
void low_interrupt()
{
_asm
GOTO ccp1_isr
_endasm
}
#pragma code
#pragma interruptlow ccp1_isr
void ccp1_isr()
{
if(seq == 0)
{
if (ServoPin1 == 1)
{
ServoPin1 = 0;
CCPR1 = ServoPos2;
ServoPin2 = 1;
seq = 1;
}
else
{
ServoPin1 = 1;
CCPR1 = ServoPos1;
}
}
else
{
if (ServoPin2 == 1)
{
ServoPin2 = 0;
CCPR1 = (20000 - (ServoPos1 + ServoPos2));
seq = 0;
}
}
PIR1bits.CCP1IF = 0;
}
#pragma code
void main(void)
{
seq =0;
IncPin1Tris = 1;
DecPin2Tris = 1;
IncPin2Tris = 1;
DecPin2Tris = 1;
Servo1Tris = 0;
ServoPin1 = 0;
Servo2Tris = 0;
ServoPin2 = 0;
IncPin1 = 1;
DecPin1 = 1;
IncPin2 = 1;
DecPin2 = 1;
//
CCP1CON = 0b00001011; //Activate CCP1 mode with interrupt trigger
T1CON = 0b10010001; //Set timer 1 on with 2 prescaler
ServoPos1 = 1500;
ServoPos2 = 1500;
CCPR1 = ServoPos1; //Compare register value loaded
PIE1bits.CCP1IE = 1; //Peripheral interrupts enable
INTCONbits.PEIE = 1;
INTCONbits.GIE = 1;
//INTCON2bits.RBPU = 0;
//while(!ServoPin1);
//while(ServoPos1);
while(1)
{
if(IncPin1 == 0)
{
while(IncPin1 == 0);
ServoPos1 = ServoPos1+50;
}
if(DecPin1 == 0)
{
while(DecPin1 == 0);
ServoPos1 = ServoPos1-50;
}
if(IncPin2 == 0)
{
while(IncPin2 == 0);
ServoPos2 = ServoPos2+50;
}
if(DecPin2 == 0)
{
while(DecPin2 == 0);
ServoPos2 = ServoPos2-50;
}
}
}
The output
**broken link removed**