AnsweredHot!How to use ADC in PIC18

Page: 12 > Showing page 1 of 2
Author
parkcoding
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
2019/04/09 22:36:14 (permalink)
0

How to use ADC in PIC18

Hi, I have some questions.
 
I try to operating ADC that I can check ADC_VALUE on display as change as the variable resistance value in PIC18F87J60
 
but it doesn't work.
 
Please help 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_HEX



 
 
void Initial_PORT(void)
{
    TRISAbits.RA0 = 1;
    TRISAbits.TRISA0 = 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 EUSART1_Write(uint8_t txData)
{
    TXREG1 = txData;
    while(!TXSTA1bits.TRMT);
}

uint8_t EUSART1_Read(void)
{
    while(!RC1IF);
    return RCREG1;
}

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

    ADCON2bits.ADFM = 1;
    ADCON2bits.ADCS0 = 1;
    ADCON2bits.ADCS1 = 0;
    ADCON2bits.ADCS2 = 1;

    ADCON1bits.VCFG0 = 0;
    ADCON1bits.VCFG1 = 0;

    ADCON0bits.CHS0 = 0;
    ADCON0bits.CHS1 = 0;
    ADCON0bits.CHS2 = 0;
    ADCON0bits.CHS3 = 0;

    ADCON0bits.CHS = 0;

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

unsigned int adc_read(unsigned char ANx_Channel)
{
    unsigned int adc_buf;

    ADCON0bits.CHS = ANx_Channel;

    ADCON2bits.ADFM = 1;
    GO_nDONE = 1;
    while(GO_nDONE);

    adc_buf = (unsigned int)(ADRESH <<8) + ADRESL;

    return adc_buf;
}

int main(int argc, char** argv)
{
    unsigned int adc_value;
    OSCCON = 0b01111000;

     Initial_UART();

     Initial_ADC();

    while(1)
 {
    adc_value = adc_read(0);
    printf("ADC_Value = 0x%3x\r", adc_value);
 }
}
 
 
#1
qhb
Superb Member
  • Total Posts : 9621
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: How to use ADC in PIC18 2019/04/09 23:40:29 (permalink)
+1 (1)
parkcoding
but it doesn't work.

You need to be a lot more descriptive about what "doesn't work" actually means if you want quality help.
What did you expect to see, and what did you actually see?
 
I see a lot of things in your code that could be improved, but I'm not sure any of them would actually stop you getting a result.
 
#2
mbrowning
Just a Member
  • Total Posts : 1280
  • Reward points : 0
  • Joined: 2005/03/16 14:32:56
  • Location: Melbourne, FL
  • Status: offline
Re: How to use ADC in PIC18 2019/04/10 03:30:28 (permalink) ☼ Best Answerby parkcoding 2019/04/14 18:09:02
+3 (3)
Printf requires you to create a putch function or it does nothing. See the compiler manual. Either rename eusart_write to putch or create another duplicate function named putch. - assuming you’re using xc8

Oh well - there's always next year
#3
parkcoding
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How to use ADC in PIC18 2019/04/14 18:10:25 (permalink)
0
I resolved problem what I can't see 'printf' on putty.
 
But I can't still check ADC value......
 
What should I do?
#4
pcbbc
Super Member
  • Total Posts : 794
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: How to use ADC in PIC18 2019/04/14 23:39:27 (permalink)
0
See above reply?
#5
parkcoding
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How to use ADC in PIC18 2019/04/15 01:20:11 (permalink)
0
I mean, I can check the ADC value,
 
but only the fixed value will continue to be output.
 
 
I'd like to check the changing ADC value.
 
 
 
#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
#define ADC_10BIT
 
unsigned int adc_value;
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 = 0;
    ADCON2bits.ADCS1 = 1;
    ADCON2bits.ADCS0 = 0;

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

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

    ADCON0bits.ADON = 0;
    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 AN0)
{
    unsigned int adc_buf;

    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);
  }
}
#6
John Linq
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2016/08/31 19:23:12
  • Location: 0
  • Status: offline
Re: How to use ADC in PIC18 2019/04/15 01:24:58 (permalink)
+1 (1)
 
Maybe turn it on?
 
void Initial_ADC()
{

    ADCON0bits.ADON = 0;

}

post edited by John Linq - 2019/04/15 01:31:34
#7
parkcoding
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How to use ADC in PIC18 2019/04/15 01:45:30 (permalink)
0
I've just checked this part and corrected it.
 
but I can't still check the value between 5V and 0V.
 
Only the two end values can be checked. :(
#8
John Linq
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2016/08/31 19:23:12
  • Location: 0
  • Status: offline
Re: How to use ADC in PIC18 2019/04/15 01:58:27 (permalink)
0
 
Maybe check ANSELA0?
 
 
#9
parkcoding
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How to use ADC in PIC18 2019/04/15 16:36:18 (permalink)
0
ANSELA0?
 
you mean, I have to check AN0?
 
 
#10
NKurzman
A Guy on the Net
  • Total Posts : 17131
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: How to use ADC in PIC18 2019/04/15 16:39:41 (permalink)
0
No
did you configure the pin to be an input
and configure it to analog mode.
#11
parkcoding
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How to use ADC in PIC18 2019/04/15 16:40:43 (permalink)
0
I can read only 5V and 0V.....
 
What's problem.....?
 
#12
parkcoding
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How to use ADC in PIC18 2019/04/15 16:41:50 (permalink)
0
yeah.
 
I configure the AN0 pin to be an input
 
and configure it to analog mode.
 
#13
parkcoding
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How to use ADC in PIC18 2019/04/15 16:45:21 (permalink)
0
#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;
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 = 1;
    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 AN0)
{
    unsigned int adc_buf;

    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);
}
}
#14
Bob White
Super Member
  • Total Posts : 231
  • Reward points : 0
  • Joined: 2010/11/06 19:52:38
  • Location: Denver, Colorado
  • Status: online
Re: How to use ADC in PIC18 2019/04/15 17:15:34 (permalink)
0
Get ride of this "#include <pic18f87j60.h>"
 
Where exactly in your code did you set the input to your A/C converter to be an analog pin?  I don't see that anywhere.
#15
dan1138
Super Member
  • Total Posts : 3039
  • Reward points : 0
  • Joined: 2007/02/21 23:04:16
  • Location: 0
  • Status: offline
Re: How to use ADC in PIC18 2019/04/15 17:16:17 (permalink)
0
This is your code from post #14:
#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;
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 = 1;
    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 AN0)
{
    unsigned int adc_buf;

    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);
    }
}

I see you have enabled interrupts from the ADC, UART transmit and receive and the global interrupt system, yet you have no interrupt handler.
 
This will be a problem.
#16
Bob White
Super Member
  • Total Posts : 231
  • Reward points : 0
  • Joined: 2010/11/06 19:52:38
  • Location: Denver, Colorado
  • Status: online
Re: How to use ADC in PIC18 2019/04/15 17:18:09 (permalink)
0
P.S.  Use code tags.  [CODE] (with CODE actually in lower case, "code") to start your code block and [/CODE] (again, with CODE actually in lower case) to end the code block.  This will cause the forum software to format your code so it is much easier to read.
 
Example:
 

#define _XTAL_FREQ 25000000
#define Baud_rate 9600
#define ADC_10BIT
#define ADC_VOLT
#define ADC_HEX
 
unsigned int adc_value;
float adc_volt;

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

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

 
 
#17
parkcoding
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How to use ADC in PIC18 2019/04/15 17:24:15 (permalink)
0
I gotta check
#18
parkcoding
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How to use ADC in PIC18 2019/04/15 17:35:57 (permalink)
0
I added
 
void interrupt isr(void)

    if(RC1IF == 1) 
    putch(getch());
}
 
 
but it doesn't work.....
#19
parkcoding
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How to use ADC in PIC18 2019/04/15 17:48:33 (permalink)
0
on datasheet,
 
PIC19F87J60 hasn't ANSEL register.
 
so I used PORTA
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2019 APG vNext Commercial Version 4.5