• AVR Freaks

AnsweredHot!How to configure ADC of PIC18F45K80

Page: 12345.. > >> Showing page 1 of 6
Author
Djsarkar
Super Member
  • Total Posts : 210
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
2020/10/23 20:56:37 (permalink)
0

How to configure ADC of PIC18F45K80

Hi
I have started working on PIC just before one month. I am reading datasheet of PIC18F45k80 for ADC Module.  ADCON0 information is given in chapter 23. Page 350
I want to make AN0 and AN1 pin analog pin for joystick and other should digital pin  AN0 and AN1 is default analog pin so I do not understand how to make other pins of PORT A as digital pins ?

Attached Image(s)

#1
ric
Super Member
  • Total Posts : 28933
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: How to configure ADC of PIC18F45K80 2020/10/23 22:03:05 (permalink) ☄ Helpfulby Djsarkar 2020/10/24 00:03:35
+2 (2)
Analog/digital selection for each pin is done using the ANCON0 and ANCON1 registers.
(note this is ANCONx, not ADCONx) (This is mentioned in your datasheet under "11.1.4 ANALOG AND DIGITAL PORTS" )
The "other" pins of PORTA are AN2, AN3 and AN4, so you need to clear the ANSEL3 and ANSEL4 bits in ANCON0 to make those pins digital.
 
Yes, this is a bit clumsy. Newer PIC devices have one control register per port to do this, named ANSELA, ANSELB, and so on.
 
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#2
ric
Super Member
  • Total Posts : 28933
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: How to configure ADC of PIC18F45K80 2020/10/23 22:03:06 (permalink)
0
(delete accidental double post)
post edited by ric - 2020/10/23 23:42:24

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#3
Mysil
Super Member
  • Total Posts : 3943
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: How to configure ADC of PIC18F45K80 2020/10/23 23:37:14 (permalink) ☄ Helpfulby Djsarkar 2020/10/24 00:03:14
+1 (1)
Hi,
ADC in PIC18F__K80  is quite similar to ADC in many other 8-bit PIC microcontrollers,
( except _K83, _K42, K40 and _Q__  devices with ADCC peripheral).
 
There have been numerous tutorials, example codes, and threads about ADC use.
Note that control register layout  may be different between different families of devices,
so pay attention to datasheet details of register and field names.
There is also MCC Example code generator, to help making you confused.
 
Basic operation is: 
1:  Set up control registers.
2:  Set ADCON0bits.ADON = 1
3:  Wait a suitable time
4:  Change channel selection in CHS field, and CHSN field if needed.
5:  Wait a suitable time for sampling capacitor to charge.
6:  Set ADCON0bits.GO = 1;   // Go/Done bit, Do not change any other bits.
7:  Wait until GO/DONE bit go back to zero by itself
8:  Read ADRESH  and ADRESL registers.
9:  Go back to 4:
There are many other tricks.
 
Yes, Pins that may be used for Analog inputs, are configured for analog use when microcontroller is Reset.
This is controlled by ANCON0 and ANCON1 registers. There is one bit for each analog Channel.
You may use:
    ANCON0bits.ANSEL0 = 1;    /* ADC channel 0 Analog mode */
    ANCON0bits.ANSEL1 = 1;    /* ADC channel 1 Analog mode */
    ANCON0bits.ANSEL2 = 0;    /* ADC channel 2 is used for Digital Input */
    ANCON0bits.ANSEL3 = 0;
    ANCON0bits.ANSEL4 = 0;    /* This is PORT pin RA5 on PIC18F__K80  */
      ...

Note that if there is something like:  ANCON0 = 0b0000000;  somewhere else in the code,
then it may destroy the setup shown above.
There is no simple relation between analog Channel numbers, and Port Pin numbers,
so read the datasheet carefully.
 
    Mysil
