Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC18F1230 ADC problem - the ADC is not working.

Status
Not open for further replies.

eem2am

Banned
Joined
Jun 22, 2008
Messages
1,179
Helped
37
Reputation
74
Reaction score
24
Trophy points
1,318
Activity points
0
adcon2bits.acqt2 adc

hello,


I would be very grateful for help with my ADC problem.

I am using PIC18F1230 with C18 C compiler.

However, the ADC is not working.

My code is simple .....it just reads the AN0 voltage in a loop, then lights an LED on RB1 if Volts > 1 Volt but turns it off if voltage < 1 Volt.

Here is my code...

//Program to light LED if ADC
//input is > 1V
//Turns LED off if ADC input
//is < 1V

//ADREF is 5V

//PIC18F1230
//Compiler = Microchip C18 v3.22
//MPLAB v8.10


#include <p18f1230.h>
#include <adc.h>
#include <portb.h>

#pragma config OSC = INTIO2
#pragma config IESO = OFF
#pragma config PWRT = OFF
#pragma config BOR = OFF
#pragma config WDT = OFF
#pragma config MCLRE = OFF
#pragma config DEBUG = OFF
#pragma config CP0 = OFF
#pragma config CP1 = OFF
#pragma config CPB = OFF
#pragma config CPD = OFF
#pragma config WRT0 = OFF
#pragma config WRT1 = OFF
#pragma config WRTB = OFF
#pragma config WRTC = OFF
#pragma config WRTD = OFF


unsigned char m,n,c,ADRESL_1,ADRESH_1;
volatile unsigned conversion_val;

void main(void) {

//Set up ports
PORTB = 0x00;

//----------------------------------------
//Set up OSCTUNE
//Disable PLL
OSCTUNEbits.PLLEN = 0;

//Set up OSCCON
//4MHZ
OSCCONbits.IRCF2 = 1;
OSCCONbits.IRCF1 = 1;
OSCCONbits.IRCF0 = 0;
//Internal oscillator block
OSCCONbits.SCS1 = 1;
OSCCONbits.SCS1 = 0;
//----------------------------------------

//Set up port direction
//AN0 is analog input (0-5V)
TRISA = 0x01;
TRISB = 0x00;

//----------------------
//Disable PWM
//$$ PWM time base timer disable $$
PTCON1bits.PTEN = 0;
//SET UP pwm control register 0
//PWM disabled...pins GPIO
PWMCON0bits.PWMEN2 = 0;
PWMCON0bits.PWMEN1 = 0;
PWMCON0bits.PWMEN0 = 0;
//----------------------

//----------------------
//Disable the comparator
CMCONbits.CMEN2 = 0;
CMCONbits.CMEN1 = 0;
CMCONbits.CMEN0 = 0;
CVRCONbits.CVREN = 0;
//----------------------

// ------------------------------------------
// SET-UP ADC:-
//Set-Up ADCON0bits

//disable the special event trigger from PWM
ADCON0bits.SEVTEN = 0;
//ADC MODULE IS DISABLED
ADCON0bits.ADON = 0;

//Set-up ADCON1bits
//+ve ref for AD is AVDD
ADCON1bits.VCFG0 = 0;
//RA6
ADCON1bits.PCFG3 = 0;
//RA4
ADCON1bits.PCFG2 = 0;
//RA1
ADCON1bits.PCFG1 = 0;
//AN0
ADCON1bits.PCFG0 = 1;


//Set-up ADCON2bits
//Right justify
ADCON2bits.ADFM = 1;
//20 TAD = AD acquisition time
ADCON2bits.ACQT2 = 1;
ADCON2bits.ACQT1 = 1;
ADCON2bits.ACQT0 = 1;
//AD conversion clk select bits = Fosc/64
ADCON2bits.ADCS2 = 1;
ADCON2bits.ADCS1 = 1;
ADCON2bits.ADCS0 = 0;
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//Channel 0 = AN0
ADCON0bits.CHS1 = 0;
ADCON0bits.CHS0 = 0;
//ADC MODULE IS ENABLED
ADCON0bits.ADON = 1;

//DISABLE INTERRUPTS
INTCONbits.GIE = 0;
PIE1bits.ADIE = 0;

// *** THIS IS A LOOP WHERE AD CONVERSIONS ARE DONE. ***
for(;;) {
//Acquisition time wait
for(m=0;m<55;m++) {;}

//clear conversion value
conversion_val = 0x0000;

//Start AD Conversion
ADCON0bits.GO = 1;


while (ADCON0bits.GO) {;}


//Clear AD flag
PIR1bits.ADIF = 0;

//Load up temporary Conversion value holders
ADRESL_1 = ADRESL;
ADRESH_1 = ADRESH;

//Load up conversion_val from ADRESH_1 & ADRESL_1
if (ADRESL_1 & 0x01) {(conversion_val = (conversion_val | 0x0001));}
if (ADRESL_1 & 0x02) {(conversion_val = (conversion_val | 0x0002));}
if (ADRESL_1 & 0x04) {(conversion_val = (conversion_val | 0x0004));}
if (ADRESL_1 & 0x08) {(conversion_val = (conversion_val | 0x0008));}

if (ADRESL_1 & 0x10) {(conversion_val = (conversion_val | 0x0010));}
if (ADRESL_1 & 0x20) {(conversion_val = (conversion_val | 0x0020));}
if (ADRESL_1 & 0x40) {(conversion_val = (conversion_val | 0x0040));}
if (ADRESL_1 & 0x80) {(conversion_val = (conversion_val | 0x0080));}

if (ADRESH_1 & 0x01) {(conversion_val = (conversion_val | 0x0100));}
if (ADRESH_1 & 0x02) {(conversion_val = (conversion_val | 0x0200));}

//...conversion_val is now loaded.
//...zero out the 6 MSB's...
conversion_val = conversion_val & 0x03FF;

//Light LED on RB1 if AD input was > 1 V
if (conversion_val > 0x00CC) {LATBbits.LATB1 = 1;}

//Extinguish LED on RB1 if AD input was < 1V
if (conversion_val < 0x00CC) {LATBbits.LATB1 = 0;}


}
// *** END OF ADC LOOP ****
return;
}
// *** END OF MAIN ***



.......................................................................................................
here is my cct diagram..

334rxiv.jpg


many thanks for your reading.

Added after 3 minutes:

hello, sorry that face somehow appeared in my code, i don't know why.

that bit is just the two semi-colons to get the loop going in which the ADC happens.

code was
for (;;) {

(i hope the face did not appear again)

Added after 43 seconds:

sorry again, i guess this PC is set up to make two semi-colons appear as a face.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top