Created
April 18, 2019 07:08
-
-
Save iosifnicolae2/c654e6904c4a1208cbc538d803184e75 to your computer and use it in GitHub Desktop.
Arduino timer
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
#include <avr/interrupt.h> | |
//COMMON CATHODE | |
// define the way to activate 7 Segment leds | |
byte segment7code[] = { | |
0b00111111, // 0 | |
0b00000110, // 1 | |
0b01011011, // 2 | |
0b01001111, // 3 | |
0b01100110, // 4 | |
0b01101101, // 5 | |
0b01111101, // 6 | |
0b00000111, // 7 | |
0b01111111, // 8 | |
0b01101111 // 9 | |
}; | |
// define the 7 Segment display to be activated to update hours, minutes, seconds | |
byte portB_To_segment7[] = { //seven-segment activation codes | |
0b111110, //from seconds to hours | |
0b111101, | |
0b111011, | |
0b110111, | |
0b101111, | |
0b011111 | |
}; | |
// initial time | |
int timeArray[] = { | |
0, //seconds unit | |
0, //seconds decimal | |
0, //minutes unit | |
0, //minutes decimal | |
0, //hours unit | |
0 //hours decimal | |
}; | |
int counter = 0; //timer counter | |
int dataPin = 5, clockPin = 6, latchPin = 7; //74HC595 wiring | |
// send data to 74HC595 | |
void shiftOutput(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val) | |
{ | |
uint8_t i; | |
for (i = 0; i < 8; i++) { | |
if (bitOrder == LSBFIRST) | |
digitalWrite(dataPin, !!(val & (1 << i))); | |
else | |
digitalWrite(dataPin, !!(val & (1 << (7 - i)))); | |
digitalWrite(clockPin, HIGH); | |
digitalWrite(clockPin, LOW); | |
} | |
} | |
ISR(TIMER1_COMPA_vect) | |
{ | |
/* | |
This method is executed when timer1 match the value stored in OCR0A | |
*/ | |
SREG &= (0 << SREG_I); | |
counter++; | |
if(counter>=62) | |
{ | |
counter = 0; | |
timeArray[0]++; //adds 1 second | |
} | |
if (timeArray[0] > 9) //checks second units | |
{ | |
timeArray[0] = 0; | |
timeArray[1]++; | |
if (timeArray[1] > 5) //checks second decimals | |
{ | |
timeArray[1] = 0; | |
timeArray[2]++; | |
if (timeArray[2] > 9) //checks minute units | |
{ | |
timeArray[2] = 0; | |
timeArray[3]++; | |
if (timeArray[3] > 5) //checks minute decimals | |
{ | |
timeArray[3] = 0; | |
timeArray[4]++; | |
if (timeArray[4] > 9) //checks hour units | |
{ | |
timeArray[4] = 0; | |
timeArray[5]++; | |
} | |
if(timeArray[5] == 2 && timeArray[4] == 4) //checks for new day | |
{ | |
timeArray[4] = 0; | |
timeArray[5] = 0; | |
} | |
} | |
} | |
} | |
} | |
SREG |= (1 << SREG_I); | |
} | |
void setup() | |
{ | |
DDRB = 0xFF; // 0b11111111 | |
DDRD = 0xFF; // 0b11111111 | |
PORTB = 0xFF; // 0b11111111 | |
PORTD = 0x00;// 0b00000000 | |
//16 bit timer configuration attempt | |
SREG |= (1 << SREG_I); // enable global interrupt | |
TCCR1A = (1 << WGM11) | (0 << WGM10); // PWM, phase correct, 9-bit - incrementeaza si apoi decrementeaza la 0 | |
OCR1A = 0xFA00; // 64000 out of 65536 | |
TIMSK1 |= (1 << OCIE1A); // enable timer 1 | |
sei(); // enable global interrupt, wait, why? | |
TCCR1B = (1 << CS12 ) | (0 << CS11 ) | (0 << CS10 ) ; // prescale with 256 | |
} | |
void loop() | |
{ | |
for (int i=0; i<=5; i++) | |
{ | |
PORTD = (0 << latchPin); | |
shiftOutput(dataPin, clockPin, MSBFIRST, segment7code[timeArray[i]]); | |
PORTB = portB_To_segment7[i]; // define which display to update | |
PORTD = (1 << latchPin); // "submit" the changes to 74HC595 | |
delay(4); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment