• AVR Freaks

AnsweredHot!How to use ADC in PIC18

Page: < 12 Showing page 2 of 2
Author
Mysil
Super Member
  • Total Posts : 3330
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: How to use ADC in PIC18 2019/04/15 18:49:02 (permalink)
0
Hi,
PORTA is not the same as ANSEL register!
And there is no such thing as PIC19F87J60.
 
Instead, PIC18F87J60 have ADCON1  register with PCFGx bits for configuration of I/O pins as Digital / Analog.
See datasheet DS39762F-page 340:  ADCON1 register
 
All pins that may be used for Analog inputs, are in Analog mode from device Reset. 
    ADCON1bits.PCFG3 = 1;
    ADCON1bits.PCFG2 = 1;
    ADCON1bits.PCFG1 = 1;
    ADCON1bits.PCFG0 = 0;

Means that all pins other than AN0 is configured as Digital mode.
 
Whatever is done with:    PORTAbits.RA0 = 1;
Do not matter, as long as:    TRISAbits.TRISA0 = 1;


    Mysil
#21
parkcoding
Starting Member
  • Total Posts : 87
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How to use ADC in PIC18 2019/04/15 19:17:59 (permalink)
0
finally I resolved this problem.
 
Thx for helping me~!
 
 
 
#include <xc.h>
#include <pic18f87j60.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

#pragma config DEBUG = OFF
#pragma config XINST = OFF
#pragma config WDT = OFF
#pragma config FOSC = HS

#define _XTAL_FREQ 25000000
#define Baud_rate 9600
#define ADC_10BIT
#define ADC_VOLT
#define ADC_HEX

unsigned int adc_value = 0;
float adc_volt = 0;
void Initial_PORT(void)
{
TRISAbits.TRISA0 = 1; 
PORTAbits.RA0 = 1;
TRISCbits.RC7 = 1;
TRISCbits.RC6 = 0;
}
void Initial_UART(void)
{
TXSTA1bits.TX9 = 0;
TXSTA1bits.TXEN = 1;
TXSTA1bits.SYNC = 0;
TXSTA1bits.BRGH = 1;
RCSTA1bits.SPEN = 1;
RCSTA1bits.RX9 = 0;
RCSTA1bits.CREN = 1;
BAUDCON1bits.BRG16 = 0;
SPBRG = 0xA2;
}

void Initial_Interrupts(void)
{
RCONbits.IPEN = 1;
INTCONbits.GIE = 1;
INTCONbits.PEIE = 1;
PIE1bits.RCIE = 1;
PIE1bits.TX1IE = 1;
}

void Initial_ADC()
{
ADCON2bits.ADFM = 1; 
ADCON2bits.ACQT2 = 1; 
ADCON2bits.ACQT1 = 1;
ADCON2bits.ACQT0 = 1;
ADCON2bits.ADCS2 = 1; 
ADCON2bits.ADCS1 = 1;
ADCON2bits.ADCS0 = 1;
ADCON1bits.VCFG1 = 0; 
ADCON1bits.VCFG0 = 0; 
ADCON1bits.PCFG3 = 1; 
ADCON1bits.PCFG2 = 1;
ADCON1bits.PCFG1 = 1;
ADCON1bits.PCFG0 = 0;
ADCON0bits.ADCAL = 0; 
ADCON0bits.CHS3 = 0; 
ADCON0bits.CHS2 = 0;
ADCON0bits.CHS1 = 0;
ADCON0bits.CHS0 = 0;
ADCON0bits.ADON = 1; 
ADCON0bits.GODONE = 1; 
PIR1bits.ADIF = 0;
PIE1bits.ADIE = 1; 
}

void putch(char txData)
{
TXREG1 = txData;
while(!TXSTA1bits.TRMT);
}
char getch(void)
{
while(!RC1IF);
return RCREG1;
}

unsigned int adc_read(unsigned char)
{
unsigned int adc_buf; 
ADCON0bits.CHS = 0; 
ADCON0bits.GO = 1;
while(ADCON0bits.GODONE); 
adc_buf = (unsigned int)(ADRESH <<8) + ADRESL; 
return adc_buf; 
}
int main(int argc, char** argv)
{
OSCCON = 0xF2;
Initial_PORT();
Initial_UART();
Initial_Interrupts();
Initial_ADC();
while(1)
{
adc_volt = (float)(5*((float)(adc_value)/1023)); 
adc_value = adc_read(0); 
printf("ADC_Value = 0x%3x\r\n", adc_value); 
printf("ADC_Value = %d\r\n", adc_value); 
printf("ADC_Volt = %1.2fV\r\n", adc_volt);
}
}
#22
pcbbc
Super Member
  • Total Posts : 1252
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: How to use ADC in PIC18 2019/04/15 20:43:08 (permalink) ☄ Helpfulby parkcoding 2019/07/03 16:17:43
+1 (1)
Well done. But it seems you still haven’t learnt how to use [CODE]tags to format your listings correctly[/CODE]

