|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
16 bit math on PIC
I've been racking my brain on this for hours and I'm starting to get a headache.
I have a 12F683. My program (written in assembly) has gotten to the point where there is a 16 bit number somewhere. One byte is the upper 8 bits and the other byte is the lower 8 bits. Now the problem is that I need to do some math with this number. The first step is to subtract 2000 (decimal) from it. If the number is less than 2000, it either needs to leave it at 0 or I need some way of determining that it has become negative. If I can get that part, I should be able to figure out the rest on my own. I thought of simply subtracting 7 from the upper byte then subtracting 215 from the lower byte but that presents all sorts of carry issues if the lower byte is less than 215. It seems that many of the mathematical instructions on the PIC are designed for signed bytes. This is trouble for the lower byte that needs to remain unsigned. I tried Google, but there doesn't seem to be much info on doing multibyte math. |
|
#2
|
||||
|
||||
|
Re: 16 bit math on PIC
its not as hard as you have psyched yourself up for.
You subtract the lower byte first, and check the carry flag. If there is a carry you subtract one from the upper byte, then subtract the upper bytes. If the number was less than 2000 the upperbyte will have the msb set (negative) when you are done. I dont remember the instruction set off the top of my head, but many µC have 'subtract with carry' instructions, that take care of the carry for you. |
|
#3
|
||||
|
||||
|
Re: 16 bit math on PIC
Basically, the trick here is to think way back to when you first learned to add and subtract multi-digit numbers on paper. Then just apply the same methods here. Subtract the least significant bytes of each number first, and if the carry bit is set after the subtraction then that means you have to "borrow" from the more-significant byte by decrementing it and then performing the subtraction on those upper bytes.
This same method applies to addition, and as you can tell it's easy to build up routines that will add or subtract numbers of any number of bytes. (edit) Guess Ken beat me to it ![]() |
|
#4
|
|||
|
|||
|
Re: 16 bit math on PIC
Alright, thanks guys, I'm surprised I didn't figure that out.
But I still have a problem Let's say that I want to do the following: 00000010 01000001 + 00000001 01001010 --------------------- 00000011 10001011 The lower byte of the result takes a full 8 bits. However, according to the datasheet, the addwf instruction that I would use only goes up to 127 (7 bits which I presume is to keep it signed with the 8th bit). How would this affect what I want to do? I guess a more general question on this topic is what happens if I perform an arthithmatic operation and end up with something larger than 127. addwf, subwf, and a few other instructions only work up to 127 (according to the datasheet). If I get 128 does it overflow and give me garbage (as I would expect from code in a high level language) or does it just overflow into the MSB and behave nicely? |
|
#5
|
||||
|
||||
|
Re: 16 bit math on PIC
Quote:
|
|
#6
|
|||
|
|||
|
Re: 16 bit math on PIC
Quote:
I'm not sure if this is what you are reading, but f is limited to 7 bits, as each bank goes to 7F. edit: switched rb0 to rp0 (the correct bitname in the status register) Last edited by BrianBSL : 04-10-2005 at 19:41. |
|
#7
|
||||
|
||||
|
Re: 16 bit math on PIC
Quote:
|
|
#8
|
|||
|
|||
|
Re: 16 bit math on PIC
Quote:
|
|
#9
|
||||
|
||||
|
Re: 16 bit math on PIC
Quote:
Quote:
|
|
#10
|
|||
|
|||
|
Re: 16 bit math on PIC
Quote:
|
|
#11
|
||||
|
||||
|
Re: 16 bit math on PIC
Quote:
|
|
#12
|
|||
|
|||
|
Re: 16 bit math on PIC
http://www.mstracey.btinternet.co.uk...l/progtut8.htm
Toward the bottom of the page, under the "Increment" heading: Quote:
|
|
#13
|
|||||
|
|||||
|
Re: 16 bit math on PIC
Quote:
|
|
#14
|
||||
|
||||
|
Re: 16 bit math on PIC
Quote:
no editors checking your work no accountablility if you post mistakes on your website when in doubt refer to the Microchip data sheets, and if something weird is happening, check the errata data sheet for the part you are using. Some micros do have bugs, so some instructions dont work as originally intended. But I dont remember any 7 vs 8 bit math bugs on an PIC devices. Last edited by KenWittlief : 05-10-2005 at 09:38. |
|
#15
|
|||
|
|||
|
Re: 16 bit math on PIC
Quote:
As a side not to that, the 12F683 is an 8 pin device with 6 I/Os. 3 of I/Os are multipurpose. If you use the internal clock, you can configure the two oscillator pins to be I/O. And if you don't need a reset pin, you configure that to be an I/O also. In the process of doing that, it occured to me that the chip would start running the program and disable the reset as soon as it's plugged into my JDM programmer. But, in order to put it into program mode, you need to do something with the reset pin (I'm fuzzy on the details though). It crossed my mind that it might not be reprogramable under those conditions, but I didn't pay much attention to it. I figured that the protocol would be smart enough to get around that. Why would they make it impossible to reprogram if I didn't set the code protect bits? Well, apparently, the protocol was changed to occomidate these new style chips. The JDM programmer, however, was designed before this change occured. With that said, I now have 2 chips that can't be reprogrammed with the current incarnation of my programmer. The issue was big enough for me to think that it was strange, but it wasn't so screwed up that I thought it would be a problem. Now, if you'll excuse me, I think I have some 2N7000's in the garage somewhere to fix my programmmer. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| PIC 18F2431 | Sparks333 | Programming | 9 | 24-08-2005 01:24 |
| Can the pic controller send the data to pc? | sunnyrx7turbo | Control System | 2 | 05-08-2005 13:14 |
| Linux and new microcontollers. | djcapelis | Programming | 48 | 29-01-2005 00:26 |