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.

problem anag. input pic16f690

Status
Not open for further replies.

teragiga

New Member
Hi
I am using a pic16f690, mplab and hitech compiler for a program in C.
Practically I have an input voltage value (coming from a light to voltage converter or even from other sources of tension), connected to RA0/AN0, and PORTC connected to a 7segments display and another 7segments display connected to PORTB and the last to output of PORTA.
What I need to do is to visualize the input voltage on the 2 display.
I am perfectly capable to use the 2 display to visualize the values I want on them but not the input voltage value.
(if I send void set7segB(5); then I see a 5).

The register of the ADC input in the 16f690 is based on 10 bits( either 2 on ADRESH and 8 on ADRESL, or the other way around).

Idependently from the input (even if I connect 5V or 2V direct to AN0 from a supplier) I see on the display alway two "8". Where is the mistake ?
I am using an adpted version of the Gooligum Electronics tutorial.
Please Help me :) I have no idea how to debug this. Cheers

Code:

Code:
 #include 
 __CONFIG(MCLREN & UNPROTECT & WDTDIS  & INTIO);
 
void set7segC(char digit);      // display digit on 7-segment display
 void set7segB(char digit);           // display digit on 7-segment display
 
 void main()
 {
     unsigned int    adc_dec;        // scaled ADC output (0-99)
     
     // configure ports
     TRISA = 0b11001011;
     ANSEL = 0b00000001;
     ANSELH = 0b00000001; 
     TRISB = 0b00000000;          // configure PORTB as outputs
     TRISC = 0b00000000;          // configure PORTC as outputs
     CM1CON0 = 0;                    // disable comparator 1 (RB0, RB1, RB2 usable)
     CM2CON0 = 0;                    // disable comparator 2 (RC0, RC1, RC4 usable)
     VRCON = 0;                      // disable CVref (RC2 usable)
     // configure ADC     
     ADCON0 = 0b10000011;       
              //1------             Right justified
              //-0------            VDD
              //--0000--            Set AN0 as the input
              //-------11             turn ADC on (ADON = 1)
     ADCON1 = 0b00110000;
 
     // Main loop
     for (;;)
     {
         // sample input
         GODONE = 1;             // start conversion
         while (GODONE);          // wait until done
         
         // scale result to 0-99
         adc_dec = (ADRESL + (ADRESH << 8)) * 99/1024;
         set7segC(adc_dec%10);    // output ones digit of result on segment bus 
         set7segB(adc_dec/10);    // output tens digit of result on segment bus 
 
     }      
}
 
 
 /***** FUNCTIONS *****/
 void set7segC(char digit)
 {
     // Lookup pattern table for 7-segment display on ports B and C
     const char pat7seg[10] = 
        {
         0b0111111,  // 0
         0b0011000,  // 1
         0b1110110,  // 2
         0b1111100,  // 3
         0b1011001,  // 4
         0b1101101,  // 5
         0b1101111,  // 6
         0b0111000,  // 7
         0b1111111,  // 8
         0b1111101   // 9
        };
     
     PORTC = pat7seg[digit] ;
 } 

 void set7segB(char digit)
 {
     // Lookup pattern table for 7-segment display on ports B and C
     const char pat7seg[10] = 
        {
         0b0111111,  // 0
         0b0011000,  // 1
         0b1110110,  // 2
         0b1111100,  // 3
         0b1011001,  // 4
         0b1101101,  // 5
         0b1101111,  // 6
         0b0111000,  // 7
         0b1111111,  // 8
         0b1111101   // 9
        };
     
     PORTB = pat7seg[digit] ;
     PORTA = pat7seg[digit]  & 1100000;
 }
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top