Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

MPLab variables

Status
Not open for further replies.

danuke

New 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

and the Cu command is a macro containing:
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

I'm stuck as to how I can accomplish the if statement using a method that doesn't reset upon the goto command at the end of the time sequence. I would use a table for the project, but it will be a different number of commands for each loop that I want carried out, and the only way I could fill them would be nop's -- which I feel would be a waste of memory for a long table.

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
 
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.
 
Nigel Goodwin said:
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.

I'm not trying to use the if or CN+1 as a runtime command. Correct me if I misunderstand this, but the if command works sort of like a macro. When the statement is true for the if command, it will replace the series of if commands with the statements inside the true if command while not including the other that are not true. Is that correct?

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

blueroomelectronics said:
"goto PC+W"
W is loaded with the jump location offset is often popular. Terminate with a "retlw"
How large is the table?


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.
 
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
 
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.
 
danuke said:
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.
It appears that you're kind of trying to make assembler into something it's not. It's not a high level language. There are no IF or WHILE statements. But you can make your own very easily.

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
The number is the start address of the block of RAM where you're putting those d1, d2 and d3 variables. They're one byte each. This definition goes in your code before the main program and ISR orgs (outside program memory).

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.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top