|
|
View previous topic :: View next topic |
Author |
Message |
ljbeng
Joined: 10 Feb 2004 Posts: 205
|
Math not right.... |
Posted: Wed Jan 05, 2005 10:48 am |
|
|
I have a 16F876.
Version 3.212 CCS.
My math is not cooperating. Can anyone tell me if this listing looks right.
Position and Temp are INT16.
RC1 and RC2 are int 8. RC2 will never be < 0x80 and RC1 will never be < 0x40.
.................... temp = (rc2 - 0x80) * 64;
0100: MOVLW 80
0101: SUBWF 2B,W
0102: MOVWF 79
0103: SWAPF 79,W
0104: MOVWF 37
0105: RLF 37,F
0106: RLF 37,F
0107: MOVLW C0
0108: ANDWF 37,F
.................... position = (rc1 - 0x40) + temp;
0109: MOVLW 40
010A: SUBWF 2A,W
010B: ADDWF 37,W
010C: MOVWF 31
010D: MOVF 38,W
010E: MOVWF 32
010F: BTFSC 03.0
0110: INCF 32,F |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Jan 05, 2005 11:43 am |
|
|
Your problem is most likely do to casting. even though temp is 16 bits the compiler will treat the result on the right hand side of the equation as 8 bit since rc2 is 8 bit. You could do this
Code: |
temp = rc2 - 0x80;
temp *= 64;
|
|
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Wed Jan 05, 2005 11:54 am |
|
|
Plus, could your result ever be a negative number? If so, make sure you declare your variables 'signed int16'. Simply declaring a variable int16 or int8 makes it an 'unsigned'.
Ronald |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Jan 05, 2005 11:56 am |
|
|
Shouldn't ever be negative according to this statement
Quote: | RC1 and RC2 are int 8. RC2 will never be < 0x80 and RC1 will never be < 0x40.
|
|
|
|
ljbeng
Joined: 10 Feb 2004 Posts: 205
|
|
Posted: Wed Jan 05, 2005 1:26 pm |
|
|
This worked. You were right about casting.
.................... temp = (rc2 - 0x80);
0108: MOVLW 80
0109: SUBWF 2B,W
010A: MOVWF 37
010B: CLRF 38
.................... temp = temp * 64;
010C: RLF 37,F
010D: RLF 38,F
010E: RLF 37,F
010F: RLF 38,F
0110: RLF 37,F
0111: RLF 38,F
0112: RLF 37,F
0113: RLF 38,F
0114: RLF 37,F
0115: RLF 38,F
0116: RLF 37,F
0117: RLF 38,F
0118: MOVLW C0
0119: ANDWF 37,F
.................... position = (rc1 - 0x40) + temp;
011A: MOVLW 40
011B: SUBWF 2A,W
011C: ADDWF 37,W
011D: MOVWF 31
011E: MOVF 38,W
011F: MOVWF 32
0120: BTFSC 03.0
0121: INCF 32,F |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|