Go Down

Topic: Software PWM for Arduino Due (Read 2371 times) previous topic - next topic


Has a library been created yet that will produce software PWM on the Due?

I tried using the library from the Arduino website:

but I keep getting an error that it can't find avr\io.h. After doing some research I've learned that that is pertaining to the AVR chip on the mega and is not used for the atmel on the Due so they can be removed. When I do that, it starts complaining about the code itself in the SoftPWM library. So, is there a library that accommodates the Due or is there an issue in the compiler that needs to be addressed before it can be done smoothly?



Do you not have enough hardware PWM on the Due?
"The problem is in the code you didn't post."


I'm using a touchscreen shield that takes up half of the HW PWM signals so I needed to use software PWM


How many lines are you needing?  Might be easier to hack the shield but then you'd also have to hack the TFT library.  Henning Karlssen's stuff is fairly nice to follow.


That sounds really wasteful. Is it possible to use an I²C or SPI driven display instead?


I'm using this shield:
Over another shield that utilizes a ds3234 rtc, a xbee wifi module, a buzzer, 2 rj45 connectors for some peripheral connections, one requiring 8 PWM channels, and several more I/O ports for various sensors. I'm using that shield because it covers everything nicely without requiring extra supports to keep it from bending like other tft shields I've seen. When all is said and done, I need to use some digital ports for software pwm, but I'm assuming by these comments there isn't one.


Henning Karlssen's stuff is fairly nice to follow.

I'm already using the UTFT libraries. My compinents won't allow me to adjust pin placement very easily. My easiest route is software pwm which I'm sure the Due is more than capable of handling especially since people do it on the Uno and Mega.


I removed the avr/io.h and avr/interrupt.h since they don't pertain to the Due, but of course that removes the support for the timer in the SoftPWM library. Is there a way to convert this so it uses the Due timers instead of the avr timers?

Code: [Select]

#if defined(USE_TIMER2)
#define SOFTPWM_TIMER_SET(val)     (TCNT2 = (val))
#define SOFTPWM_TIMER_INIT(ocr) ({\
  TIFR2 = (1 << TOV2);    /* clear interrupt flag */ \
  TCCR2B = (1 << CS21);   /* start timer (ck/8 prescalar) */ \
  TCCR2A = (1 << WGM21);  /* CTC mode */ \
  OCR2A = (ocr);          /* We want to have at least 30Hz or else it gets choppy */ \
  TIMSK2 = (1 << OCIE2A); /* enable timer2 output compare match interrupt */ \
#elif defined(USE_TIMER4_HS)
#define SOFTPWM_TIMER_SET(val)     (TCNT4 = (val))
#define SOFTPWM_TIMER_INIT(ocr) ({\
  TCCR4A = 0; \
  TCCR4B = 0x04; /* CTC Mode */\
  TCCR4C = 0; \
  TCCR4D  = 0; \
  TCCR4E  = 0; \
  OCR4C  = 0; \
  OCR4C  = (ocr); \
  TIMSK4  = (1 << OCIE4A); \


I ported SoftPWM (and many other libraries) to Teensy 3.1 within the last year.

Due has very different timers, so none of that work will help directly.  But it can at least give anyone who tries a head-start to identify the places that need porting to move from AVR to ARM.


So I guess I get to learn about Due timers now. Woohoo! Or I can try switching back to the Mega... which will work, but just be very slow.

Go Up