• AVR Freaks

AnsweredHot!How to use ADC in 18F87J60

Author
parkcoding
Starting Member
  • Total Posts : 73
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
2019/04/04 18:26:49 (permalink)
0

How to use ADC in 18F87J60

Hi, everyone!
 
I have some questions about ADC in 18F87J60
 
I want to use variable resistance to display changing voltage values on the screen,
 
 
but I don't know how to write the code.
 
 
I have to write it using ADC and UART functions, but I can't make it.
 
I'll put an example what I make here.
 
Please help me.
 
 
 
#include <xc.h>
#include <pic18f87j60.h>
#include <stdbool.h>
#include <stdint.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
 
unsigned int AdcData;
void Initial_UART(void)
{
TXSTA1bits.TX9 = 0; //9Bit Transmit Enable bit
TXSTA1bits.TXEN = 1; //Transmit Enable bit
TXSTA1bits.SYNC = 0; //EUSARTx Mode select bit
TXSTA1bits.BRGH = 1; //High Baud Rate Select bit

RCSTA1bits.SPEN = 1; //Serial Port Enable bit
RCSTA1bits.RX9 = 0; //9Bit Receive Enable bit
RCSTA1bits.CREN = 1; //Continuous Erceive Enable bit

BAUDCON1bits.BRG16 = 0; //16Bit Baud Rate Resister Enable bit

SPBRG = 0xA2; //25MHz/(16*9600) - 1 = 162
//SPBRG :
}

void Initial_ADC(void)
{
ADCON0 = 0x83;
ADCON1 = 0x0E;
ADCON2 = 0x0F;
}
void Initial_Interrupts(void) //
{
RCONbits.IPEN = 1; //

INTCONbits.GIE = 1; //Global
INTCONbits.PEIE = 1; //Pherpheral

PIE1bits.RCIE = 1; //RC enable
IPR1bits.RCIP = 1; //RC flag
PIE1bits.TX1IE = 1; //TX enable
IPR1bits.TX1IP = 1; //TX flag

PIR1bits.ADIF = 1;
PIE1bits.ADIE = 1;
IPR1bits.ADIP = 1;
}

void Initial_PORT(void)
{
TRISCbits.RC7 = 1;
TRISCbits.RC6 = 0;

TRISA = 0xFF;
PORTAbits.RA0 = 1;
}
void ADC(void)
{
TXREG1 = AdcData;
}
void EUSART1_Write(uint8_t txData)
{
TXREG1 = txData;
while(!TXSTA1bits.TRMT);

}

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

void interrupt isr(void)
{
if(RC1IF == 1)
{
EUSART1_Write(EUSART1_Read());
}

if(ADIF == 1)
{
Initial_ADC;
}
}


void main(void)
{
OSCCON = 0x76;

Initial_PORT();
Initial_UART();
Initial_Interrupts();


while (1)
{
//EUSART1_Write(EUSART1_Read());
}
}
#1
qhb
Superb Member
  • Total Posts : 9998
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: How to use ADC in 18F87J60 2019/04/04 20:02:12 (permalink) ☼ Best Answerby parkcoding 2019/04/04 21:04:48
+1 (1)
For starters, I would throw away all that interrupt code.
You don't need interrupts, and you're not doing them right anyway.
Break your problem into small steps. Get the UART sending a known string to your PC first.
 
 

Nearly there...
#2
parkcoding
Starting Member
  • Total Posts : 73
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How to use ADC in 18F87J60 2019/04/04 21:07:05 (permalink)
0
here are UART sending string to my PC using PUTTY

#include <xc.h>
#include <pic18f87j60.h>
#include <stdbool.h> 
#include <stdint.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 
void Initial_UART(void)
{
TXSTA1bits.TX9 = 0; //9Bit Transmit Enable bit
TXSTA1bits.TXEN = 1; //Transmit Enable bit
TXSTA1bits.SYNC = 0; //EUSARTx Mode select bit
TXSTA1bits.BRGH = 1; //High Baud Rate Select bit

RCSTA1bits.SPEN = 1; //Serial Port Enable bit
RCSTA1bits.RX9 = 0; //9Bit Receive Enable bit
RCSTA1bits.CREN = 1; //Continuous Erceive Enable bit

BAUDCON1bits.BRG16 = 0; //16Bit Baud Rate Resister Enable bit

SPBRG = 0xA2; //25MHz/(16*9600) - 1 = 162
//SPBRG :
}

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

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

