Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC18F1230 ADC with C18 not working

Status
Not open for further replies.

Flyback

Well-Known Member
Hello,

I have a program which reads the voltage on AN0 using ADC on PIC18F1230.

It then should output the 10 bit conversion result on to 10 LEDs on ports A and B.

However, the ADC always just gives me a reading of zero.

here is my code....

//Program to read Voltage on AN0 using ADC.
//Then output the 10 bit binary number on...
//RA2 RA3 RB0 RB1 RB4 RB5 RA6 RA7 RB2 RB3
//LSB................................MSB

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

#include <p18f1230.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;

void main(void) {

//Set up ports
PORTB = 0x00;
PORTA = 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.SCS0 = 0;
//----------------------------------------

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

TRISB = 0x00;

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

//----------------------
//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;



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

//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;

//zero out 6 MSB's
ADRESH_1 = ADRESH_1 & 0x03;

//Output the 10 bit ADC reading on ports A and B
if (ADRESL_1 & 0x01) {LATAbits.LATA2 = 1;} else {LATAbits.LATA2 = 0;}
if (ADRESL_1 & 0x02) {LATAbits.LATA3 = 1;} else {LATAbits.LATA3 = 0;}
if (ADRESL_1 & 0x04) {LATBbits.LATB0 = 1;} else {LATBbits.LATB0 = 0;}
if (ADRESL_1 & 0x08) {LATBbits.LATB1 = 1;} else {LATBbits.LATB1 = 0;}
if (ADRESL_1 & 0x10) {LATBbits.LATB4 = 1;} else {LATBbits.LATB4 = 0;}
if (ADRESL_1 & 0x20) {LATBbits.LATB5 = 1;} else {LATBbits.LATB5 = 0;}
if (ADRESL_1 & 0x40) {LATAbits.LATA6 = 1;} else {LATAbits.LATA6 = 0;}
if (ADRESL_1 & 0x80) {LATAbits.LATA7 = 1;} else {LATAbits.LATA7 = 0;}
if (ADRESH_1 & 0x01) {LATBbits.LATB2 = 1;} else {LATBbits.LATB2 = 0;}
if (ADRESH_1 & 0x02) {LATBbits.LATB3 = 1;} else {LATBbits.LATB3 = 0;}


}
// *** END OF ADC LOOP ****
return;
}
// *** END OF MAIN ***
// end of code

here is the circuit

i'd be very grateful for any help
 
Your code is hard to follow at this time of night and without formating.

When I want to read the ADC I use this function,
Code:
int ReadADC(unsigned char Channel){
    ADCON0=(Channel<<2) | 1;
    ADCON2=0b10110101;          //Right justify - Fosc/16
    ADCON0bits.GO=1;            //start conversion
    while(ADCON0bits.GO);       //Wait for it to complete
    return ADRES;
}
It requires that you set tris and adcon1 before you call it.

Mike.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top