help with pic
help with pic
i am new to programming the pic microcontroller. I need help wiht a project. I would like to use the adc to convert voltage to serial data i.e 9.5 volts are coming in so it outputs 9.5 volts. my voltage range would be from 020. also does the pic output TTL or RS232 serial data
Re: help with pic
The PIC (you don't say which one) commonly operates on a 5V supply. The ADC is limited to the supply voltage, so you have to divide your input voltage by at least 4. This is done with a voltage divider or better still be an op amp, I believe there was an article in a recent N&V. Most PIC ADCs are 10bit, so 1 bit is 5/1024 or 0.00488V. A little math can convert, or you can calibrate your divider circuit.metroid wrote:i am new to programming the pic microcontroller. I need help wiht a project. I would like to use the adc to convert voltage to serial data i.e 9.5 volts are coming in so it outputs 9.5 volts. my voltage range would be from 020. also does the pic output TTL or RS232 serial data
The PIC USART outputs TTL, so if you have to convert to RS232, you will need to add something like a MAX232 chip.
I might add that you can get several MAX232s from Maxim's website free, if you're a student. It might take a week or two (or longer, depending on where you are). Also, if you just want a serial ADC, there is probably a simpler way to do it. I can't think of anything off the top of my head, but that sounds like something someone would have made a dedicated chip for by now. I'll take a look.
reply
i've gottem a free sample of the MAX232. i was thinking of using one of the smaller pics(8 pin or 16 pin i don't know exact names) becasue the circuit will have to fit in a small space
Re: help with pic
I really don't undrstand how you would program that into the pic.muntron wrote: Most PIC ADCs are 10bit, so 1 bit is 5/1024 or 0.00488V. A little math can convert, or you can calibrate your divider circuit.
Re: help with pic
Greetings (No First Name Supplied),
can only operate up to the internal reference voltage (or the power
supply voltage) which is 5V. You need an external resistor divider to
scale the input voltage by 20/5 or 20/(Vref). Two resistors are
needed.
The input impedance of your ADC with divider is the sum of the
resistors, and if this loads your input voltage a buffer amplifier is
needed.
Once the ADC spits out a value your firmware must scale the
result to compensate for the resistor divider. If the ref was 5V
and a 20V signal was applied, the ADC would report
20/(5 * 1024) = 4096.
Next your firmware must change 4096 to 20 (divide by 204.8)
and send it out as serial data.
Because 204.8 is a fractional binary number you might change
the resistor ratio to give an easier math routine in your code.
For example, 200 would be good and 256 (a binary number)
would be better yet.
Comments Welcome!
Your desired input range is twenty volts. The ADC in the PICmetroid wrote:I really don't undrstand how you would program that into the pic.muntron wrote: Most PIC ADCs are 10bit, so 1 bit is 5/1024 or 0.00488V. A little math can convert, or you can calibrate your divider circuit.
can only operate up to the internal reference voltage (or the power
supply voltage) which is 5V. You need an external resistor divider to
scale the input voltage by 20/5 or 20/(Vref). Two resistors are
needed.
The input impedance of your ADC with divider is the sum of the
resistors, and if this loads your input voltage a buffer amplifier is
needed.
Once the ADC spits out a value your firmware must scale the
result to compensate for the resistor divider. If the ref was 5V
and a 20V signal was applied, the ADC would report
20/(5 * 1024) = 4096.
Next your firmware must change 4096 to 20 (divide by 204.8)
and send it out as serial data.
Because 204.8 is a fractional binary number you might change
the resistor ratio to give an easier math routine in your code.
For example, 200 would be good and 256 (a binary number)
would be better yet.
Comments Welcome!
Re: help with pic
Several compilers offer floating point routines, but of course this takes a lot of memory. The usual alternative is fixed point math. Here is some example code from MikroBasicmetroid wrote:I really don't undrstand how you would program that into the pic.muntron wrote: Most PIC ADCs are 10bit, so 1 bit is 5/1024 or 0.00488V. A little math can convert, or you can calibrate your divider circuit.
Code: Select all
t = ADC_read(2) ' get ADC value from 2nd channel
tlong = t*5000
t = tlong >> 10 'Shift to / by 1024
Code: Select all
ch = t div 1000 ' prepare value for diplay
Lcd_Chr(2,9,48+ch) ' write ASCII at 2nd row, 9th column
Lcd_Chr(2,10,".")
ch = (t div 100) mod 10
Lcd_Chr(2,11,48+ch)
ch = (t div 10) mod 10
Lcd_Chr(2,12,48+ch)
ch = t mod 10
Lcd_Chr(2,13,48+ch)

 Posts: 458
 Joined: Thu Feb 05, 2004 1:01 am
 Location: Minneapolis, MN
 Contact:
thanks
thanks muntron and everybody that really helped. I just needed held with the programming for the ADC
To make it really easy, not only scale the input accordingly but also pick an ADC reference that makes conversion trivial (or a lot simpler).
For example the LM34 outputs 10 mV per degree F. Pick your reference as 2.55V and then each unit of an 8bit ADC is equal to one degree. 100F is 1V or a value of 100. With a 10 bit ADC, you divide by 4 (2 shifts) to get the integer temp. Granted, it won't work above 255 F but for many applications, that's just fine.
Selecting a reference that is some power of 2 relative to the ADC reading makes it easy to convert. So, in your case (I'm just guessing here), you could assume 10.24V max, use a by 4 voltage divider get 2.56 max. Use 2.55V as your ref and then your 12 bit ADC will give you 256/4096 mV per scaled ADC unit. Multiply by 4 (left shift 2) to get the original voltage value. If your original input will go above 10.24, you could move up to 20.48 as the max and apply an additional power of 2 (divide down by 8, multiply the result by 8 ).
Note that you need to be careful to live within the impedance input limits of the ADC and use 1% or better resistors. You can get precision references for a number of voltages.
edit: you might want to use a 4.096V reference. in that case presume your max is 4 * 4.096V (~16V), divide by 4 and then every unit of a 12 bit ADC is 1 mV which represents 4 mV of the original signal. That will give you better noise immunity.
For example the LM34 outputs 10 mV per degree F. Pick your reference as 2.55V and then each unit of an 8bit ADC is equal to one degree. 100F is 1V or a value of 100. With a 10 bit ADC, you divide by 4 (2 shifts) to get the integer temp. Granted, it won't work above 255 F but for many applications, that's just fine.
Selecting a reference that is some power of 2 relative to the ADC reading makes it easy to convert. So, in your case (I'm just guessing here), you could assume 10.24V max, use a by 4 voltage divider get 2.56 max. Use 2.55V as your ref and then your 12 bit ADC will give you 256/4096 mV per scaled ADC unit. Multiply by 4 (left shift 2) to get the original voltage value. If your original input will go above 10.24, you could move up to 20.48 as the max and apply an additional power of 2 (divide down by 8, multiply the result by 8 ).
Note that you need to be careful to live within the impedance input limits of the ADC and use 1% or better resistors. You can get precision references for a number of voltages.
edit: you might want to use a 4.096V reference. in that case presume your max is 4 * 4.096V (~16V), divide by 4 and then every unit of a 12 bit ADC is 1 mV which represents 4 mV of the original signal. That will give you better noise immunity.
Who is online
Users browsing this forum: No registered users and 3 guests