16F18345 ADC channel mapping?

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
dslocum
Posts: 102
Joined: 31 Aug 2009 12:21

16F18345 ADC channel mapping?

#1 Post by dslocum » 12 Apr 2023 17:29

For CPUs like the 16F18345 that have ADC channels mapped into different pins on different ports, it is not at all clear (EG. NOT documented, that I can find!) how to use MikroC's ADC library. I'm sure I'm not the only person needing to do this.

I could use some help and examples please.

dslocum
Posts: 102
Joined: 31 Aug 2009 12:21

Re: 16F18345 ADC channel mapping?

#2 Post by dslocum » 15 Apr 2023 17:42

Anyone?

jumboelettronica
Posts: 45
Joined: 26 Dec 2018 10:29

Re: 16F18345 ADC channel mapping?

#3 Post by jumboelettronica » 15 Apr 2023 17:49

simplified
working
PIC16F18346 (it's the same)
Internal 16 MHz oscillator
[// -----------------------------------------------------------------------------
/*
* Project name:


* Copyright: (c) MikroElektronika, 2015.
JUMBO elettronica
jumboelettronica.altervista.org
jumboelettromania.altervista.org
jumboelettromania@gmail.com

* Revision History:
15/04/23 - working

* Description:


* Test configuration:
MCU: PIC16F18346
Dev.Board: Centrale Universale 3C (CS Viola)
Schema:
Oscillator: INT 8 MHz * 2 (PLL) = 16 MHz
Ext. Modules:
SW: mikroC PRO for PIC

* NOTES:

*/
// *****************************************************************************
// --- LED (OUTPUT)
sbit LED_1 at LATC0_bit; // OUT 1
sbit LED_2 at LATC2_bit; // OUT 2
//
// *****************************************************************************
//
// *****************************************************************************
void interrupt(){
//
if(PIR0.TMR0IF) {
TMR0H = 250; // 250
LED_2 ^= 1;
PIR0.TMR0IF = 0;
}
}
// *****************************************************************************
//
// *****************************************************************************
void main() {
// unsigned short get_resp_timeout, j;
//
//- --- Oscillatore
OSCCON1 = 0x00; // NOSC: HFINTOSC with 2x PLL; NDIV: 1 (16MHz);
OSCCON3 = 0x00; // CSWHOLD may proceed; SOSCPWR Low power; SOSCBE crystal oscillator;
OSCEN = 0x00; // LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled;
OSCFRQ = 0x04; // HFFRQ 8_MHz;
OSCTUNE = 0x00; // HFTUN 0;
// --- PortA
PORTA = 0b00000000; // Esistenti: --XX.XXXX
TRISA = 0b11001110;
ANSELA = 0b00000000; // Esistenti: --XX.-XXX
WPUA = 0b00000000;
// --- PortB (Solo 20 pins)
PORTB = 0b00000000; // Esistenti: XXXX.----
TRISB = 0b00010000;
ANSELB = 0b00000000; // Esistenti: XXXX.----
WPUB = 0b00000000;
// --- PortC
PORTC = 0b00000000; // Esistenti: XXXX.XXXX (14 pins: --XX.XXXX)
TRISC = 0b11000010;
ANSELC = 0b00000000; // Esistenti: XXXX.XXXX (14 pins: --XX.XXXX)
WPUC = 0b00000000;
Delay_ms(100);
//
// --- TMR0
T0CON0 = 0b10000000; // TMR0 ON, 8 bit, Post 1/1
T0CON1 = 0b01000100; // Fosc/4, Pre 1/16
// Fosc = 16MHz, /4 = 4MHz
// for 1ms, 16MHz /0,001 = 4000
// Prescaler: 1/4096 = 1024
// 65536 - 1000 = 64536 (0xFC, 0x18)
TMR0L = 0x00;
TMR0H = 250; // 250
// --- Interrupt
PIE0.TMR0IE = 1; // TMR0 enable interrupt
PIR0.TMR0IF = 0; // Clear flag
INTCON.GIE = 1; // Enable global interrupts
//
while (1) {
LED_1 ^= 1;
Delay_ms(100);
}
}
]

jumboelettronica
Posts: 45
Joined: 26 Dec 2018 10:29

Re: 16F18345 ADC channel mapping?

#4 Post by jumboelettronica » 15 Apr 2023 18:00

Sorry, I sent the attachment in a hurry.
Do not read the comments concerning the TMR0 are quite another thing.
// 1/4,000,000 = 0.00000025
// 0.00000025 * 16 = 0.000004
// 0.001 (1ms) / 0.000004 = 250
// TMR0H = 250
Corrected comments

dslocum
Posts: 102
Joined: 31 Aug 2009 12:21

Re: 16F18345 ADC channel mapping?

#5 Post by dslocum » 15 Apr 2023 19:59

Thank you. Thank you! Got it going.

Even though I think you answered my other question thread about interrupts here, I figured it out.

It's difficult to find the information for this chip that uses the correct registers / names.

Now, do you know about the ADC channel question? :D

jumboelettronica
Posts: 45
Joined: 26 Dec 2018 10:29

Re: 16F18345 ADC channel mapping?

#6 Post by jumboelettronica » 16 Apr 2023 09:19

There are no associations for the ADC inputs, they are fixed you can see them from the "PIN ALLOCATION TABLE".
For the use of PPS, I'll give you an example:
Unlock_IOLOCK();
RA0PPS = 0x14; //RA0->EUSART1: TX1;
RXPPS = 0x01; //RA1->EUSART1: RX1;
Lock_IOLOCK();
"Unlock unlocks the PPS function, Lock locks it.
RA0PPS uses the RA0 output to which the EUSART1 Tx1 module is associated (see table on page 163 of the datasheet). Instead of RA0 you can choose any output and associate it with the module you are interested in.
RXPPS is the EUSART1 Rx module and is associated with the RA1 input (0x01 see table on page 162).
If it's not clear to you, ask.

dslocum
Posts: 102
Joined: 31 Aug 2009 12:21

Re: 16F18345 ADC channel mapping?

#7 Post by dslocum » 30 Apr 2023 00:47

Thank you once again.

On a related subject, do you have any examples of how to set up the ADC -not- using the MikroC library? I'm struggling getting reliable conversions. The conversion does not output consistent values.

I'm using a potentiometer into ANC2 to control the speed of an output pulse to a port. Timer0 is set to interrupt at 1ms intervals (that's working fine). The interrupt (should) toggle a bit on the output port at a rate set by the pot.

