News:

Let's find out together what makes a PIC Tick!

Main Menu

Using the ADC on dsPIC33CK128MP505

Started by charliecoutas, Nov 10, 2021, 03:31 PM

Previous topic - Next topic

charliecoutas

Has anybody used the ADC on the 33CK chips? I need fast conversions on two channels but am confused about the number of cores etc

Charlie

top204

#1
They are rather complex ADC peripherals on them, and I do not fully understand their operations. In fact they are far too complex for such a simple peripheral module. However, I have knocked together a simple library that will allow you to read the ADC on a dsPIC33CK device.

The code below is the library listing and should be named "ADC_dsPIC33CK.inc". It has quite a few $defines that are not used yet, but they will allow me to extend the library when I get the time, and when I understand the ADC peripheral better.

$ifndef ADC_DSPIC33CK_INC
$define ADC_DSPIC33CK_INC
'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Experimental ADC library for the dsPIC33xxCK devices
'
' Written for the Positron16 compiler by Les Johnson
'
' Channel availability
'
$if (_device = _33CK32MC102) Or (_device = _33CK32MP202) Or (_device = _33CK32MP502)        '
     Or (_device = _33CK64MC102) Or (_device = _33CK64MP202) Or (_device = _33CK64MP502)    '
     Or (_device = _33CK128MP202) Or (_device = _33CK128MP502) Or (_device = _33CK256MP202) '
     Or (_device = _33CK256MP502)
$define cADC_CHANNEL_COUNT 14

$elseif (_device = _33CK32MC103) Or (_device = _33CK32MC105) Or (_device = _33CK32MP203)    '
     Or (_device = _33CK32MP503) Or (_device = _33CK64MC103) Or (_device = _33CK64MC105)    '
     Or (_device = _33CK64MP203) Or (_device = _33CK64MP503) Or (_device = _33CK128MP203)   '
     Or (_device = _33CK128MP503) Or (_device = _33CK256MP203) Or (_device = _33CK256MP503)
$define cADC_CHANNEL_COUNT 18

$elseif (_device = _33CK32MP102) Or (_device = _33CK32MP103)                                '
     Or (_device = _33CK32MP105) Or (_device = _33CK32MP205) Or (_device = _33CK32MP505)    '
     Or (_device = _33CK64MP102) Or (_device = _33CK64MP103) Or (_device = _33CK64MP105)    '
     Or (_device = _33CK64MP205) Or (_device = _33CK64MP505) Or (_device = _33CK128MP205)   '
     Or (_device = _33CK128MP505) Or (_device = _33CK256MP205) Or (_device = _33CK256MP505)
$define cADC_CHANNEL_COUNT 21

$elseif (_device = _33CK32MP206) Or (_device = _33CK32MP506) Or (_device = _33CK64MP206)    '
     Or (_device = _33CK64MP506) Or (_device = _33CK128MP206) Or (_device = _33CK128MP506)  '
     Or (_device = _33CK256MP206) Or (_device = _33CK256MP506)
$define cADC_CHANNEL_COUNT 22

$elseif (_device = _33CK256MP305) Or (_device = _33CK256MP605) Or (_device = _33CK512MP305) '
     Or (_device = _33CK512MP605)
$define cADC_CHANNEL_COUNT 23

$elseif (_device = _33CK256MP306) Or (_device = _33CK256MP606) Or (_device = _33CK512MP306) '
     Or (_device = _33CK512MP606)
$define cADC_CHANNEL_COUNT 24

$elseif (_device = _33CK64MP208) Or (_device = _33CK64MP508) Or (_device = _33CK128MP208)   '
     Or (_device = _33CK128MP508) Or (_device = _33CK256MP208) Or (_device = _33CK256MP508)
$define cADC_CHANNEL_COUNT 26

$elseif (_device = _33CK256MP308) Or (_device = _33CK256MP608) Or (_device = _33CK512MP308) '
     Or (_device = _33CK512MP608)
$define cADC_CHANNEL_COUNT 28

$else
$define cADC_CHANNEL_COUNT 0

$endif

$if (_device = _33CK32MC102) Or (_device = _33CK32MC103) Or (_device = _33CK32MC105)        '
     Or (_device = _33CK64MC102) Or (_device = _33CK64MC103) Or (_device = _33CK64MC105)
$define cADC_CHANNEL_MAX 18

$elseif (_device = _33CK32MP102) Or (_device = _33CK32MP103)                                '
     Or (_device = _33CK32MP105) Or (_device = _33CK64MP102) Or (_device = _33CK64MP103)    '
     Or (_device = _33CK64MP105)
$define cADC_CHANNEL_MAX 21

$elseif (_device = _33CK32MP202) Or (_device = _33CK32MP203) Or (_device = _33CK32MP205)    '
     Or (_device = _33CK32MP206) Or (_device = _33CK32MP502) Or (_device = _33CK32MP503)    '
     Or (_device = _33CK32MP505) Or (_device = _33CK32MP506) Or (_device = _33CK64MP202)    '
     Or (_device = _33CK64MP203) Or (_device = _33CK64MP205) Or (_device = _33CK64MP206)    '
     Or (_device = _33CK64MP208) Or (_device = _33CK64MP502) Or (_device = _33CK64MP503)    '
     Or (_device = _33CK64MP505) Or (_device = _33CK64MP506) Or (_device = _33CK64MP508)    '
     Or (_device = _33CK128MP202) Or (_device = _33CK128MP203) Or (_device = _33CK128MP205) '
     Or (_device = _33CK128MP206) Or (_device = _33CK128MP208) Or (_device = _33CK128MP502) '
     Or (_device = _33CK128MP503) Or (_device = _33CK128MP505) Or (_device = _33CK128MP506) '
     Or (_device = _33CK128MP508) Or (_device = _33CK256MP202) Or (_device = _33CK256MP203) '
     Or (_device = _33CK256MP205) Or (_device = _33CK256MP206) Or (_device = _33CK256MP208) '
     Or (_device = _33CK256MP502) Or (_device = _33CK256MP503) Or (_device = _33CK256MP505) '
     Or (_device = _33CK256MP506) Or (_device = _33CK256MP508)
$define cADC_CHANNEL_MAX 26

$elseif (_device = _33CK256MP305) Or (_device = _33CK256MP306) Or (_device = _33CK256MP308) '
     Or (_device = _33CK256MP605) Or (_device = _33CK256MP606) Or (_device = _33CK256MP608) '
     Or (_device = _33CK512MP305) Or (_device = _33CK512MP306) Or (_device = _33CK512MP308) '
     Or (_device = _33CK512MP605) Or (_device = _33CK512MP606) Or (_device = _33CK512MP608)
$define cADC_CHANNEL_MAX 28

$elseif (_device = _33CK32MC102) Or (_device = _33CK32MC103) Or (_device = _33CK32MC105)    '
     Or (_device = _33CK64MC102) Or (_device = _33CK64MC103) Or (_device = _33CK64MC105)
$define cADC_CHANNEL_MAX 18

$elseif (_device = _33CK32MP102) Or (_device = _33CK32MP103) Or (_device = _33CK32MP105)    '
      Or (_device = _33CK64MP102) Or (_device = _33CK64MP103) Or (_device = _33CK64MP105)
$define cADC_CHANNEL_MAX 21

$elseif (_device = _33CK32MP202) Or (_device = _33CK32MP203) Or (_device = _33CK32MP205)    '
     Or (_device = _33CK32MP206) Or (_device = _33CK32MP502) Or (_device = _33CK32MP503)    '
     Or (_device = _33CK32MP505) Or (_device = _33CK32MP506) Or (_device = _33CK64MP202)    '
     Or (_device = _33CK64MP203) Or (_device = _33CK64MP205) Or (_device = _33CK64MP206)    '
     Or (_device = _33CK64MP208) Or (_device = _33CK64MP502) Or (_device = _33CK64MP503)    '
     Or (_device = _33CK64MP505) Or (_device = _33CK64MP506) Or (_device = _33CK64MP508)    '
     Or (_device = _33CK128MP202) Or (_device = _33CK128MP203) Or (_device = _33CK128MP205) '
     Or (_device = _33CK128MP206) Or (_device = _33CK128MP208) Or (_device = _33CK128MP502) '
     Or (_device = _33CK128MP503) Or (_device = _33CK128MP505) Or (_device = _33CK128MP506) '
     Or (_device = _33CK128MP508) Or (_device = _33CK256MP202) Or (_device = _33CK256MP203) '
     Or (_device = _33CK256MP205) Or (_device = _33CK256MP206) Or (_device = _33CK256MP208) '
     Or (_device = _33CK256MP502) Or (_device = _33CK256MP503) Or (_device = _33CK256MP505) '
     Or (_device = _33CK256MP506) Or (_device = _33CK256MP508)
$define cADC_CHANNEL_MAX 26

$elseif (_device = _33CK256MP305) Or (_device = _33CK256MP306) Or (_device = _33CK256MP308) '
     Or (_device = _33CK256MP605) Or (_device = _33CK256MP606) Or (_device = _33CK256MP608) '
     Or (_device = _33CK512MP305) Or (_device = _33CK512MP306) Or (_device = _33CK512MP308) '
     Or (_device = _33CK512MP605) Or (_device = _33CK512MP606) Or (_device = _33CK512MP608)
$define cADC_CHANNEL_MAX 28
$endif

$if (_device = _33CK256MP305) Or (_device = _33CK256MP306) Or (_device = _33CK256MP308)     '
     Or (_device = _33CK256MP605) Or (_device = _33CK256MP606) Or (_device = _33CK256MP608) '
     Or (_device = _33CK512MP305) Or (_device = _33CK512MP306) Or (_device = _33CK512MP308) '
     Or (_device = _33CK512MP605) Or (_device = _33CK512MP606) Or (_device = _33CK512MP608)
$define cADC_HAS_CH26
$define cADC_HAS_CH27
$endif

$if (_device = _33CK64MP208) Or (_device = _33CK64MP508) Or (_device = _33CK128MP208)       '
     Or (_device = _33CK128MP508) Or (_device = _33CK256MP208) Or (_device = _33CK256MP308) '
     Or (_device = _33CK256MP508) Or (_device = _33CK256MP608) Or (_device = _33CK512MP308) '
     Or (_device = _33CK512MP608)
$define cADC_HAS_CH22
$define cADC_HAS_CH23
$endif

