Creating global variable and using it in ISR. ( C ) SOLVED


Ive been struggling trying to get my global variable ‘numOverflows’ to be accesable in my ISR. It’s only purpose is to keep track of time. Right now I’m just trying to get it to display in the main(). When i printf() in main, it displays a 0, which makes no sense considering I know that the ISR is being called (thats what the PORTB ^= is for.). I was expecting it to count up as the ISR is called and then overflow. I’m sure this is a simple error but I have already spent hours researching and couldn’t find help, so I do appreciate any.

I’ve included my main.c on the post.


main.c (1.45 KB)

Can't see your code cause I'm on a phone. If it's short you should post it inline with code tags.

Have you made the variable volatile?

Can’t see your code cause I’m on a phone. If it’s short you should post it inline with code tags.

Have you made the variable volatile?

Yes, thats the first thing I did.

anyway, heres the code:

#define F_CPU 16000000
#include <avr/io.h>
#include <stdlib.h>
#include <stdio.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include “uartDriver.h”

volatile int numOverflows = 0;

typedef union
uint8_t LED_0 : 1; // LED 0 state
uint8_t LED_1 : 1; // LED 1 state
uint8_t LED_2 : 1; // LED 2 state, also the game over state
uint8_t Strike : 1; // State for tweezers striking the board
uint8_t Counter_0 : 1; // Bits which count overflows (keep track of game time)
uint8_t Counter_1 : 1; // ^
uint8_t Counter_2 : 1; // ^^
uint8_t Counter_3 : 1; // ^^^

unsigned char full;
} data_field;

int main(void)
// Create File Pointers for in and out
FILE uart_output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
FILE uart_input = FDEV_SETUP_STREAM(NULL, uart_getchar, _FDEV_SETUP_READ);

// Initialize the UART Driver

// Hijack STDIN/STDOUT and send then to the UART Driver
stdout = &uart_output;
stdin = &uart_input;

// Char used for getting info from UART
char input;

DDRD |= 0xFF;

TCCR0B |= (1 << CS02) | (0 << CS01) | (1 << CS00);

TIMSK0 |= (1 << TOIE0) | (1 << OCIE0A);

TCNT0 = 0;


while (1)
printf("%d\n", numOverflows);
PORTD ^= 0xE0;

I figured it out. The compare register was also enabled, and that was messing EVERYTHING up. Thanks for your reply though.

You missed the part where Delta_G asked you to use code tags.

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.