|
@@ -1,190 +1,190 @@
|
|
|
-#include <avr/io.h>
|
|
|
-#include <avr/interrupt.h>
|
|
|
-#include "io_atmega2560.h"
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-enum class States : uint8_t {
|
|
|
- ZERO_START = 0,
|
|
|
- ZERO,
|
|
|
- ZERO_TO_RISE,
|
|
|
- RISE,
|
|
|
- RISE_TO_ONE,
|
|
|
- ONE,
|
|
|
- ONE_TO_FALL,
|
|
|
- FALL,
|
|
|
- FALL_TO_ZERO
|
|
|
-};
|
|
|
-
|
|
|
-
|
|
|
-static States state = States::ZERO_START;
|
|
|
-
|
|
|
-bool bedPWMDisabled = 0;
|
|
|
-
|
|
|
-
|
|
|
-static uint8_t slowCounter = 0;
|
|
|
-
|
|
|
-static uint8_t fastCounter = 0;
|
|
|
-
|
|
|
-static uint8_t pwm = 0;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-extern unsigned char soft_pwm_bed;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-static const uint8_t fastMax = 16;
|
|
|
-
|
|
|
-
|
|
|
-static const uint8_t fastShift = 4;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-static const uint8_t slowInc = 1;
|
|
|
-
|
|
|
-ISR(TIMER0_OVF_vect)
|
|
|
-{
|
|
|
- switch(state){
|
|
|
- case States::ZERO_START:
|
|
|
- if (bedPWMDisabled) return;
|
|
|
- pwm = soft_pwm_bed << 1;
|
|
|
- if( pwm != 0 ){
|
|
|
- state = States::ZERO;
|
|
|
- }
|
|
|
- break;
|
|
|
- case States::ZERO:
|
|
|
-
|
|
|
- slowCounter += slowInc;
|
|
|
- if( slowCounter > pwm ){
|
|
|
- return;
|
|
|
- }
|
|
|
- state = States::ZERO_TO_RISE;
|
|
|
- break;
|
|
|
-
|
|
|
-
|
|
|
- case States::ZERO_TO_RISE:
|
|
|
-
|
|
|
-
|
|
|
- state = States::RISE;
|
|
|
- fastCounter = fastMax - 1;
|
|
|
- TCNT0 = 255;
|
|
|
- TCCR0B = (1 << CS00);
|
|
|
- TCCR0A &= ~(1 << COM0B0);
|
|
|
- break;
|
|
|
- case States::RISE:
|
|
|
- OCR0B = (fastMax - fastCounter) << fastShift;
|
|
|
- if( fastCounter ){
|
|
|
- --fastCounter;
|
|
|
- } else {
|
|
|
- state = States::RISE_TO_ONE;
|
|
|
- OCR0B = 255;
|
|
|
- TCNT0 = 254;
|
|
|
-
|
|
|
- }
|
|
|
- break;
|
|
|
- case States::RISE_TO_ONE:
|
|
|
- state = States::ONE;
|
|
|
- OCR0B = 255;
|
|
|
- TCNT0 = 255;
|
|
|
- TCCR0B = (1 << CS01);
|
|
|
- break;
|
|
|
- case States::ONE:
|
|
|
- OCR0B = 255;
|
|
|
- if (bedPWMDisabled) return;
|
|
|
- slowCounter += slowInc;
|
|
|
- if( slowCounter < pwm ){
|
|
|
- return;
|
|
|
- }
|
|
|
- if( (soft_pwm_bed << 1) >= (255 - slowInc - 1) ){
|
|
|
-
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- state = States::ONE;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- state=States::FALL;
|
|
|
- fastCounter = fastMax - 1;
|
|
|
- TCNT0 = 255;
|
|
|
- TCCR0B = (1 << CS00);
|
|
|
-
|
|
|
-
|
|
|
- TCCR0A |= (1 << COM0B0);
|
|
|
- break;
|
|
|
- case States::FALL:
|
|
|
- OCR0B = (fastMax - fastCounter) << fastShift;
|
|
|
-
|
|
|
- if( fastCounter ){
|
|
|
- --fastCounter;
|
|
|
- } else {
|
|
|
- state = States::FALL_TO_ZERO;
|
|
|
- TCNT0 = 128;
|
|
|
- OCR0B = 255;
|
|
|
- }
|
|
|
- break;
|
|
|
- case States::FALL_TO_ZERO:
|
|
|
- state = States::ZERO_START;
|
|
|
- TCNT0 = 128;
|
|
|
- OCR0B = 255;
|
|
|
- TCCR0B = (1 << CS01);
|
|
|
- break;
|
|
|
- }
|
|
|
-}
|
|
|
+#include <avr/io.h>
|
|
|
+#include <avr/interrupt.h>
|
|
|
+#include "io_atmega2560.h"
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+enum class States : uint8_t {
|
|
|
+ ZERO_START = 0,
|
|
|
+ ZERO,
|
|
|
+ ZERO_TO_RISE,
|
|
|
+ RISE,
|
|
|
+ RISE_TO_ONE,
|
|
|
+ ONE,
|
|
|
+ ONE_TO_FALL,
|
|
|
+ FALL,
|
|
|
+ FALL_TO_ZERO
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+static States state = States::ZERO_START;
|
|
|
+
|
|
|
+bool bedPWMDisabled = 0;
|
|
|
+
|
|
|
+
|
|
|
+static uint8_t slowCounter = 0;
|
|
|
+
|
|
|
+static uint8_t fastCounter = 0;
|
|
|
+
|
|
|
+static uint8_t pwm = 0;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+extern unsigned char soft_pwm_bed;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+static const uint8_t fastMax = 16;
|
|
|
+
|
|
|
+
|
|
|
+static const uint8_t fastShift = 4;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+static const uint8_t slowInc = 1;
|
|
|
+
|
|
|
+ISR(TIMER0_OVF_vect)
|
|
|
+{
|
|
|
+ switch(state){
|
|
|
+ case States::ZERO_START:
|
|
|
+ if (bedPWMDisabled) return;
|
|
|
+ pwm = soft_pwm_bed << 1;
|
|
|
+ if( pwm != 0 ){
|
|
|
+ state = States::ZERO;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case States::ZERO:
|
|
|
+
|
|
|
+ slowCounter += slowInc;
|
|
|
+ if( slowCounter > pwm ){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ state = States::ZERO_TO_RISE;
|
|
|
+ break;
|
|
|
+
|
|
|
+
|
|
|
+ case States::ZERO_TO_RISE:
|
|
|
+
|
|
|
+
|
|
|
+ state = States::RISE;
|
|
|
+ fastCounter = fastMax - 1;
|
|
|
+ TCNT0 = 255;
|
|
|
+ TCCR0B = (1 << CS00);
|
|
|
+ TCCR0A &= ~(1 << COM0B0);
|
|
|
+ break;
|
|
|
+ case States::RISE:
|
|
|
+ OCR0B = (fastMax - fastCounter) << fastShift;
|
|
|
+ if( fastCounter ){
|
|
|
+ --fastCounter;
|
|
|
+ } else {
|
|
|
+ state = States::RISE_TO_ONE;
|
|
|
+ OCR0B = 255;
|
|
|
+ TCNT0 = 254;
|
|
|
+
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case States::RISE_TO_ONE:
|
|
|
+ state = States::ONE;
|
|
|
+ OCR0B = 255;
|
|
|
+ TCNT0 = 255;
|
|
|
+ TCCR0B = (1 << CS01);
|
|
|
+ break;
|
|
|
+ case States::ONE:
|
|
|
+ OCR0B = 255;
|
|
|
+ if (bedPWMDisabled) return;
|
|
|
+ slowCounter += slowInc;
|
|
|
+ if( slowCounter < pwm ){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if( (soft_pwm_bed << 1) >= (255 - slowInc - 1) ){
|
|
|
+
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ state = States::ONE;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ state=States::FALL;
|
|
|
+ fastCounter = fastMax - 1;
|
|
|
+ TCNT0 = 255;
|
|
|
+ TCCR0B = (1 << CS00);
|
|
|
+
|
|
|
+
|
|
|
+ TCCR0A |= (1 << COM0B0);
|
|
|
+ break;
|
|
|
+ case States::FALL:
|
|
|
+ OCR0B = (fastMax - fastCounter) << fastShift;
|
|
|
+
|
|
|
+ if( fastCounter ){
|
|
|
+ --fastCounter;
|
|
|
+ } else {
|
|
|
+ state = States::FALL_TO_ZERO;
|
|
|
+ TCNT0 = 128;
|
|
|
+ OCR0B = 255;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case States::FALL_TO_ZERO:
|
|
|
+ state = States::ZERO_START;
|
|
|
+ TCNT0 = 128;
|
|
|
+ OCR0B = 255;
|
|
|
+ TCCR0B = (1 << CS01);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+}
|