$if (_device = _33CK32MP202) Or (_device = _33CK32MP203) Or (_device = _33CK32MP205)        '
     Or (_device = _33CK32MP206) Or (_device = _33CK32MP502) Or (_device = _33CK32MP503)    '
     Or (_device = _33CK32MP505) Or (_device = _33CK32MP506) Or (_device = _33CK64MP202)    '
     Or (_device = _33CK64MP203) Or (_device = _33CK64MP205) Or (_device = _33CK64MP206)    '
     Or (_device = _33CK64MP208) Or (_device = _33CK64MP502) Or (_device = _33CK64MP503)    '
     Or (_device = _33CK64MP505) Or (_device = _33CK64MP506) Or (_device = _33CK64MP508)    '
     Or (_device = _33CK128MP202) Or (_device = _33CK128MP203) Or (_device = _33CK128MP205) '
     Or (_device = _33CK128MP206) Or (_device = _33CK128MP208) Or (_device = _33CK128MP502) '
     Or (_device = _33CK128MP503) Or (_device = _33CK128MP505) Or (_device = _33CK128MP506) '
     Or (_device = _33CK128MP508) Or (_device = _33CK256MP202) Or (_device = _33CK256MP203) '
     Or (_device = _33CK256MP205) Or (_device = _33CK256MP206) Or (_device = _33CK256MP208) '
     Or (_device = _33CK256MP305) Or (_device = _33CK256MP306) Or (_device = _33CK256MP308) '
     Or (_device = _33CK256MP502) Or (_device = _33CK256MP503) Or (_device = _33CK256MP505) '
     Or (_device = _33CK256MP506) Or (_device = _33CK256MP508) Or (_device = _33CK256MP605) '
     Or (_device = _33CK256MP606) Or (_device = _33CK256MP608) Or (_device = _33CK512MP305) '
     Or (_device = _33CK512MP306) Or (_device = _33CK512MP308) Or (_device = _33CK512MP605) '
     Or (_device = _33CK512MP606) Or (_device = _33CK512MP608)
$define cADC_HAS_CH24
$define cADC_HAS_CH25
$endif

$if (_device = _33CK32MC102) Or (_device = _33CK32MC103)                                    '
     Or (_device = _33CK32MC105) Or (_device = _33CK32MP102) Or (_device = _33CK32MP103)    '
     Or (_device = _33CK32MP105) Or (_device = _33CK32MP205) Or (_device = _33CK32MP206)    '
     Or (_device = _33CK32MP505) Or (_device = _33CK32MP506) Or (_device = _33CK64MC102)    '
     Or (_device = _33CK64MC103) Or (_device = _33CK64MC105) Or (_device = _33CK64MP102)    '
     Or (_device = _33CK64MP103) Or (_device = _33CK64MP105) Or (_device = _33CK64MP205)    '
     Or (_device = _33CK64MP206) Or (_device = _33CK64MP208) Or (_device = _33CK64MP505)    '
     Or (_device = _33CK64MP506) Or (_device = _33CK64MP508) Or (_device = _33CK128MP205)   '
     Or (_device = _33CK128MP206) Or (_device = _33CK128MP208) Or (_device = _33CK128MP505) '
     Or (_device = _33CK128MP506) Or (_device = _33CK128MP508) Or (_device = _33CK256MP205) '
     Or (_device = _33CK256MP206) Or (_device = _33CK256MP208) Or (_device = _33CK256MP305) '
     Or (_device = _33CK256MP306) Or (_device = _33CK256MP308) Or (_device = _33CK256MP505) '
     Or (_device = _33CK256MP506) Or (_device = _33CK256MP508) Or (_device = _33CK256MP605) '
     Or (_device = _33CK256MP606) Or (_device = _33CK256MP608) Or (_device = _33CK512MP305) '
     Or (_device = _33CK512MP306) Or (_device = _33CK512MP308) Or (_device = _33CK512MP605) '
     Or (_device = _33CK512MP606) Or (_device = _33CK512MP608)
$define cADC_HAS_CH16
$endif

$if (_device = _33CK32MC103) Or (_device = _33CK32MC105)                                    '
     Or (_device = _33CK32MP102) Or (_device = _33CK32MP103) Or (_device = _33CK32MP105)     '
     Or (_device = _33CK32MP203) Or (_device = _33CK32MP205) Or (_device = _33CK32MP206)     '
     Or (_device = _33CK32MP503) Or (_device = _33CK32MP505) Or (_device = _33CK32MP506)     '
     Or (_device = _33CK64MC103) Or (_device = _33CK64MC105) Or (_device = _33CK64MP102)     '
     Or (_device = _33CK64MP103) Or (_device = _33CK64MP105) Or (_device = _33CK64MP203)     '
     Or (_device = _33CK64MP205) Or (_device = _33CK64MP206) Or (_device = _33CK64MP208)     '
     Or (_device = _33CK64MP503) Or (_device = _33CK64MP505) Or (_device = _33CK64MP506)     '
     Or (_device = _33CK64MP508) Or (_device = _33CK128MP203) Or (_device = _33CK128MP205)   '
     Or (_device = _33CK128MP206) Or (_device = _33CK128MP208) Or (_device = _33CK128MP503)  '
     Or (_device = _33CK128MP505) Or (_device = _33CK128MP506) Or (_device = _33CK128MP508)  '
     Or (_device = _33CK256MP203) Or (_device = _33CK256MP205) Or (_device = _33CK256MP206)  '
     Or (_device = _33CK256MP208) Or (_device = _33CK256MP305) Or (_device = _33CK256MP306)  '
     Or (_device = _33CK256MP308) Or (_device = _33CK256MP503) Or (_device = _33CK256MP505)  '
     Or (_device = _33CK256MP506) Or (_device = _33CK256MP508) Or (_device = _33CK256MP605)  '
     Or (_device = _33CK256MP606) Or (_device = _33CK256MP608) Or (_device = _33CK512MP305)  '
     Or (_device = _33CK512MP306) Or (_device = _33CK512MP308) Or (_device = _33CK512MP605)  '
     Or (_device = _33CK512MP606) Or (_device = _33CK512MP608)
$define cADC_HAS_CH15
$endif

$if (_device = _33CK32MP102) Or (_device = _33CK32MP103) Or (_device = _33CK32MP105)        '
     Or (_device = _33CK32MP203) Or (_device = _33CK32MP205) Or (_device = _33CK32MP206)     '
     Or (_device = _33CK32MP503) Or (_device = _33CK32MP505) Or (_device = _33CK32MP506)     '
     Or (_device = _33CK64MC103) Or (_device = _33CK64MC105) Or (_device = _33CK64MP102)     '
     Or (_device = _33CK64MP103) Or (_device = _33CK64MP105) Or (_device = _33CK64MP203)     '
     Or (_device = _33CK64MP205) Or (_device = _33CK64MP206) Or (_device = _33CK64MP208)     '
     Or (_device = _33CK64MP503) Or (_device = _33CK64MP505) Or (_device = _33CK64MP506)     '
     Or (_device = _33CK64MP508) Or (_device = _33CK128MP203) Or (_device = _33CK128MP205)   '
     Or (_device = _33CK128MP206) Or (_device = _33CK128MP208) Or (_device = _33CK128MP503)  '
     Or (_device = _33CK128MP505) Or (_device = _33CK128MP506) Or (_device = _33CK128MP508)  '
     Or (_device = _33CK256MP203) Or (_device = _33CK256MP205) Or (_device = _33CK256MP206)  '
     Or (_device = _33CK256MP208) Or (_device = _33CK256MP305) Or (_device = _33CK256MP306)  '
     Or (_device = _33CK256MP308) Or (_device = _33CK256MP503) Or (_device = _33CK256MP505)  '
     Or (_device = _33CK256MP506) Or (_device = _33CK256MP508) Or (_device = _33CK256MP605)  '
     Or (_device = _33CK256MP606) Or (_device = _33CK256MP608) Or (_device = _33CK512MP305)  '
     Or (_device = _33CK512MP306) Or (_device = _33CK512MP308) Or (_device = _33CK512MP605)  '
     Or (_device = _33CK512MP606) Or (_device = _33CK512MP608)
$define cADC_HAS_CH13
$endif

$if (_device = _33CK32MC103) Or (_device = _33CK32MC105)                                    '
     Or (_device = _33CK32MP102) Or (_device = _33CK32MP103) Or (_device = _33CK32MP105)     '
     Or (_device = _33CK32MP203) Or (_device = _33CK32MP205) Or (_device = _33CK32MP206)     '
     Or (_device = _33CK32MP503) Or (_device = _33CK32MP505) Or (_device = _33CK32MP506)     '
     Or (_device = _33CK64MC103) Or (_device = _33CK64MC105) Or (_device = _33CK64MP102)     '
     Or (_device = _33CK64MP103) Or (_device = _33CK64MP105) Or (_device = _33CK64MP203)     '
     Or (_device = _33CK64MP205) Or (_device = _33CK64MP206) Or (_device = _33CK64MP208)     '
     Or (_device = _33CK64MP503) Or (_device = _33CK64MP505) Or (_device = _33CK64MP506)     '
     Or (_device = _33CK64MP508) Or (_device = _33CK128MP203) Or (_device = _33CK128MP205)   '
     Or (_device = _33CK128MP206) Or (_device = _33CK128MP208) Or (_device = _33CK128MP503)  '
     Or (_device = _33CK128MP505) Or (_device = _33CK128MP506) Or (_device = _33CK128MP508)  '
     Or (_device = _33CK256MP203) Or (_device = _33CK256MP205) Or (_device = _33CK256MP206)  '
     Or (_device = _33CK256MP208) Or (_device = _33CK256MP305) Or (_device = _33CK256MP306)  '
     Or (_device = _33CK256MP308) Or (_device = _33CK256MP503) Or (_device = _33CK256MP505)  '
     Or (_device = _33CK256MP506) Or (_device = _33CK256MP508) Or (_device = _33CK256MP605)  '
     Or (_device = _33CK256MP606) Or (_device = _33CK256MP608) Or (_device = _33CK512MP305)  '
     Or (_device = _33CK512MP306) Or (_device = _33CK512MP308) Or (_device = _33CK512MP605)  '
     Or (_device = _33CK512MP606) Or (_device = _33CK512MP608)
$define cADC_HAS_CH12
$endif

