123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- #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;
- 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:
- 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;
- 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;
- }
- }
|