ADC Problem! How to Work with ADC with PIC32MZ!?

mikroC, mikroBasic and mikroPascal PRO for Microchip’s 32-bit PIC MCUs
Post Reply
Author
Message
motazAmer
Posts: 1
Joined: 24 Aug 2019 07:20

ADC Problem! How to Work with ADC with PIC32MZ!?

#1 Post by motazAmer » 24 Aug 2019 07:40

Hi
I'm using PIC32MZ2048EFH064, and now I'm trying to use ADC with it!
the resources about the topic are very insufficient, and I feel that the library is missing a lot of parts!
like for example, how to change the ADC resolution from the library! my PIC32 supports 12bit and the library stick to 10bits.
that's not even the problem, my PIC32 have 8 ADC modules on the datasheet, but the library only supports ADC1! and whenever I type ADC2, for example, it gives an error.
I tried reading ADC with ADC1 by setting the number of Analog channel I have like "AN5" which is connected to module #0, but it gave a fixed value 0X0FFF!

can you please give an example about the ADC, or direct me to solve the issue by MikroC.
I know I can solve this issue referring to the original registers! but what's the use of MikroC if so!

Here is the code I used for ADC:

Code: Select all

unsigned int adcValue;
unsigned char adcBytes[2];

void main() {

 UART1_Init(115200);
 U1RXR |= 0x0002;
 RPF5R |= 0x0001;

 TRISB.B10 = 1;            //AN5 Pin
 ANSELB.ANSB10 = 1;
 ADC1_Init(); //[here if I type ADC0_Init() it gives error]

 while(1)
 {
  adcValue = ADC1_Read(AN5); //[tried also //adcValue = ADC1_Get_Sample(AN5);]

  adcBytes[0] = adcValue>>8;
  adcBytes[1] = adcValue;
  UART1_Write(adcBytes[0]);
  UART1_Write(adcBytes[1]);
 }
}

User avatar
petar.suknjaja
mikroElektronika team
Posts: 683
Joined: 05 Mar 2018 09:44
Location: Belgrade

Re: ADC Problem! How to Work with ADC with PIC32MZ!?

#2 Post by petar.suknjaja » 27 Aug 2019 08:30

Hi,

PIC32MZ2048EFH064 use 24 channel ADC library, uses only ADC1 and
24 channels which go from 0 to 18 and 45-49 (AN0 - AN18 | AN45-AN49)


The ADC1_Get_Sample() takes the one parameter - which is basically the channel number.

So if you want to use AN5, use the

ADC1_Get_Sample(5) in your project.


Prototype
unsigned ADCx_Get_Sample(unsigned channel);

Description
The function enables ADC module and reads the specified analog channel input.

Parameters channel represents the channel from which the analog value is to be acquired.

Returns 10-bit unsigned value from the specified channel.

Requires The MCU with built-in ADC module.

Prior to using this routine, ADC module needs to be initialized. See ADCx_Init and ADCx_Init_Advanced.
Before using the function, be sure to configure the appropriate TRISx bits to designate pins as inputs.

Code: Select all

Example unsigned adc_value;
...
adc_value = ADC1_Get_Sample(10);    // read analog value from ADC module channel 10

Notes The function sets the appropriate bit in the AD1PCFG registers to enable analog function of the chosen pin.
Refer to the appropriate Datasheet for channel-to-pin mapping.

Dany79
Posts: 12
Joined: 11 Jun 2015 15:39
Location: Canada

Re: ADC Problem! How to Work with ADC with PIC32MZ!?

#3 Post by Dany79 » 30 Aug 2019 21:52

The adc can scan the AN port. The cpu dont have to wait after the ADC.

Some code from one of my firmware using the ADC DMA

ADC init for scan to ram

Code: Select all

