PIC16f676 5k Potentiometer Analog Read Problem

Discussion in 'Embedded Systems and Microcontrollers' started by Ali_Ahmer, Jan 7, 2016.

  1. Ali_Ahmer

    Thread Starter New Member

    Jan 7, 2016
    2
    1
    Hello Respected All Members,

    I am new in this forum and in PIC Programming field. I need a little help in my small assignment.
    I am using PIC16F676 MCU and MikroC compiler with internal Oscillator and internal MCLR voltage settings. In my circuit two 5k Potentiometer are connected with RA4 and RA3 for analog read input. One led connected on (Pin 13) RA0 of MCU for output. I want to adjust the time from 1 second to 10 seconds delay with respect to potentiometer for LED off/on. Means I wanna use RA4 potentiometer to set LED OFF delay and RA3 potentiometer to set LED ON delay.
    This is my code written in Mikro C
    Code (Text):
    1. sbit LED at RA0_bit;
    2.  
    3. unsigned int VR1_Value = 0;
    4. unsigned int VR2_Value = 0;
    5.  
    6. void main(void)
    7. {
    8.  
    9. CMCON = 0x07;   //Turn off Comparators
    10. ANSEL = 0x18;  //AN3 and AN4 as Analog Input
    11. ADCON1 = 0x10;  /* set FOSC/8 as ADC clock source */
    12. ADCON0 = 0x00;  /* select channel 0 and turn off ADC */
    13.  
    14. TRISA3_bit = 1;   //Analog VR2 input
    15. TRISA4_bit = 1;   // VR1 input
    16.  
    17. TRISA0_bit = 0; // Led pin as an output
    18.  
    19. LED = 0;
    20.  
    21. VR1_Value = ADC_Read(4);  // LED OFF Time
    22. VR2_Value = ADC_Read(3);    // LED ON Time
    23.  
    24. LED = 0;
    25. Delay_ms(VR1_Value);
    26. LED = 1;
    27. Delay_ms(VR2_Value);
    28.  
    29.  
    30. }
    This is my Proteus Simulation Snap
    [​IMG] http://prntscr.com/9n2lmm

    And this is my MikroC configuration Bit Settings:
    [​IMG] http://prntscr.com/9n2lo8
     
  2. sailorjoe

    Member

    Jun 4, 2013
    361
    63
    Can you print out your variables VR1_Value and VR2_Value, please?
    Also, your Delay function is for milliseconds, but you said that you want it to be seconds. Could that be the problem?
     
    Last edited: Jan 7, 2016
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    You need to create a loop where you read the pots and make the delays.

    Also you need to convert the A2D reading range. You read zero to 1023 and need to output 1,000 to 10,000 milliseconds to the delay routine. See if you can make that equation. Also check your variables are large enough to hold the results.
     
  4. Ali_Ahmer

    Thread Starter New Member

    Jan 7, 2016
    2
    1
    Thanks sir, I have solved it.
     
    ErnieM likes this.
Loading...