Wednesday, April 23, 2008

Let's C... Some ADC... 4U2C...



Updated:

After some of you got a little sober after the smooth operator 60s' twists, here's an an example of ADC usage in MikroC.

The codes are commented so you should roughly knows what the codes is all about and what each line is doing. I'll do a write up on ADC on the next post. In most cases, some form of scaling is required so some understanding of ADC reference voltage is crucial.

How the board is wired up for the lesson... noticed the purposed-built LCD ribbon cable?

Update 080425: A short brief on ADC.
PIC18F4550 (and most ADC capable PIC) has a 10-bit ADC core. The ADC is based on analog reading between VRef- to VRef+. When input is at VRef-, the value returned will be 0 (zero) and when input is at VRef+, the value returned will be 1023 (2^10 - 1, i.e., 2 to the power of 10 minus 1).
In the current example, via setting done with ADCON1, VRef- is set to VSS (GND) and VRef+ is set to VDD (+5V). Under this environment, the resolution of the ADC is +5V/1024 = about 5mV. When the potentiometer is at minimum (0V) the result is 0 and when at maximum (+5V) the result is 1023. To get the actual voltage reading, you need to do scaling, the math as follows:
Actual Voltage = (ADC Reading * Value at VRef+) / 1024
i.e., if result is 512, then the actual voltage is 512 * 5 / 1024 = 2.5V

If you need higher resolution, you can provide the PIC with your own VRef- & VRef+ directly to the AN2 & AN3 pins. Change ADCON1 settings to 0b00111011. If you set VRef+ to about 1V, the resolution becomes 1mV (1V/1024).

For quick reference, ADCON1 setting information is below:

On PIC18F4550, the analog pins are on 3 separate ports... do your own searching below... Remember to set TRISA, TRISB, TRISE and ADCON1 configuration accordingly...

4 comments:

9w2gu said...

Sir! Are the setting CMCON and ADCON1 PIC dependent? Meaning only for your PIC or all PIC's that use ADC?

Unknown said...

Yes.
CMCON pretty much standard but ADCON1 vary occasionally between PIC. You need to check the datasheet under the ADC section to see how to set it up. MikroC takes care of the rest, things like left/right justification, acquisition time, etc...

Best way to find out is to look at the pins descriptions... if you see multiple functions (IO/ANx/CMx/RX/etc) then you know you have to crosscheck multiple section of the datasheet...

Anonymous said...

can this code used to transfer the digital signal using ir led???

can u tell me all pin connection used for this code...

which pin no for analog signal input and which digital output signal pin no???

Unknown said...

To transfer digital signal using IR, best to use normal digital IO... ADC not required. Use the receiver for TV IR and connect output to digital IO. For IR transmit, encode the signal with 38-40kHz carrier to avoid false triggering by ambient IR...