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.

adc not working properly

Status
Not open for further replies.

maria258

Member level 2
Joined
Feb 10, 2011
Messages
42
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,577
whilst the compiling is successful, when put to the proteus simulation, it is not working as required. can someone help me to find the fault? could it be something regarding the tris bits? but i dont know whats the problem with that.
thanks
code is shown below
Code:
#include <pic.h>
 
 unsigned int itime;
 void delay (unsigned char itime);
 
 void main (void)
 {
 TRISA=0b10000000;    //RA0 input for analog
 TRISC=0b11000000; //RC0, RC1 are set as outputs
 TRISD=0x11111111;    //D is set as output
 
 ADCON1=0b10000000;
 //bit7:ADFM:right justified
 //bit6-4:unused, read as 0
 //bit3-0:all ports configured as analog channels
 
 ADCON0=0b0000001;
 //bit7-6:Fosc/2
 //bit5-3:using analog channel RA0/AN0
 //bit2:ADON not on
 //bit1:unimplemented
 //bit0: ADON converter module is operating
 
 while(1)
 {
 delay(100);
 //GODONE=1;    //start converting ***ADGO=1
 //while(GO);
 PORTC=ADRESL;
 PORTD=ADRESH;
 delay(100);
 }
 }
 
 void delay (unsigned char itime)
 {
     unsigned int i,j;
 
     for(i=0;i<itime;i++)
         for(j=0;j<200;j++);
 }
 

Can I ask whay IDE are you using? What PIC do you plan to use it? I currently have a project and I am able to do ADC in just few lines using PIC16F877A in MikroC.
 

i am using mplab v8.63 whilst using the pic16f877
 

Okay, as a reference you can test this code on Proteus, use MikroC to compile:

Code:
/*Header******************************************************/

// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

unsigned char ch;                    //
unsigned int adc_rd;                 // Declare variables
char *text;                          //
long tlong;                          //

void main() {
    INTCON = 0;                      // All interrupts disabled
    TRISA = 0x01;                    //RA0 as input
    
    Lcd_Init();                      // LCD display initialization
    Lcd_Cmd(_LCD_CURSOR_OFF);        // LCD command (cursor off)
    Lcd_Cmd(_LCD_CLEAR);             // LCD command (clear LCD)
    
    text = "mikroElektronika";       // Define the first message
    Lcd_Out(1,1,text);               // Write the first message in the first line
    text = "LCD example";            // Define the second message
    Lcd_Out(2,1,text);               // Define the first message
    
    ADCON1 = 0x82;                   // A/D voltage reference is VCC
     Delay_ms(2000);
    
    text = "voltage:";               // Define the third message
    
    while (1) {
        adc_rd = ADC_Read(0);        // A/D conversion. Pin RA2 is an input.
        Lcd_Out(2,1,text);           // Write result in the second line
        tlong = (long)adc_rd * 5000; // Convert the result in millivolts
        tlong = tlong / 1023;        // 0..1023 -> 0-5000mV
        ch = tlong / 1000;           // Extract volts (thousands of millivolts)
                                     // from result
        Lcd_Chr(2,9,48+ch);          // Write result in ASCII format
        Lcd_Chr_CP('.');
        ch = (tlong / 100) % 10;     // Extract hundreds of millivolts
        Lcd_Chr_CP(48+ch);           // Write result in ASCII format
        ch = (tlong / 10) % 10;      // Extract tens of millivolts
        Lcd_Chr_CP(48+ch);           // Write result in ASCII format
        ch = tlong % 10;             // Extract digits for millivolts
        Lcd_Chr_CP(48+ch);           // Write result in ASCII format
        Lcd_Chr_CP('V');
        Delay_ms(1);
    }
}

Put a DC on RA0, and you will see it on an LCD. For wirings, you can check the tutorial here:

**broken link removed**
 

ok thanks anyway but this is a whole lot different from mplab.
thanks for your time
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top