help with pic

Electronics Computer Programming Q&A
Post Reply
metroid
Posts: 12
Joined: Mon Feb 25, 2008 9:29 am
Location: NY
Contact:

help with pic

Post by metroid » Wed Apr 09, 2008 3:26 pm

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 0-20. also does the pic output TTL or RS232 serial data

muntron
Posts: 13
Joined: Tue Feb 12, 2008 9:02 am
Location: Colorado, USA
Contact:

Re: help with pic

Post by muntron » Wed Apr 09, 2008 4:20 pm

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 0-20. also does the pic output TTL or RS232 serial data
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.
The PIC USART outputs TTL, so if you have to convert to RS-232, you will need to add something like a MAX232 chip.

User avatar
jaem
Posts: 148
Joined: Thu Feb 15, 2007 3:36 pm
Location: BC, Canada
Contact:

Post by jaem » Wed Apr 09, 2008 4:41 pm

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.

metroid
Posts: 12
Joined: Mon Feb 25, 2008 9:29 am
Location: NY
Contact:

reply

Post by metroid » Thu Apr 10, 2008 9:44 am

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

metroid
Posts: 12
Joined: Mon Feb 25, 2008 9:29 am
Location: NY
Contact:

Re: help with pic

Post by metroid » Thu Apr 10, 2008 9:46 am

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.
I really don't undrstand how you would program that into the pic.

Bigglez
Posts: 1282
Joined: Mon Oct 15, 2007 7:39 pm
Contact:

Re: help with pic

Post by Bigglez » Thu Apr 10, 2008 10:47 am

Greetings (No First Name Supplied),
metroid wrote:
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.
I really don't undrstand how you would program that into the pic.
Your desired input range is twenty volts. The ADC in the PIC
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!

muntron
Posts: 13
Joined: Tue Feb 12, 2008 9:02 am
Location: Colorado, USA
Contact:

Re: help with pic

Post by muntron » Tue Apr 15, 2008 7:38 am

metroid wrote:
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.
I really don't undrstand how you would program that into the 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 MikroBasic

Code: Select all

    t  = ADC_read(2)         ' get ADC value from 2nd channel

    tlong = t*5000
    t     = tlong >> 10 'Shift to / by 1024
The value in t is now scaled by 5/1024 but with the decimal point between the third and fourth digits. This can then be displayed as follows:

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)

Engineer1138
Posts: 458
Joined: Thu Feb 05, 2004 1:01 am
Location: Minneapolis, MN
Contact:

Post by Engineer1138 » Tue Apr 15, 2008 12:25 pm

You never really said what part of the project you need help with.

As far as the input dividers go, just use a trim pot rather than fixed resistors if this is a one-off and you don't need to worry about software calibration.

metroid
Posts: 12
Joined: Mon Feb 25, 2008 9:29 am
Location: NY
Contact:

thanks

Post by metroid » Mon Apr 21, 2008 8:44 am

thanks muntron and everybody that really helped. I just needed held with the programming for the ADC

User avatar
philba
Posts: 2050
Joined: Tue Nov 30, 2004 1:01 am
Location: Seattle
Contact:

Post by philba » Mon Apr 21, 2008 11:37 am

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest