AVRly - AVR Development Resources
digital_clock.c
Go to the documentation of this file.
1/******************************************************************************
2 @copyright Copyright © 2022 by Jason Duffy
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21******************************************************************************/
22
23/**
24 * @file digital_clock.c
25 * @ingroup digital_clock
26 * @author Jason Duffy
27 * @date 2nd April 2022
28 * @brief Driver to provide setup and configuration of the timer/counter
29 * hardware on the ATmega328P.
30 * @details
31 * @bug No known bugs.
32 * @see
33 */
34
35#include <avr/interrupt.h>
36
37#include "digital_clock.h"
38
39#define PRESCALER 64
40#define UNITS_PER_SECOND 1000
41
42/**
43 * File scope variables for digital clock functionality.
44 * Must be declared volatile if used in ISR.
45 */
46static volatile uint16_t milliseconds = 0;
47static volatile uint8_t seconds = 0;
48static volatile uint8_t minutes = 31;
49static volatile uint8_t hours = 22;
50
51static char msg[9] = {0};
52
53
54/**
55 * Interrupt Service Routine.
56 */
57ISR(TIMER0_COMPA_vect)
58{
59 if (milliseconds < 999)
60 {
61 ++milliseconds;
62 }
63 else
64 {
65 milliseconds = 0;
66
67 if (seconds < 59)
68 {
69 ++seconds;
70 }
71 else
72 {
73 seconds = 0;
74
75 if (minutes < 59)
76 {
77 ++minutes;
78 }
79 else
80 {
81 minutes = 0;
82
83 if (hours < 23)
84 {
85 ++hours;
86 }
87 else
88 {
89 hours = 0;
90 }
91 }
92 }
93 }
94}
95
96/*
97 * Initialisation routine for 8-bit timer 0.
98 */
100{
101 /*
102 cli();
103 OCR0A = 125; // Set initial compare value A.
104
105 TCCR0A |= (1 << WGM01); // CTC mode, OCR0A is TOP
106 TCCR0B |= (1 << CS00) | (1 << CS01); // start timer with 64 prescaler
107 TIMSK0 |= (1 << OCIE0A); // enable output compare interrupt for OCR4A
108 sei();
109 */
110
111 cli();
112 OCR0A = (F_CPU / PRESCALER / UNITS_PER_SECOND);
113 TCCR0A |= (1 << WGM01); // CTC mode, OCR0A is TOP
114 TCCR0B |= (1 << CS00) | (1 << CS01); // start timer with 64 prescaler
115 TIMSK0 |= (1 << OCIE0A); // enable output compare interrupt for OCR4A
116 sei();
117
118}
119
120
121/*
122 * Setter function for time variables.
123 */
124void set_time(uint8_t hrs, uint8_t mins, uint8_t secs)
125{
126 cli();
127 hours = hrs;
128 minutes = mins;
129 seconds = secs;
130 sei();
131}
132
133
134/*
135 * Getter function for time variables.
136 * Returns current time as a string eg. 22:03:11
137 */
138char* get_time(void)
139{
140 // Store hours as a string.
141 msg[0] = '0' + (hours / 10); // 10's
142 msg[1] = '0' + (hours % 10); // 1's
143
144 msg[2] = ':';
145
146 // Store minutes as a string.
147 msg[3] = '0' + (minutes / 10); // 10's
148 msg[4] = '0' + (minutes % 10); // 1's
149
150 msg[5] = ':';
151
152 // Store seconds as a string.
153 msg[6] = '0' + (seconds / 10); // 10's
154 msg[7] = '0' + (seconds % 10); // 1's
155
156 return msg;
157}
158
159
160/*** end of file ***/
char * get_time(void)
Getter function for time variables.
void set_time(uint8_t hrs, uint8_t mins, uint8_t secs)
Setter function for time variables.
void init_timer_counter_0(void)
Initialisation routine for 8-bit timer 0.
Definition: digital_clock.c:99
ISR(TIMER0_COMPA_vect)
Interrupt Service Routine.
Definition: digital_clock.c:57
Driver to provide setup and configuration of the timer/counter hardware on the ATmega328P.