ADC is essentially important as most of the sensors acquire data in analogue manner. Thus, for the microcontroller to process, these data are required to be converted to digital form. In PIC18F4550, the ADC span from port A to port B giving the developer 13 channels of ADC. Figure 1 and 2 shows the Registers related. Here, we would focus on ADCON1 register.

Using PIC18F4550 ADC in MCC18 Figure 1: Registers related to port A

Using PIC18F4550 ADC in MCC18 Figure 2: Registers related to port B

Let’s begin with ADCON0 register. This register is used to select the channel for ADC from channel 0 to channel 12 by controlling CHS3: CHS0. ADON will be used to on/off the ADC module while to determine whether the ADC conversion has completed, GO/DONE’ can be checked. When GO/DONE’ = 1, the ADC conversion is completed. Vice versa, when GO/DONE’ = 0, this means that the A/C conversion is completed and idle.

PIC18F4550 ADC in MCC18 Figure 3: ADCON0 control registers

Another important register to be taken care of when using ADC is ADCON1. This register allows to set whether the input channel will be digital or analogue by using PCFG3: PCFG0, which is bit 0 to bit bit 3 of ADCON1. VCFG1 and VCFG2 allows you to have some external voltage reference.

PIC18F4550 ADC icFigure 4: ADCON1 control registers

Upon setting ADCON0 and ADCON1, ADCON2 has to be set also. This register is used to control the format and the A/D acquisition time. In addition, it is also used to select the A/D conversion clock. By referring to Figure 6 and 7, the appropriate minimum acquisition time is calculated and AD clock source is chosen based on the PIC clock frequency. After doing so, then, the appropriate settings can be set.

PIC18F4550 ADCFigure 5: ADCON2 control registers

PIC18F4550 icFigure 6: Equations to calculate minimum acquisition time

PIC18F4550 ic programmingFigure 7: AD clock source settings

Figure 8 shows the A/D block diagram. From here, we should notice that this device’s ADC has some limitation. That is, only one channel can be active at a time by controlling CHS3: CHS0. Thus, simultaneous acquiring for data more than 1 channel is impossible but parallel ADC IC can be added to perform this task.

PIC18F4550 ic programmingFigure 8: A/D Diagram

Steps to configure ADC

1. Configure ADC:
a. Configure analogue pins, voltage references as well as digital I/O through ADCON 1
b. Set A/D input channel through ADCON0 (12 channels but only one can be selected at a time)
c. Select A/D time acquisition time and conversion clock through ADCON 2
d. Turn on ADC through ADCON0
2. If interrupt is acquired, it can be set by:
a. ADIF = 0
b. ADIE = 1
c. GIE = 1
3. Wait for acquisition time if acquired
4. Start conversion by setting GO/DONE’ bit in the ADCON0
5. Wait for the ADC to complete by:
a. Checking for the GO/DONE’ bit to be cleared
b. Wait for ADC interrupt
6. Read the A/D Result Register ( ADRESH: ADRESL) ADIF = 0 if interrupt is used
7. Go to step 1 to reconfigure another pin to acquire data or continue by going through step 3 to 5.
8. A/D conversion per bit is defined as TAD. Before the next acquisition can be done, a minimum of 3 TAD is required.

Sample coding

sample coding pic chip

sample coding pic chipThe sample code above acquires data from AN0 or channel 0 and display them to PC using serial. This is to verify that our sampling data is correct.
Conclusion
Setting up the ADC correctly is important to make sure that we acquire the correct data.

About

Engineering Student

Leave a Reply

Your email address will not be published. Required fields are marked *