Normal programming question

Status
Not open for further replies.

stevenlkz

New Member
I got two questions about the programming of PIC16F877.
1. Normally, when we rotate 8 time with 8 bit data, we will get back the same data right? But if I rotate one time, then call a subroutine or goto other place, the carry flag already change. Then back to here and rotate again. This loop will go on for 8 time, is it finally still can get back the same data?
2. If I add 8 bit data with another 8 bit data and the result is is overload to 9 or 10 bit, then how a result will store?
 
The PIC16 series does a 9 bit rotate - the 9th bit being the carry flag. If you rotate 9 times then you will get the original value back. If you corrupt the carry flag then that bit will be lost.

Adding two 8 bit numbers that overflow will result in a 9 bit answer with the 9th bit stored in the carry flag.

Mike.
 
Ok. Thanks for help. But I really need to get back the original data after rotate it and the carry flag change is the must. So how can I solve it?
 
couldnt you initially copy it into a register , thus preserving its integrity..
then copy it back to W and rotate as much as you like..?
 
You would need to rotate a register 9 times to get back to the original register value if using a PIC with 14-bit core... The 16-bit core PICs have a rrncf instruction which "wraps around" the 8-bit byte...

Here's one way you might preserve that carry bit for the subroutine call... Simply rotate it into a temporary register, call the subroutine, then rotate the temporary register in the other direction to extract the carry bit...

Code:
;
        rrf     target,f        ; bit 0 now in Carry
        rrf     save_c,f        ; carry bit now in save_c bit 7
        call    some_code       ; call subroutine
        rlf     save_c,f        ; extract carry bit from save_c bit 7
;
Regards, Mike
 
I don't do PIC programming but what's wrong with the following pair of instructions which does not even need a temporary carry storage:

1. Shift bit into carry using : rrf f,w

then

2. rrf f

The above will place the correct rotated value into the register f.

After the subroutine, the value in f is already correct and does not need any "adjustment".

Come on, tell me something I'm missing.
 
Thanks for helping hands from all of u.
However, I have another 6 question with MPlab assembler programming.
1. Is the PIC can use to check the value whether it is a positive or negative value?
2. Is the IF/ELSE statement available in MPlab?
3. How to compare and get the smallest number between three number?
4. After I use the fixed point division of the application note, AN617 in microchip, but the output is in floating point, then how to read the result?
5. If the maximum number of floating point is 0.125 in decimal number, then how many bits should I use for math operation?
6. In application note, AN575 in microchip, it said that floating point 0X823C5198, but how to read it? What decimal value is it?
 
stevenlkz said:
Thanks for helping hands from all of u.
However, I have another 6 question with MPlab assembler programming.
1. Is the PIC can use to check the value whether it is a positive or negative value?

There's no specific 'negative' in a processor (of almost any type), it's up to you how you treat positive integers to decide what's negative or not. The usual way is to use the highest bit of the number, if it's high the number is negative, if it's low it's positive.

2. Is the IF/ELSE statement available in MPlab?

MPLAB isn't a programming language, so the question has no relevance?.

However, the assembler MPLAB uses (MPASM) is an assembler, as such it assembles machine code - the IF/THEN statement is a high level language construct. You can write your own easily in assembler, check the PICList for LOT'S of information about comparisons.

3. How to compare and get the smallest number between three number?

See above!.

4. After I use the fixed point division of the application note, AN617 in microchip, but the output is in floating point, then how to read the result?

The MicroChip floating point routines are well known for being flawed, again you should consult the PICList.

5. If the maximum number of floating point is 0.125 in decimal number, then how many bits should I use for math operation?

It depends entirely what you are trying to do, as I have mentioned previously (and others have as well), there's rarely any need to do floating point, it's too slow and too inaccurate.

6. In application note, AN575 in microchip, it said that floating point 0X823C5198, but how to read it? What decimal value is it?

Don't use it then! - how about telling us the range of numbers you want to cover? (and why?), I can't really see as you need floating point at all!, scaled intergers would be MANY times better.
 
For the IF/THEN/ELSE construct, I found the following explanation clear and precise.

If you like what you see, you can read the rest here:

**broken link removed**
 

Attachments

  • if_then_else.gif
    44.1 KB · Views: 963
But if I rotate one time, then call a subroutine or goto other place, the carry flag already change.

The standard way to do this is to push any registers that the subroutine uses onto the stack. The subroutine stores the old values on the stack and then pops them off and restores the registers before returning. This way you don't have to worry about the subroutine messing up the calling code.
 

You obviously don't program PIC's! - it's a RISC processor, and doesn't have instructions for storing and popping registers in the stack - there's no point really, the stack is EXTREMELY small. The usual stack size is either 4 or 8 bytes, that's all.

But basically he's never explained what he's actually wanting to do?, just how he's trying to implement it - and he 'might' not be going about it the best way?.
 
Yep, Stack is dedicated for storing PC, not other SFRs!
 
You obviously don't program PIC's!
Yeah, I don't program PICs. It should be fairly easy to make a "virtual" stack though. Push and Pop are just standard stack operations. The idea's the same even if there isn't an instruction for them. Just set asside the top of memory to be the stack and crate a variable to be a stack pointer.

A stack is just a more generic version of what wiliB suggested:
couldnt you initially copy it into a register , thus preserving its integrity..

What's the standard method with PICs for preserving register contents over a function call?
 

Again, your lack of PIC experience shows :lol:

You have very little 'RAM' in a PIC, and there's no real need to make your own stack, just access the GPR's directly. Stack based programming (like Forth) isn't really very well suited to a PIC, although you can get PIC Forth, and it does simulate it's own stack.


As already mentioned, you simply store them in allocated GPR's. It's all simple enough, just a bit strange if you come from other machine codes - I came from 6502!.
 
Again, your lack of PIC experience shows
:lol:

Stack based programming (like Forth) isn't really very well suited to a PIC
Having a stack is a C thing too. The stack is used to store the CPU registers during a function call.

As already mentioned, you simply store them in allocated GPR's.
I guess if you have a small app you know how deep you will have functions nested.

It's all simple enough, just a bit strange if you come from other machine codes - I came from 6502!.
:lol: I see what you mean.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…