Hi All,
I'm confused about the relationship between timer 2 and millis() in Arduino-0021. I am trying to use timer 2 for an overflow interrupt but as soon as I enable the timer, the millis() function stops working. I thought that millis() just used timer0... at least this is what it looks like from reading wiring.c
I am using an Atmega 328p.
The code is below. Any advice is greatly appreciated.
/* First disable the timer overflow interrupt while we're configuring */
TIMSK2 &= ~(1<<TOIE2);
/* Configure timer2 in normal mode (pure counting, no PWM etc.) */
TCCR2A &= ~((1<<WGM21) | (1<<WGM20));
TCCR2B &= ~(1<<WGM22);
/* Select clock source: internal I/O clock */
ASSR &= ~(1<<AS2);
/* Disable Compare Match A interrupt enable (only want overflow) */
TIMSK2 &= ~(1<<OCIE2A);
/* Now configure the prescaler to none */
TCCR2B |= (1<<CS20); // Set bit
TCCR2B &= ~(1<<CS21); // Clear bit
TCCR2B &= ~(1<<CS22); // Clear bit
/* Finally load end enable the timer */
TIMSK2 |= (1<<TOIE2);
ISR(TIMER2_OVF_vect) {
/* Reload the timer */
TCNT2 = 255;
digitalWrite(8, toggle == 0 ? HIGH : LOW); //test output
toggle = ~toggle; //test output
timer_clicks = (timer_clicks + 1) & 0x1F; //circular to 32
post_frames();
}
void post_frames(void)
{
for(char row = 0 ; row < 8; row++)shift_out_line(row); //Send all 8 rows of colors to the Matrix
}
void shift_out_line(volatile uint8_t row_num)
{
cbi(PORTC, LATCH); //Disable the shift registers
//Send Red Values
for(uint8_t LED = row_num*8 ; LED < (row_num*8)+8 ; LED++) //Step through bits
{
cbi(PORTC, CLK); //Lower the shift register clock so we can configure the data
//Compare the current color value to timer_clicks to Pulse Width Modulate the LED to create the designated brightness
if(timer_clicks < red_frame[LED])
sbi(PORTC, DATA);
else
cbi(PORTC, DATA);
sbi(PORTC, CLK); //Raise the shift register clock to lock in the data
}
//Send Blue Values
for(uint8_t LED = row_num*8 ; LED < (row_num*8)+8 ; LED++) //Step through bits
{
cbi(PORTC, CLK); //Lower the shift register clock so we can configure the data
//Compare the current color value to timer_clicks to Pulse Width Modulate the LED to create the designated brightness
if(timer_clicks < blue_frame[LED])
sbi(PORTC, DATA);
else
cbi(PORTC, DATA);
sbi(PORTC, CLK); //Raise the shift register clock to lock in the data
}
//Send Green Values
for(uint8_t i = row_num*8 ; i < (row_num*8)+8 ; i++) //Step through bits
{
cbi(PORTC, CLK); //Lower the shift register clock so we can configure the data
//Compare the current color value to timer_clicks to Pulse Width Modulate the LED to create the designated brightness
if(timer_clicks < green_frame[i])
sbi(PORTC, DATA);
else
cbi(PORTC, DATA);
sbi(PORTC, CLK); //Raise the shift register clock to lock in the data
}
sbi(PORTC, EN); //Disable the Shift Register Outputs
sbi(PORTC, LATCH); //Put the new data onto the outputs of the shift register
PORTD = (1<<row_num); //Sink current through row (Turns colors 'ON' for the given row. Keep in mind that we can only display to 1 row at a time.)
cbi(PORTC, EN); //Enable the Shift Register Outputs
}
The last line of this breaks millis(). What is going on?
Thanks!