PDA

View Full Version : ADCIN Problems with PIC16F1783



Aussie Barry
- 25th April 2014, 16:02
Hi All,

I am having no luck trying to configure AN0 on a PIC16F1783.
My code is as follow:



' Test results displayed on a 4 x 20 LCD display.
' Connect LCD D4 to RC4 (Pin 15)
' Connect LCD D5 to RC5 (Pin 16)
' Connect LCD D6 to RC6 (Pin 17)
' Connect LCD D7 to RC7 (Pin 18)
' Connect LCD RS to RB2 (Pin 23)
' Connect LCD E to RB1 (Pin 22)
' Connect ADC1 to AN0 (Pin 2)
' Pin 2 fed from the wiper of a 10k pot with the pot connected across Vdd & Vss


#CONFIG
__config _CONFIG1, _FOSC_INTOSC & _MCLRE_OFF & _CP_OFF & _CPD_OFF
__config _CONFIG2, _PLLEN_ON & _LVP_OFF
#ENDCONFIG

OSCCON = %11111000 ; Fosc = 32MHz
; Clock determined by Fosc<1:0> Config Word
DEFINE OSC 32 ; Define oscillator as 32MHz

' Define LCD registers and bits
DEFINE LCD_DREG PORTC
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 2
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 1
DEFINE LCD_BITS 4
DEFINE LCD_LINES 4
PAUSE 100 ' Pause to initialise LCD Display

' Define ADCIN parameters
DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS

' Set up Registers
ANSELA = %00000001 ' Enable AN0
ANSELB = 0 ' Disable PORTB ADC
DACCON0.7 = 0 ' Disable DAC
CM1CON0.7 = 0 ' Disable comparator 1
CM2CON0.7 = 0 ' Disable comparator 2
CM3CON0.7 = 0 ' Disable comparator 3
TRISA = %11111111 ' Set PORTA as digital inputs
TRISB = %11111001 ' Set PORTB.1 and 2 as digital outputs
TRISC = %00001111 ' Set PORTC.4 - 7 as digital outputs
ADCON0 = %10000001 ' Select 10 Bit ADC, AN0 and Enable ADC
ADCON1 = %11110000 ' 2's Complement Output Format
' Set A/D clock to FRC
' Set Vref = VDD

' Define program variables
Adval1 var word ' ADC variable

Start:
ADCIN 0, Adval1
LCDOUT $FE, $01
LCDOUT $FE, $80, DEC Adval1
Pause 300
Goto Start

END


I measure the voltage at pin 2 and confirm it swings from 0 to +5V but the displayed values of the ADCIN result merely jiggles around 2-6.
I can confirm that the LCD is working properly as I am able to display text without problem.

The ADC module on the 16F1783 is somewhat different to anything I have used before. It does not have an option for Left or Right justified output, rather ADFM bit selects between sign-magnitude or 2's complement output formatting.
I have selected the 2's complement option as it most closely resembles the right justified output that I am used to working with. I do not believe this is the cause of my problem but thought it pertinent to mention.

Has anyone dealt with this device previously or had any luck setting up ADC on AN0?
Can anyone see any glaring mistakes in my setup or code?

All comments and suggestions would be greatly appreciated.

Cheers
Barry
VK2XBP

(PBP 3.0.5.4 + MCS Ver 5.0.0.0)

Aussie Barry
- 25th April 2014, 17:08
Hi All,

I am pleased to report that I have resolved this problem :)
I had omitted setting up the ADCON2 register


ADCON2 = %00001111


I also needed to set the output format to Sign-Magnitude and then shift the result six places to the right to achieve right justified result


ADCIN 0, Adval1
Adval1 = Adval1 >> 6


Hopefully this post may help someone in the future.

Cheers
Barry
VK2XBP