Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save iosifnicolae2/31e4e2f9942f18fed6fe35844bf75e73 to your computer and use it in GitHub Desktop.
Save iosifnicolae2/31e4e2f9942f18fed6fe35844bf75e73 to your computer and use it in GitHub Desktop.
Arduino timer
#include <avr/interrupt.h>
//COMMON CATHODE
byte segment7code[] = {
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111 // 9
};
byte portB_To_segment7[] = { //seven-segment activation codes
0b111110, //from seconds to hours
0b111101,
0b111011,
0b110111,
0b101111,
0b011111
};
/*
//COMMON ANODE
byte segment7code[] = {
0b11000000, // 0
0b11111001, // 1
0b10100100, // 2
0b10110000, // 3
0b01100110, // 4
0b10010010, // 5
0b10000010, // 6
0b11111000, // 7
0b10000000, // 8
0b10010000 // 9
};
byte portB_To_segment7[] = { //seven-segment activation codes
0b000001, //from seconds to hours
0b000010,
0b000100,
0b001000,
0b010000,
0b100000
};
*/
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
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(TIMER0_COMPA_vect)
{
SREG &= (0 << SREG_I);
counter++;
if(counter>=250)
{
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);
}
*/
ISR(TIMER1_COMPA_vect)
{
/*
Aceasta metoda se executa cand acumulatorul din timer-ul 1 este egal cu valoarea din 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
/*
//8 bit timer configuration
SREG |= (1 << SREG_I);
TCCR0A = (1 << WGM01) | (0 << WGM00);
OCR0A = 0xF9;
TIMSK0 |= (1 << OCIE0A);
sei();
TCCR0B = (1 << CS02 ) | (0 << CS01 ) | (0 << CS00 ) ;
*/
//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);
// trimit configuratia bitilor de iesire a lui 74hc595
shiftOutput(dataPin, clockPin, MSBFIRST, segment7code[timeArray[i]]);
PORTB = portB_To_segment7[i]; // controleaza display-ul updated
PORTD = (1 << latchPin); // submit modificarea la latch-ul din 74hc595
delay(4);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment