CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Multiplication

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Acid
Guest







Multiplication
PostPosted: Fri May 23, 2003 7:08 am     Reply with quote

<font face="Courier New" size=-1>Hello,

Do you know which is the best way to make a multiplication?

my code is :

Int Value;
long scale;
Set_adc_channel(0);
delay_us(10);
Value=read_adc();
delay_us(10);
scale=value*1.25;

but it is slow and little efficient.

Tahnk








</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514698
Tomi
Guest







Re: Multiplication
PostPosted: Fri May 23, 2003 8:49 am     Reply with quote

If the 1.25 factor is constant, it means "one and a quarter" so:
scale = value + (value >> 2);

:=<font face="Courier New" size=-1>Hello,
:=
:=Do you know which is the best way to make a multiplication?
:=
:=my code is :
:=
:=Int Value;
:=long scale;
:= Set_adc_channel(0);
:= delay_us(10);
:= Value=read_adc();
:= delay_us(10);
:= scale=value*1.25;
:=
:=but it is slow and little efficient.
:=
:=Tahnk
:=
:=
:=
:=
:=
:=
:=
:=
:=</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514701
R.J.Hamlett
Guest







Re: Multiplication
PostPosted: Fri May 23, 2003 12:23 pm     Reply with quote

:=<font face="Courier New" size=-1>Hello,
:=
:=Do you know which is the best way to make a multiplication?
:=
:=my code is :
:=
:=Int Value;
:=long scale;
:= Set_adc_channel(0);
:= delay_us(10);
:= Value=read_adc();
:= delay_us(10);
:= scale=value*1.25;
:=
:=but it is slow and little efficient.
:=
:=Tahnk
:=
:=</font>
It will be.
Floating point arithmetic takes a lot of work. As Tomi has pointed out, it will be much faster to read the number as a long integer, and perform the scaleing, by adding 1/4 of itself back.
Why are you delaying by 10uSec after reading the value?. At this point, the maths itself will provide plenty of delay...

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514711
acid
Guest







Re: Multiplication
PostPosted: Mon May 26, 2003 1:37 am     Reply with quote

Hello!

Your solution is a good idea
but if Scale(long int) exceeds the 255 value it clear himself
instead if I does write:
scale=(long)value+( (long)value >> 2);
the memory amount is similar to scale=value*1.25;
I don't use floating point in any case and I does think that there isn't other solution.
What do you think about ?
Thanks






:=If the 1.25 factor is constant, it means "one and a quarter" so:
:=scale = value + (value >> 2);
:=
:=:=<font face="Courier New" size=-1>Hello,
:=:=
:=:=Do you know which is the best way to make a multiplication?
:=:=
:=:=my code is :
:=:=
:=:=Int Value;
:=:=long scale;
:=:= Set_adc_channel(0);
:=:= delay_us(10);
:=:= Value=read_adc();
:=:= delay_us(10);
:=:= scale=value*1.25;
:=:=
:=:=but it is slow and little efficient.
:=:=
:=:=Tahnk
:=:=
:=:=
:=:=
:=:=
:=:=
:=:=
:=:=
:=:=
:=:=</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514752
acid
Guest







Re: Multiplication
PostPosted: Mon May 26, 2003 1:45 am     Reply with quote

Hello
I does use 10uS delay because If the loop( do.. read_adc.. while ) is fast, read_adc return a wrong value. I don't know the cause.
Don't you use the "delay_us" usually?


:=:=<font face="Courier New" size=-1>Hello,
:=:=
:=:=Do you know which is the best way to make a multiplication?
:=:=
:=:=my code is :
:=:=
:=:=Int Value;
:=:=long scale;
:=:= Set_adc_channel(0);
:=:= delay_us(10);
:=:= Value=read_adc();
:=:= delay_us(10);
:=:= scale=value*1.25;
:=:=
:=:=but it is slow and little efficient.
:=:=
:=:=Tahnk
:=:=
:=:=</font>
:=It will be.
:=Floating point arithmetic takes a lot of work. As Tomi has pointed out, it will be much faster to read the number as a long integer, and perform the scaleing, by adding 1/4 of itself back.
:=Why are you delaying by 10uSec after reading the value?. At this point, the maths itself will provide plenty of delay...
:=
:=Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514753
R.J.Hamlett
Guest







Re: Multiplication
PostPosted: Mon May 26, 2003 2:32 am     Reply with quote