PIE1bits.RCIE = 1; 
IPR1bits.RCIP = 1; 
PIE1bits.TX1IE = 1; 
IPR1bits.TX1IP = 1; 
}

void Initial_PORT(void) 
{
TRISCbits.RC7 = 1; 
TRISCbits.RC6 = 0;
}

void EUSART1_Write(uint8_t txData)
{
TXREG1 = txData; 
while(!TXSTA1bits.TRMT); 
}

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

void interrupt isr(void) 
{
if(RC1IF == 1) 
EUSART1_Write(EUSART1_Read()); 
}


void main(void)
{

Initial_PORT();
Initial_UART();
Initial_Interrupts();

while (1)
{
}
}
#3
qhb
Superb Member
  • Total Posts : 9998
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: How to use ADC in 18F87J60 2019/04/04 21:30:50 (permalink)
+1 (1)
So you ignored most of my advice.
 

Nearly there...
#4
parkcoding
Starting Member
  • Total Posts : 73
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How to use ADC in 18F87J60 2019/04/04 22:25:09 (permalink)
0
Is it real that I have to delete all of Interrupt codes?
But isn't operating UART?
how should I do?
#5
qhb
Superb Member
  • Total Posts : 9998
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: How to use ADC in 18F87J60 2019/04/04 22:45:44 (permalink)
+1 (1)
parkcoding
Is it real that I have to delete all of Interrupt codes?

Yes. You plainly don't understand how it works, so do it the simple way.
 

But isn't operating UART?

This sentence does not make any sense in English.
 
Get rid of all your interrupt code, do NOT set the GIE bit, and change your main function from
while (1)
{
}

to
while (1)
{
    EUSART1_Write(0x55);
}

 
Your EUSART1_Write() function could be improved, but it will work ok as-is.
Once you are able to see "U" characters appearing on your PC, change the main function to
while (1)
{
    EUSART1_Write(EUSART1_Read());
}

and see if it echoes characters the way you wanted.,
 
post edited by qhb - 2019/04/04 22:48:36

Nearly there...
#6
parkcoding
Starting Member
  • Total Posts : 73
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How to use ADC in 18F87J60 2019/04/04 23:40:46 (permalink)
0
I tried it, but it doesn't work.
 
When I write code what I wrote is doing well for UART sending strings as I as press the keyboard.
 
Now I'd like to post the value on the screen, Putty, according to the variable resistance value.
 
I think the setting ADC register is correct.
 
I guess there is a problem with applying the RX, TX function with the ADC.
 
I believe you can help me because you look smart :)
 
Would you help me a little more?
 
p.s) I'm South korean
#7
qhb
Superb Member
  • Total Posts : 9998
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: How to use ADC in 18F87J60 2019/04/04 23:46:22 (permalink)
+1 (1)
parkcoding
I tried it, but it doesn't work.

Can't help if you don't show what you tried. Can't see your screen from here.
 

Nearly there...
#8
parkcoding
Starting Member
  • Total Posts : 73
  • Reward points : 0
  • Joined: 2019/03/13 16:17:10
  • Location: 0
  • Status: offline
Re: How to use ADC in 18F87J60 2019/04/05 01:03:42 (permalink)
0
I can't upload image file.
 
just Garbage values continue to come up repeatedly.
 
My goal is to change the value on the putty as the variable resistance value changes.
 
haha thanks! :)
#9
jack@kksound
code tags!
  • Total Posts : 3201
  • Reward points : 0
  • Joined: 2014/05/14 10:03:19
  • Location: 0
  • Status: offline
Re: How to use ADC in 18F87J60 2019/04/05 08:36:37 (permalink)
+1 (1)
You are not operating the A/D correctly. where do you expect a value to come from for this variable: AdcData ? 
#10
acharnley
Super Member
  • Total Posts : 315
  • Reward points : 0
  • Joined: 2016/05/01 06:51:28
  • Location: 0
  • Status: offline
Re: How to use ADC in 18F87J60 2019/04/05 15:40:07 (permalink)
0
Use MCC until you get to grips with it.

As has been said there's not much point using the ADC interrupt to do your work, you lock the time that could be needed for servicing other interrupts. The way I do it with the PIC18 is to fire the ADC interrupt, immediately start the adc sampling again, save the value (buffer!) and exit the routine. Now your adc is working in the background and your main() can deal with the buffer while servicing other interrupts like your UART.

Obviously your main() has to be faster than the adc is filling the buffer or you'll need some smarts, but it most likely is.
#11
Jump to:
© 2019 APG vNext Commercial Version 4.5