$if (_device = _33CK32MC102) Or (_device = _33CK32MC103)                                    '
     Or (_device = _33CK32MC105) Or (_device = _33CK32MP102) Or (_device = _33CK32MP103)     '
     Or (_device = _33CK32MP105) Or (_device = _33CK32MP202) Or (_device = _33CK32MP203)     '
     Or (_device = _33CK32MP205) Or (_device = _33CK32MP206) Or (_device = _33CK32MP502)     '
     Or (_device = _33CK32MP503) Or (_device = _33CK32MP505) Or (_device = _33CK32MP506)     '
     Or (_device = _33CK64MC102) Or (_device = _33CK64MC103) Or (_device = _33CK64MC105)     '
     Or (_device = _33CK64MP102) Or (_device = _33CK64MP103) Or (_device = _33CK64MP105)     '
     Or (_device = _33CK64MP202) Or (_device = _33CK64MP203) Or (_device = _33CK64MP205)     '
     Or (_device = _33CK64MP206) Or (_device = _33CK64MP208) Or (_device = _33CK64MP502)     '
     Or (_device = _33CK64MP503) Or (_device = _33CK64MP505) Or (_device = _33CK64MP506)     '
     Or (_device = _33CK64MP508) Or (_device = _33CK128MP202) Or (_device = _33CK128MP203)   '
     Or (_device = _33CK128MP205) Or (_device = _33CK128MP206) Or (_device = _33CK128MP208)  '
     Or (_device = _33CK128MP502) Or (_device = _33CK128MP503) Or (_device = _33CK128MP505)  '
     Or (_device = _33CK128MP506) Or (_device = _33CK128MP508) Or (_device = _33CK256MP202)  '
     Or (_device = _33CK256MP203) Or (_device = _33CK256MP205) Or (_device = _33CK256MP206)  '
     Or (_device = _33CK256MP208) Or (_device = _33CK256MP305) Or (_device = _33CK256MP306)  '
     Or (_device = _33CK256MP308) Or (_device = _33CK256MP502) Or (_device = _33CK256MP503)  '
     Or (_device = _33CK256MP505) Or (_device = _33CK256MP506) Or (_device = _33CK256MP508)  '
     Or (_device = _33CK256MP605) Or (_device = _33CK256MP606) Or (_device = _33CK256MP608)  '
     Or (_device = _33CK512MP305) Or (_device = _33CK512MP306) Or (_device = _33CK512MP308)  '
     Or (_device = _33CK512MP605) Or (_device = _33CK512MP606) Or (_device = _33CK512MP608)
$define cADC_HAS_CH5
$endif

$if (_device = _33CK32MC102) Or (_device = _33CK32MC103)                                    '
     Or (_device = _33CK32MC105) Or (_device = _33CK32MP102) Or (_device = _33CK32MP103)     '
     Or (_device = _33CK32MP105) Or (_device = _33CK32MP205) Or (_device = _33CK32MP206)     '
     Or (_device = _33CK32MP505) Or (_device = _33CK32MP506) Or (_device = _33CK64MC102)     '
     Or (_device = _33CK64MC103) Or (_device = _33CK64MC105) Or (_device = _33CK64MP102)     '
     Or (_device = _33CK64MP103) Or (_device = _33CK64MP105) Or (_device = _33CK64MP205)     '
     Or (_device = _33CK64MP206) Or (_device = _33CK64MP208) Or (_device = _33CK64MP505)     '
     Or (_device = _33CK64MP506) Or (_device = _33CK64MP508) Or (_device = _33CK128MP205)    '
     Or (_device = _33CK128MP206) Or (_device = _33CK128MP208) Or (_device = _33CK128MP505)  '
     Or (_device = _33CK128MP506) Or (_device = _33CK128MP508) Or (_device = _33CK256MP205)  '
     Or (_device = _33CK256MP206) Or (_device = _33CK256MP208) Or (_device = _33CK256MP305)  '
     Or (_device = _33CK256MP306) Or (_device = _33CK256MP308) Or (_device = _33CK256MP505)  '
     Or (_device = _33CK256MP506) Or (_device = _33CK256MP508) Or (_device = _33CK256MP605)  '
     Or (_device = _33CK256MP606) Or (_device = _33CK256MP608) Or (_device = _33CK512MP305)  '
     Or (_device = _33CK512MP306) Or (_device = _33CK512MP308) Or (_device = _33CK512MP605)  '
     Or (_device = _33CK512MP606) Or (_device = _33CK512MP608)
$define cADC_HAS_CH17
$endif

$if (_device = _33CK32MC103) Or (_device = _33CK32MC105)                                    '
     Or (_device = _33CK32MP102) Or (_device = _33CK32MP103) Or (_device = _33CK32MP105)     '
     Or (_device = _33CK32MP203) Or (_device = _33CK32MP205) Or (_device = _33CK32MP206)     '
     Or (_device = _33CK32MP503) Or (_device = _33CK32MP505) Or (_device = _33CK32MP506)     '
     Or (_device = _33CK64MC103) Or (_device = _33CK64MC105) Or (_device = _33CK64MP102)     '
     Or (_device = _33CK64MP103) Or (_device = _33CK64MP105) Or (_device = _33CK64MP203)     '
     Or (_device = _33CK64MP205) Or (_device = _33CK64MP206) Or (_device = _33CK64MP208)     '
     Or (_device = _33CK64MP503) Or (_device = _33CK64MP505) Or (_device = _33CK64MP506)     '
     Or (_device = _33CK64MP508) Or (_device = _33CK128MP203) Or (_device = _33CK128MP205)   '
     Or (_device = _33CK128MP206) Or (_device = _33CK128MP208) Or (_device = _33CK128MP503)  '
     Or (_device = _33CK128MP505) Or (_device = _33CK128MP506) Or (_device = _33CK128MP508)  '
     Or (_device = _33CK256MP203) Or (_device = _33CK256MP205) Or (_device = _33CK256MP206)  '
     Or (_device = _33CK256MP208) Or (_device = _33CK256MP305) Or (_device = _33CK256MP306)  '
     Or (_device = _33CK256MP308) Or (_device = _33CK256MP503) Or (_device = _33CK256MP505)  '
     Or (_device = _33CK256MP506) Or (_device = _33CK256MP508) Or (_device = _33CK256MP605)  '
     Or (_device = _33CK256MP606) Or (_device = _33CK256MP608) Or (_device = _33CK512MP305)  '
     Or (_device = _33CK512MP306) Or (_device = _33CK512MP308) Or (_device = _33CK512MP605)  '
     Or (_device = _33CK512MP606) Or (_device = _33CK512MP608)
$define cADC_HAS_CH14
$endif

$if (_device = _33CK32MC102) Or (_device = _33CK32MC103)                                    '
     Or (_device = _33CK32MC105) Or (_device = _33CK32MP102) Or (_device = _33CK32MP103)     '
     Or (_device = _33CK32MP105) Or (_device = _33CK32MP202) Or (_device = _33CK32MP203)     '
     Or (_device = _33CK32MP205) Or (_device = _33CK32MP206) Or (_device = _33CK32MP502)     '
     Or (_device = _33CK32MP503) Or (_device = _33CK32MP505) Or (_device = _33CK32MP506)     '
     Or (_device = _33CK64MC102) Or (_device = _33CK64MC103) Or (_device = _33CK64MC105)     '
     Or (_device = _33CK64MP102) Or (_device = _33CK64MP103) Or (_device = _33CK64MP105)     '
     Or (_device = _33CK64MP202) Or (_device = _33CK64MP203) Or (_device = _33CK64MP205)     '
     Or (_device = _33CK64MP206) Or (_device = _33CK64MP208) Or (_device = _33CK64MP502)     '
     Or (_device = _33CK64MP503) Or (_device = _33CK64MP505) Or (_device = _33CK64MP506)     '
     Or (_device = _33CK64MP508) Or (_device = _33CK128MP202) Or (_device = _33CK128MP203)   '
     Or (_device = _33CK128MP205) Or (_device = _33CK128MP206) Or (_device = _33CK128MP208)  '
     Or (_device = _33CK128MP502) Or (_device = _33CK128MP503) Or (_device = _33CK128MP505)  '
     Or (_device = _33CK128MP506) Or (_device = _33CK128MP508) Or (_device = _33CK256MP202)  '
     Or (_device = _33CK256MP203) Or (_device = _33CK256MP205) Or (_device = _33CK256MP206)  '
     Or (_device = _33CK256MP208) Or (_device = _33CK256MP305) Or (_device = _33CK256MP306)  '
     Or (_device = _33CK256MP308) Or (_device = _33CK256MP502) Or (_device = _33CK256MP503)  '
     Or (_device = _33CK256MP505) Or (_device = _33CK256MP506) Or (_device = _33CK256MP508)  '
     Or (_device = _33CK256MP605) Or (_device = _33CK256MP606) Or (_device = _33CK256MP608)  '
     Or (_device = _33CK512MP305) Or (_device = _33CK512MP306) Or (_device = _33CK512MP308)  '
     Or (_device = _33CK512MP605) Or (_device = _33CK512MP606) Or (_device = _33CK512MP608)
$define cADC_HAS_CH8
$define cADC_HAS_CH9
$define cADC_HAS_CH10
$define cADC_HAS_CH11
$endif

$if (_device = _33CK64MP208) Or (_device = _33CK64MP508) Or (_device = _33CK128MP208)        '
     Or (_device = _33CK128MP508) Or (_device = _33CK256MP208) Or (_device = _33CK256MP308)  '
     Or (_device = _33CK256MP508) Or (_device = _33CK256MP608) Or (_device = _33CK512MP308)  '
     Or (_device = _33CK512MP608)
$define cADC_HAS_CH21
$endif

$if (_device = _33CK32MP102) Or (_device = _33CK32MP103)                                    '
     Or (_device = _33CK32MP105) Or (_device = _33CK64MP102) Or (_device = _33CK64MP103)     '
     Or (_device = _33CK64MP105) Or (_device = _33CK64MP208) Or (_device = _33CK64MP508)     '
     Or (_device = _33CK128MP208) Or (_device = _33CK128MP508) Or (_device = _33CK256MP208)  '
     Or (_device = _33CK256MP308) Or (_device = _33CK256MP508) Or (_device = _33CK256MP608)  '
     Or (_device = _33CK512MP308) Or (_device = _33CK512MP608)
$define cADC_HAS_CH20
$endif

$if (_device = _33CK32MP102) Or (_device = _33CK32MP103)                                    '
     Or (_device = _33CK32MP105) Or (_device = _33CK32MP206) Or (_device = _33CK32MP506)     '
     Or (_device = _33CK64MP102) Or (_device = _33CK64MP103) Or (_device = _33CK64MP105)     '
     Or (_device = _33CK64MP206) Or (_device = _33CK64MP208) Or (_device = _33CK64MP506)     '
     Or (_device = _33CK64MP508) Or (_device = _33CK128MP206) Or (_device = _33CK128MP208)   '
     Or (_device = _33CK128MP506) Or (_device = _33CK128MP508) Or (_device = _33CK256MP206)  '
     Or (_device = _33CK256MP208) Or (_device = _33CK256MP306) Or (_device = _33CK256MP308)  '
     Or (_device = _33CK256MP506) Or (_device = _33CK256MP508) Or (_device = _33CK256MP606)  '
     Or (_device = _33CK256MP608) Or (_device = _33CK512MP306) Or (_device = _33CK512MP308)  '
     Or (_device = _33CK512MP606) Or (_device = _33CK512MP608)
$define cADC_HAS_CH19
$endif

