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.

Registers add up, but don't transfer value.

Status
Not open for further replies.

RobertD

New Member
Hi folks, still working on my program here, and slowly making progress. But I have a problem that should not be a problem. I add up five registers through the W register, and then want to move the result from W to LEDON register, but the value doesn't move over. These are simple defined registers, in cbloc, all of them, so I don't know what the problem is.

The same thing happens when I want to turn a bit on PORTA, I put a value into W and move it to PORTA but nothing happens, the value doesn't move to PORTA to turn the pin on.

Code:
	addwf	W0,0
	addwf 	W1,0
	addwf	W2,0
	addwf	W3,0
	addwf	W4,0
	addwf	W5,0	
	movwf	LEDON
 
Last edited:
RobertD said:
Hi folks, still working on my program here, and slowly making progress. But I have a problem that should not be a problem. I add up five registers through the W register, and then want to move the result from W to LEDON register, but the value doesn't move over. These are simple defined registers, in cbloc, all of them, so I don't know what the problem is.
What values do you store into W1-5? Can those values cause an overflow of the W register? If that's the case, an 'unexpected' value could be moved to LEDON after the five additions.
 
Last edited:
That's a good point, the registers can add up to more than 255. But not always, so I must change the code for that. But in this case the value all added up were about 131 or so. But no matter what the value is, it just doesn't move over.
Is there a reason why the code doesn't work?


Now I have to figure out how to make the code for a two byte number.
 
Last edited:
The same thing happens when I want to turn a bit on PORTA, I put a value into W and move it to PORTA but nothing happens, the value doesn't move to PORTA to turn the pin on.

RobertD said:
But in this case the value all added up were about 131 or so. But no matter what the value is, it just doesn't move over.

That's strange. Is PORTA configured properly as an output port by the TRISA register?
 
Yes it is, all output, I'm trying to set individual bits instead.
Funny thing, when I reset the chip, the LEDON register showed the correct value. Maybe I should not be in debug mode or something.

i should learn to work in sim mode maybe, and go to debug only when I'm through programming. I have yet to learn how to do that.
 
Last edited:
Another thing, the LEDON register doesn't decrease when I go through a decrease routine. At least not on the MPLAB register watch.

Now I have to make provisions for a two byte number in the addup routine, how do I do that?
 
Firstly, there are a couple of things to make your code easier for you to read and less likely to cause problems in the future.

1) Use "W" or "F" as the destination for instructions. They mean the same as 0 and 1 in this case but it makes it easier to see what is going on.

2)Don't use W0, W1 etc as register names, as they have specific meanings on the 24F series PICs. Use more meaningful names like temperature1, voltage3 etc.

If you want to add 5 numbers, do you should add 5 numbers not 7

Code:
	addwf	W0,0
	addwf 	W1,0
	addwf	W2,0
	addwf	W3,0
	addwf	W4,0
	addwf	W5,0	
	movwf	LEDON

This adds the wreg to W0, W1, W2, W3, W4 and W5. That is 7 numbers. The first line adds in whatever is in the working register at the start.

If you want to add W0, W1, W2, W3, and W4 (which are 5 numbers) you should use:-
Code:
	movf	W0, w
	addwf 	W1, w
	addwf	W2, w
	addwf	W3, w
	addwf	W4, w
	movwf	LEDON


That will roll over if the sum of W0:W4 is more than 255
If you want to keep the total, you could use something like:-

Code:
	clrf	top_byte_reg
	movf	w0, w

	addwf	w1, w
	btfsc	status, C
	incf	top_byte_reg, f

	addwf	w2, w
	btfsc	status, C
	incf	top_byte_reg, f

	addwf	w3, w
	btfsc	status, C
	incf	top_byte_reg, f

	addwf	w4, w
	btfsc	status, C
	incf	top_byte_reg, f

	movwf	bottom_byte_reg

On each addition there can be an overflow. The carry bit is set if that happens, and the number of times that happens is added up in top_byte_reg

There are loads of routines in:-

**broken link removed**
 
Last edited:
Indeed, thanks, I will modify the code from the W register to the F register, reduce by two lines of code.

To get the PORTA bits up, I'll just have to go to square one, and write a program to make these bits to work all by themselves and then try to get these to work. I had it working a couple of times, but as I diddled with the program they stopped working. I figure I'll get to them when I get there, and I do once in a while. I'm debugging the program from the top down, and I'm getting near the end now. The AD works fine, tables work fine, and I think the EEPROM value is working, seems to be anyway. Debugging is quite fastidious, seems like everytime I fix a bug, another pops up further down the program. :)

And I'm up to two hundred lines of code. I wonder if I'm OK for memory, how do I run the memory gauge in MPLAB?
 
Last edited:
And I'm up to two hundred lines of code. I wonder if I'm OK for memory, how do I run the memory gauge in MPLAB?

The memory usage gauge will only work when you have used the linker to compile your code.
I prefer to just compile the code as usual then open with notepad,the .lst file that it creates in your project folder.
If you scroll down the listing to the end, it shows how much is used and remains and also shows a memory usage map.
 
The memory usage gauge will only work when you have used the linker to compile your code.
I prefer to just compile the code as usual then open with notepad,the .lst file that it creates in your project folder.
If you scroll down the listing to the end, it shows how much is used and remains and also shows a memory usage map.
Not really. Just import the hex file into MPLAB after assembling the code, then you'll be able to see the memory usage gauge.
 
Click "View" and then select "Memory Usage guage".

It says program memory '0' total 4096, and data memory '0' total 368. So I think it's not working just by clicking view and then program memory gauge.

I'll try the other two methods.
 
Not really. Just import the hex file into MPLAB after assembling the code, then you'll be able to see the memory usage gauge.

This works, I imported the HEX file and started the memory gauge, and I get 215 out of 4096, so there is plenty of space left. I don't have to worry about that.

Thanks Bananasiong.

Now I have to decrement a two byte number. And find out why the number doesn't transfer over. And why the PORTA pins don't set when I want them to.... then I'm done. :)
 
Code:
	clr	top_byte_reg
	mov	w0, w

	addwf	w1, w
	btfsc	status, C
	incf	top_byte_reg

	addwf	w2, w
	btfsc	status, C
	incf	top_byte_reg

	addwf	w3, w
	btfsc	status, C
	incf	top_byte_reg

	addwf	w4, w
	btfsc	status, C
	incf	top_byte_reg

	movwf	bottom_byte_reg

On each addition there can be an overflow. The carry bit is set if that happens, and the number of times that happens is added up in top_byte_reg.

What is going on...? I use this routine, and the command; "clrf top_byte_reg".... doesn't clear the register.
 
I moved the "clrf" command to the beginning of the program and now it clears.

Now I have to find out why the PORTA pins don't set.
 
Now I have to find out why the PORTA pins don't set.
PORTA pins are multiplexed with the built-in A/D converter and comparator in many PIC microcontrollers. If you're trying to set individual bits but you did not configure the PORT for digital I/O, you're very likely to get weird results. RA5 is input only in 18-pin PICs; RA6 and RA7 can be used for general I/O - assuming that you're using the internal oscillator - but you have to set the configuration bits properly.
BTW have you mentioned which PIC you're using? have you already posted the complete code on these forums?
 
Last edited:
The first time I used a PIC with analogue capable pins, I had similar problems. I recall that the default PORTA set-up was for analogue - I had to clear ANSEL and ANSELH registers before I got the desired digital behaviour.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top