7

I have a problem with the Servo library.

I need to read a PPM signal and create a PWM signal of each channel. The problem is that PPM has a higher resolution than PWM so I want to use the 16 bit Timer1 for reading PPM. However the Servo library that writes the PWM signal also uses Timer1.

Is it possible to generate a PWM signal with 8 bit / Timer2? And has someone made an example, or an explanation about, how to do that?

Greenonline
3,1527 gold badges36 silver badges48 bronze badges
asked Oct 8, 2016 at 10:06
2
  • 1
    Yes, there is example in this Tutorial Commented Oct 9, 2016 at 11:19
  • Maybe use the servo library as is, and do the PPM counting with timer2 with some overflow handling. Commented Dec 2, 2016 at 15:32

3 Answers 3

2

Here is a simple example from my page about timers of using Timer 2 to output a square wave of 50 kHz:

const byte LED = 3; // Timer 2 "B" output: OC2B
const long frequency = 50000L; // Hz
void setup() 
 {
 pinMode (LED, OUTPUT);
 TCCR2A = bit (WGM20) | bit (WGM21) | bit (COM2B1); // fast PWM, clear OC2B on compare
 TCCR2B = bit (WGM22) | bit (CS21); // fast PWM, prescaler of 8
 OCR2A = ((F_CPU / 8) / frequency) - 1; // zero relative 
 OCR2B = ((OCR2A + 1) / 2) - 1; // 50% duty cycle
 } // end of setup
void loop()
 {
 // do other stuff here
 }
answered Jul 21, 2017 at 6:23
2

Yes, Arduino Servo library uses Timer1 (and other ones, depending on Arduino boards) to manage FastPWM via software interrupts. Timer2 is used by tone() function: be careful if you are using that function.

A good example using Timer2 to generate PWM is the Infrared remote library for Arduino . If IR_USE_TIMER2 is defined it will use Timer2. The key code is at IRremote.cpp where the ISR is managed.

answered Mar 22, 2017 at 18:45
1

Is it possible to generate a PWM signal with 8 bit / Timer2?

yes. I implemented multiple approaches, some on avrs and some on pics but the basic principles are the same: https://dannyelectronics.wordpress.com/2017/02/18/driving-multiple-servos-off-a-pic-timer2-ranged-extended/

that post provided a list of links to other implementations - one of them off a timer 2 on avr.

answered Mar 22, 2017 at 23:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.