$if (_device = _33CK32MP102) Or (_device = _33CK32MP103)                                    '
     Or (_device = _33CK32MP105) Or (_device = _33CK32MP205) Or (_device = _33CK32MP206)     '
     Or (_device = _33CK32MP505) Or (_device = _33CK32MP506) Or (_device = _33CK64MP102)     '
     Or (_device = _33CK64MP103) Or (_device = _33CK64MP105) Or (_device = _33CK64MP205)     '
     Or (_device = _33CK64MP206) Or (_device = _33CK64MP208) Or (_device = _33CK64MP505)     '
     Or (_device = _33CK64MP506) Or (_device = _33CK64MP508) Or (_device = _33CK128MP205)    '
     Or (_device = _33CK128MP206) Or (_device = _33CK128MP208) Or (_device = _33CK128MP505)  '
     Or (_device = _33CK128MP506) Or (_device = _33CK128MP508) Or (_device = _33CK256MP205)  '
     Or (_device = _33CK256MP206) Or (_device = _33CK256MP208) Or (_device = _33CK256MP305)  '
     Or (_device = _33CK256MP306) Or (_device = _33CK256MP308) Or (_device = _33CK256MP505)  '
     Or (_device = _33CK256MP506) Or (_device = _33CK256MP508) Or (_device = _33CK256MP605)  '
     Or (_device = _33CK256MP606) Or (_device = _33CK256MP608) Or (_device = _33CK512MP305)  '
     Or (_device = _33CK512MP306) Or (_device = _33CK512MP308) Or (_device = _33CK512MP605)  '
     Or (_device = _33CK512MP606) Or (_device = _33CK512MP608)
$define cADC_HAS_CH18
$endif

$if (_device = _33CK32MC102) Or (_device = _33CK32MC103)                                    '
     Or (_device = _33CK32MC105) Or (_device = _33CK32MP102) Or (_device = _33CK32MP103)     '
     Or (_device = _33CK32MP105) Or (_device = _33CK32MP202) Or (_device = _33CK32MP203)     '
     Or (_device = _33CK32MP205) Or (_device = _33CK32MP206) Or (_device = _33CK32MP502)     '
     Or (_device = _33CK32MP503) Or (_device = _33CK32MP505) Or (_device = _33CK32MP506)     '
     Or (_device = _33CK64MC102) Or (_device = _33CK64MC103) Or (_device = _33CK64MC105)     '
     Or (_device = _33CK64MP102) Or (_device = _33CK64MP103) Or (_device = _33CK64MP105)     '
     Or (_device = _33CK64MP202) Or (_device = _33CK64MP203) Or (_device = _33CK64MP205)     '
     Or (_device = _33CK64MP206) Or (_device = _33CK64MP208) Or (_device = _33CK64MP502)     '
     Or (_device = _33CK64MP503) Or (_device = _33CK64MP505) Or (_device = _33CK64MP506)     '
     Or (_device = _33CK64MP508) Or (_device = _33CK128MP202) Or (_device = _33CK128MP203)   '
     Or (_device = _33CK128MP205) Or (_device = _33CK128MP206) Or (_device = _33CK128MP208)  '
     Or (_device = _33CK128MP502) Or (_device = _33CK128MP503) Or (_device = _33CK128MP505)  '
     Or (_device = _33CK128MP506) Or (_device = _33CK128MP508) Or (_device = _33CK256MP202)  '
     Or (_device = _33CK256MP203) Or (_device = _33CK256MP205) Or (_device = _33CK256MP206)  '
     Or (_device = _33CK256MP208) Or (_device = _33CK256MP305) Or (_device = _33CK256MP306)  '
     Or (_device = _33CK256MP308) Or (_device = _33CK256MP502) Or (_device = _33CK256MP503)  '
     Or (_device = _33CK256MP505) Or (_device = _33CK256MP506) Or (_device = _33CK256MP508)  '
     Or (_device = _33CK256MP605) Or (_device = _33CK256MP606) Or (_device = _33CK256MP608)  '
     Or (_device = _33CK512MP305) Or (_device = _33CK512MP306) Or (_device = _33CK512MP308)  '
     Or (_device = _33CK512MP605) Or (_device = _33CK512MP606) Or (_device = _33CK512MP608)
$define cADC_HAS_CH0
$define cADC_HAS_CH1
$define cADC_HAS_CH2
$define cADC_HAS_CH3
$define cADC_HAS_CH4
$define cADC_HAS_CH6
$define cADC_HAS_CH7
$endif

'-------------------------------------------------------------------------------------------------------------
' ADC cores available
'
$if (_device = _33CK128MP202)                                                           '
     Or (_device = _33CK128MP203) Or (_device = _33CK128MP205) Or (_device = _33CK128MP206)  '
     Or (_device = _33CK128MP208) Or (_device = _33CK128MP502) Or (_device = _33CK128MP503)  '
     Or (_device = _33CK128MP505) Or (_device = _33CK128MP506) Or (_device = _33CK128MP508)  '
     Or (_device = _33CK256MP202) Or (_device = _33CK256MP203) Or (_device = _33CK256MP205)  '
     Or (_device = _33CK256MP206) Or (_device = _33CK256MP208) Or (_device = _33CK256MP502)  '
     Or (_device = _33CK256MP503) Or (_device = _33CK256MP505) Or (_device = _33CK256MP506)  '
     Or (_device = _33CK256MP508) Or (_device = _33CK32MP102) Or (_device = _33CK32MP103)    '
     Or (_device = _33CK32MP105) Or (_device = _33CK32MP202) Or (_device = _33CK32MP203)     '
     Or (_device = _33CK32MP205) Or (_device = _33CK32MP206) Or (_device = _33CK32MP502)     '
     Or (_device = _33CK32MP503) Or (_device = _33CK32MP505) Or (_device = _33CK32MP506)     '
     Or (_device = _33CK64MP102) Or (_device = _33CK64MP103) Or (_device = _33CK64MP105)     '
     Or (_device = _33CK64MP202) Or (_device = _33CK64MP203) Or (_device = _33CK64MP205)     '
     Or (_device = _33CK64MP206) Or (_device = _33CK64MP208) Or (_device = _33CK64MP502)     '
     Or (_device = _33CK64MP503) Or (_device = _33CK64MP505) Or (_device = _33CK64MP506)     '
     Or (_device = _33CK64MP508) Or (_device = _33EP16GS202) Or (_device = _33EP32GS202)
$define cADC_HAS_CORE0
$define cADC_HAS_CORE1
$endif

'-------------------------------------------------------------------------------------------------------------
' Comparator availability
'
$if (_device = _33CK32MC102) Or (_device = _33CK32MC103)                                    '
     Or (_device = _33CK32MC105) Or (_device = _33CK32MP102) Or (_device = _33CK32MP103)     '
     Or (_device = _33CK32MP105) Or (_device = _33CK32MP202) Or (_device = _33CK32MP203)     '
     Or (_device = _33CK32MP205) Or (_device = _33CK32MP206) Or (_device = _33CK32MP502)     '
     Or (_device = _33CK32MP503) Or (_device = _33CK32MP505) Or (_device = _33CK32MP506)     '
     Or (_device = _33CK64MC102) Or (_device = _33CK64MC103) Or (_device = _33CK64MC105)     '
     Or (_device = _33CK64MP102) Or (_device = _33CK64MP103) Or (_device = _33CK64MP105)     '
     Or (_device = _33CK64MP202) Or (_device = _33CK64MP203) Or (_device = _33CK64MP205)     '
     Or (_device = _33CK64MP206) Or (_device = _33CK64MP208) Or (_device = _33CK64MP502)     '
     Or (_device = _33CK64MP503) Or (_device = _33CK64MP505) Or (_device = _33CK64MP506)     '
     Or (_device = _33CK64MP508) Or (_device = _33CK128MP202) Or (_device = _33CK128MP203)   '
     Or (_device = _33CK128MP205) Or (_device = _33CK128MP206) Or (_device = _33CK128MP208)  '
     Or (_device = _33CK128MP502) Or (_device = _33CK128MP503) Or (_device = _33CK128MP505)  '
     Or (_device = _33CK128MP506) Or (_device = _33CK128MP508) Or (_device = _33CK256MP202)  '
     Or (_device = _33CK256MP203) Or (_device = _33CK256MP205) Or (_device = _33CK256MP206)  '
     Or (_device = _33CK256MP208) Or (_device = _33CK256MP305) Or (_device = _33CK256MP306)  '
     Or (_device = _33CK256MP308) Or (_device = _33CK256MP502) Or (_device = _33CK256MP503)  '
     Or (_device = _33CK256MP505) Or (_device = _33CK256MP506) Or (_device = _33CK256MP508)  '
     Or (_device = _33CK256MP605) Or (_device = _33CK256MP606) Or (_device = _33CK256MP608)  '
     Or (_device = _33CK512MP305) Or (_device = _33CK512MP306) Or (_device = _33CK512MP308)  '
     Or (_device = _33CK512MP605) Or (_device = _33CK512MP606) Or (_device = _33CK512MP608)
$define cADC_COMPARATOR_COUNT 4

$else
$define cADC_COMPARATOR_COUNT 0
$endif

'-------------------------------------------------------------------------------------------------------------
' Digital filter availability
'
$if (_device = _33CK32MC102) Or (_device = _33CK32MC103)                                    '
     Or (_device = _33CK32MC105) Or (_device = _33CK32MP102) Or (_device = _33CK32MP103)     '
     Or (_device = _33CK32MP105) Or (_device = _33CK32MP202) Or (_device = _33CK32MP203)     '
     Or (_device = _33CK32MP205) Or (_device = _33CK32MP206) Or (_device = _33CK32MP502)     '
     Or (_device = _33CK32MP503) Or (_device = _33CK32MP505) Or (_device = _33CK32MP506)     '
     Or (_device = _33CK64MC102) Or (_device = _33CK64MC103) Or (_device = _33CK64MC105)     '
     Or (_device = _33CK64MP102) Or (_device = _33CK64MP103) Or (_device = _33CK64MP105)     '
     Or (_device = _33CK64MP202) Or (_device = _33CK64MP203) Or (_device = _33CK64MP205)     '
     Or (_device = _33CK64MP206) Or (_device = _33CK64MP208) Or (_device = _33CK64MP502)     '
     Or (_device = _33CK64MP503) Or (_device = _33CK64MP505) Or (_device = _33CK64MP506)     '
     Or (_device = _33CK64MP508) Or (_device = _33CK128MP202) Or (_device = _33CK128MP203)   '
     Or (_device = _33CK128MP205) Or (_device = _33CK128MP206) Or (_device = _33CK128MP208)  '
     Or (_device = _33CK128MP502) Or (_device = _33CK128MP503) Or (_device = _33CK128MP505)  '
     Or (_device = _33CK128MP506) Or (_device = _33CK128MP508) Or (_device = _33CK256MP202)  '
     Or (_device = _33CK256MP203) Or (_device = _33CK256MP205) Or (_device = _33CK256MP206)  '
     Or (_device = _33CK256MP208) Or (_device = _33CK256MP305) Or (_device = _33CK256MP306)  '
     Or (_device = _33CK256MP308) Or (_device = _33CK256MP502) Or (_device = _33CK256MP503)  '
     Or (_device = _33CK256MP505) Or (_device = _33CK256MP506) Or (_device = _33CK256MP508)  '
     Or (_device = _33CK256MP605) Or (_device = _33CK256MP606) Or (_device = _33CK256MP608)  '
     Or (_device = _33CK512MP305) Or (_device = _33CK512MP306) Or (_device = _33CK512MP308)  '
     Or (_device = _33CK512MP605) Or (_device = _33CK512MP606) Or (_device = _33CK512MP608)
