PDA

View Full Version : PIC16F18446 - 12 bits ADC gives 16 bits results!!!!



flotulopex
- 24th February 2024, 18:28
Still going ahead with the PIC16F18446...

I'm testing the 12 bits ADC.

First, I had to find out the correspondance between Pin and Channel (I'm using a 20-pin device). Here it is:
9639


The code I use is here:
' PIC 16F18446
' ====== FUSES ================================================== ===================================
#CONFIG
__config _CONFIG1, _FEXTOSC_OFF & _RSTOSC_HFINT1 & _CLKOUTEN_OFF & _CSWEN_ON & _FCMEN_ON
__config _CONFIG2, _MCLRE_ON & _PWRTS_PWRT_64 & _LPBOREN_OFF & _BOREN_SBOREN & _BORV_LO & _ZCDDIS_OFF & _PPS1WAY_OFF & _STVREN_ON
__config _CONFIG3, _WDTCPS_WDTCPS_31 & _WDTE_SWDTEN & _WDTCWS_WDTCWS_7 & _WDTCCS_LFINTOSC
__config _CONFIG4, _BBSIZE_BB512 & _BBEN_OFF & _SAFEN_OFF & _WRTAPP_OFF & _WRTB_OFF & _WRTC_OFF & _WRTD_OFF & _WRTSAF_OFF & _LVP_OFF
__config _CONFIG5, _CP_OFF
#ENDCONFIG

' ====== REGISTERS ================================================== ===============================
' Oscillator Control Register1
OSCCON1 = %01100000 ' $60

' HFFRQ - Oscillator's Frequency Selection Register
OSCFRQ = %00000010 ' 4MHz

' ====== DEFINES ================================================== =================================
DEFINE ADC_BITS 12 'Number of bits in ADCIN result

' ====== VARIABLES ================================================== ===============================
ADC_Value var word
Serial_Out VAR PORTB.7
Mode Var BYTE

' ====== INITIALIZE VARIABLES ================================================== ====================
Mode = 84 ' serial com BdRate settings

' ====== TEST PROGRAM ================================================== ============================
TEST:
ADCIN 23, ADC_Value ' ADC channel 23 is RC7
Serout2 Serial_Out,Mode,["ADC in: ", dEC ADC_Value,13,10]
PAUSE 500
GOTO TEST
END


Now, the strange thing is: where I expect a 12 bits result (0..4095), I get a 16 bits value 0..65520 as result and it's amazingly steady, not a digit variation!!!

I'm using a 10 turns 10kOhms pot and that's all.

How can that be please? :confused:

Ioannis
- 24th February 2024, 21:48
Your code does not set the ADCON0 register properly to right justify the result. The default is Left justification, hence the 65520, or FFF0 or 1111 1111 1111 0000 result, which seems as 12 bit just fine, only left justified.

You need to set FRM bit to 1. It is the bit 2 in the ADCON0 register. Look at the page 536 of the datasheet. There are many other settings regarding ADC converter if you need other features.

I bet that your first reading after 0 was 16 then 32, then 48 etc instead of 1,2,3

Ioannis

flotulopex
- 25th February 2024, 08:15
Hum....its now obvious that I didn't read the DS first :o

Thanks a lot for your time and patience for dreamers like me.

Okay, let's go to page 536 then....


PS: I forgot to mention that your information makes my PIC show the "correct" values now

Ioannis
- 25th February 2024, 12:04
Glad you got it working.

Ioannis