Powered by Invision Power Board


Forum Rules Forum Rules (Please read before posting)
  Reply to this topicStart new topicStart Poll

> 255 Or 256?
chephia
Posted: December 11, 2010 06:54 am
Reply to this postQuote Post


Member
****

Group: Trusted Members
Posts: 102
Member No.: 23,058
Joined: March 17, 2009




Hi folks…
I’m learning how to make a 4-digit digital voltmeter using 8-bit ADC, µC, and 7-segment. I’m a little bit confused of the value I should use in program for division to get value for each 7-segment. Which one should I use 256 (from 2^8) or 255 (decimal value of 11111111B)?
PMEmail Poster
Top
GPG
Posted: December 11, 2010 11:12 am
Reply to this postQuote Post


Forum Addict ++
*******

Group: Spamminator Taskforce
Posts: 6,201
Member No.: 792
Joined: April 25, 2004




255. There are 256 values in 2^8 but one of them is zero. 255 = 11111111
PMEmail Poster
Top
millwood
Posted: December 11, 2010 02:07 pm
Reply to this postQuote Post


Forum Addict ++
*******

Group: Trusted Members
Posts: 1,823
Member No.: 25,377
Joined: July 26, 2009




255 is the right number.

but then, dividing by 256 is so much easier and so much faster.

but in case you really want to divide by 255, there is a (slightly) faster way:

1/255 = 1/(256-1) = 1/256 * (1/(1-1/256)) ~= 1/256 * (1+1/256)

obviously, the math doesn't matter until you get into 16-bit figures.
PMEmail Poster
Top
Sch3mat1c
Posted: December 11, 2010 08:14 pm
Reply to this postQuote Post


Forum Addict ++
Group Icon

Group: Moderators
Posts: 19,208
Member No.: 73
Joined: July 24, 2002




You'll probably end up multiplying by a calibration factor anyway, so you can simply take your 8 bit value, treat it like a 16 bit value, and multiply by your scale factor. Then you get between zero and eight bits in the upper byte. Discard the lower eight bits. This is equivalent to dividing by 256 if you multiply by 1 (i.e., no bits in the result), or any other number when you multiply by the reciprocal. Multiplication is faster than division on most microcontrollers (particularly the ones with a hardware multiplier, e.g. AVR), so you save on code and execution time.

Tim


--------------------
Answering questions is a tricky subject to practice. Not due to the difficulty of formulating or locating answers, but due to the human inability of asking the right questions; a skill that, were one to possess, would put them in the "answering" category.
PMEmail PosterUsers Website
Top
chephia
Posted: December 23, 2010 12:33 pm
Reply to this postQuote Post


Member
****

Group: Trusted Members
Posts: 102
Member No.: 23,058
Joined: March 17, 2009




Hi...thanks for the answers. Sorry for this late responds.

@millwood: you are right. I found dividing by 256 is so much easier and so much faster.
@Tim: your reply was so helpful. This is the first time I use ADC and µC, if you didn't tell me about the "upper byte" probably I had lost when I wrote the code. Thanks smile.gif

Below is code to get the ADC output value and keep them in R0-R3.
CODE

MOV A, P0        ; Get ADC output value from port0
MOV B, #0F0H     ; Vinput max = 240V
MUL AB           ; B= upper byte, A= lower byte
SWAP A           ; exchange the upper 4-bit and the lower 4-bit
MOV R0, A        ; keep the result in R0. I will use this value to get the decimal fraction
XCH A, B            
MOV B, #0AH
DIV AB
MOV R1, B        ; R1 contains value for 2nd 7-segment
MOV B, #0AH
DIV AB
MOV R2, B        ; R2 contains value for 3rd 7-segment
MOV R3, A        ; R3 contains value for 4th 7-segment
MOV A, R0
MOV B, #0AH
MUL AB
SWAP A
ANL A, #0FH
MOV R0, A

More question. May I build the circuit for ADC Vinput (IN0) in these stage: AC source (220-240V) --> diode --> voltage divider --> ADC IN0?
PMEmail Poster
Top
millwood
Posted: December 24, 2010 02:28 am
Reply to this postQuote Post


Forum Addict ++
*******

Group: Trusted Members
Posts: 1,823
Member No.: 25,377
Joined: July 26, 2009




the answer to your question is "yes" and "no".