$define cADC_DIGITAL_FILTER_COUNT 4

$else
$define cADC_DIGITAL_FILTER_COUNT 0
$endif

'-------------------------------------------------------------------------------------------------------------
' Trigger sources
'
$define cADC_TRIGSRC00 ADTRIG0Lbits.TRGSRC0
$define cADC_TRIGSRC01 ADTRIG0Lbits.TRGSRC1
$define cADC_TRIGSRC02 ADTRIG0Hbits.TRGSRC2
$define cADC_TRIGSRC03 ADTRIG0Hbits.TRGSRC3
$define cADC_TRIGSRC04 ADTRIG1Lbits.TRGSRC4
$define cADC_TRIGSRC05 ADTRIG1Lbits.TRGSRC5
$define cADC_TRIGSRC06 ADTRIG1Hbits.TRGSRC6
$define cADC_TRIGSRC07 ADTRIG1Hbits.TRGSRC7
$define cADC_TRIGSRC08 ADTRIG2Lbits.TRGSRC8
$define cADC_TRIGSRC09 ADTRIG2Lbits.TRGSRC9
$define cADC_TRIGSRC10 ADTRIG2Hbits.TRGSRC10
$define cADC_TRIGSRC11 ADTRIG2Hbits.TRGSRC11
$define cADC_TRIGSRC12 ADTRIG3Lbits.TRGSRC12
$define cADC_TRIGSRC13 ADTRIG3Lbits.TRGSRC13
$define cADC_TRIGSRC14 ADTRIG3Hbits.TRGSRC14
$define cADC_TRIGSRC15 ADTRIG3Hbits.TRGSRC15
$define cADC_TRIGSRC16 ADTRIG4Lbits.TRGSRC16
$define cADC_TRIGSRC17 ADTRIG4Lbits.TRGSRC17
$define cADC_TRIGSRC18 ADTRIG4Hbits.TRGSRC18
$define cADC_TRIGSRC19 ADTRIG4Hbits.TRGSRC19
$define cADC_TRIGSRC20 ADTRIG5Lbits.TRGSRC20
$define cADC_TRIGSRC21 ADTRIG5Lbits.TRGSRC21
$define cADC_TRIGSRC22 ADTRIG5Hbits.TRGSRC22
$define cADC_TRIGSRC23 ADTRIG5Hbits.TRGSRC23
$define cADC_TRIGSRC24 ADTRIG6Lbits.TRGSRC24
$define cADC_TRIGSRC25 ADTRIG6Lbits.TRGSRC25
$define cADC_TRIGSRC26 ADTRIG6Hbits.TRGSRC26
$define cADC_TRIGSRC27 ADTRIG6Hbits.TRGSRC27
$define cADC_TRIGSRC28 ADTRIG7Lbits.TRGSRC28
$define cADC_TRIGSRC29 ADTRIG7Lbits.TRGSRC29
$define cADC_TRIGSRC30 ADTRIG7Hbits.TRGSRC30
$define cADC_TRIGSRC31 ADTRIG7Hbits.TRGSRC31

$if (_device = _33CK32MC102) Or (_device = _33CK32MC103) Or (_device = _33CK32MC105)         '
     Or (_device = _33CK64MC102) Or (_device = _33CK64MC103) Or (_device = _33CK64MC105)     '
     Or (_device = _33CK32MP102) Or (_device = _33CK32MP103) Or (_device = _33CK32MP105)     '
     Or (_device = _33CK64MP102) Or (_device = _33CK64MP103) Or (_device = _33CK64MP105)
'
' CK MP1XX
'
$define cADC_TRGSRC_ADTRG31       %00011111
$define cADC_TRGSRC_MSTR_PTG      %00011110
$define cADC_TRGSRC_CLC4          %00010010
$define cADC_TRGSRC_CLC3          %00010001
$define cADC_TRGSRC_CLC2          %00011101
$define cADC_TRGSRC_CLC1          %00011100
$define cADC_TRGSRC_MCCP5         %00011000
$define cADC_TRGSRC_SCCP4         %00010111
$define cADC_TRGSRC_SCCP3         %00010110
$define cADC_TRGSRC_SCCP2         %00010101
$define cADC_TRGSRC_SCCP1         %00010100
$define cADC_TRGSRC_MCCP5_TRG     %00010000
$define cADC_TRGSRC_SCCP4_TRG     %00001111
$define cADC_TRGSRC_SCCP3_TRG     %00001110
$define cADC_TRGSRC_SCCP2_TRG     %00001101
$define cADC_TRGSRC_SCCP1_TRG     %00001100
$define cADC_TRGSRC_PWM4_TRG2     %00001011
$define cADC_TRGSRC_PWM4_TRG1     %00001010
$define cADC_TRGSRC_PWM3_TRG2     %00001001
$define cADC_TRGSRC_PWM3_TRG1     %00001000
$define cADC_TRGSRC_PWM2_TRG2     %00000111
$define cADC_TRGSRC_PWM2_TRG1     %00000110
$define cADC_TRGSRC_PWM1_TRG2     %00000101
$define cADC_TRGSRC_PWM1_TRG1     %00000100
$define cADC_TRGSRC_SOFTTRG       %00000010
$define cADC_TRGSRC_COMMONSOFTTRG %00000001
$define cADC_TRGSRC_NOTRIG        %00000000

$elseif (_device = _33CK32MP202) Or (_device = _33CK32MP203) Or (_device = _33CK32MP205)     '
     Or (_device = _33CK32MP206) Or (_device = _33CK32MP502) Or (_device = _33CK32MP503)     '
     Or (_device = _33CK32MP505) Or (_device = _33CK32MP506) Or (_device = _33CK64MP202)     '
     Or (_device = _33CK64MP203) Or (_device = _33CK64MP205) Or (_device = _33CK64MP206)     '
     Or (_device = _33CK64MP208) Or (_device = _33CK64MP502) Or (_device = _33CK64MP503)     '
     Or (_device = _33CK64MP505) Or (_device = _33CK64MP506) Or (_device = _33CK64MP508)     '
     Or (_device = _33CK128MP202) Or (_device = _33CK128MP203) Or (_device = _33CK128MP205)  '
     Or (_device = _33CK128MP206) Or (_device = _33CK128MP208) Or (_device = _33CK128MP502)  '
     Or (_device = _33CK128MP503) Or (_device = _33CK128MP505) Or (_device = _33CK128MP506)  '
     Or (_device = _33CK128MP508) Or (_device = _33CK256MP202) Or (_device = _33CK256MP203)  '
     Or (_device = _33CK256MP205) Or (_device = _33CK256MP206) Or (_device = _33CK256MP208)  '
     Or (_device = _33CK256MP305) Or (_device = _33CK256MP306) Or (_device = _33CK256MP308)  '
     Or (_device = _33CK256MP502) Or (_device = _33CK256MP503) Or (_device = _33CK256MP505)  '
     Or (_device = _33CK256MP506) Or (_device = _33CK256MP508) Or (_device = _33CK256MP605)  '
     Or (_device = _33CK256MP606) Or (_device = _33CK256MP608) Or (_device = _33CK512MP305)  '
     Or (_device = _33CK512MP306) Or (_device = _33CK512MP308) Or (_device = _33CK512MP605)  '
     Or (_device = _33CK512MP606) Or (_device = _33CK512MP608)
'
' CK MP2/3/5/6XX
'
$define cADC_TRGSRC_ADTRG31       %00011111
$define cADC_TRGSRC_MSTR_PTG      %00011110
$define cADC_TRGSRC_CLC2          %00011101
$define cADC_TRGSRC_CLC1          %00011100
$define cADC_TRGSRC_MCCP9         %00011011
$define cADC_TRGSRC_SCCP7         %00011010
$define cADC_TRGSRC_SCCP6         %00011001
$define cADC_TRGSRC_SCCP5         %00011000
$define cADC_TRGSRC_SCCP4         %00010111
$define cADC_TRGSRC_SCCP3         %00010110
$define cADC_TRGSRC_SCCP2         %00010101
$define cADC_TRGSRC_SCCP1         %00010100
$define cADC_TRGSRC_PWM8_TRG2     %00010011
$define cADC_TRGSRC_PWM8_TRG1     %00010010
$define cADC_TRGSRC_PWM7_TRG2     %00010001
$define cADC_TRGSRC_PWM7_TRG1     %00010000
$define cADC_TRGSRC_PWM6_TRG2     %00001111
$define cADC_TRGSRC_PWM6_TRG1     %00001110
$define cADC_TRGSRC_PWM5_TRG2     %00001101
$define cADC_TRGSRC_PWM5_TRG1     %00001100
$define cADC_TRGSRC_PWM4_TRG2     %00001011
$define cADC_TRGSRC_PWM4_TRG1     %00001010
$define cADC_TRGSRC_PWM3_TRG2     %00001001
$define cADC_TRGSRC_PWM3_TRG1     %00001000
$define cADC_TRGSRC_PWM2_TRG2     %00000111
$define cADC_TRGSRC_PWM2_TRG1     %00000110
$define cADC_TRGSRC_PWM1_TRG2     %00000101
$define cADC_TRGSRC_PWM1_TRG1     %00000100
$define cADC_TRGSRC_SOFTTRG       %00000010
$define cADC_TRGSRC_COMMONSOFTTRG %00000001
$define cADC_TRGSRC_NOTRIG        %00000000
$endif

'---------------------------------------------------------------------------------------------
' Load the SHRRES bits of the ADCON1H SFR
' Input     : pRes holds the resolution value
'               3 = 12-bit resolution
'               2 = 10-bit resolution
'               1 = 8-bit resolution
'               0 = 6-bit resolution
' Output    : None
' Notes     : None
'
$define ADCON1H_SHRRES(pRes) '
    $if pRes = 0             '
        ADCON1H.5 = 0        '
        ADCON1H.6 = 0        '
    $elseif pRes = 1         '
        ADCON1H.5 = 1        '
        ADCON1H.6 = 0        '
    $elseif pRes = 2         '
        ADCON1H.5 = 0        '
        ADCON1H.6 = 1        '
    $elseif pRes = 3         '
        ADCON1H.5 = 1        '
        ADCON1H.6 = 1        '
    $endif

