AVRly - AVR Development Resources
digital_clock.h File Reference

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

#include <stdint.h>

Go to the source code of this file.

Functions

void init_timer_counter_0 (void)
 Initialisation routine for 8-bit timer 0. More...
 
void set_time (uint8_t hours, 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.h.

Function Documentation

◆ 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  hours,
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}