I have a very simple program which is giving me a headache.
Some background: I am programing an arduino Uno "Atmega328P" in atmelstudio 6. I am using a JTAGICE mkII in debugwire mode to program and debug. I have also used this method to use the arduino libraries to make things easier.
I wrote a simple program to just count transitions on one of the external interrupt pins. In my case im using INT0_vect. The value im pointing to, just increments for every transition.
The problem is using a global pointer in an ISR. You may be thinking that I forgot to add the volatile keyword but you would be wrong.
The ISR is serviced for every transition but wouldn't change the value. I couldnt figure out why so I stepped through the program and found out that my pointer points to R00, one of the working registers. This should be fine but before the ISR is serviced R00 is pushed to the stack R00 is incremented and the previous value is restored.
I have no idea why this would be the case. As I said I am using a "volatile uint8_t*" which should be a pointer to a volatile uint8_t.
This is what the code looks like:
#include "Arduino.h"
void setup();
void loop();
void update();
volatile uint8_t* Count;
void setup()
{
*Count = 0;
attachInterrupt(0,update, CHANGE); // makes ISR call update
}
void loop()
{
// delay(1000);
}
void update()
{
(*Count)++;
}
The main loop calls setup once then loop repeatedly.
attach interrupt is a macro which sets up the interrupt then effectively creates this:
`ISR(INT0_vect){update();}`
Some additional info:
- I am using -Os for both the c++ and c compilers.
- avr-gcc Full Options: -funsigned-char -funsigned-bitfields -DF_CPU=16000000UL -I"../../../../arduino-1.0.5/hardware/arduino/cores/arduino" -I"../../../../arduino-1.0.5/hardware/arduino/variants/standard" -Os -fdata-sections -ffunction-sections -fpack-struct -fshort-enums -Wall -c -std=gnu99 -fno-exceptions -MD -MP -MF "$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -MT"$(@:%.o=%.o)" -mmcu=atmega328p
- avr-g++ Full Options: -funsigned-char -funsigned-bitfields -DF_CPU=16000000UL -I"../../../../arduino-1.0.5/hardware/arduino/cores/arduino" -I"../../../../arduino-1.0.5/hardware/arduino/variants/standard" -Os -fdata-sections -ffunction-sections -fpack-struct -fshort-enums -Wall -c -fno-exceptions -MD -MP -MF "$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -MT"$(@:%.o=%.o)" -mmcu=atmega328p
- avr-g++ Linker Options: -Wl,-Map="$(OutputFileName).map" -Wl,--start-group -Wl,-lm -Wl,-lcore -Wl,--end-group -Wl,-L"../../../ArduinoCore" -Wl,--gc-sections -mmcu=atmega328p
Countpointer, what's it pointing at? And should it be a pointer at all? - Mark Ransom