'---------------------------------------------------------------------------------------------
' Load the CNVCHSEL bits of the ADCON3L SFR
' Input     : pChan holds the channel value
' Output    : None
' Notes     : None
'
$define ADCON3L_CNVCHSEL(pChan)           '
    ADCON3L = ADCON3L & %1111111111000000 '
    ADCON3L = ADCON3L | pChan

'---------------------------------------------------------------------------------------------
' Load the REFSEL bits of the ADCON3L SFR
' Input     : pValue holds the value to place in the bits
' Output    : None
' Notes     : None
'
$define ADCON3L_REFSEL(pValue)                '
    ADCON3L.Byte1 = ADCON3L.Byte1 & %00011111 '
    ADCON3L.Byte1 = ADCON3L.Byte1 | (pValue << 7)

'---------------------------------------------------------------------------------------------
' Load the CLKDIV bits of the ADCON3H SFR
' Input     : pValue holds the clk value
' Output    : None
' Notes     : None
'
$define ADCON3H_CLKDIV(pValue)             '
    ADCON3H.Byte1 =  %00000000             '
    ADCON3H.Byte1 = ADCON3H.Byte1 | pValue

'---------------------------------------------------------------------------------------------
' Load the CLKSEL bits of the ADCON3H SFR
' Input     : pValue holds the value to place in the bits
' Output    : None
' Notes     : None
'
$define ADCON3H_CLKSEL(pValue)                '
    ADCON3H.Byte1 = ADCON3H.Byte1 & %00111111 '
    ADCON3H.Byte1 = ADCON3H.Byte1 | (pValue << 7)

'---------------------------------------------------------------------------------------------
' Load the WARMTIME bits of the ADCON5H SFR
' Input     : pValue holds the warm up time value
' Output    : None
' Notes     : None
'
$define ADCON5H_WARMTIME(pValue)              '
    ADCON5H.Byte1 = ADCON5H.Byte1 & %11110000 '
    ADCON5H.Byte1 = ADCON5H.Byte1 | pValue

'---------------------------------------------------------------------------------------------
' Load the SHRSAMC bits of the ADCON2H SFR
' Input     : pValue holds the value to place in the bits
' Output    : None
' Notes     : None
'
$define ADCON2H_SHRSAMC(pValue)           '
    ADCON2H = ADCON2H & %1111110000000000 '
    ADCON2H = ADCON2H | (pValue << 9)

'---------------------------------------------------------------------------------------------
' Load the RES bits of the ADCORE0H SFR
' Input     : pRes holds the resolution value
'               3 = 12-bit resolution
'               2 = 10-bit resolution
'               1 = 8-bit resolution
'               0 = 6-bit resolution
' Output    : None
' Notes     : None
'
$define ADCORE0H_RES(pRes) '
    $if pRes = 0           '
        ADCORE0H.8 = 0     '
        ADCORE0H.9 = 0     '
    $elseif pRes = 1       '
        ADCORE0H.8 = 1     '
        ADCORE0H.9 = 0     '
    $elseif pRes = 2       '
        ADCORE0H.8 = 0     '
        ADCORE0H.9 = 1     '
    $elseif pRes = 3       '
        ADCORE0H.8 = 1     '
        ADCORE0H.9 = 1     '
    $endif

'---------------------------------------------------------------------------------------------
' Load the RES bits of the ADCORE1H SFR
' Input     : pRes holds the resolution value
'               3 = 12-bit resolution
'               2 = 10-bit resolution
'               1 = 8-bit resolution
'               0 = 6-bit resolution
' Output    : None
' Notes     : None
'
$define ADCORE1H_RES(pRes) '
    $if pRes = 0           '
        ADCORE1H.8 = 0     '
        ADCORE1H.9 = 0     '
    $elseif pRes = 1       '
        ADCORE1H.8 = 1     '
        ADCORE1H.9 = 0     '
    $elseif pRes = 2       '
        ADCORE1H.8 = 0     '
        ADCORE1H.9 = 1     '
    $elseif pRes = 3       '
        ADCORE1H.8 = 1     '
        ADCORE1H.9 = 1     '
    $endif

'---------------------------------------------------------------------------------------------
' Load the RES bits of the ADCORE2H SFR
' Input     : pRes holds the resolution value
'               3 = 12-bit resolution
'               2 = 10-bit resolution
'               1 = 8-bit resolution
'               0 = 6-bit resolution
' Output    : None
' Notes     : None
'
$define ADCORE2H_RES(pRes) '
    $if pRes = 0           '
        ADCORE2H.8 = 0     '
        ADCORE2H.9 = 0     '
    $elseif pRes = 1       '
        ADCORE2H.8 = 1     '
        ADCORE2H.9 = 0     '
    $elseif pRes = 2       '
        ADCORE2H.8 = 0     '
        ADCORE2H.9 = 1     '
    $elseif pRes = 3       '
        ADCORE2H.8 = 1     '
        ADCORE2H.9 = 1     '
    $endif

'---------------------------------------------------------------------------------------------
' Load the RES bits of the ADCORE3H SFR
' Input     : pRes holds the resolution value
'               3 = 12-bit resolution
'               2 = 10-bit resolution
'               1 = 8-bit resolution
'               0 = 6-bit resolution
' Output    : None
' Notes     : None
'
$define ADCORE3H_RES(pRes) '
    $if pRes = 0           '
        ADCORE3H.8 = 0     '
        ADCORE3H.9 = 0     '
    $elseif pRes = 1       '
        ADCORE3H.8 = 1     '
        ADCORE3H.9 = 0     '
    $elseif pRes = 2       '
        ADCORE3H.8 = 0     '
        ADCORE3H.9 = 1     '
    $elseif pRes = 3       '
        ADCORE3H.8 = 1     '
        ADCORE3H.9 = 1     '
    $endif

'---------------------------------------------------------------------------------------------
' Load the SAMC bits of the ADCORE0L SFR
' Input     : pValue holds the value to place into the bits
' Output    : None
' Notes     : None
'
$define ADCORE0L_SAMC(pValue)               '
    ADCORE0L = ADCORE0L & %1111110000000000 '
    ADCORE0L = ADCORE0L | pValue

'---------------------------------------------------------------------------------------------
' Load the SAMC bits of the ADCORE1L SFR
' Input     : pValue holds the value to place into the bits
' Output    : None
' Notes     : None
'
$define ADCORE1L_SAMC(pValue)               '
    ADCORE1L = ADCORE1L & %1111110000000000 '
    ADCORE1L = ADCORE1L | pValue

'---------------------------------------------------------------------------------------------
' Load the SAMC bits of the ADCORE2L SFR
' Input     : pValue holds the value to place into the bits
' Output    : None
' Notes     : None
'
$define ADCORE2L_SAMC(pValue)               '
    ADCORE2L = ADCORE2L & %1111110000000000 '
    ADCORE2L = ADCORE2L | pValue

'---------------------------------------------------------------------------------------------
' Load the SAMC bits of the ADCORE3L SFR
' Input     : pValue holds the value to place into the bits
' Output    : None
' Notes     : None
'
$define ADCORE3L_SAMC(pValue)               '
    ADCORE3L = ADCORE3L & %1111110000000000 '
    ADCORE3L = ADCORE3L | pValue

'---------------------------------------------------------------------------------------------
' Load the ADCS bits of the ADCORE0H SFR
' Input     : pValue holds the value to place into the bits
' Output    : None
' Notes     : None
'
$define ADCORE0H_ADCS(pValue)                   '
    ADCORE0H.Byte0 = ADCORE0H.Byte0 & %10000000 '
    ADCORE0H.Byte0 = ADCORE0H.Byte0 | pValue

'---------------------------------------------------------------------------------------------
' Load the ADCS bits of the ADCORE1H SFR
' Input     : pValue holds the value to place into the bits
' Output    : None
' Notes     : None
'
$define ADCORE1H_ADCS(pValue)                   '
    ADCORE1H.Byte0 = ADCORE1H.Byte0 & %10000000 '
    ADCORE1H.Byte0 = ADCORE1H.Byte0 | pValue

'---------------------------------------------------------------------------------------------
' Load the ADCS bits of the ADCORE2H SFR
' Input     : pValue holds the value to place into the bits
' Output    : None
' Notes     : None
'
$define ADCORE2H_ADCS(pValue)                   '
    ADCORE2H.Byte0 = ADCORE2H.Byte0 & %10000000 '
    ADCORE2H.Byte0 = ADCORE2H.Byte0 | pValue

'---------------------------------------------------------------------------------------------
' Load the ADCS bits of the ADCORE3H SFR
' Input     : pValue holds the value to place into the bits
' Output    : None
' Notes     : None
'
$define ADCORE3H_ADCS(pValue)                   '
    ADCORE3H.Byte0 = ADCORE3H.Byte0 & %10000000 '
    ADCORE3H.Byte0 = ADCORE3H.Byte0 | pValue

'---------------------------------------------------------------------------------------------
' Load the SHRADCS bits of the ADCON2L SFR
' Input     : pValue holds the value to place into the bits
' Output    : None
' Notes     : None
'
$define ADCON2L_SHRADCS(pValue)               '
    ADCON2L.Byte0 = ADCON2L.Byte0 & %10000000 '
    ADCON2L.Byte0 = ADCON2L.Byte0 | pValue
   
'---------------------------------------------------------------------------------------------
' Create some variables for the library routines   
'   
    Dim ADC_bMasterClock_Src As Byte = 0        ' Default is clock from FP (FOSC /2)
    Dim ADC_bMasterClock_Div As Byte = 0        ' Default is 1:1 clock divider
    Dim ADC_wChannelBitMask  As Word            ' Holds a bitmask for an ADC channel
   
'------------------------------------------------------------------------------------
' Initialise the ADCs with default values
' Input     : None
' Output    : None
' Notes     : Initialises all the ADCs that the device has
'
Proc ADC_Init()

$ifdef _ADC1MD
    ADC1MD = 0
    DelayCS 1
$endif
'
' Configure the common ADC clock
'
    ADCON3H_CLKSEL(ADC_bMasterClock_Src)
    ADCON3H_CLKDIV(ADC_bMasterClock_Div)
'
' Configure the cores ADC clock
'
$ifdef cADC_HAS_CORE0   
    ADCORE0H_ADCS(1)                            ' Clock divider (1:2)
    ADCORE0H_RES(3)                             ' 12-bits
    ADCORE0L_SAMC(18)                           ' 0 Tad delay before sampling
    ADCON4Lbits_SAMC0EN = 1                     ' Conversion delay enabled
$endif
 
