PDA

View Full Version : Div32



Archangel
- 2nd February 2007, 00:48
Hello Everyone,
Still in an effort to understand this programming language, I now am playing with DIV32.
I have taken the code from the manual and added some serial routines so I can view whats happening. I have posted the code below.


' Use MPASM
@MyConfig = _HS_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _LVP_OFF
@MyConfig = MyConfig & _CP_OFF & _BODEN_OFF & _DATA_CP_OFF
@ __config MyConfig
Define PIC16F628A
define OSC 20
include "modedefs.bas"


a var word
b var word
c var word
Dummy var word
start:
b = 500
c = 1000

disable '

dummy = b * c
a = div32 100

enable
serout PortB.4,T9600,[254,1]
pause 100
serout PortB.4,T9600,[254,128,"dummy is:",# Dummy]
pause 1000
serout PortB.4,T9600,[254,192,"A is:", # a]
pause 1000
serout PortB.4,T9600,[254,148,"B is:", # b]
pause 1000
serout PortB.4,T9600,[254,212,"C is:", # c]
pause 1000
goto start
end



The code does what the book says it should do. I have a couple of questions:
the line, a = DIV32 100 . I get that the 100 causes DIV32 to divide by 100, what I do not get is how DIV32 knows to use the math in the above formula, is "Dummy " a default place holder? My second question is the value of Dummy as outputted in line 1 of the display, I get the fact, 5 hundred thousand is too large for 16 bits to hold but 41248 makes no sense to me, can anyone explain?
Thank You
JS

mister_e
- 2nd February 2007, 00:59
Windows Calc commes pretty handy in this case... 500,000=07 A1 20 hex right?

As Dummy is a WORD, it keep only A1 20, witch equate to 41248.

you could still use


A = b * c
a = div32 100
or


B = b * c
B = div32 100

Archangel
- 2nd February 2007, 05:14
Windows Calc commes pretty handy in this case... 500,000=07 A1 20 hex right?


Thank You Mister_e, that leads me into another question:
I thought microprocessors worked only in binary, logic high and logic low, so why do we use hex anyway? Is it because we have 16 bits?

mister_e
- 2nd February 2007, 17:42
Why decimal, why Hex, why Octal and why Binary... it's just different measure like Km/h Mph, Mils or mm, Yard Meter.

In the previous, it's easier to figure out the problem with Hex than Decimal. A word is indeed 16 bits (uh...). PBP don't have any LongWord or DoubleWord var, DIV32 work around that.

Bad explanation i know... It's clear in my little head, but right words won't go out :D

SteveB
- 2nd February 2007, 18:07
Hey Joe,
Have a look at Darrel's Threads using DIV32 and other 32 bit math magic. Good stuff from the Wizard.
Retrieving 32bit Multiply Result (http://www.picbasic.co.uk/forum/showthread.php?t=24)
Retrieving Div32 Remainder (http://www.picbasic.co.uk/forum/showthread.php?t=48)
32-bit Variables and DIV32, Hourmeter 99999.9


SteveB

Archangel
- 2nd February 2007, 20:26
Thanks.
Iwill look at those threads too.
Joe

Edit:Those are great, I have bookmarked these pages and stashed the code samples for study.