Created
June 16, 2016 05:55
-
-
Save andreydung/b3a58dcb9022b9230d8e183c593517d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Clock.c | |
* | |
* Created: 5/25/2016 1:01:13 AM | |
* Author : andrey | |
*/ | |
#define F_CPU 1000000UL | |
#include <avr/io.h> | |
#include <util/delay.h> | |
#include <avr/interrupt.h> | |
#define TRUE 1 | |
#define FALSE 0 | |
typedef unsigned char bool; | |
#define Ndigits 4 // only work with power of two | |
static unsigned char mode = 0; | |
volatile unsigned char digits[4]; | |
const unsigned char seg7[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F}; | |
volatile unsigned char button_disable; | |
static unsigned char index = 0; | |
void setupTimer1() { | |
//setup TIMER1 | |
TCCR1A |= 0; //CTC mode | |
TCCR1B |= (1<<WGM12)|(1<<CS12)|(1<<CS10); // prescaler 1024 | |
OCR1A = F_CPU/1024; // compare value of CTC mode | |
TIMSK|=(1<<OCIE1A); //enable interrupt | |
} | |
void setupTimer0() { | |
TCCR0 |= (1 << CS01); | |
TCNT0 = 0; | |
TIMSK |= (1 << TOIE0); | |
} | |
bool increaseMinute() { | |
digits[3] ++; | |
if (digits[3] > 9) { | |
digits[3] = 0; | |
digits[2] ++; | |
if (digits[2] == 6) { | |
digits[2] = 0; | |
return TRUE; | |
} | |
} | |
return FALSE; | |
} | |
void increaseHour() { | |
digits[3] = 0; | |
digits[2] = 0; | |
digits[1] ++; | |
if (digits[1] > 9) { | |
digits[1] = 0; | |
digits[0] ++; | |
} | |
if ((digits[0]*10 + digits[1]) > 12) { | |
digits[0] = 0; | |
digits[1] = 1; | |
} | |
} | |
int main(void) | |
{ | |
PORTB = 1 << 2; //pull up activated at PB2 | |
PORTD = 0b00001100; | |
GICR = (1<<INT0)|(1<<INT1)|(1<<INT2); //external interrupt 0, 1 and 2 | |
setupTimer1(); | |
setupTimer0(); | |
sei(); | |
digits[0] = 1; | |
digits[1] = 1; | |
digits[2] = 5; | |
digits[3] = 5; | |
DDRC = 0xFF; | |
DDRA = 0xFF; // Port A for the multiplex | |
mode = 0; | |
index = 0; | |
while(1) {}; | |
return 0; | |
} | |
ISR(TIMER0_OVF_vect) { | |
if (button_disable > 0) | |
button_disable --; | |
index ++; | |
index &= (Ndigits - 1); | |
PORTA = 1 << index; | |
PORTC = seg7[digits[index]]; | |
} | |
ISR(INT0_vect) { | |
if (button_disable == 0) { | |
if (mode == 1) { | |
increaseHour(); | |
} | |
button_disable = 1; | |
} | |
} | |
ISR(INT1_vect) { | |
if (button_disable == 0) { | |
if (mode == 1) { | |
increaseMinute(); | |
} | |
button_disable = 1; | |
} | |
} | |
ISR(INT2_vect) { | |
mode ++; | |
mode &= 1; | |
} | |
ISR(TIMER1_COMPA_vect) { | |
if (mode == 0) { | |
if (increaseMinute()) { | |
increaseHour(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment