stuck with interrupt timer to control a servo

Thread Starter

rocker123uk

Joined Dec 6, 2015
33
Hi all im trying to control a servo using the timer interrupt. so when the 20ms interrrupt occurs a counter is incremented which sets the servo at a specific position which does happen however, i want it to change position when the second counter increments but this is not working... can any one help?

my code:
Code:
unsigned int count, Adcval;
unsigned int a =2000;

//Timer0
//Prescaler 1:256; TMR0 Preload = 100; Actual Interrupt Time : 19.968 ms

//Place/Copy this part in declaration section
void InitTimer0(){
  OPTION_REG         = 0x87;
  TMR0                 = 100;
  INTCON         = 0xA0;
}

void Interrupt(){
  if (TMR0IF_bit){
    TMR0IF_bit         = 0;
    TMR0                 = 100;
    count++;
  }
}


void main()
{
TRISB=0; // PORTB IS OUTPUT
PORTB=0;// PORTB INITIAL VALUE IS 0

ADC_init();

//count=0;
InitTimer0();
while(1)
{
if (count==1)
{
RB0_BIT=1;
//Adcval = ADC_Read(2);
Delay_us(500);
RB0_BIT=0;
//count=0;
TMR0=100;
}

if (count==2)
{
RB0_BIT=1;
//Adcval = ADC_Read(2);
Delay_us(1500);
RB0_BIT=0;
TMR0=100;
count=0;
}
}
}
Moderator edit: added code tags
 

MaxHeadRoom

Joined Jul 18, 2013
28,684
RC servo? DC or BLDC motion servo? If the latter, nature of feedback device, if any??
Servo cover a wide gamut of devices!
Max.
 

spinnaker

Joined Oct 29, 2009
7,830
Hi all im trying to control a servo using the timer interrupt. so when the 20ms interrrupt occurs a counter is incremented which sets the servo at a specific position which does happen however, i want it to change position when the second counter increments but this is not working... can any one help?

my code:
Code:
unsigned int count, Adcval;
unsigned int a =2000;

//Timer0
//Prescaler 1:256; TMR0 Preload = 100; Actual Interrupt Time : 19.968 ms

//Place/Copy this part in declaration section
void InitTimer0(){
  OPTION_REG         = 0x87;
  TMR0                 = 100;
  INTCON         = 0xA0;
}

void Interrupt(){
  if (TMR0IF_bit){
    TMR0IF_bit         = 0;
    TMR0                 = 100;
    count++;
  }
}


void main()
{
TRISB=0; // PORTB IS OUTPUT
PORTB=0;// PORTB INITIAL VALUE IS 0

ADC_init();

//count=0;
InitTimer0();
while(1)
{
if (count==1)
{
RB0_BIT=1;
//Adcval = ADC_Read(2);
Delay_us(500);
RB0_BIT=0;
//count=0;
TMR0=100;
}

if (count==2)
{
RB0_BIT=1;
//Adcval = ADC_Read(2);
Delay_us(1500);
RB0_BIT=0;
TMR0=100;
count=0;
}
}
}
Moderator edit: added code tags
Your post is confusing and just one big run on sentence "which does happen however," what does happen however?


Are you getting an output from RB0_BIT? Is your timer interrupt being called? Exactly what have you done to troubleshoot this issue?

And you have count=0 commented out so it is starting with an unknown value.
 

Thread Starter

rocker123uk

Joined Dec 6, 2015
33
I do get an output and the servo turns to the specific position as counter reaches 1, however, when i set it to if count =2 then it just doesnt work and i think its probably because of timing that is wrong as the servo i have is standard and requires 20ms to turn to its position
 

jjw

Joined Dec 24, 2013
823
This is your third thread about the same problem.
You should tell more about the circuit, type of the servos, power supply ( max current ) ...
 
Top