Traumflug / Teacup_Firmware

Firmware for RepRap and other 3D printers
http://forums.reprap.org/read.php?147
GNU General Public License v2.0
310 stars 198 forks source link

PWM M106 command extra pulse at value = 0 (0%) and solution #331

Open papilinou opened 2 years ago

papilinou commented 2 years ago

Using "teacup" to drive a two axis laser and pin DIO11 defined as heater in "hardware PWM mode" to be used as ttl output to drive a led laser using M106 P0 S0 >=255 (0 to 100%). Everything worked fine but i saw that it was impossible to shut completely the laser.Checking with an oscilloscope there was a pulse equal to 1 cycle of the timer in duration which was confirmed in the section "OFF-BY-ONE" in the document:

https://docs.arduino.cc/tutorials/generic/secrets-of-arduino-pwm#using-the-atmega-pwm-registers-directly

Below i'll speak about fastpwm, not the option defined in Teacup firmware but the one defined in Atmel ATMEGA 328P manual as one of PWM mode : fastpwm or phase-correct. This fastpwm mode is set in "heather-avr.c" file line 70 and pin DIO11 use timer TCCR2A (see line 121 in "arduino-168-328p.h" file). Setting the mode to phase-correct avoid the extra pulse and at the same time allow setting a higher frequency for the laser.(3.921kHz). The lines to modify in "heather-avr.c" : line 85 : TCCR2A = MASK(WGM21) | MASK(WGM20); to : TCCR2A = MASK(WGM20); line 89 : TCCR2B = MASK(CS20) | MASK(CS21) | MASK(CS22); // F_CPU / 256 / 1024 to : TCCR2B = MASK(CS21) ; // F_CPU / 8 / 255 / 2 / = 3.921kHz

Hope this will help.