Athosza
Newbie level 5
- Joined
- Aug 3, 2010
- Messages
- 8
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Location
- South Africa
- Activity points
- 1,363
Greetings all,
I am having a problem with getting a simple ADC working. I have tried to read the data sheet of the PIC with as much understanding as possible but I am still stuck. What I am trying to do is simply read a change in voltage and get the PIC to convert the value to digital and store it in 'variable'. I am simply using either 0V or 5V however no matter what I select my TAD or TOSC the only value that is stored in variable is 0x03FF and this is why I included the LED code so that if there was a change I could see it straight away. I am not sure if my code is correct perhaps I am making a mistake somewhere.
Any help/feedback will be greatly appreciated.
Thanks
Athosza
my code: (C 18 compiler)
#include <p18f2431.h>
#include <stdlib.h>
#include <adc.h>
#define ADC_CH0 0x00 //Select Channel 0
void oscillator_setup()
{
OSCCON = 0x76; // 8MHz internal clock frequency
TRISB = 0;
PORTB = 0;
}
void Setup_ADC()
{
ADCON0 = 0x03;// Group A is taken
ADCON1 = 0x18; // AVdd and AVss selected as voltage reference
ADCON2 = 0xc7; // 20 TAD and RC Tosc
ADCON3 = 0xc0; // No trigers in use
ADCHS = 0x00; // All channels are selected, only AN0 is in Group A
ANSEL0 = 0x11; // AN0 is selected as analog input
}
void main()
{
int variable = 0;
oscillator_setup();
Setup_ADC();
while(1) {
ConvertADC();
BusyADC();
variable = ReadADC();
variable = variable;
if (variable == 0x03FF)
PORTB = 0x03;
else if (variable != 0x03FF)
PORTB = 0x0c;
}
}
I am having a problem with getting a simple ADC working. I have tried to read the data sheet of the PIC with as much understanding as possible but I am still stuck. What I am trying to do is simply read a change in voltage and get the PIC to convert the value to digital and store it in 'variable'. I am simply using either 0V or 5V however no matter what I select my TAD or TOSC the only value that is stored in variable is 0x03FF and this is why I included the LED code so that if there was a change I could see it straight away. I am not sure if my code is correct perhaps I am making a mistake somewhere.
Any help/feedback will be greatly appreciated.
Thanks
Athosza
my code: (C 18 compiler)
#include <p18f2431.h>
#include <stdlib.h>
#include <adc.h>
#define ADC_CH0 0x00 //Select Channel 0
void oscillator_setup()
{
OSCCON = 0x76; // 8MHz internal clock frequency
TRISB = 0;
PORTB = 0;
}
void Setup_ADC()
{
ADCON0 = 0x03;// Group A is taken
ADCON1 = 0x18; // AVdd and AVss selected as voltage reference
ADCON2 = 0xc7; // 20 TAD and RC Tosc
ADCON3 = 0xc0; // No trigers in use
ADCHS = 0x00; // All channels are selected, only AN0 is in Group A
ANSEL0 = 0x11; // AN0 is selected as analog input
}
void main()
{
int variable = 0;
oscillator_setup();
Setup_ADC();
while(1) {
ConvertADC();
BusyADC();
variable = ReadADC();
variable = variable;
if (variable == 0x03FF)
PORTB = 0x03;
else if (variable != 0x03FF)
PORTB = 0x0c;
}
}