directly rectifying ac mains is OK, and as long as your divider utilizes high resistance, it wouldn't fry your mcu pin - they are clamp'd in both direction.

however, if you check the mcu's datasheet, you may notice that it likely has requirements on source impedance, which is likely to have been violated by your divider.

not to mention the safety issue from connecting your mcu to mains directly.

the "proper" approach is to use at least an isolation transformer to separate the mains from your mcu, or to use a current transformer + a resistor to "infer" the ac mains voltage.

or to use an opto-coupler, etc. - not as accurate though.

and then buffer the isolated ac voltage, if necessary.

it all depends on your design requirements.

QUOTE
I found dividing by 256 is so much easier and so much faster.


and for 12-bit adc, dividing by 255 vs. 256 makes no difference, accuracy wise.
PMEmail Poster
Top
chephia
Posted: December 25, 2010 06:01 am
Reply to this postQuote Post


Member
****

Group: Trusted Members
Posts: 102
Member No.: 23,058
Joined: March 17, 2009




Source impedance? I don't understand about impedance so far. Could you explain it in very simple language? I've already googling and read about it but still don't get it. I know the teory but practically I'm still confused.

Anyway thanks for your reply smile.gif
PMEmail Poster
Top
colin55
Posted: January 30, 2011 08:47 pm
Reply to this postQuote Post


Member
****

Group: Trusted Members
Posts: 107
Member No.: 30,119
Joined: July 24, 2010




Impedance is really resistance. When we say "impedance" we really means "resistance."
But if the resistance changes when the frequency of the signal (going through the resistance) changes, we call the resistance an IMPEDANCE. For instance, a 1/2watt resistor will have the same resistance if the signal going through it is 50Hz or 100kHz.
But if we have a coil of wire with many turns, the current through it will be less when the signal increases from 50Hz to 100kHz. The current reduces because the effective resistance of the coil increases and because the resistance increases, we call the resistance AN IMPEDANCE.
The same reasoning applies to SOURCE IMPEDANCE.
A SOURCE is something that “supplies” or “delivers” a quantity. The quantity may be a voltage or a current.
Whenever we draw (take) current from a source, the voltage of the source drops (decreases). This applies to the 120v mains or 240v mains or even a 12v battery when we use the starter-motor. The voltage may drop from 12v to 8v.
We can work out the exact resistance of the battery that is causing the 4v drop when 300 amps is flowing but because the resistance is not an actual resistor, we call the value an IMPEDANCE. You can call the value: INTERNAL RESISTANCE of the battery or INTERNAL IMPEDANCE.
When it comes to a mains transformer supplying 120v or 240v, the winding has a resistance (called the DC resistance) but when a current is flowing, the resistance-value of the secondary winding will be higher than the DC resistance of the winding. Thus we have to give the value a different name and we call it IMPEDANCE.


PMEmail Poster
Top
chephia
Posted: March 08, 2011 09:02 am
Reply to this postQuote Post


Member
****

Group: Trusted Members
Posts: 102
Member No.: 23,058
Joined: March 17, 2009




Colin55...thank you so much for the answer. You explained it very well. smile.gif

For others, thanks for helping me here, finally I did it. wink.gif
PMEmail Poster
Top
Gorgon
Posted: March 08, 2011 12:21 pm
Reply to this postQuote Post


Forum Addict ++
*******

Group: Trusted Members
Posts: 3,003
Member No.: 3,861
Joined: December 18, 2005




You should be aware that the 240vac rectified via a diode will be pulses of about 340V(dc), since 240V is the rms value of the ac voltage.

To make 240(F0h) equal to 240V you need to adjust the resistor network accordingly.

TOK wink.gif


--------------------
Light travels faster than sound. That's why some people appear bright until they speak!
PMEmail Poster
Top
chephia
Posted: March 11, 2011 03:33 pm
Reply to this postQuote Post


Member
****

Group: Trusted Members
Posts: 102
Member No.: 23,058
Joined: March 17, 2009




Thanks..I know Vp=2^0.5*Vef
For ADC input I took millwood advice to use a transformer smile.gif

PMEmail Poster
Top
0 User(s) are reading this topic (0 Guests and 0 Anonymous Users)
0 Members:

Topic Options Reply to this topicStart new topicStart Poll

 


:: support us ::




ElectronicsSkin by DutchDork & The-Force