Help with simple code for rookie please
Jim Barnes
Posts: 9
Hi, rookie here, trying to get a simple program running that will read the RC port, and output a code on the RB port based on the input.· I'm using the internal oscillator, and just made a very short pgm to light an LED on RB.0 if RC.0 is sent low, and light RC.0-4 if nothing is sent low.· I have 2.2 k pull up resistors on all eight RC bits.·
I'm sure I'm just doing something silly here.· When I go into debug I can get through one cycle and then the code gets lost somewhere.· Prob my subroutines?· So if the input bit is low, the led will light but the pgm won't make it back to check again to see if it went high.· If high, same thing, will light appropriately the first time through then gets lost.·
I don't need sophistication here, this will ultimately be eight buttons and eight coded outputs. ·If anything, maybe an interrupt routine to stop everything, disable all outputs·and go back to start.·
Thank you for your time.·
Jim
I'm sure I'm just doing something silly here.· When I go into debug I can get through one cycle and then the code gets lost somewhere.· Prob my subroutines?· So if the input bit is low, the led will light but the pgm won't make it back to check again to see if it went high.· If high, same thing, will light appropriately the first time through then gets lost.·
I don't need sophistication here, this will ultimately be eight buttons and eight coded outputs. ·If anything, maybe an interrupt routine to stop everything, disable all outputs·and go back to start.·
Thank you for your time.·
Jim
Comments
Just a couple things went wrong.
First you must have "TURBO, STACKX, OPTIONX" on the device line for SX/B to work properly.
When you have "If inputs = 255 THEN pressed" the compiler assumes you mean "GOTO pressed". So you need to put the "pressed" on a seperate line, and use ENDIF.
You can enable the built-in pullup resistors on port C and you won't need the external ones.
I have attached a modified program showing these changes.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
“The United States is a nation of laws -· poorly written and randomly enforced.” - Frank Zappa
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
You are correct in assessing where your program is going amiss. Your IF... THEN statements are acting as GOTO events. In other words, they cause the program to conditionally jump to a new program location without the expectation of needing to return to the jump point. However, you are expecting your jump locations to act as subroutines and expecting your RETURN statements to take you back to your jump point.
Quick Fix: The fastest fix is the replace your RETURN statements with “GOTO Main”. This should make your program perform as expected.
Advanced Learning: At some point you will likely receive great benefit from understanding how to declare and call subroutines. I recommend reading the relevant subroutine section under the Definitions entry of the help file for information on how to properly declare them. Then read about the GOSUB... RETURN statements to learn how to call and return from them.
Technical Answer: What is actually happening now is that you are trying to use RETURN to cause your program execution to continue from a location you did not instruct your program to save. GOSUB saves the address of what should be the next program instruction into a special memory area so that it can RETURN to it later when so instructed. GOTO (and your implied use of GOTO) does not do this as it is assumed that your program should execute a different code segment as apposed to an additional code segment.
I hope this is helpful.
- Sparks
Thank you so very much!!!!!
Jim
Your program reads eight bits to look at the status of one -- you can in fact look at a single bit on any port and deal with it accordingly.