$ifdef cADC_HAS_CORE1
    ADCORE1H_ADCS(1)                            ' Clock divider (1:2)
    ADCORE1H_RES(3)                             ' 12-bits
    ADCORE1L_SAMC(18)                           ' 0 Tad delay before sampling
    ADCON4Lbits_SAMC1EN = 1                     ' Conversion delay enabled
$endif
   
$ifdef cADC_HAS_CORE2      
    ADCORE2H_ADCS(1)                            ' Clock divider (1:2)
    ADCORE2H_RES(3)                             ' 12-bits
    ADCORE2L_SAMC(18)                           ' 0 Tad delay before sampling
    ADCON4Lbits_SAMC2EN = 1                     ' Conversion delay enabled
$endif
   
$ifdef cADC_HAS_CORE3     
    ADCORE3H_ADCS(1)                            ' Clock divider (1:2)
    ADCORE3H_RES(3)                             ' 12-bits
    ADCORE3L_SAMC(18)                           ' 0 Tad delay before sampling
    ADCON4Lbits_SAMC3EN = 1                     ' Conversion delay enabled
$endif

    ADCON3L_REFSEL(0)                           ' AVdd as voltage reference
    ADCON1Hbits_FORM = 0                        ' Integer format
    ADCON2L_SHRADCS(1)                          ' Clock divider (1:2)
    ADCON1H_SHRRES(3)                           ' 12-bits
    ADCON2H_SHRSAMC(18)                         ' 20 Tad

    ADC_Setup()                                 ' Setup the ADC cores
EndProc

'------------------------------------------------------------------------------------
' Setup the ADCs
' Input     : None
' Output    : None
' Notes     : Sets up all the ADCs that the device has
'
Proc ADC_Setup()
    ADCON5H_WARMTIME(15)                        ' Set initialisation time to maximum
    ADCON1Lbits_ADON = 1                        ' Turn on the ADC module

$ifdef cADC_HAS_CORE0
    ADCON5Lbits_C0PWR = 1                       ' Turn on analogue power for ADC core 0
    While ADCON5Lbits_C0RDY = 0: Wend           ' Wait until core 0 is ready for operation
    ADCON3Hbits_C0EN = 1                        ' Turn on digital power to enable triggers to core 0
$endif
  
$ifdef cADC_HAS_CORE1      
    ADCON5Lbits_C1PWR = 1                       ' Turn on analogue power for ADC core 1
    While ADCON5Lbits_C1RDY = 0: Wend           ' Wait until core 1 is ready for operation
    ADCON3Hbits_C1EN = 1                        ' Turn on digital power to enable triggers to core 1
$endif
   
$ifdef cADC_HAS_CORE2       
    ADCON5Lbits_C2PWR = 1                       ' Turn on analogue power for ADC core 2
    While ADCON5Lbits_C2RDY = 0: Wend           ' Wait until core 1 is ready for operation
    ADCON3Hbits_C2EN = 1                        ' Turn on digital power to enable triggers to core 2
$endif
   
$ifdef cADC_HAS_CORE3
    ADCON5Lbits_C3PWR = 1                       ' Turn on analogue power for ADC core 3
    While ADCON5Lbits_C3RDY = 0: Wend           ' Wait until core 1 is ready for operation
    ADCON3Hbits_C3EN = 1                        ' Turn on digital power to enable triggers to core 3
$endif
  
    ADCON5Lbits_SHRPWR = 1                      ' Turn on analogue power for the shared ADC core
    While ADCON5Lbits_SHRRDY = 0: Wend          ' Wait until the shared core is ready for operation
    ADCON3Hbits_SHREN = 1                       ' Turn on digital power to enable triggers to the shared core
EndProc

'------------------------------------------------------------------------------------
' Read an ADC sample channel
' Input     : pChannel holds the ADC channel to read
' Output    : Returns the ADC value, or $FFFF if an incorrect channel is chosen
' Notes     : None
'
Proc ADC_Read(pChannel As Byte), Word
    Dim wADCBUF_Address As Word = (AddressOf(ADCBUF0)) + (pChannel * 2) ' Point to the correct ADC channel's buffer

    If pChannel >= cADC_CHANNEL_MAX Then
        Result = $FFFF
        ExitProc
    EndIf

    While ADCON3Lbits_CNVRTCH = 1: Wend

    ADCON3L_CNVCHSEL(pChannel)          ' Select the channel to read
    ADCON3Lbits_CNVRTCH = 1             ' Start sampling

    If pChannel < 16 Then
        ADC_wChannelBitMask = 1 << pChannel
        While (ADSTATL & ADC_wChannelBitMask) = 0: Wend
    Else
        pChannel = pChannel - 16
        ADC_wChannelBitMask = 1 << pChannel
        While (ADSTATH & ADC_wChannelBitMask) = 0: Wend
    EndIf

    Result = Ptr16(wADCBUF_Address)      ' Read and return the ADC conversion result
EndProc

'------------------------------------------------------------------------------------
' Set an ADC pin to analogue
' Input     : pPin holds the pin to make analogue
' Output    : None
' Notes     : The ADC channel starts from AN0, which is pin RA0
'
Proc ADC_Pin(pPin As Byte)
    If pPin < 16 Then
        $ifdef _PORTA
        SetBit ANSELA, pPin
        $endif
   
    ElseIf pPin < 32 Then
        $ifdef _PORTB
        SetBit ANSELB, pPin
        $endif
   
    $ifdef _PORTC
    ElseIf pPin < 48 Then
        SetBit ANSELC, pPin
    $endif
    $ifdef _PORTD
    ElseIf pPin < 64 Then
        SetBit ANSELD, pPin
    $endif
    EndIf
EndProc

$endif  ' ADC_DSPIC33CK_INC

The code below is a test demo of the above library. I've ran it successfully several times on a dsPIC33CK128MP202 device and it gives a good 12-bit result.

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' A test demo to read ADC channel AN0 (pin RA0) on a dsPIC33CK128MP202 device, and output the 12-bit value to a serial terminal
'
' Written for the Positron16 compiler by Les Johnson
'
    Device = 33CK128MP202
    Declare Xtal = 160

    Declare Hserial1_Baud = 9600
    Declare HRSOut1_Pin = PORTB.0
 
    Include "ADC_dsPIC33CK.inc"
'
' PPS output defines for a dsPIC33CK128MP202 device
'
$define PPS_OutPin_RB0  cOut_Pin_RP32
$define PPS_OutPin_RB1  cOut_Pin_RP33
$define PPS_OutPin_RB2  cOut_Pin_RP34
$define PPS_OutPin_RB3  cOut_Pin_RP35
$define PPS_OutPin_RB4  cOut_Pin_RP36
$define PPS_OutPin_RB5  cOut_Pin_RP37
$define PPS_OutPin_RB6  cOut_Pin_RP38
$define PPS_OutPin_RB7  cOut_Pin_RP39
$define PPS_OutPin_RB8  cOut_Pin_RP40
$define PPS_OutPin_RB9  cOut_Pin_RP41
$define PPS_OutPin_RB10 cOut_Pin_RP42
$define PPS_OutPin_RB11 cOut_Pin_RP43
$define PPS_OutPin_RB12 cOut_Pin_RP44
$define PPS_OutPin_RB13 cOut_Pin_RP45
$define PPS_OutPin_RB14 cOut_Pin_RP46
$define PPS_OutPin_RB15 cOut_Pin_RP47
'
' Create a variable for the demo
'
    Dim Wordout As Word
   
'------------------------------------------------------------------------------------
' The main program starts here
'
Main:  
    IntOsc_160MHz()                             ' Set the device to operate at 160MHz with its internal oscillator  
 
    PPS_Unlock()                                ' Unlock the PPS peripheral
    PPS_Output(PPS_OutPin_RB0, cOut_Fn_U1TX)    ' Set the appropriate PPS pin for the USART1 peripheral TX
   
    HRSOutLn "Start"                            ' Send an initial test string to know the device is operating
   
    ADC_Init()                                  ' Initialise the ADCs
    ADC_Pin(Pin_A0)                             ' Set the appropriate pin to analogue
    Do                                          ' Create a loop
        Wordout = ADC_Read(0)                   ' Read the ADC value from AN0
        HRSOutLn Dec Wordout                    ' Output the value to a serial terminal
        DelayMS 500                             ' A half second delay so we do not see a rush of values
    Loop                                        ' Do it forever

'----------------------------------------------------------------------------------
' Set the internal oscillator to 160MHz
' Input     : None
' Output    : None
' Notes     : None
'
Proc IntOsc_160MHz()
    CLKDIV = %0011000000000001
    PLLFBD = 160                   
    OSCTUN = $00                   
    PLLDIV = %0000000001000001
    ACLKCON1 = $0101               
    APLLFBD1 = 150
    APLLDIV1 = $41
    Write_OSCCON($0101)
'
' Wait for clock switchig to finish
'
    While OSCCONbits_OSWEN <> 0 : Wend
    While OSCCONbits_LOCK <> 1 : Wend
EndProc

'----------------------------------------------------------------------------------
' Set the fuses for internal oscillator
'
    Config FSEC     = BWRP_OFF, BSS_DISABLED, BSEN_OFF, GWRP_OFF, GSS_DISABLED, CWRP_OFF, CSS_DISABLED, AIVTDIS_OFF
    Config FOSCSEL  = FNOSC_FRC, IESO_OFF
    Config FOSC     = POSCMD_NONE, OSCIOFNC_ON, FCKSM_CSECMD, PLLKEN_OFF, XTCFG_G0, XTBST_ENABLE
    Config FWDT     = SWDTPS_PS2147483648, RCLKSEL_LPRC
    Config FPOR     = BISTDIS_DISABLED
    Config FICD     = ICS_PGD1, JTAGEN_OFF, NOBTSWP_DISABLED
    Config FDMT     = DMTDIS_OFF
    Config FDEVOPT  = ALTI2C1_ON, ALTI2C2_ON, ALTI2C3_ON, SMBEN_STANDARD, SPI2PIN_PPS
    Config FALTREG  = CTXT1_OFF, CTXT2_OFF, CTXT3_OFF, CTXT4_OFF

I hope this helps you out a little Charlie. :-)

charliecoutas

Wow, or to borrow a word from our Aussie friends: Strewth! The reason I asked about anybody using the USART on 33CK devices was because they look so bl**dy complicated and I couldn't work out what was going on.

Thanks Les, you've done a lot of work on this. How close to 200Mhz can I get with the 33CK256MP505? I am trying for the advertised 100MIPs, to control a closed-loop constant power pulse generator.

You are a STAR Les

Charlie

top204

#3
With the internal oscillator, the maximum I could get reliably is about 180MHz, but make sure your decoupling and PCB layout are absolutely marvelous. :-) On a breadboard, I don't think you will get much past 160MHz to 170MHz with the internal oscillator, and keep reliability.

