View previous topic :: View next topic |
Author |
Message |
KU5D
Joined: 10 Feb 2008 Posts: 46 Location: Asheville, North Carolina
|
18F4520 Status Register |
Posted: Sun Feb 10, 2008 11:58 am |
|
|
Welcome to my inaugural post on this forum...seems there are smart folks in here, so here's my question:
Using the CCS compiler and the rotate_right (or _left) function on a variable in memory, does the bit rotated out set the carry bit in the status register (located at 0xFD8 in the 4520)? I have set up the following test, but it doesn't seem to do this (think of this as pseudo-code...bet you get the picture):
#byte statusReg = 0xFD8
int8 test = 0x80; //this inits me to 10000000
I trigger the rotate externally to do the following:
rotate_right(&test,1); //should rotate the bit right thru the int8 test
fprintf(myStream, "test byte: %x", test); //show me in hex
fprintf(myStream, "status: %x",statusReg); //show me the status reg
When I trigger the test, I can see the rotate happening on the test variable, but the status register never reflects a change in the carry bit when a 1 falls out the LSB end. Seems that the status register doesn't work with variables...do I have to use a specific register as a working register to get the status register involved? _________________ Confidence is the feeling you have right before you fully understand the situation... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Feb 10, 2008 2:31 pm |
|
|
Look at the .LST file in your project directory. The fprintf() statements
execute at least four instructions that change the Carry flag before it's
printed. Any ADD or SUB, or INC or DEC statement will change it.
To see the Carry flag, you could move the StatusReg into a temp
variable immediately after the rotate_right() statement. Then print
the temp variable. This will work in this particular case, because the
rotate_right() line in your program is translated into one line of ASM code.
However, it's not a good idea to write code in this way. The C language
does not require that any particular hardware status be available after
a line of source code is executed. The compiler writers are not under
any requirement to do so, and they don't do it. I would never write C
code that depends upon the state of the Status register after a line of
high level code is executed.
Note:
To see the full list file, including CCS library routines, edit 18F4520.H,
and comment out the #nolist statement, as shown in bold below:
Quote: |
//////// Standard Header file for the PIC18F4520 device ////////////////
#device PIC18F4520
//#nolist |
|
|
|
KU5D
Joined: 10 Feb 2008 Posts: 46 Location: Asheville, North Carolina
|
|
Posted: Mon Feb 11, 2008 8:33 am |
|
|
Many thanx. Copying the register to a variable works as predicted. In the past with other hardware (and using assembly) the carry bit was a nice place to hold a bit temporarily when rotated out of a register. However as you pointed out, using C we apparently can't necessarily depend on that. This has helped me find better answers.
thanx, _________________ Confidence is the feeling you have right before you fully understand the situation... |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Feb 11, 2008 9:31 am |
|
|
Just another note why your code would probably fail: Compared to the PIC16 the PIC18 has a new extra instruction for rotation. There is the old one through the carry bit and a new one which doesn't affect the carry bit. Your given C-example uses the non-carry bit instruction.... |
|
|
KU5D
Joined: 10 Feb 2008 Posts: 46 Location: Asheville, North Carolina
|
|
Posted: Mon Feb 11, 2008 9:38 am |
|
|
OK...for curiosity's sake, what's the other one? _________________ Confidence is the feeling you have right before you fully understand the situation... |
|
|
|