post edited by Mysil - 2020/10/23 23:46:33
#4
Djsarkar
Super Member
  • Total Posts : 210
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
Re: How to configure ADC of PIC18F45K80 2020/10/23 23:59:20 (permalink)
0
ric
Analog/digital selection for each pin is done using the ANCON0 and ANCON1 registers.
(note this is ANCONx, not ADCONx) (This is mentioned in your datasheet under "11.1.4 ANALOG AND DIGITAL PORTS" )
 
 

Thank you ric  for fast responding 
ANCON0 = 0x00000011 // AN0 and AN1 as analog pins
TRISA = 0x00000011 // AN0 and AN1 as input pins
 
ADCON0: A/D CONTROL REGISTER 0
 
CHS<4:0>: Analog Channel Select bits
00000 = Channel 00 (AN0) 10000 = (Reserved)
00001 = Channel 01 (AN1) 10001 = (Reserved)
00010 = Channel 02 (AN2) 10010 = (Reserved)
 
I have been read many time for ADCON0 and I have been searching on forum Analog Channel Select bits but I still don't understand what I should select ?
#5
1and0
Access is Denied
  • Total Posts : 11501
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: How to configure ADC of PIC18F45K80 2020/10/24 00:09:47 (permalink)
+1 (1)
Djsarkar
ADCON0: A/D CONTROL REGISTER 0
 
CHS<4:0>: Analog Channel Select bits
00000 = Channel 00 (AN0)
00001 = Channel 01 (AN1)
00010 = Channel 02 (AN2)
 
I have been read many time for ADCON0 and I have been searching on forum Analog Channel Select bits but I still don't understand what I should select ?

You can only read one ADC channel at a time, so select the channel you want and do the conversion. Then repeat for the next channel, if desired.
#6
Mysil
Super Member
  • Total Posts : 3943
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: How to configure ADC of PIC18F45K80 2020/10/24 00:46:10 (permalink)
0
Hi,
Table in Channel select field description have 2 independent columns:
CHS<4:0>: Analog Channel Select bits
0b00000 = Channel 00 (AN0)
0b00001 = Channel 01 (AN1)
0b00010 = Channel 02 (AN2)
...
 0b10000 = (Reserved) Channel 16 cannot be used
 0b10001 = (Reserved)
 0b10010 = (Reserved)
 
    Mysil
#7
Djsarkar
Super Member
  • Total Posts : 210
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
Re: How to configure ADC of PIC18F45K80 2020/10/24 01:14:05 (permalink)
0
Thank you for useful information.
 
There are four option for Special Trigger Select bits
 
bit 7-6 TRIGSEL<1:0>: Special Trigger Select bits
11 = Selects the special trigger from the CCP2
10 = Selects the special trigger from the Timer1
01 = Selects the special trigger from the CTMU
00 = Selects the special trigger from the ECCP1
 
The default setting is 00.
 
Why need to trigger an ADC? What happens if I Selects the special trigger from the ECCP1?
#8
Mysil
Super Member
  • Total Posts : 3943
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: How to configure ADC of PIC18F45K80 2020/10/24 01:59:49 (permalink)
0
Hi,
Special Trigger Select bits, have to do with special tricks, you do not need to use them.
See Datasheet section 23.8
Trigger will only happen if ECCP is set up with Mode 11:
    CCP1CONbits.CCP1M = 11;
0b1011 = Compare mode: Trigger special event (ECCP1 resets TMR1 or TMR3, starts A/D conversion, sets CCP1IF bit)
See datasheet REGISTER 20-1: CCP1CON:
 
    Mysil
#9
1and0
Access is Denied
  • Total Posts : 11501
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: How to configure ADC of PIC18F45K80 2020/10/24 02:25:00 (permalink)
+1 (1)
Djsarkar
Why need to trigger an ADC? What happens if I Selects the special trigger from the ECCP1?

Usually the GO/DONE bit is manually set to initiate the A/D conversion. The Special Event Trigger mode allows you to initiate A/D conversions automatically whenever that event occurs; e.g. compare mode of the ECCP can be used to start A/D conversion every time there's a comparison match, which can be used to take A/D conversions at regular intervals.
 
