C code; compiler problems


(Andrew Wheatley) #1

Hi all,

If anyone knows C I could use a hand. I’m using a USART which needs to have an interrupt enabled so that when a packet is received it is taken to the correct memory buffer without the user program needing to call anything or poll. I previously bodged this working by adding extern variables in main.c, then creating them in the interrupt file:


I’m not a big fan of the bodge and need to create a single memory location that both the interrupt code and the user code can access. How can I do this?


(Filip) #2

Hey - wouldn’t it be possible to put the extern declaration in a header file which you then #include from your main.c?


(Andrea Campanella) #3

Create the buffer in your interrupt file, every time the interrupt read data from the UART it does it store in there.

Create a function called something like buffer_read() into your interrupt file.
import the interrupt header in main.c and call the function buffer_read() when you need to read the buffer.

The best approach would be to have a different library that hold the buffer and exposes functions like read(), write(), delete() etc etc and both interrupt and main use the function to access to the bus.


(Andrew Wheatley) #4

Thanks for the help Andrea.
You’re right about the best approach using get() and put(), I can’t believe I didn’t think about that. I did what you said and created an extra header and source file, and the code compiled no problem. However, the compiler appears to be using the data buffer to store intermediate calculation numbers; even when I have the transmitter off, the receiver shows up as having data:

image

The receive should be all 0’s because at this point the transmitter is off.

Files, for explanation:
Header:

Source:

Interrupt File:

Main:

Any ideas or help appreciated. Thanks!


(Andrea Campanella) #5

Maybe you need to flush the buffer?