ADC (Analogue to Digital Conversion) is needed to interface many sensors with the microcontroller. The sensor output generally is in the analogue form and so we need to convert it to digital form so that microcontroller can understand it. Today in this tutorial we are going to work with ADC single and multiple channels in PIC18.
I am using PIC18F4550 and it have
- Single ADC with 13 channels. This means that we can connect 13 different ADC devices and work with them at the same time.
- ADC Resolution is 10 bit which means that the converted digital values will vary from 0-1023 (2^10)
The ADC in PIC18F4550 works with 5 Registers and they are as follows
- A/D Control Register 0 (ADCON0)
- A/D Result High Register (ADRESH)
- A/D Result Low Register (ADRESL)
- A/D Control Register 1 (ADCON1)
- A/D Control Register 2 (ADCON2)
Registers Used in ADC
In order to program the ADC module, we have to do the following
1.) Configure the A/D module:
• Configure analog pins, voltage reference and digital I/O (ADCON1)
• Select A/D input channel (ADCON0)
• Select A/D acquisition time (ADCON2)
• Select A/D conversion clock (ADCON2)
• Turn on A/D module (ADCON0)
2.) Start conversion:
• Set GO/DONE bit (ADCON0 register)
3.) Wait for A/D conversion to complete:
• Polling for the GO/DONE bit to be cleared
4.) Read A/D Result registers (ADRESH:ADRESL)
Some Insight into the CODE
ADCON1 = 0x0E; Selects the Vref Source as Vss and Vdd and AN0 is configured as analogue
ADCON2 = 0x92; A/D clock is FOSC/32, Acquasition time is 4TAD, and the A/D Result is RIGHT Justified.
Reads the particular channel by performing the following:-
ADCON0 =((channel<<2));selects the channel to read
ADCON0|=0x03;Turns the ADC ON (ADON) and START Conversion (GO)
while(ADCON0bits.GO_nDONE);wait for the conversion to complete by checking GO_nDONE bit
- return the 10 bit data obtained from the sensor
Using single channel is pretty straight forward. Read the ADC value from the respective channel and print in on the LCD or store it.
In order to read multiple channels, we need to first configure the respective channel Pins as analogue in the ADCON1 Register. For example if I want to read 3 channels AN0, AN1, AN2, I will configure Bits PCFG3:PCFG0 as 1100 to set these 3 Pins as analogue.
As shown above, we can read all channels one by one. We can also read them at any (random) point in our program.
YOU CAN DOWNLOAD FULL CODE AT THE END OF THIS POST
- Interface LCD 16×2 with STM32 without I2C January 21, 2020
- Interface DS3231 RTC module with STM32 January 13, 2020
- How to use Binary Semaphore in STM32 January 11, 2020
- Managing Multiple UARTs in STM32 December 18, 2019
- Multiplexer 74HC4051 and STM32 December 6, 2019
- Free RTOS Tutorial 2.0 with STM32 November 27, 2019