:=Hello
:=I does use 10uS delay because If the loop( do.. read_adc.. while ) is fast, read_adc return a wrong value. I don't know the cause.
:=Don't you use the "delay_us" usually?
:=
You _must_ have a delay after selecting the AD channel, but you should not need a delay after taking the reading. The delay needed after selecting the channel, will depend on the source impedance of your hardware connected to the channel, & the accuracy required. The 10uSec, you allow between selecting the channel, and starting the conversion, is shorter than required if your analog source is on the upper end of the recommended range (look at the chip data sheet - if the source impedance is 2.5K ohms - the maximum recommended, the time is normally just under 13uSec).

Best Wishes

:=:=:=<font face="Courier New" size=-1>Hello,
:=:=:=
:=:=:=Do you know which is the best way to make a multiplication?
:=:=:=
:=:=:=my code is :
:=:=:=
:=:=:=Int Value;
:=:=:=long scale;
:=:=:= Set_adc_channel(0);
:=:=:= delay_us(10);
:=:=:= Value=read_adc();
:=:=:= delay_us(10);
:=:=:= scale=value*1.25;
:=:=:=
:=:=:=but it is slow and little efficient.
:=:=:=
:=:=:=Tahnk
:=:=:=
:=:=:=</font>
:=:=It will be.
:=:=Floating point arithmetic takes a lot of work. As Tomi has pointed out, it will be much faster to read the number as a long integer, and perform the scaleing, by adding 1/4 of itself back.
:=:=Why are you delaying by 10uSec after reading the value?. At this point, the maths itself will provide plenty of delay...
:=:=
:=:=Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514755
Tomi
Guest







Re: Multiplication
PostPosted: Mon May 26, 2003 3:20 am     Reply with quote

Why do you use multiple casting?
You can simply use:

scale = value;
scale += scale >> 2;

The list:
.................... scale = value;
015E: CLRF 12
0160: MOVFF 0E,11
.................... scale += scale >> 2;
0164: RRCF 12,W
0166: MOVWF 03
0168: RRCF 11,W
016A: MOVWF 02
016C: RRCF 03,F
016E: RRCF 02,F
0170: MOVLW 3F
0172: ANDWF 03,F
0174: MOVF 02,W
0176: ADDWF 11,F
0178: MOVF 03,W
017A: ADDWFC 12,F

It is only 14 instructions. A floating point arithm. is much bigger.

:=Hello!
:=
:=Your solution is a good idea
:=but if Scale(long int) exceeds the 255 value it clear himself
:=instead if I does write:
:=scale=(long)value+( (long)value >> 2);
:=the memory amount is similar to scale=value*1.25;
:=I don't use floating point in any case and I does think that there isn't other solution.
:=What do you think about ?
:=Thanks
:=
:=
:=
:=
:=
:=
:=:=If the 1.25 factor is constant, it means "one and a quarter" so:
:=:=scale = value + (value >> 2);
:=:=
:=:=:=<font face="Courier New" size=-1>Hello,
:=:=:=
:=:=:=Do you know which is the best way to make a multiplication?
:=:=:=
:=:=:=my code is :
:=:=:=
:=:=:=Int Value;
:=:=:=long scale;
:=:=:= Set_adc_channel(0);
:=:=:= delay_us(10);
:=:=:= Value=read_adc();
:=:=:= delay_us(10);
:=:=:= scale=value*1.25;
:=:=:=
:=:=:=but it is slow and little efficient.
:=:=:=
:=:=:=Tahnk
:=:=:=
:=:=:=
:=:=:=
:=:=:=
:=:=:=
:=:=:=
:=:=:=
:=:=:=
:=:=:=</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514756
acid
Guest







Re: Multiplication
PostPosted: Mon May 26, 2003 3:36 am     Reply with quote

:=:=Hello
:=:=I does use 10uS delay because If the loop( do.. read_adc.. while ) is fast, read_adc return a wrong value. I don't know the cause.
:=:=Don't you use the "delay_us" usually?
:=:=
:=You _must_ have a delay after selecting the AD channel, but you should not need a delay after taking the reading. The delay needed after selecting the channel, will depend on the source impedance of your hardware connected to the channel, & the accuracy required. The 10uSec, you allow between selecting the channel, and starting the conversion, is shorter than required if your analog source is on the upper end of the recommended range (look at the chip data sheet - if the source impedance is 2.5K ohms - the maximum recommended, the time is normally just under 13uSec).
:=
:=Best Wishes
:=

last question...
I does use a rc ( 10kohm and 100nF) for AD input . I have read that the maximum recommended impedence is 10K not 2.5K
can ask you what value do you use usually
Tahnks
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514757
acid
Guest







Re: Multiplication
PostPosted: Mon May 26, 2003 5:15 am     Reply with quote

Yes , it does function well
Tahnks
Which Does is the difference between "long int scale" and "scale(long)" ?
Thanks

