Important aspects of outputting to more than one pin are as follows:
- Make sure the pins that will output the PWM are set for output data direction (i.e. DDRB = 0xFF for all PORT B pins output)
- Make sure that the pins will be set high in the interrupt service routine (ISR)
- Add another line within code block of the if (TCNT1>=800 && TCNT1<=2400) and make sure the pin reflect the correct output pin and the number after the TCNT1 >= value is correctly set for the position for that servo on that pin.
#include <avr/io.h>
#include <avr/interrupt.h>
int main(void)
{
DDRA = 0xFF;
DDRB = 0xFF;
TCCR1A |= 1<<WGM11;
TCCR1B |= 1<<WGM12 | 1<<WGM13 | 1<<CS10;
TIMSK1 |= 1<<OCIE1A;
ICR1 = 19999;
sei();
while(1)
{
if (TCNT1 >= 2000 && bit_is_set(PORTB, PINB0)) PORTB &= ~(1<<PINB0);
if (TCNT1 >= 1000 && bit_is_set(PORTB, PINB1)) PORTB &= ~(1<<PINB1);
if (TCNT1 >= 1500 && bit_is_set(PORTB, PINB2)) PORTB &= ~(1<<PINB2);
if (TCNT1 >= 800 && bit_is_set(PORTB, PINB3)) PORTB &= ~(1<<PINB3);
if (TCNT1 >= 2300 && bit_is_set(PORTA, PINA0)) PORTA &= ~(1<<PINA0);
if (TCNT1 >= 1390 && bit_is_set(PORTA, PINA1)) PORTA &= ~(1<<PINA1);
if (TCNT1 >= 1800 && bit_is_set(PORTA, PINA2)) PORTA &= ~(1<<PINA2);
if (TCNT1 >= 1850 && bit_is_set(PORTA, PINA3)) PORTA &= ~(1<<PINA3);
}
}
ISR (TIMER1_COMPA_vect)
{
PORTA = 0xFF;
PORTB = 0xFF;
}
Comments and Additional Information
Have some code to share? Or additional information? Respond here:
You need to be logged in to save a response on this page. The response must be constructive, helpful, supplimentary or to correct the existing video, code or narrative content.