ANSELA := 0;
          ANSELB := 0;
          ANSELC := 0;
          ANSELD := 0;
          ANSELE := 0;
          ANSELF := 0;
          ANSELG := 0;

          ANSELA.0:=1;
          ANSELA.1:=1;
          ANSELA.5:=1;
          ANSELA.9:=1;
          ANSELA.10:=1;
          ANSELB.1:=1;
          ANSELB.10:=1;
          ANSELB.13:=1;
          ANSELB.15:=1;
          ANSELC.1:=1;
          ANSELC.2:=1;
          ANSELC.3:=1;
          ANSELC.4:=1;
          ANSELD.15:=1;
          ANSELE.5:=1;
          ANSELE.6:=1;
          ANSELE.7:=1;
          ANSELE.8:=1;
          ANSELE.9:=1;
          ANSELE.28:=1;
          ANSELF.12:=1;
          ANSELG.9:=1;
          ANSELG.15:=1;
TRISA.0:=1;
          TRISA.1:=1;
          TRISA.5:=1;
          TRISA.9:=1;
          TRISA.10:=1;
          TRISB.1:=1;
          TRISB.10:=1;
          TRISB.13:=1;
          TRISB.15:=1;
          TRISC.1:=1;
          TRISC.2:=1;
          TRISC.3:=1;
          TRISC.4:=1;
          TRISD.15:=1;
          TRISE.5:=1;
          TRISE.6:=1;
          TRISE.7:=1;
          TRISE.8:=1;
          TRISE.9:=1;
          TRISE.28:=1;
          TRISF.12:=1;
          TRISG.9:=1;
          TRISG.15:=1;
