Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Question on variable values in register(more than 255)

Status
Not open for further replies.

eddy12345

Newbie level 3
Joined
Jan 22, 2008
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
united kingdom
Activity points
1,321
Hi there,

Am i right to assume that every variable created in the register in pic16F84(or in other pics) can only have a value from 0-255?

And the only way to have a value larger than 255(for a variable) is by increasing the value of another variable everytime the first variable overflow. I know my explanation is a bit confusing :|...

Example, "counta" for values from 0-255 and "countb" is for overflow of "counta".

so if i have counta= 20 and countb=2,

The total value will be = (255*2) + 20 = 530

If its true, how do you perform calculations higher than 255?

Example, ( 530 - 4) / 6

And what if the calculation yields a value higher than 255? How do you store it?

Any help would be greatly appreciated
 

eddy12345 said:
Hi there,

Am i right to assume that every variable created in the register in pic16F84(or in other pics) can only have a value from 0-255?

And the only way to have a value larger than 255(for a variable) is by increasing the value of another variable everytime the first variable overflow. I know my explanation is a bit confusing :|...

Example, "counta" for values from 0-255 and "countb" is for overflow of "counta".

so if i have counta= 20 and countb=2,

The total value will be = (255*2) + 20 = 530

If its true, how do you perform calculations higher than 255?

Example, ( 530 - 4) / 6

And what if the calculation yields a value higher than 255? How do you store it?

Any help would be greatly appreciated



Ok, first thought to get into your head, before anything else about binary math:

Binary math relates directly to REGULAR simple math. Math is math, binary (bitwise or bytewise) only changes limits and roll over etc. Any time you want to figure out what you're doing, you write it out the long way for decimal and put bits/bytes where you have decimal digits.

Ok some numbers..

A decimal digit holds 0-9, that's it. If you need more than 9, you add one into the next digit. But it's the next number that goes into the next digit, 10 for decimal. 255 would still be in the first byte, it is 256s that are put into the next byte, so it would be 256 * 2 + the lower byte value. The next byte up would be 256*256, just like the next digit up for decimal is 10*10 for the hundreds digit.

ANYTHING you can do out the long way for decimal math, you can do the same for bytewise binary simply by writing out the decimal way and put a byte where each digit is. Only difference is where your carry/borrows occur, at 0 or 9 roll over for decimal or 0 or 255 roll overs for binary bytes. You can put a point between bytes and go above and below whole numbers in binary, just as easily as you put a decimal point and go above and below with decimals. It is trivial once you do it a few times and get the hang of it.

IOOW, realize that anyone who swears to you up and down you have to have floating point to handle fractional values doesn't have the slightest clue what they're talking about. Fixed point binary will handle about anything, and is far faster than using someone else's floating point routines. Trivial also to make your own floating point binary routines, all you do is have an index keeping track of where the point is.. Computers use binary, all operations work best by staying in binary. About the only time you should ever do anything decimal is converting input or output for human use, simply for convenience. There are only rare exceptions where what you're doing is tied so closely to using the decimal as output etc that it makes sense to work in binary coded decimal, or when you need the errors to more closely match normal decimal rounding errors etc..

Just some extra, best to get it into your head now that it's the same, math is math, and anyone saying it's harder isn't a good source. It's absolutely as easy as writing out decimal math even for multiplication and division, because it uses exactly the same process. Many otherwise competent programmers just don't get that, because they either suck at math in general or don't know how to relate using binary bytes to using decimal digits. Important to find good clear sources on this, so you don't end up sucking at it too just because someone else didn't know how to explain it to you..
 

hi there,

Thank you for your reply, i think i understand most the part but im not sure how to write a floating point routine. I simply want to perform calculation and the result should be rounded to the nearest number.

This is an example code that i found which shows the calculation of numbers more than 255. The result will correspond to an address in a map.

rpmhi=counter overflow for rpmlow.
value of rpm : 74<(rpmhi+rpmlow)<600

Code:
        ; the formula to get the data stored in the map is as follows
        ;
        ;                (rpmhi+rpmlow count)  -  74
        ;    131   -    ----- ----------------------
        ;                           4
        ;
caladv  movlw d'74'    		; (rpmhi+rpmlow count) - 74
        subwf rpmlo,F		; rpmlo = rpmlo - 74
        btfss STATUS,C
        decf rpmhi,F
        ;
        ; divide result by 4
        ;
        bcf STATUS,C
        rrf rpmhi,F
        rrf rpmlo,F     	; / 2
        ;
        bcf STATUS,C
        rrf rpmhi,F
        rrf rpmlo,F     	; / 4
        ;
        movlw .131      	; 131 entries in map list
        movwf math
        ;
        movf rpmlo,W
        subwf math,W    	; W = 131 - result
        ;
        bcf PCLATH,0		; be sure to go h'200'
        bsf PCLATH,1		; where is the map.
        call map		; read map
        movwf rtdset    	; come back with new retard value



Im not sure how the codes work.Ive tried the logic, and it seems fine for numbers <256,

example :

say value of rpm : (rpmhi+rpmlow)=240

where rpmhi=0 and rpmlow=d'240'

ive done 2 different approach :
a) using calculator =41.5
b) using the logic operator (subwf,rrf) =41


But when rpm value > 256, say (rpmhi+rpmlow)=496

where rpmhi=1 and rpmlow=d'240'


same approach as above :
a) using calculator =105.5
b) using the logic operator (subwf,rrf) =41

or am i missing something here..:|
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top