AVRly - AVR Development Resources
digital_clock.c File Reference

Driver to provide setup and configuration of the timer/counter hardware on the ATmega328P. More...

#include <avr/interrupt.h>
#include "digital_clock.h"

Go to the source code of this file.

Macros

#define PRESCALER   64
 
#define UNITS_PER_SECOND   1000
 

Functions

 ISR (TIMER0_COMPA_vect)
 Interrupt Service Routine. More...
 
void init_timer_counter_0 (void)
 Initialisation routine for 8-bit timer 0. More...
 
void set_time (uint8_t hrs, uint8_t mins, uint8_t secs)
 Setter function for time variables. More...
 
char * get_time (void)
 Getter function for time variables. More...
 

Detailed Description

Driver to provide setup and configuration of the timer/counter hardware on the ATmega328P.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Author
Jason Duffy
Date
2nd April 2022
See also

Definition in file digital_clock.c.

Macro Definition Documentation

◆ PRESCALER

#define PRESCALER   64

Definition at line 39 of file digital_clock.c.

◆ UNITS_PER_SECOND

#define UNITS_PER_SECOND   1000

Definition at line 40 of file digital_clock.c.

Function Documentation

◆ ISR()

ISR ( TIMER0_COMPA_vect  )

Interrupt Service Routine.

Definition at line 57 of file digital_clock.c.

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}

◆ init_timer_counter_0()

void init_timer_counter_0 ( void  )

Initialisation routine for 8-bit timer 0.

Definition at line 99 of file digital_clock.c.

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}

◆ set_time()

void set_time ( uint8_t  hrs,
uint8_t  mins,
uint8_t  secs 
)

Setter function for time variables.

Definition at line 124 of file digital_clock.c.

125{
126 cli();
127 hours = hrs;
128 minutes = mins;
129 seconds = secs;
130 sei();
131}

◆ get_time()

char * get_time ( void  )

Getter function for time variables.

Returns current time as a string eg. 22:03:11

Definition at line 138 of file digital_clock.c.

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}