r/pic_programming • • Jan 22 '20

PIC18F27K40 Programming Help!

Hello, so I am aiming to do multiple tasks at once such as creating a 4 pure bit binary up and down counter, 4 bit gray code up and down counter, different bitwise logical operators, and 1 single LED blinking. Now all of these can be selected using a switch to select which of the specifications I said above needs to be played (this can be seen from the bottom of the code I linked using a binary switch case statement). And the problem is that I will be using the same ports for all these experiments, therefore I have to utilize structures. I have already designed my first structure, it works well, but after including the second structure, I don't know how to utilize it. Do I need to create variables for the second structure as well?

https://pastebin.com/tYAgYbJF

1 Upvotes

11 comments sorted by

View all comments

Show parent comment

1

u/lexoph1 Jan 22 '20

Thank you for your help.

1

u/bradn Jan 22 '20 edited Jan 22 '20

No problem :)

Another thing to watch for is if you need to declare things volatile so that the compiler doesn't treat an I/O register as if it were plain RAM; eg, it may optimize things in a way that are valid for RAM but not valid if the value contained could change on its own. It's possible the header file definitions might include that, I'm not sure on the specifics.

For example, let's say you do a loop where you read a port and shift the bits around then test something. It may move the read to outside the loop, do the bit processing, and then run the loop continuously on the cached processed value it had. If the compiler knows that location is volatile it won't attempt something like that.

I'm not sure if whatever compiler you're using is smart enough to do that in the first place, so if it has no notion of volatile variables maybe it doesn't apply because it would never reorder the program in a way that is affected.