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.

analog values on AN0

Status
Not open for further replies.

picozero

New Member
Hi
I am using a PIC16F690, programming wiht a MPLAB IDE of a startkit 2 in connection with a HITECH compiler for C.

What I'd like to do is to display on a 7segments a tension value coming from an Input. How can I read the exact value from the ADC ? I mean I receive 3.2V on AN0 and with a case I detect something 3<x<4 and I set the output of a 7segments as 3(0b1111001) . So far I was only able to read if it was High or Low:

Code:
 #include <htc.h>
 __CONFIG(MCLREN & UNPROTECT & WDTDIS  & INTIO);
 
 void main() 
 {
      ANSEL = 0b10010000;    // Select A/D inputs 
      ANSELH =0b00000000; 
      ADCON0 =0b00000000;    // left justified                    
 
      CM1CON0 = 0; //Turn off comparator
      CM2CON0 = 0; //Turn off comparator
      TRISA = 0b011101; //Set pin in output mode
      TRISB = 0; //Set pin in output mode
      TRISC = 0; //Set pin in output mode
 
     // ANALOG CHANNEL 1 (AN0)
     while(1==1)    
     {
              CHS0 = 1; // set channel AN0    
              GODONE = 1;    // start the A-to-D process     
              while(GODONE) continue; 
               int advalue = ADRESH;// A-to-D result to "advalue"
 
              //This "if" can be change in a case where depending the value I activate differently the 7segments
              if(advalue)
             {
                              PORTA =    0b11111111;
                              PORTB =    0b11111111;
                              PORTC =    0b11111111;
              }
              else
             {
                              PORTA =    0b00000000;
                              PORTB =    0b00000000;
                              PORTC =    0b00000000;
              } 
        }
 };

The strange thing is also that when I send a low value to AN0, the outputs don't turn off completely but it is like they dim a bit.
Thanks in advance
 
I think it is unusual to open an analog input signal as an output.
I think you've also misinterpreted CHS0. It is not a bit to select the channel, it is
one of a set of bits which selects a channel number. Read the data sheet more carefully and look at the diagrams.

To do an A/D conversion you have to:

Set the port as A/D input
Select an ADC conversion clock
TURN ON the ADC unit
Select the channel to convert
Set the go/done bit and wait for it to clear
Read the result

(Now that I typed that, I see it is almost verbatim of 9.2.6 of the data sheet)

Code:
ANSEL= 0b00000001;  // channel AN0 (RA0) as analog in (0bxxxxxxx1)
TRISA = 0b00000001; // set port as input (0bxxxxxxx1)
ADCON1 = 0b01110000; // use internal oscillator
ADCON0 = 0b00000001; // left justified, Vdd ref, Channel 0, ADON
ADC_WARMUP_DELAY(); // create a function or macro to wait a bit. (datasheet 9.3)
GODONE=1; // start conversion
while (GODONE) continue;
advalue = ADRESH;

Should about do it. (But I'm still new at this)

Rufus
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top