I need help with the following question please. Thank you Here is a program to c
ID: 3918194 • Letter: I
Question
I need help with the following question please.
Thank you
Here is a program to count fairly frequent (could be up to 100 times a second) pulses on the input named INTO (the input change pin, same one as PD2) and display the count every 2 s on the Atmel ATmega328P CPU. The interrupt service counts the number of pulses. The main routine reads this count and displays it on an LCD. Assume that the code for the functions lcd.display () and delay ms) are linked into the program and work correctly. Also assume that the values written to EICRA and EIMSK I/O registers are correct and have the desired effect of turning on the INT0 interrupts. Identify significant problems with this code (there are a few; one will lead to compile errors but others will only exhibit problems when the program is run) unsigned int count int main(void) /* Activate interrupt on detected rising edge on input INTO* EIMSK (1Explanation / Answer
In this particular case we have a shared variable between an ISR and
a mainline C-program with single threading
We need to use volatile keyword with the declaration as follows:
unsigned int volatile count
The main point of using volatile keyword is to prevent compiler from generating a code that uses CPU registers as faster ways to represent variables. This ensures that compiled code to access the exact memory location in RAM on every access to the variable to get the latest value of it which may have been changed by another entity. By adding volatile we ensure that our code is aware of any change made to a variable by anyone else like hardware or ISR and no coherency issue happens.
If volatile keyword is not present, compiler tries to generate faster code by reading the content of variable from RAM into a CPU register once and use that cached value in a loop or function. Accessing RAM could be tens of times slower than accessing the CPU register.