#include #include "mystd.h" #include "utils.h" #define D1 PORTB0 #define D2 PORTB1 #define D3 PORTB2 #define D4 PORTB4 #define PRESCALER 256 #define TIMER_FREQ (F_CPU / PRESCALER) #define DUTY_CYCLE 1 // the PWM is something that counts from 0 to TOP // when it reached the value i'll name TOGGLE, it'll set the OUTPUT high // when it reaches TOP it'll reset COUNTER and set OUTPUT to low // // this means we have a clock that reaches TOP every X // and during this, we can set TOGGLE such that we set OUTPUT high void timer1_init(void) { // Set PB1 (OC1A) as output DDRB |= _BV(PB1); // Non-inverting OC1B -> the OC1B is set to low on TOP reached TCCR1A |= _BV(COM1A1); // set to PWM-fast with ICR1 as TOP TCCR1A |= _BV(WGM11); TCCR1B |= _BV(WGM13) | _BV(WGM12); // TOP => 1hz ICR1 = TIMER_FREQ - 1; // TOGGLE => 1/10th of ICR1 OCR1A = ((TIMER_FREQ / 10) * DUTY_CYCLE); // Start timer with prescaler 256 (CS12) TCCR1B |= _BV(CS12); } int main(void) { timer1_init(); while (1) ; }