![]() |
![]() |
![]() |
|
|
|||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
|
|
Thread Tools | Display Modes |
|
|
(permalink) |
|
Experienced Member
|
I'm trying to write a program that will keep a count going using variables, but have discovered something about my code causes the variable to return to the original set value when a jump occurs. I'm using a variable because I couldn't get the if statements to work with a memory location variable.
The loop part of my code looks like: Code:
NXTCue ;Start the show call Time ; Get delay time TDelay ; Delay for time called for in Time table Cu ; Set pins to advance firing movlw d'5' ; Hold the pin high for TDelay ; WREG x 100ms clrout ; Clear Outputs CN set CN+1 ; increment Cue number by 1 goto NXTCue ; Return to start and do it again Code:
Cu MACRO if CN == 1 bsf S1 bsf S3 endif if CN == 2 bsf S7 bsf S18 endif if CN == 3 bsf S9 endif endm Does anybody have any suggestions how how I can get such code to work without being a HUGE waste of memory in the PIC? Thanks, Joe |
|
|
|
|
|
(permalink) |
|
Super Moderator
|
I feel you're confusing assembler instructions and PIC instructions, IF and CN+1 are instructions to the assembler at assemble time, and have no effect at run time.
|
|
|
|
|
|
(permalink) |
|
Experienced Member
|
"goto PC+W"
W is loaded with the jump location offset is often popular. Terminate with a "retlw" How large is the table? |
|
|
|
|
|
(permalink) | ||
|
Experienced Member
|
Quote:
If so, it's acting as intended for that part of the program. I'm having trouble with the CN variable. I'm using the CN variable inside the assembler to dictate which if command is true. I tested it before putting it in the loop as just a series of Cu commands and it ran through all the series without any trouble. When I put it inside the loop it resets to the original value set at the beginning of the code where I was declaring memory location variables. I added the code below and found it is incrementing fine and then resets after the goto statement. Code:
NXTCue ;Start the show movlw CN movwf TEMP call Time ; Get delay time TDelay ; Delay for time called for in Time table Cu ; Set pins to advance firing movlw d'5' ; Hold the pin high for TDelay ; WREG x 100ms clrout ; Clear Outputs CN set CN+1 ; increment Cue number by 1 movlw CN movwf TEMP goto NXTCue ; Return to start and do it again Quote:
Are you saying to replace the " goto NXTCue ; Return to start and do it again" line with the "goto PC+W"? The table may be as short as 30 table locations or as long as 300 locations. Each containing anywhere from 1 bsf command to 15 bsf commands. |
||
|
|
|
|
|
(permalink) |
|
Experienced Member
|
Nigel is right, you are confused about macro variables.
The value of CN is always zero at NXTCue and is always 1 after the "CN set CN+1". CN does not get incremented at run time. Mike |
|
|
|
|
|
(permalink) |
|
Experienced Member
|
After taking a break from it, it's sinking in more why it performs the way it does at run time. Is there a way to use a memory variable with "if" statements or something similar? I'm trying to keep from using a table if possible because of the variance of how many instructions are performed on each call/command. I may end up being better off to setup each command in a macro and call them from the table like that.
I also played with the while statement a little bit and never could get it to follow the count, but that was earlier. I haven't really put much thought into how this works since I stepped away from the project for a while. Thanks again for the help. Everyones responses make it clearer how the PICs work with the assembler. |
|
|
|
|
|
(permalink) | |
|
Experienced Member
|
Quote:
Look in the datasheet for your PIC at the Instruction Set Summary. Almost every instruction with an F in it can be applied to a variable. Those are the commands you use. There are lots of tests and compares and conditional jumps you can combine in a myriad of different ways to accomplish what you want to do. Use a cblock to define your variables: Code:
cblock 0x20 d1,d2,d3 endc If you're just starting out, leave macros alone for now. Learn the basics first. If you're having trouble grasping the low level-ness of assembler (it's not terribly difficult - keep trying), you could switch to C. There are several good free C compilers available. All your favorite higher level instructions are there. I recommend SourceBoost BoostC. A good useable demo, and when you hit the limit on that, a full license is around $75. Good compiler.
__________________
========================= Futz's Microcontrollers & Robotics ========================= Last edited by futz; 14th May 2008 at 07:22 PM. |
|
|
|
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Latest |
| Help with ICD2 clone!!!!!!!!!!!! | juanbbv | Micro Controllers | 17 | 5th April 2008 05:07 AM |
| Assembly Variables for PICs! | Peter_wadley | Micro Controllers | 9 | 22nd June 2007 05:04 AM |
| Debugging 877A with MPLAB | williB | Micro Controllers | 10 | 7th May 2007 03:53 PM |
| Need help badly on Inchworm and MPLAB | thushy | Micro Controllers | 14 | 11th March 2007 06:05 PM |
| Inchworm project started | williB | Micro Controllers | 69 | 5th March 2007 06:55 PM |