ADC0CFG :=devadc0;
          ADC1CFG :=devadc1;
          ADC2CFG :=devadc2;
          ADC3CFG :=devadc3;
          ADC4CFG :=devadc4;
          ADC7CFG :=devadc7;
          
          adccon1:=0;
          adccon1.selres0 :=1;
          adccon1.selres1 :=1;
          adccon1.strgsrc0 :=1;
          adccon1.strgsrc1 :=0;
          adccon1.strgsrc2 :=1;
          adccon1.strgsrc3 :=0;
          adccon1.strgsrc4 :=0;

          adccon2:=0;
          adccon2.samc0 :=1;
          adccon2.samc2 :=1;
          adccon2.adcdiv0  :=1;

          adcancon :=0;
          adcancon.wkupclkcnt0 :=1;
          adcancon.wkupclkcnt2 :=1;
          
          adccon3.ADCSEL0:=1  ;
          adccon3.adcsel1 :=1;
          adccon3.CONCLKDIV0:=1;
          adccon3.CONCLKDIV1 :=0;
          adccon3.CONCLKDIV2 :=0;
          adccon3.CONCLKDIV3 :=0;
          adccon3.CONCLKDIV4 :=0;
          adccon3.CONCLKDIV5 :=0;
          adccon3.VREFSEL0:=0;
          adccon3.VREFSEL1:=0;
          adccon3.VREFSEL2:=0;

          adc1time.adcdiv0:=1;
          adc1time.adcdiv1:=0;
          adc1time.adcdiv2:=0;
          adc1time.adcdiv3:=0;
          adc1time.adcdiv4:=0;
          adc1time.adcdiv5:=0;
          adc1time.adcdiv6:=0;
          adc1time.samc0:=1;
          adc1time.samc1:=0;
          adc1time.samc2:=1;
          adc1time.samc3:=0;
          adc1time.samc4:=0;
          adc1time.samc5:=0;
          adc1time.samc6:=0;
          adc1time.samc7:=0;
          adc1time.samc8:=0;
          adc1time.samc9:=0;
          adc1time.SELRES0:=1;
          adc1time.selres1:=1;

          adctrgmode.SH1ALT0:=0;
          adctrgmode.SH1ALT1:=0;

          adcimcon1:=0;
          adcimcon2:=0;
          adcimcon3:=0;

          adcgirqen1:=0;
          adcgirqen2:=0;

          adccss1:=0;
          adccss2:=0;

          ADCCSS1.CSS1:=1;
          ADCCSS1.CSS5:=1;
          ADCCSS1.CSS8:=1;
          ADCCSS1.CSS10:=1;
          ADCCSS1.CSS11:=1;
          ADCCSS1.CSS15:=1;
          ADCCSS1.CSS16:=1;
          ADCCSS1.CSS17:=1;
          ADCCSS1.CSS19:=1;
          ADCCSS1.CSS20:=1;
          ADCCSS1.CSS21:=1;
          ADCCSS1.CSS22:=1;
          ADCCSS1.CSS23:=1;
          ADCCSS1.CSS24:=1;
          ADCCSS1.CSS25:=1;
          ADCCSS1.CSS26:=1;
          ADCCSS1.CSS27:=1;
          ADCCSS1.CSS28:=1;
          ADCCSS1.CSS29:=1;
          ADCCSS1.CSS31:=1;
          ADCCSS2.CSS33:=1;
          ADCCSS2.CSS34:=1;
          
          adccmpcon1:=0;
          adccmpcon2:=0;
          adccmpcon3:=0;
          adccmpcon4:=0;
          adccmpcon5:=0;
          adccmpcon6:=0;

          adcfltr1:=0;
          adcfltr2:=0;
          adcfltr3:=0;
          adcfltr4:=0;
          adcfltr5:=0;
          adcfltr6:=0;
          
          ADCTRG1.TRGSRC10:=1;
          ADCTRG1.TRGSRC11:=0;
          ADCTRG1.TRGSRC12:=1;
          ADCTRG1.TRGSRC13:=0;
          ADCTRG2.TRGSRC50:=1;
          ADCTRG2.TRGSRC51:=0;
          ADCTRG2.TRGSRC52:=1;
          ADCTRG2.TRGSRC53:=0;
          ADCTRG3.TRGSRC80:=1;
          ADCTRG3.TRGSRC81:=0;
          ADCTRG3.TRGSRC82:=1;
          ADCTRG3.TRGSRC83:=0;
          ADCTRG3.TRGSRC100:=1;
          ADCTRG3.TRGSRC101:=0;
          ADCTRG3.TRGSRC102:=1;
          ADCTRG3.TRGSRC103:=0;
          ADCTRG3.TRGSRC110:=1;
          ADCTRG3.TRGSRC111:=0;
          ADCTRG3.TRGSRC112:=1;
          ADCTRG3.TRGSRC113:=0;

          adceien1:=0;
          adceien2:=0;

          adccon1.on_ :=1;
          adcancon.ANEN1:=1;
          adcancon.ANEN7:=1;

          adccon3.DIGEN1:=1;
          adccon3.digen7:=1;
and the procedure in pascal to read the memory

Code: Select all

PROCEDURE ADC_READ;
          BEGIN
          AN_PWR1:=ADCDATA11;
          AN_PWR2:=ADCDATA24;
          AN_PWR3:=ADCDATA20;
          AN_PWR4:=ADCDATA19;
          AN_12V:=ADCDATA25;
          AN_33V:=ADCDATA26;
          AN_GEN1:=ADCDATA33;
          AN_GEN2:=ADCDATA10;
          AN_GEN3:=ADCDATA8;
          AN_GEN4:=ADCDATA21;
          AN_GEN10:=ADCDATA23;
          AN_GEN9:=ADCDATA34;
          AN_GEN8:=ADCDATA17;
          AN_GEN7:=ADCDATA16;
          AN_GEN6:=ADCDATA15;
          AN_GEN5:=ADCDATA22;
          AN_BUZZER:=ADCDATA31;
          AN_DETECTOR:=ADCDATA1;
          AN_STROB1:=ADCDATA29;
          AN_STROB2:=ADCDATA5;
          AN_ACTIVATE:=ADCDATA27;
          AN_MUTE:=ADCDATA28;
          adccon3.GSWTRG:=1;
          END;

Post Reply

Return to “PIC32 PRO Compilers”