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.

Newbie in PIC - Multiplication and Division

Status
Not open for further replies.

devonsc

Advanced Member level 4
Joined
Nov 30, 2004
Messages
115
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,298
Activity points
1,931
example multiply pic assembler

Hi there, again, me and my "lousy" questions regarding PIC microcontroller.

I would like to ask about programming a multiplication and division in PIC. I've come across reading materials as well as opinions from the smart ones that it can be done using various ways. Please correct me if I'm wrong:

a.) Through repeated addition and subtraction - Slow but it seems to be easy to me :)
b.) Through look-up tables - Fast but take up a lot of memory?
c.) Through somekind of shifting method? - I've read about this but really don't quite understand how it can be done.

I was thinking to do the following:

I'm trying to write a PWM code in PIC that varies according to an analogue input which I will feed the PIC through the ADC module. I was thinking to have the PIC to perform some calculation based on this analogue input and changes the my CCPR1L and CCP1CON value for the PWM. Any advice? Any guidance is very much needed. Thanks in advance.

By the way, I still don't quite understand how do I store a 10-bit value. I was trying to store the 2 LSB of my calculation result, bit-0 and bit-1 on a location to have it stored in CCP1CON later and the others on another location to have it stored in CCPR1L later. Is this possible? Help very much needed. Thanks in advance.
 

checkmate

Advanced Member level 3
Joined
Feb 25, 2004
Messages
833
Helped
178
Reputation
356
Reaction score
125
Trophy points
1,323
Location
Toilet Seat
Activity points
7,815
pic instruction for multiplication 2 number

devonsc said:
I would like to ask about programming a multiplication and division in PIC.
Hop over to piclist. They have a pretty good selection of example code to implement a host of mathematical functions.

devonsc said:
b.) Through look-up tables - Fast but take up a lot of memory?
You can use a PIC18. Most of them have hardware multipliers.

devonsc said:
c.) Through somekind of shifting method? - I've read about this but really don't quite understand how it can be done.
It's quite simple really.
When you do a shift-left (<<), you multiply the value by 2.
When you do a shift-right (>>), you divide the value by 2.
In PIC, each of these operations take ony 2 instructions, so they are VERY fast.
You can play around with the values. So if you want a x3 multiplier, you go
y = (x<<1) + x

devonsc said:
By the way, I still don't quite understand how do I store a 10-bit value. I was trying to store the 2 LSB of my calculation result, bit-0 and bit-1 on a location to have it stored in CCP1CON later and the others on another location to have it stored in CCPR1L later. Is this possible? Help very much needed. Thanks in advance.
Usually we store the 8 LSB as a byte, and the 2 MSB as another byte.
 

devonsc

Advanced Member level 4
Joined
Nov 30, 2004
Messages
115
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,298
Activity points
1,931
multiplication using pic assembly language

Thanks checkmate, I'll try piclist.

By the way, I do understand what you mean by LSB as a byte and the other 2 MSB as another byte but I don't have any idea on how should I do the storing....(still thinking on how to do it "???")

Slow learner here...
 

SphinX

Advanced Member level 3
Joined
Jan 25, 2002
Messages
822
Helped
58
Reputation
116
Reaction score
29
Trophy points
1,308
Location
EGYPT
Activity points
7,045
pic c float division

Salam,

Look also to EPE Jan-2005 issue.
It has good "32 bit signed integer maths for pic" article.

SphinX
 

ilker

Member level 3
Joined
Dec 13, 2004
Messages
58
Helped
3
Reputation
6
Reaction score
1
Trophy points
1,288
Location
TURKEY
Activity points
445
how to do division using pic

this book touches this subject
 

devonsc

Advanced Member level 4
Joined
Nov 30, 2004
Messages
115
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,298
Activity points
1,931
asm pic division

Hi, thanks to all, I've understand how to perform multiplication. By the way, ilker? Is it true that the book is only for C programming? i've not downloaded that book but it by looking at the contents, it seems that it is for C language. Sorry for not making myself clear, I'm actually using assembly language for the PIC for my program.

By the way, now I have problem understanding doing multiplication in which the number involves decimal point. It seems that I have to consider the decimal point in my multiplication as I'm doing this calculation to set-up my PWM signal.

Help really needed...
 

devonsc

Advanced Member level 4
Joined
Nov 30, 2004
Messages
115
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,298
Activity points
1,931
multiplication in assembly pic

Hi there, do you guys mind giving me guide on the following matters?

a.) Can anyone pls provide me the 'routine' or 'library' to do multiplication/division/addision/substraction in assembly language?

b) Can anyone pls adivse how to represent floating point number in assembly language?

Thanks in advance...
 

mipedja

Junior Member level 3
Joined
Sep 20, 2004
Messages
28
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,283
Activity points
301
multiplication pic

Shifting method...
It is same as you present number as a sum of 2^n factors, which is suitable for microcontroller operations.
For example if you need to multiply some number with 100 it is same if you multiply with (64+32+4). This can be be easily realized with shifting (faster than 100x add)
(shl 6 + shl 5 + shl 2)
You dont need to transform 100 manuallly into 64+32+4, because that information is already contained in number binary representation as 1 at place 6, 5, and 2.
100 dec= 01100100 bin
So you have to read bits of one number, and if is at some place 1 you have to shift another operand for amount of bit position. At the end you just have to add all shifted results.
 

banh

Advanced Member level 1
Joined
Dec 16, 2004
Messages
458
Helped
17
Reputation
34
Reaction score
5
Trophy points
1,298
Activity points
3,937
pic18 32-bit multiplication

ilker said:
this book touches this subject

-> topic does not exist?
 

nicleo

Advanced Member level 2
Joined
Sep 6, 2004
Messages
661
Helped
66
Reputation
132
Reaction score
12
Trophy points
1,298
Activity points
7,153
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top