#10
Djsarkar
Super Member
  • Total Posts : 210
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
Re: How to configure ADC of PIC18F45K80 2020/10/24 20:10:26 (permalink)
0
I have temperature sensorky03 that output is analog. I would use my working code of UART to display temperature data on hyper terminal.   
 
Am I initialized ADC correctly ?
 

 

#define _XTAL_FREQ 20000000     // crystal 20MHz

// PIC18F45K80 Configuration Bit Settings
// CONFIG1L
#pragma config RETEN = ON       // VREG Sleep Enable bit (Ultra low-power regulator is Enabled (Controlled by SRETEN bit))
#pragma config INTOSCSEL = LOW  // LF-INTOSC Low-power Enable bit (LF-INTOSC in Low-power mode during Sleep)
// SOSCSEL = No Setting
#pragma config XINST = OFF      // Extended Instruction Set (Disabled)
// CONFIG1H
#pragma config FOSC = HS2       // HS oscillator (high power, 16 MHz-25 MHz
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)
// CONFIG2L
#pragma config PWRTEN = ON      // Power Up Timer (Enabled)
#pragma config BOREN = OFF      // Brown Out Detect (Disabled in hardware, SBOREN disabled)
#pragma config BORV = 0         // Brown-out Reset Voltage bits (3.0V)
#pragma config BORPWR = LOW     // BORMV Power level (BORMV set to low power level)
// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1        // Watchdog Postscaler (1:1)
// CONFIG3H
#pragma config CANMX = PORTC    // ECAN Mux bit (ECAN TX and RX pins are located on RC6 and RC7, respectively)
#pragma config MSSPMSK = MSK5   // MSSP address masking (5 bit address masking mode)
#pragma config MCLRE = ON      // Master Clear Enable (MCLR Enabled, RE3 Disabled)
// CONFIG4L
#pragma config STVREN = OFF     // Stack Overflow Reset (Disabled)
#pragma config BBSIZ = BB1K     // Boot Block Size (1K word Boot Block size)
// CONFIG5L
#pragma config CP0 = ON         // Code Protect 00800-01FFF (Enabled)
#pragma config CP1 = ON         // Code Protect 02000-03FFF (Enabled)
#pragma config CP2 = ON         // Code Protect 04000-05FFF (Enabled)
#pragma config CP3 = ON         // Code Protect 06000-07FFF (Enabled)
// CONFIG5H
#pragma config CPB = ON         // Code Protect Boot (Enabled)
#pragma config CPD = ON         // Data EE Read Protect (Enabled)
// CONFIG6L
#pragma config WRT0 = ON        // Table Write Protect 00800-01FFF (Enabled)
#pragma config WRT1 = ON        // Table Write Protect 02000-03FFF (Enabled)
#pragma config WRT2 = ON        // Table Write Protect 04000-05FFF (Enabled)
#pragma config WRT3 = ON        // Table Write Protect 06000-07FFF (Enabled)
// CONFIG6H
#pragma config WRTC = ON        // Config. Write Protect (Enabled)
#pragma config WRTB = ON        // Table Write Protect Boot (Enabled)
#pragma config WRTD = ON        // Data EE Write Protect (Enabled)
// CONFIG7L
#pragma config EBTR0 = ON       // Table Read Protect 00800-01FFF (Enabled)
#pragma config EBTR1 = ON       // Table Read Protect 02000-03FFF (Enabled)
#pragma config EBTR2 = ON       // Table Read Protect 04000-05FFF (Enabled)
#pragma config EBTR3 = ON       // Table Read Protect 06000-07FFF (Enabled)
// CONFIG7H
#pragma config EBTRB = ON       // Table Read Protect Boot (Enabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>

                                        
void Port_Initialized (void)
{
    ANCON0 = 0x01;    // AN0 analog pin
    ANCON1 = 0;       // Set to digital port
    CM1CON = 0;      // Comparator off
    CM2CON = 0;      // Comparator off
    ADCON0 = 0;      // A/D conversion Disabled
    ADCON1 = 0;      // A/D conversion Disabled
    ADCON2 = 0;     // A/D conversion Disabled
 
    LATA =  0;
    LATB =  0;
    LATC =  0;
    LATD =  0;
    LATE =  0;
 
    TRISA = 0b0000001;    // AN0 as input,
    TRISB = 0b0000000;   // all are output, Unused
    TRISC = 0b0000000;   //  C6=Tx, C7=Rx
    TRISD = 0b0000000;   // all are output, Unused
    TRISE = 0b0000000;   // All are output, Unused
}

