State Variables / Interrupt

Status
Not open for further replies.

wuchy143

Member
Hi,

I'm learning how to program pics(PIC12F675) and am trying to simply make a software UART thought it's really just a UAT because I only need to be able to transmit data to my computer.(i have a TTL to RS232 converter I will hook it up to b/t my computer and my PIC)

My mentor said that using a state variable and interrupts should be used to TX my data because the smaller pics have a tiny stack.

Can someone explain state variables and how I can use them in my context here. I'm just not getting it and it's been tough finding info on them. Or if you know of a good place online to read about them that would be great!!

Thanks

-mike
 
oops. Sorry about that. I'm using C and Microchip's High Tech C Lite compiler...cause it's free
 
No problem, it seems I was splitting hairs anyway. I guess state variable the term used for the variable that maintains the state in a state machine. The last post on this page explains the problem well RS232 In C On 12F675

I hope I didn't add confusion as I've managed to do in the past.
 
no worries. this stuff isn't easy. makes it all worth while tho. Really appreciate the help. Let me check out what you sent me.
 
- Be careful here. The best practice regarding firmware is to have a state variable within the interrupt subroutine. This means, you have a variable that you assign a value i.e. state = PUT_DATA_IN_PORT Then you exit the interrupt subroutine and use a switch/case statement on another part of your code and execute whatever you want; then, you change the value of the state variable so it does something else... like state = SHIFT_BITS to prepare the bit to be send, then state = PUT_DATA and, finally, you may want to set state = SLEEP to send the PIC to sleep until the next Timer1 interrupt. This is just an oversimplified explanation of course.
- This is the best practice since small pics have small stacks and you don't want to call subroutines within the interrupt subroutine; in addition, what will it happen if you have another interruption that goes of while you are within a subroutine that is already within the interrupt subroutine ??? I mean, it gets complicated and can disrupt your program.




I assume the above is what you are talking about. What do they mean by having a state variable within the interrupt subroutine? It just like he's skipping a bunch of steps....though it seems like you are agreeing on what he's saying so I must be being a little thick. Any thoughts?
 
That is the post I was referring to.


Not sure I'll do much better but here I go...

Let's say there are three things that have to happen in a specific order to send a byte of data. To initiate a transmission you must create a start condition. You do so and set the state variable to step 2 because step one has been completed (set the variable with an integer, string, etc., your choice, just something to remember where you've been). later in your interrupt routine you check the variable and it's set for step 2. For step two you need to send the data. The data is sent and the state variable is set to step 3. When you re-enter the interrupt you look and see you're at step three, the final step. Now to finish the transmission you need to create a stop condition. The stop condition occurs and the state variable is cleared for the next use.

The variable maintains state, where you are in a routine/function/loop/switch. It's like a bookmark. If you can only read for a specific time per sitting but need to remember where you were, you flag your spot/state. When you sit back down to read again you can proceed right where you left off without forgetting whether you needed to read a page, flip the page or close the book.

I can only hope I didn't make things worse.
 
Last edited:
. Good example with the bookmark. That made it click. Thank you very much. uve been extremely helpfull. Now its bedtime for me and ill start coding in the am.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…