With an external crystal, it should operate very nicely indeed, in fact it should operate faster than 200MHz, as most other devices run faster than their specs. :-)

charliecoutas

It's a proper pcb with decoupling caps very close to the chip. I'll try 190Mhz

diebobo

Les,

Could you please clarify how the following part of code should work, it's part of your .INC from above. For example i need the value of AN13, According to the datasgeet AN13 = PortC.1. According to the code below it will try to set ANSELA.13 ?

'------------------------------------------------------------------------------------
' Set an ADC channel to analogue
' Input    : pChannel holds the channel to make analogue
' Output    : None
' Notes    : The ADC channel starts from AN0, which is channel 0
'
Proc ADC_Channel(pChannel As Byte)
    If pChannel < 16 Then
        $ifdef _PORTA
        SetBit ANSELA, pChannel
        $endif
 
    ElseIf pChannel < 32 Then
        $ifdef _PORTB
        SetBit ANSELB, pChannel
        $endif
 
    $ifdef _PORTC
    ElseIf pChannel < 48 Then
        SetBit ANSELC, pChannel
    $endif
    $ifdef _PORTD
    ElseIf pChannel < 64 Then
        SetBit ANSELD, pChannel
    $endif
    EndIf
EndProc

There is also a fault in this part of your code :

Proc ADC_Read(pChannel As Byte), Word
        Dim wADCBUF_Address As Word = AddressOf(ADCBUF0) + pChannel ' Point to the correct ADC channel's buffer

It should be :

Proc ADC_Read(pChannel As Byte), Word
        Dim wADCBUF_Address As Word = AddressOf(ADCBUF0) + ( pChannel * 2 )' Point to the correct ADC channel's buffer

For example, wADCBUF_Address will hold HEX C0C when pchannel = 0, which is correct.. However when pchannel = 13 is used wADCBUF_Address will hold HEX C19 which is incorrect, it should be C26 according to datasheet.. that's why AddressOf(ADCBUF0) + ( pChannel * 2 )

top204

#6
Please remember, I knocked out the code quickly so Charlie could have something to work with, so it is an alpha build.

You are correct about the buffer, because I forgot they are 16-bit SFRs. I tested on AN0, so the buffer was correct regardless. :-) Testing should never be done by the creator of the code, because an uncounsious bias moves in.

The analogue pin select procedure will need further checks to get the ANSELx SFRs to match the channel selected. The procedure should have the name Pin instead of Channel. I wrote it quickly based upon what the ANSELx SFRs do on other devices, so ANSELA is always the SFR for analogue pins on PORTA and ANSELB is for PORTB etc, but the dsPIC33CK devices seem the be pushing "common sense" for some peripherals. :-)

Personally, I would simply load the appropriate ANSELx SFR with the value that relates to the pin in use. Or a preprocessor meta-macro can be created to match a constant channel parameter to teh bit within the ANSELx SFRs.

diebobo

Quote from: top204 on Nov 15, 2021, 06:04 PMPlease remember, I knocked out the code quicky so Charlie could have something to work with, so it is an alpha build.

Ooooh i didn't mean it as critisism, just trying to crush the problems i encounter ;)

For Charlie:

I am using multiple AD's now, but i find the different AD channels interfered with each other. So if one channel wend up, another one did as well to some degree. I found that changing the TAD delay to 5 instead of 18 solved that ( search for SAMC ) in the INC file from Les. U need to alter Core 0/1/2/3 and Shared one in Proc ADC_Init

charliecoutas

diebobo, Les

Thanks for the feedback. I'm sure Les didn't take your comments as criticism, this is a complex area and there are bound to be slip-ups.
I can't run anything yet, my PCBs don't arrive until the end of this week. I'm glad you are getting somewhere, it will be right on the money soon.

I hadn't realised how complex the ADCs are in the CK range (probably other ranges too).

Charlie

top204

#9
No criticism taken.

I had a few hours spare so I looked into the ADC peripheral on the CK devices and wow! what a surprise to see how complex the idiots have made such a standard peripheral. The information on them is scarse and it seems that a particular core has particular pins, but when I tried to enable seperate cores, some pins would not work, and if I disabled cores and used shared only, nothing worked. :-)

I even tried the dreadful MPLABX libraries and compiled the C code, but it still did not work as expected and it seems even Microchip do not actually know how to use them. :-)

It is definately the work of a university nerd who does not actually live in the real world yet, so they do not quite understand what is actually required, and not just theoretically possible. As dad always told me as a boy, "just because it looks good on paper doesn't always mean it works in the real world" :-)

Please use the above inc file code as a reference and add to it so it becomes a good library for users to use.

RGV250

Hi Les,
QuoteAs dad always told me as a boy, "just because it looks good on paper doesn't always mean it works in the real world" :-)
I think most of us are the age when we would have been told that so I have to wonder when the chain got broken.

Bob 

top204

#11
Here in the UK, it seems to have taken hold in the 1990s, when going to university was classed as necessary, even if going in for pointless courses such as Klingon, or sociology etc...

Without true appreticeships, the young-uns coming out of university have had to do very little for themselves, and they do and think as they are taught, even if the teacher has no real idea because they have not had to live in the real world, so it is a downward cycle. :-)

Also, now, after a 6 to 8 week course, one is classed as an expert, and I am not exaggerating on that!

I have worked with, so called engineers, that did not have a clue. One of them did not know what a rectifier diode looked like in a satellite receiver or what it did for the switched mode PSU circuit! I know.... I could not believe it either at the time, when I advised him to look at that particular component in a unit that came in for repair and it was a common problem with them. But he had "HNC" or some other piece of paper, so he was obviously a qualified electronics engineer to the manager of the company, who also had no idea whatsoever on what he was managing. LOL

That was about 25 years ago, so you can tell what impression it gave to me at the time, because I still remember it, but it happened a few times when I was working for Sky in the repair department. :-) The smaller companies I worked for had stricter rules, so it was not as common.

John Lawton

Oddly enough an HNC would imply that they would have been be working in a job and studying in evenings or possibly day-release for the qualification, so it should not have been too theoretical.

One would hope they at least knew which end of the hot soldering iron to hold. That's a lesson learnt only once.

charliecoutas

I proudly wear the HNC badge but I agree, it's actual experience that matters in the long run. I used to teach part of a technology course at Thames Valley Uni in Reading several years ago. One young student wrote a paragraph that I thought I had seen before. Sure enough, he had lifted it straight from the Internet. A row ensued with his Mum coming and complaining about our treatment of him. I asked other students if they thought it was OK to use other people's work so they could pass their own exams. The majority said it was OK. I think things have got worse since then.

Poking around in the ADC spec (for CK series) I got a little bit excited that not only is there a 12 bit DAC in there, but it is part of a high speed comparator arrangement which looked as though it would be useful in my constant power pulse generator. But then I got lost in the complicated way it is explained. I might revisit this when I've got basic system working.

Thanks to Les, diebobo and others who are involved in this.

Charlie

charliecoutas

The device I have is a 33CK256MP505, it's all I could get from Farnell. But this isn't in the list of supported chips. I assume that if I declare it as 33CK32MP505 (which is in the list) then it will be OK? (With reduced memory of course.)

diebobo

Quote from: charliecoutas on Nov 16, 2021, 03:21 PMThe device I have is a 33CK256MP505, it's all I could get from Farnell. But this isn't in the list of supported chips. I assume that if I declare it as 33CK32MP505 (which is in the list) then it will be OK? (With reduced memory of course.)
Why u think it's not supported ? If u use this as device it just compiles fine with latest version
device = 33CK256MP505

charliecoutas


charliecoutas

I just thought I would report the progress I'm making with the dsPIC33CK256MP505, in case anybody is thinking of using one.

I soldered the TQFP-48 without the expected trouble - flux, flux and then flux on the de-solder wick seems to be the secret.

I got Les's example ADC code running and my god, it's fast. I put a port pin high at the start of an ADC conversion then dropped it after. At 160MHz the chip takes 1.4uS to do a 12 bit conversion. That means that my closed loop constant current pulse generator will indeed be possible. Thanks Les.

I've started using HC-12 as a diagnostic aid: by putting an HC-12 on the board I can check all manner of things that otherwise would have been tricky.

So onwards we go.... Thanks for the help I get from this fab forum.

Charlie

top204

#18
Glad it worked for you Charlie. It will probably need tweaking somewhere, at some time, and you can try to alter the ADC setups to try and make it faster if required. However, for more speed, you can try the procedures below. They will alter the device's frequency using its internal oscillator, but I have found the internal oscillator a little unreliable with my board layout at high speeds. However, with yours they may work well.

The fuses are the same as the code above

'---------------------------------------------------------------------------------
' Setup the device to operate at 180MHz with its internal oscillator
' Input     : None
' Output    : None
' Notes     : Waits for the oscillator to become stable
'
Proc IntOsc_180MHz()
    CLKDIV = $3001         
    PLLFBD = $5A
    OSCTUN = $00
    PLLDIV = $21          
    ACLKCON1 = $0101       
    APLLFBD1 = $C8         
    APLLDIV1 = $42         
    Write_OSCCON($0101)

    While OSCCONbits_OSWEN <> 0 : Wend
    While OSCCONbits_LOCK <> 1 : Wend
EndProc

'---------------------------------------------------------------------------------
' Setup the device to operate at 186MHz with its internal oscillator
' Input     : None
' Output    : None
' Notes     : Waits for the oscillator to become stable
'
Proc IntOsc_186MHz()  
    CLKDIV = $3001        
    PLLFBD = 88
    OSCTUN = $00
    PLLDIV = $21           
    ACLKCON1 = $0101       
    APLLFBD1 = $C8 
    APLLDIV1 = $42        
    Write_OSCCON($0101)

    While OSCCONbits_OSWEN <> 0 : Wend 
    While OSCCONbits_LOCK <> 1 : Wend
EndProc

'---------------------------------------------------------------------------------
' Setup the device to operate at 190MHz with its internal oscillator
' Input     : None
' Output    : None
' Notes     : Waits for the oscillator to become stable
'
Proc IntOsc_190MHz()  
    CLKDIV = $3001         
    PLLFBD = 90
    OSCTUN = $00
    PLLDIV = $21           
    ACLKCON1 = $0101       
    APLLFBD1 = $C8
    APLLDIV1 = $42         
    Write_OSCCON($0101)

    While OSCCONbits_OSWEN <> 0 : Wend 
EndProc

charliecoutas

Many thanks Les, I'll try it. What happens when the board layout gets overpowered by the oscillator speed? Does it just lock up or what happens?

I am going try the closed loop where the Volts reading times the Amps reading gives Watts. The system then adjusts the Volts drive (via the DAC) to make sure that the Watts now equls the required value. I suspect that it will oscillate at first. I need to fit quite a few SMD components before I can proceed.

Best
Charlie