pot reading either min or max

Discussion in 'Embedded Systems and Microcontrollers' started by emzdanow, May 7, 2011.

  1. emzdanow

    Thread Starter New Member

    Jun 14, 2010
    23
    0
    Hello,

    I am using a simple 10kΩ to varry an analog voltage that I can read on an A/D input on a PIC. The reading I get is always 255 (max) or 0-10 (near min). I believe this is a ciruit problem. I have one pin on pot connected to ground and the other to +5V. The center wiper is connected directly to my input pin on my PIC (PIC16F690). When I just connect ground and power and measure center pin with multimeter the voltage varries fine, but when I hook it up to the PIC, the voltage does nothing while turning the knob, then drops to zero or 5V (depending on starting point). Any suggestions? Do I need other componenets in circuit?
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Post your code. I guess it is the code. Like using wrong Vref
     
  3. someonesdad

    Senior Member

    Jul 7, 2009
    1,585
    141
    It's pretty hard to fool the hardware, so if it works when you measure it, it's your code. If you download the Low Pin Count Demo Board applications from Microchip, their examples do exactly what you want (with the same processor you're using).
     
  4. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    What does the voltage measure when you measure it with a DVM?
     
  5. emzdanow

    Thread Starter New Member

    Jun 14, 2010
    23
    0
    I have used the code before successfully from the development board, but now I am using seperate breadboard. Also, once I put output of pot to chip, I measure voltage with multimeter and see the sudden drop in voltage. It only happens when connected to pic. I will post my code later, I am on mobile.
     
  6. emzdanow

    Thread Starter New Member

    Jun 14, 2010
    23
    0
    here is my code, the lcd works fine, but value on screen is always around 0 or around 255, does not change linearly as I turn the knob.

    Code ( (Unknown Language)):
    1.  
    2. #include <pic.h>
    3. #include "lcd.h"
    4. void pause (unsigned short msvalue);
    5. void msecbase( void );
    6. __CONFIG (INTIO & WDTDIS & MCLRDIS & UNPROTECT );
    7. #define number 0x30;
    8. int level = 0;
    9. char ans = 0;
    10. char d0, d1, d2, d3;
    11. char str;
    12.  
    13. main()
    14. {
    15. CM1CON0 = 0;
    16. CM2CON0 = 0;
    17. PORTC = 0x00;
    18. PORTB = 0x00;
    19. PORTA = 0x00;
    20. TRISC = 0x00;
    21. TRISA = 0b00000001;
    22. TRISB = 0xFF;
    23. ANSEL = 1;
    24. ADCON0 = 0b00000001;
    25. ADCON1 = 0b00111111;
    26. lcd_init();
    27.  while(1)
    28.  {
    29.  GODONE = 1;
    30.   while(GODONE == 1)
    31.   {
    32.   }
    33.  level = ADRESH;
    34.  d3 = level % 10;                  /* d2 <-- 7 */
    35.  d2 = (level / 10) % 10;           /* d1 <-- 3 */
    36.  d1 = (level / 100) % 10;    /* d0 <-- 1 */
    37.  d0 = (level / 1000) %10;
    38. /* continue like this for even more digits */
    39.  /* convert digit (numbers) to ASCII (text characters) */
    40.  d0 += '0';   /* same as d0 = d0 + ‘0’ same as d0 = d0 | 0x30 */
    41.  d1 += '0';
    42.  d2 += '0';
    43.  d3 += '0';
    44.  
    45.  lcd_goto(0);
    46.  lcd_puts("Ans");
    47.  lcd_goto(0x05);
    48.  lcd_putch(d0);
    49.  lcd_goto(0x06);
    50.  lcd_puts(".");
    51.  lcd_goto(0x07);
    52.  lcd_putch(d1);
    53.  lcd_goto(0x08);
    54.  lcd_putch(d2);
    55.  lcd_goto(0x09);
    56.  lcd_putch(d3);
    57.  lcd_goto(0x0A);
    58.  lcd_puts("Alc");
    59.  pause(500);
    60.  
    61. }
    62. }
    63.  
    64.  
    65.  
     
  7. trixheim

    Member

    Apr 1, 2009
    10
    0
    Maybe the pin is configured as an output?

    Tor
     
  8. emzdanow

    Thread Starter New Member

    Jun 14, 2010
    23
    0
    The TRISA =0b00000001 command sets RA0 to an input and this is the pin I am using for a/d. It is very bizzare, the voltage varries like it should when not connected to the PIC, I even hooked it to an LED and the brightness varried linearly. Its just when it is connected to the PIC it reads 255-250 or zero. Again, it is a 10k pot with one pin connected to ground, another to supply voltage (approx 5v from 4 AA batteries) and the wiper connected to the a/d pin on PIC.
     
  9. emzdanow

    Thread Starter New Member

    Jun 14, 2010
    23
    0
    Problem solved, trixheim was correct. The main program was correct, however, the lcd_int() function in the lcd.c file sets the A port to all outputs. Sorry for any wasted time on anyones part seeing as this was a stupid mistake.
     
  10. trixheim

    Member

    Apr 1, 2009
    10
    0
    Most errors are stupid afterwards, he he.
    Good luck.

    Tor
     
Loading...