void send(char message)
{
    while(TXIF == 0 );   // Wait till the transmitter register becomes empty
    TXREG1 = message;
}

void string(char *p)
{
   while(*p != '\0') {
       __delay_ms(1);
     send(*p);
     p++;
   }
}

void Uart_Initialized(void)
{
    //TXSTAx TRANSMIT STATUS AND CONTROL REGISTER
    TXSTA1bits.CSRC = 0;    //: Don?t care.
    TXSTA1bits.TX9 = 0;    // Selects 8-bit transmission bit
    TXSTA1bits.TXEN = 1;   // Transmit Enable
    TXSTA1bits.SYNC  = 0;   // Asynchronous mode
    TXSTA1bits.SENDB = 0;
    TXSTA1bits.BRGH =  1;   // High speed mode
    TXSTA1bits.TX9D = 0;
    //RCSTAx: RECEIVE STATUS AND CONTROL REGISTER
    RCSTA1bits.SPEN  = 1 ;   // Serial port enabled
    RCSTA1bits.RX9 = 0;     // Selects 8-bit reception
    RCSTA1bits.SREN  = 1 ;   // Don?t care.
    RCSTA1bits.CREN  = 1 ;   // Enables receiver
    RCSTA1bits.ADDEN = 0;       
    RCSTA1bits.FERR  = 0 ;  // No framing error
    RCSTA1bits.OERR  = 1 ; // Overrun error
    RCSTA1bits.RX9D   = 0 ;// Selects 8-bit reception

    SPBRGH1 = 0;
    SPBRG1 = 129,

    INTCON = 0x00;
    PIR1 = 0x00;
    PIR2= 0x00;
    PIR3 = 0x00;
    PIR4 = 0x00;
    PIR5 = 0x00;
    PIE1 = 0x00;
    PIE2 = 0x00;
    PIE3 = 0x00;
    PIE4 = 0x00;
    PIE5 = 0x00;
    IPR1 = 0x00;
    IPR2 = 0x00;
    IPR3 = 0x00;
    IPR4 = 0x00;
 
}

//Initialized  ADC
void ADC_Initialized()
{
    ADCON0 = 0b00000001 ;   // Channel 00 (AN0), enable ADC conversion
    ADCON1 = 0b00000000;    // Selects the special trigger from the ECCP1, AVDD AVSS Channel 00 (AVSS)
    ADCON2 = 0b11111110;    // right justify, 20 TAD, Fosc/64                                                       
}

void main(void)
{
    Port_Initialized ();
    Uart_Initialized ();
    ADC_Initialized();
   
    while (1)
    {
                                                   
                                              
    }
}


#11
ric
Super Member
  • Total Posts : 28933
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: How to configure ADC of PIC18F45K80 2020/10/24 22:21:14 (permalink)
+1 (1)
I think that looks ok so far.
You haven't included any code to actually do an ADC reading yet.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#12
Djsarkar
Super Member
  • Total Posts : 210
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
Re: How to configure ADC of PIC18F45K80 2020/10/25 00:19:14 (permalink)
0
ric
I think that looks ok so far.
You haven't included any code to actually do an ADC reading yet.
 

