pomodoro-avr

Pomodoro timer на базе Attiny45
git clone git://git.konyahin.xyz/pomodoro-avr
Log | Files | Refs | LICENSE

timer.c (1605B)


      1 /*
      2  * avr pomodoro timer
      3  * © 2022 Anton Konyahin <me@konyahin.xyz>
      4  *
      5  * This program is free software: you can redistribute it and/or modify
      6  * it under the terms of the GNU General Public License as published by
      7  * the Free Software Foundation, either version 3 of the License, or
      8  * (at your option) any later version.
      9  *
     10  * This program is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  * GNU General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU General Public License
     16  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
     17 */
     18 
     19 #include <avr/io.h>
     20 #include <avr/interrupt.h>
     21 #include <stddef.h>
     22 
     23 #include "timer.h"
     24 
     25 // should be 60, but I calibrate it for my needs
     26 #define SEC 57
     27 
     28 static uint8_t minutes = 0;
     29 static uint8_t seconds = 0;
     30 static void (*callback) (void) = NULL;
     31 
     32 ISR (TIMER1_COMPA_vect)
     33 {
     34     if (seconds > 0)
     35     {
     36         --seconds;
     37         return;
     38     }
     39     else
     40     {
     41         seconds = SEC;
     42         --minutes;
     43     }
     44     if (minutes == 0 && callback != NULL)
     45         callback();
     46 }
     47 
     48 void
     49 init_timer(void)
     50 {
     51     cli();
     52     // value for compare
     53     OCR1A = 244;
     54     // enable compare unterrupt
     55     TIMSK |= (1 << OCIE1A);
     56     // CTC1 start comparing
     57     // CS13 CS12 CS10 - CK/4096 prescaling
     58     TCCR1 |= (1 << CTC1) | (1 << CS13) | (1 << CS12) | (1 << CS10);
     59     sei();
     60 }
     61 
     62 void
     63 set_timer_callback(uint8_t min, void (*call) (void))
     64 {
     65     minutes = min;
     66     callback = call;
     67     seconds = SEC;
     68 }