PIC16F676 5k Potentiometer Analog Read

Home Forums Kanda Forums Microcontroller Discussion Forum PIC16F676 5k Potentiometer Analog Read

Tagged: ,

This topic contains 2 replies, has 3 voices, and was last updated by Avatar BurchSung 1 year, 4 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #1045
    Avatar
    katrina
    Participant

    Hello Respected All Members,

    I am New in this Forum and PIC Programming. I need a little help.
    I am using PIC16F676 MCU (bought from http://www.kynix.com/Detail/1217420/PIC16F676-I.html)with Internal Oscillator clock and internal MCLR Voltage in MikroC Compiler. I want to read two analog inputs of 5K potentiometer connected at RA4 and RA3 to calculate the LED on/off Delay. I have connected a LED on RA0 (Pin 13) of PIC16f676 MCU. I want to limit its time from 1 second to 10 seconds with respect to 5k Potentiometer.

    I have pasted my code below, But it gives me some error. I don’t know where i was doing wrong:
    Code:
    sbit LED at RA0_bit;

    unsigned int VR1_Value = 0;
    unsigned int VR2_Value = 0;

    void main(void)
    {

    CMCON = 0x07; //Turn off Comparators
    ANSEL = 0x18; //AN3 and AN4 as Analog Input
    ADCON1 = 0x10; /* set FOSC/8 as ADC clock source */
    ADCON0 = 0x00; /* select channel 0 and turn off ADC */

    TRISA3_bit = 1; //Analog VR2 input
    TRISA4_bit = 1; // VR1 input

    TRISA0_bit = 0; // Led pin as an output

    LED = 0;

    VR1_Value = ADC_Read(4); // LED OFF Time
    VR2_Value = ADC_Read(3); // LED ON Time

    LED = 0;
    Delay_ms(VR1_Value);
    LED = 1;
    Delay_ms(VR2_Value);

    }

    #1048
    Avatar
    Kanda Admin
    Keymaster
    Post count: 324

    Hello Katrina
    I don’t know much about MikroC as I use Microchip XC8 compiler but the obvious points are these

    1. Your code will only run once as you have not put the ADC read/LED display inside a loop, like while(1){
    }

    2. You initialise ADC channel 0 (ADCON0 = 0x00) and then read ADC 3 and ADC 4. Does this external ADC_Read function also initialise the ADC channel again?

    You get some error is a bit vague – compile error? code runs but doesn’t do what you expect?

    If you want to learn PIC C programming, look at this product:
    PIC Training Kit

    KandaAdmin

    #1336
    Avatar
    BurchSung
    Participant
    Post count: 1

    Hi…as per my knowledge a temperature sensor for instance may give out 2.1V when the temperature is 25C and go upto 4.7 when the temperature is 60C. In order to know the temperature of the real world, the MCU has to just read the output voltage of this temperature sensor and relate it to the real world temperature. Hence ADC is an important work tool for MCU projects and lets learn how we can use it on our PIC16F877A.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

Microcontrollers, training, electronics and coding