Please see post #17 by Bob White.
Note the word “CODE” should be in lower case to be recognised.
#23
dan1138
Super Member
  • Total Posts : 3194
  • Reward points : 0
  • Joined: 2007/02/21 23:04:16
  • Location: 0
  • Status: offline
Re: How to use ADC in PIC18 2019/04/16 15:03:57 (permalink) ☄ Helpfulby parkcoding 2019/07/03 16:17:35
0
@parkcoding,

Could you describe how the changes you made in post #22 to your code from post #14 made it work?

Also you do not show any interrupt handling so why does your code not crash?
// Notations:
// + Line added
// - Line removed
// ± Line changed
//
#include <xc.h>
#include <pic18f87j60.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

#pragma config DEBUG = OFF
#pragma config XINST = OFF
#pragma config WDT = OFF
#pragma config FOSC = HS

#define _XTAL_FREQ 25000000
#define Baud_rate 9600
#define ADC_10BIT
#define ADC_VOLT
#define ADC_HEX

unsigned int adc_value = 0;                              // ± unsigned int adc_value;
float adc_volt = 0;                                      // ± float adc_volt;

void Initial_PORT(void)
{
    TRISAbits.TRISA0 = 1;
    PORTAbits.RA0 = 1;

    TRISCbits.RC7 = 1;
    TRISCbits.RC6 = 0;
}

void Initial_UART(void)
{
    TXSTA1bits.TX9 = 0;
    TXSTA1bits.TXEN = 1;
    TXSTA1bits.SYNC = 0;
    TXSTA1bits.BRGH = 1;

    RCSTA1bits.SPEN = 1;
    RCSTA1bits.RX9 = 0;
    RCSTA1bits.CREN = 1;

    BAUDCON1bits.BRG16 = 0;

    SPBRG = 0xA2;
}

void Initial_Interrupts(void)
{
    RCONbits.IPEN = 1;

    INTCONbits.GIE = 1;
    INTCONbits.PEIE = 1;

    PIE1bits.RCIE = 1;

    PIE1bits.TX1IE = 1;
}

void Initial_ADC()
{
                                                         // - TRISAbits.TRISA0 = 1;
                                                         // - PORTAbits.RA0 = 1;

    ADCON2bits.ADFM = 1;
    ADCON2bits.ACQT2 = 1;
    ADCON2bits.ACQT1 = 1;
    ADCON2bits.ACQT0 = 1;
    ADCON2bits.ADCS2 = 1;
    ADCON2bits.ADCS1 = 1;
    ADCON2bits.ADCS0 = 1;

    ADCON1bits.VCFG1 = 0;                                // ± ADCON1bits.VCFG1 = 1;
    ADCON1bits.VCFG0 = 0;                                // ± ADCON1bits.VCFG0 = 1;
    ADCON1bits.PCFG3 = 1;
    ADCON1bits.PCFG2 = 1;
    ADCON1bits.PCFG1 = 1;
    ADCON1bits.PCFG0 = 0;

    ADCON0bits.ADCAL = 0;
    ADCON0bits.CHS3 = 0;
    ADCON0bits.CHS2 = 0;
    ADCON0bits.CHS1 = 0;
    ADCON0bits.CHS0 = 0;
    ADCON0bits.ADON = 1;
    ADCON0bits.GODONE = 1;

    PIR1bits.ADIF = 0;
    PIE1bits.ADIE = 1;
}

void putch(char txData)
{
    TXREG1 = txData;
    while(!TXSTA1bits.TRMT);
}

char getch(void)
{
    while(!RC1IF);
    return RCREG1;
}

unsigned int adc_read(unsigned char)                     // ± unsigned int adc_read(unsigned char AN0)
{
    unsigned int adc_buf;

    ADCON0bits.CHS = 0;                                  // ± ADCON0bits.CHS = AN0;
                                                         // - __delay_us(20);
    ADCON0bits.GO = 1;
    while(ADCON0bits.GODONE);
    adc_buf = (unsigned int)(ADRESH <<8) + ADRESL;

    return adc_buf;
}

int main(int argc, char** argv)
{

    OSCCON = 0xF2;
    Initial_PORT();
    Initial_UART();
    Initial_Interrupts();
    Initial_ADC();

    while(1)
    {
        adc_volt = (float)(5*((float)(adc_value)/1023));

        adc_value = adc_read(0);
        printf("ADC_Value = 0x%3x\r\n", adc_value);
        printf("ADC_Value = %d\r\n", adc_value);
        printf("ADC_Volt = %1.2fV\r\n", adc_volt);
    }
}



#24
Page: < 12 Showing page 2 of 2
Jump to:
© 2019 APG vNext Commercial Version 4.5