Programming: Outputting a PWM (Pulse Width Modulation) on More Than One Pin of the AVR Microcontroller and Multiple Ports

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 #include int main(void) { DDRA |= 0xFF; DDRB |= 0xFF; DDRD |= 0xFF; TCCR1A |= 1< 700 && TCNT1 < 2500) { if (TCNT1 >= 2200 && bit_is_set(PORTA,PINA0)) PORTA &= ~(1<= 900 && bit_is_set(PORTA,PINA1)) PORTA &= ~(1<= 1200 && bit_is_set(PORTA,PINA2)) PORTA &= ~(1<= 2000 && bit_is_set(PORTA,PINA3)) PORTA &= ~(1<= 2200 && bit_is_set(PORTA,PINA4)) PORTA &= ~(1<= 900 && bit_is_set(PORTA,PINA5)) PORTA &= ~(1<= 1200 && bit_is_set(PORTA,PINA6)) PORTA &= ~(1< 2000 && bit_is_set(PORTA,PINA7)) PORTA &= ~(1<= 2200 && bit_is_set(PORTB,PINB0)) PORTB &= ~(1<= 900 && bit_is_set(PORTB,PINB1)) PORTB &= ~(1<= 1200 && bit_is_set(PORTB,PINB2)) PORTB &= ~(1<= 2000 && bit_is_set(PORTB,PINB3)) PORTB &= ~(1<= 2200 && bit_is_set(PORTB,PINB4)) PORTB &= ~(1<= 900 && bit_is_set(PORTB,PINB5)) PORTB &= ~(1<= 1200 && bit_is_set(PORTB,PINB6)) PORTB &= ~(1< 2000 && bit_is_set(PORTB,PINB7)) PORTB &= ~(1<= 2200 && bit_is_set(PORTD,PIND0)) PORTD &= ~(1<= 900 && bit_is_set(PORTD,PIND1)) PORTD &= ~(1<= 1200 && bit_is_set(PORTD,PIND2)) PORTD &= ~(1<= 2000 && bit_is_set(PORTD,PIND3)) PORTD &= ~(1<= 2200 && bit_is_set(PORTD,PIND4)) PORTD &= ~(1<= 900 && bit_is_set(PORTD,PIND5)) PORTD &= ~(1<= 1200 && bit_is_set(PORTD,PIND6)) PORTD &= ~(1< 2000 && bit_is_set(PORTD,PIND7)) PORTD &= ~(1<
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.