I don't get the full pot range from the ADC conversion, and there's significant jitter in the timing. I don't have a clue. Been at this for a couple days.

Here's a snippet:

Code: Select all

void interrupt(void) {
  if(PIR0.TMR0IF) {
    TMR0H = 250; // 250
      
      if(SH_ACCUM < SH_ADC_Val) {
        SH_ACCUM ++;
        if(SH_ACCUM > (SH_ADC_Val / 2) ) {
          LED_SHCLKOUT = LED_OFF;
        }
      } else {
        SH_ACCUM = 0;
        LED_SHCLKOUT = LED_ON;
      }    

    PIR0.TMR0IF = 0;
  }
  // ....

Code: Select all

  // Init ADC
  ADON_bit = 1;    // enable ADC
  ADFM_bit = 1;    // right justify ADC result register pair
  
    // S&H Rate pot analog input
  TRISC2_bit = 1;
  ANSC2_bit = 1;
  
    // S&H Trig Output / LED
  ANSA2_bit = 0;        // turn off analog
  TRISA2_bit = 0;       // set as output
  LED_SHCLKOUT = LED_OFF;

Code: Select all

void pReadADC() {    // get control values from CPU ADC
  static unsigned int adc_temp;
  
  ADCON0 = 0b01001001;   // ANC2
  Delay_us(1);
  GO_bit = 1;    // start a ADC conversion after short acquisition time
  while(GO_bit == 1);
  adc_temp = ADRES;
  SH_ADC_Val = 1023 - adc_temp;
  //...

dslocum
Posts: 102
Joined: 31 Aug 2009 12:21

Re: 16F18345 ADC channel mapping?

#8 Post by dslocum » 30 Apr 2023 15:09

Well, it's amazing what a good night of sleep can bring. Solved!

Because this CPU isn't properly supported in the ADC library and this is the first time I've needed to code the ADC stuff manually, I neglected to set the ADCON1's conversion clock source (ADCS), so it defaults to Fosc/2. With my CPU clock of 16 MHz, there wasn't enough time to do a proper conversion, so I initialized ADCS0_bit = 1 (Fosc/8) and BINGO!

Hope this help someone else.

jumboelettronica
Posts: 45
Joined: 26 Dec 2018 10:29

Re: 16F18345 ADC channel mapping?

#9 Post by jumboelettronica » 30 Apr 2023 15:47

Great, you did it yourself!
However if you look at table 22-1 with the frequency of 16 MHz you should choose fosc/16.
If I don't have time problems to respect, I choose ADCRC, it's always fine.

dslocum
Posts: 102
Joined: 31 Aug 2009 12:21

Re: 16F18345 ADC channel mapping?

#10 Post by dslocum » 30 Apr 2023 16:15

You are correct about the /16 clock, as my selection of /8 is(was) likely very marginal. I've now set it to the internal RC clock and as you suggest, it's working just fine.

Thanks so much for your kind help! Cheers!

Post Reply

Return to “mikroC PRO for PIC General”