e. How do I use PIC18FxxJ50 A/D converter with USB memory read (DateMemRead) and write (DataMemWrite) commands?

ADFM = A/D result format select bit

“0” – left justified result (default)

“1” – right justified result

ADCAL = A/D calibration bit

“0” – A/D conversion

“1” – Will be performed on next A/D conversion (bit must be cleared by SW after the calibration)

<ACQT2 ACQT1 ACQT0> = A/D acquisition time

111 = 20 TAD

110 = 16 TAD

101 = 12 TAD

100 = 8 TAD

011 = 6 TAD

010 = 4 TAD

001 = 2 TAD

000 = 0 TAD

TAD = A/D conversion time per bit. A/D acquisition time is defined on basis of conversion time per bit. This is the minimum time to wait before the next acquisition starts.

< ADCS2 ADCS1 ADCS0> = A/D Conversion Clock Select bits

110 = FOSC/64

101 = FOSC/16

100 = FOSC/4

011 = FRC (clock derived from A/D RC oscillator)

010 = FOSC/32

001 = FOSC/8

000 = FOSC/2

FOSC is microcontroller CPU oscillator frequency. A/D Conversion Clock Select bits combination defines A/D clock frequency divider. Higher crystal resonator frequency requires a lower conversion clock frequency (higher divider).

Here is a sample program to execute A/D conversion and acquire the 10-bit result using DataMemWrite, DataMemRead, SetDataMemBit and ClearDataMemBit commands:

Dim ADchannel As Byte = 0 ' Select an analog input channel

Dim result As Int16

Const ADCON0 as int16 = 0xFC2

Const ADCON1 as int16 = 0xFC1

Const ANCON0 as int16 = 0xF48

Const ANCON1 as int16 = 0xF49

Const ADRESH as int16 = 0xFC4

Const ADRESL as int16 = 0xFC3

If PIC.IsConnected(&H4D8, &HE001) Then

If Not PIC.IsConnected() Then ' Connect microcontroller if not connected

If PIC.Open(&H4D8, &HE001) < 0 Then

goto Error

End If

End If

Dim PIC As SVLib_PIC18F24J50.SVPICAPI = New SVLib_PIC18F24J50.SVPICAPI

PIC.DataMemWrite(ADCON0, 1 Or (ADchannel << 2))

PIC.DataMemWrite(ADCON0, 0x9E) ‘ Set ADC operation mode

PIC.SetDataMemBit(ADCON0, 1) ' Start a conversion

While (PIC.DataMemRead(ADCON0) And 2) = 2 ' Wait until the conversion is finished

End While

result = (PIC.DataMemRead(ADRESH) << 8) Or PIC.DataMemRead(ADRESL)

End If

Exit ....

Error:

Result is a 10-bit value. The example above is without error control and will malfunction is an error occurs. Please, include “if” sentences to verify each of the 4 data memory access functions for negative values upon execution. A negative value is an error code. Here is an example on how to include error code verification:

If PIC.DataMemWrite(ADCON0, 0x9E)<0 then

goto Error

End If

Dim x As Integer

x = PIC.DataMemRead(ADCON0)

If x < 0 Then

goto Error

End If

Label1.Text = CStr(x) ' print out a decimal value of a memory location

VCFG1 = ADC negative reference voltage select:

“0” – negative voltage reference input is internally connected to the ground (GND, 0 V) - default

“1” – AN2 input is used to supply negative reference voltage

VCFG0 = ADC positive reference voltage select:

“0” – positive voltage reference input is internally connected to Vdd (3.3 V) - default

“1” – AN3 input is used to supply positive reference voltage

<CH3 CH2 CH1 CH0> = x = 4 – bit value that defines the currently selected analog input:

x is the analog input number (ANx) which is connected to the A/D converter.

GO/DONE = A/D conversion status bit:

“0” – A/D converter is idle

“1” – A/D conversion is in progress

Microcontroller CPU may also set or reset this bit programmatically. When the bit is reset (set to logical “0”) A/D converter immediately goes to idle, if a conversion is in progress. If the bit is set to logical “1” the converter starts a new conversion. When the conversion finishes the bit is automatically reset. This enables CPU to determine the finalization of the A/D conversion.

ADON = A/D converter ON/OFF switch:

“0” – A/D converter module is OFF (default)

“1” – A/D converter module is ON (SW must enable ADC)

ADCON1 (0xFC1) register has the following structure:

PIC18FxxJ50 A/D converter (ADC) has the following registers:

- ADCON0 on data memory address 0xFC2 is control register 0

- ADCON1 on data memory address 0xFC1 is control register 1

- ANCON0 on data memory address 0xF48 is analog input select register 0

- ANCON1 on data memory address 0xF49 is analog input select register 1

- ADRESH on data memory address 0xFC4 is ADC result register high byte

- ADRESL on data memory address 0xFC3 is ADC result register low byte

ADRESH register holds upper 8 bits of the 10-bit A/D conversion result, if the ADFM bit of ADCON1 register is set to “left justified” (ADFM=0). Two lower 2 bits are stored to bits 7 and 6 of ADRESL register. The remaining ADRESL bits are set to 0.

ADRESH register bits 1 and 0 hold the upper 2 bits of the 10-bit A/D conversion result, if the ADFM bit of ADCON1 register is set to “right justified” (ADFM=1). The remaining ADRESH bits are “0”. Lower 8 bits are stored in ADRESL register.

The ADCON0 register is used to control A/D conversion and to select one of 16 input channels, while the ADCON1 register defines result format, calibration, acquisition time and A/D converter clock source.

The ANCONx registers are used to enable or disable digital logic in particular inputs. There are 10-inputs on PIC18F2xJ50. Usually, an input is used as either analog or digital input but it can be both, if a bit higher energy consumption is not an issue. A/D converter operates on all AN inputs regardless ANCONx seting. However, if an input in ANCONx is set to be analog only by setting the corresponding bit to 0, its digital logic is detached and only A/D converter may access the input.

The ANCON0 control inputs AN0 (ANCON0 register bit 0) through AN4 (ANCON0 register bit 4) and ANCON1 control inputs AN8 (ANCON0 register bit 0) through AN12 (ANCON1 register bit 5).

ANCON1 bit 7 (VBGEN) is used set a 10 ms time gap when A/D analog input multiplexor switcher to another input (contents of CHx bits in ADCON0 register changes).

ANCON1 bit 6 is reserved and should always be set to “0” to lower power the A/D converter module consumption.

ADCON0 (0xFC2) register has the following structure: