PIC32 Analogue to Digitl

Analogue to Digital Conversion

openadc
closeadc
adc()  

The built in ADC on the PIC has 15 channels each of which has a 10 bit (0 to 1023) resolution. The channels share the pins with port b thus:

ADC Channel Port Pin
AN0 RB0
AN1 RB1
AN2 RB2
AN3 RB3
AN4 RB4
AN5 RB5
AN6 RB6
AN7 RB7
AN8 RB8
AN9 RB9
AN10 RB10
AN11 RB11
AN12 RB12
AN13 RB13
AN14 RB14
AN15 RB15


Opening an ADC channel will disable the digital I/O, and set the port to an input. The above words will provide basic functionality and may serve in most instances, however the ADC module on the PIC is a very versatile module with many options. If these need to be accessed then the registers will need to be directly accessed. If this is the case some examples are shown here.

openadc
openadc <sample> <list of channels>
openadc 31 2
openadc 31 0,4,9,2

Before using an ADC channel it needs to be opened first, this statement can open one or many channels:

<sample> Is a number form 1 to 31 and will determine how long the sampling time is. This value is multiplied by the conversion time which is fixed at approximately 1.5uS. This if a sample of 10 is used then the sample time is 15uS.

<list of inputs> This is a list of all of the ADC channels that are required to be open, for example 0,8,2 would open 3 channels, channel 0, channel 8 and channel 2.

closeadc
closeadc
closeadc

Closes all open ADC channels and turns the ports back into digital I/O

adc()
adc (channel)
a=adc(2)

This will read the analogue channel. The conversion process will take <sample>*1.5+1.5 uS, so if a sample time of 10 has been chosen the process will take 16.6uS. The statement adc() will wait until the conversion process is complete before returning.