:=Why do you use multiple casting?
:=You can simply use:
:=
:=scale = value;
:=scale += scale >> 2;
:=
:=The list:
:=.................... scale = value;
:=015E: CLRF 12
:=0160: MOVFF 0E,11
:=.................... scale += scale >> 2;
:=0164: RRCF 12,W
:=0166: MOVWF 03
:=0168: RRCF 11,W
:=016A: MOVWF 02
:=016C: RRCF 03,F
:=016E: RRCF 02,F
:=0170: MOVLW 3F
:=0172: ANDWF 03,F
:=0174: MOVF 02,W
:=0176: ADDWF 11,F
:=0178: MOVF 03,W
:=017A: ADDWFC 12,F
:=
:=It is only 14 instructions. A floating point arithm. is much bigger.
:=
:=:=Hello!
:=:=
:=:=Your solution is a good idea
:=:=but if Scale(long int) exceeds the 255 value it clear himself
:=:=instead if I does write:
:=:=scale=(long)value+( (long)value >> 2);
:=:=the memory amount is similar to scale=value*1.25;
:=:=I don't use floating point in any case and I does think that there isn't other solution.
:=:=What do you think about ?
:=:=Thanks
:=:=
:=:=
:=:=
:=:=
:=:=
:=:=
:=:=:=If the 1.25 factor is constant, it means "one and a quarter" so:
:=:=:=scale = value + (value >> 2);
:=:=:=
:=:=:=:=<font face="Courier New" size=-1>Hello,
:=:=:=:=
:=:=:=:=Do you know which is the best way to make a multiplication?
:=:=:=:=
:=:=:=:=my code is :
:=:=:=:=
:=:=:=:=Int Value;
:=:=:=:=long scale;
:=:=:=:= Set_adc_channel(0);
:=:=:=:= delay_us(10);
:=:=:=:= Value=read_adc();
:=:=:=:= delay_us(10);
:=:=:=:= scale=value*1.25;
:=:=:=:=
:=:=:=:=but it is slow and little efficient.
:=:=:=:=
:=:=:=:=Tahnk
:=:=:=:=
:=:=:=:=
:=:=:=:=
:=:=:=:=
:=:=:=:=
:=:=:=:=
:=:=:=:=
:=:=:=:=
:=:=:=:=</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514758
R.J.Hamlett
Guest







Re: Multiplication
PostPosted: Tue May 27, 2003 10:28 am     Reply with quote

:=:=:=Hello
:=:=:=I does use 10uS delay because If the loop( do.. read_adc.. while ) is fast, read_adc return a wrong value. I don't know the cause.
:=:=:=Don't you use the "delay_us" usually?
:=:=:=
:=:=You _must_ have a delay after selecting the AD channel, but you should not need a delay after taking the reading. The delay needed after selecting the channel, will depend on the source impedance of your hardware connected to the channel, & the accuracy required. The 10uSec, you allow between selecting the channel, and starting the conversion, is shorter than required if your analog source is on the upper end of the recommended range (look at the chip data sheet - if the source impedance is 2.5K ohms - the maximum recommended, the time is normally just under 13uSec).
:=:=
:=:=Best Wishes
:=:=
:=
:=last question...
:=I does use a rc ( 10kohm and 100nF) for AD input . I have read that the maximum recommended impedence is 10K not 2.5K
:=can ask you what value do you use usually
:=Tahnks
The maximum allowed, depends on the resolution of the AD, and the sampling time you allow. 2.5K, is the recommended maximum for the 18Fxx2 chips, while 10K, is the maximum allowed for the 16F87x chips. Microchip have lowered the recommended source impedance, despite the input 'model' remaining the same, since the original values were at times giving problems. If you have a source impedance at the 10K level, the acquisition time has to rise to 17uSec.
The input 'model' of the AD, is a switch, connected to a 1K resistor feeding into a 120pF capacitor, paralleled by a current 'sink', of about 500nA. If you imagine the switch closes, when the analog channel is selected, and then re-opens, when the sampling starts, you can work out how close to the required voltage the internal capacitor will get.
You also have to 'watch out', for the voltage range allowed. The timings given, all assume the the AD is being operated over the normal full range, and to avoid bit errors, have to increase if you use a lower reference voltage (the minimum allowed to retain the quoted accuracy, is normally about 3v - parameter A20A in the 18Fxx2 data sheet). Also beware that the current consumption on whatever source you are using for your reference voltage, rises in the acquisition cycle to as much as 1mA.
It is better to have a much lower source impedance if possible. I am normally feeding the inputs driectly from op-amps, with impedances of only a very few ohms.

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514784
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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