I am trying to understand code for reading sensor https://tkkrlab.nl/wiki/Arduino_KY-013_Temperature_sensor_module its little bit difficult because I am not familiar with Arduino  
#13
ric
Super Member
  • Total Posts : 28933
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: How to configure ADC of PIC18F45K80 2020/10/25 00:24:42 (permalink)
0
Don't try to do too much at once.
Just do the analog measurement, and display the 10 bit ADC result (0 to 1023).
Try doing the conversion to temperature by hand to see if it makes sense.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#14
ric
Super Member
  • Total Posts : 28933
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: How to configure ADC of PIC18F45K80 2020/10/25 00:38:41 (permalink)
0
n.b. you could steal their thermistor() function as-is
Be aware it uses lots of floating point calculaitons, so won't be quick.
As I mentioned, try converting the raw results yourself using a calculator to see if you get the expected answer.
 
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#15
Djsarkar
Super Member
  • Total Posts : 210
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
Re: How to configure ADC of PIC18F45K80 2020/10/25 00:59:05 (permalink)
0
ric
Don't try to do too much at once.
 
Try doing the conversion to temperature by hand to see if it makes sense.
 

PIC18F45K80 has 12 bit inbuilt ADC A 12 bit. A/D will take a 0 to 5 volt input and convert it to 2^12 bits or offer up 0 to 4096 bits or quantization levels.
 
What's formula to calculate temperature with 12 bit ADC?
#16
ric
Super Member
  • Total Posts : 28933
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: How to configure ADC of PIC18F45K80 2020/10/25 01:37:08 (permalink)
-1 (1)

PIC18F45K80 has 12 bit inbuilt ADC A 12 bit. A/D will take a 0 to 5 volt input and convert it to 2^12 bits or offer up 0 to 4096 bits or quantization levels.

Congratulations, you're starting to get your own information from the datasheet.

What's formula to calculate temperature with 12 bit ADC?

That's your job. Do you want it all done for you?
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#17
Djsarkar
Super Member
  • Total Posts : 210
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
Re: How to configure ADC of PIC18F45K80 2020/10/25 02:29:59 (permalink)
0
ric
 
That's your job. Do you want it all done for you?

so as per my understanding 12 bit ADC would gives  range from 0 to 4096 values and I have to convert it into temperature  (0 : 22048  : 4096 )
For example
above 22048 ADC values (Plus temperature )
below 22048 ADC (Minus temperature)
post edited by Djsarkar - 2020/10/25 02:31:16
#18
ric
Super Member
  • Total Posts : 28933
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: How to configure ADC of PIC18F45K80 2020/10/25 02:56:04 (permalink)
0
Djsarkar
so as per my understanding 12 bit ADC would gives  range from 0 to 4096 values

Yes

and I have to convert it into temperature  (0 : 22048  : 4096 )

Usually you would convert it into a voltage first, then work out what the temperature is from the voltage.
I have no idea what you mean by "(0 : 22048  : 4096 )"

For example
above 22048 ADC values (Plus temperature )
below 22048 ADC (Minus temperature)

No idea what you are talking about.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#19
Djsarkar
Super Member
  • Total Posts : 210
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
Re: How to configure ADC of PIC18F45K80 2020/10/25 03:04:47 (permalink)
0
ric


I was reading old thread on the forum  https://www.microchip.com/forums/m1013700.aspx#:~:text=temperature(%C2%B0C%20)%3D%5B,%5D*(19.5%2F1000)%3Bint adc_raw;float voltage;float temperature;voltage = adc_raw * ADC_V_PER_COUNT;temperature = ((voltage - OFFSET) * T_COEFF); I am reading sensor module to get offset and t coeff value
post edited by Djsarkar - 2020/10/25 03:11:56
#20
Page: 12345.. > >> Showing page 1 of 6
Jump to:
© 2020 APG vNext Commercial Version 4.5