Code not working. Analog readings on 2 pins


Closed Thread
Results 1 to 10 of 10
  1. #1

    Default Code not working. Analog readings on 2 pins

    Hello, my following code is not working for some reason. Can someone help please.
    On my computer screen I am getting Time=0, Ambient=0. Time Pin is connected to +5V with a 4.7k, I have not attached a POT yet, even though when I did, the voltage on the pin was around 2.5V, but the reading was still 0.

    LDR voltage is going to Ldr pin. LDR is connected in voltage divider fashion with voltage going to PIC using a 4.7k. Again I tried to change the voltage as well on this pin but the reading stays as 0.

    Thanks in advance.

    Code:
    ;-----------------PIC16F676---------------------------Include "modedefs.bas"
    DEFINE OSC 4       ' OSCCON defaults to 4MHz on reset
    ;DEFINE    OSCCAL_1K 1
    DEFINE ADC_BITS 10
    DEFINE ADC_CLOCK 3
    DEFINE ADC_SAMPLEUS 50
    ;-------------CONFIGURATION FUSES SET HERE-------------------------
    
    
    @ __Config _XT_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _BODEN & _CP & _CPD
    
    
    '----------------------------------------
    DEFINE DEBUG_REG    PORTC
    DEFINE DEBUG_BIT    5
    DEFINE DEBUG_BAUD 2400
    DEFINE DEBUG_MODE 0
    
    
    
    
    '-------Analog Input--------------
    Poten        Var    PortA.1
    Ldr            Var    PortC.3
    '--------------------------------------
    
    
    Time        Var    Word
    Time1        Var    Word
    Counter    Var    Byte
    Ambient    Var    Word
    Ambient1    Var    Word
    B0            Var Word
    
    
    ;------REGISTERS SET --------
    PORTA=0
    PORTC=0
    TRISA = 0010
    TRISC = 1110
    OPTION_REG = 128
    PORTA=0
    PORTC=0
    CMCON=7
    ANSEL=000010
    ADCON1=110000
    ADCON0=000000
    '---------------------------------------
    
    Pause 3000
    
    
        GoSub    LoadTime
        Time1=Time
        GoSub LoadLight
        Counter=0
    Main:
    ; my rest of the code
    Goto Main
    
    
    LoadTime:
            ADCIN Poten,Time : Pause 10
    ;        Time=(Time/4)
            DEBUG "TIME=",DEC Time,10
    Return
    
    
    LoadLight:
            ADCIN Ldr,Ambient : Pause 10
            DEBUG "AMBIENT=",DEC AMBIENT,10
            While Setswitch=1 : Pause 330 : wend
    Return
    ___________________
    WHY things get boring when they work just fine?

  2. #2
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default Re: Code not working. Analog readings on 2 pins

    Hi ,

    Just open your manual @ the ADC page ...

    WHICH kind of parameter is used to select the input channel ???

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: Code not working. Analog readings on 2 pins

    Hi, Alain, thanks for replying. I tried the following as well from what I understood:
    Code:
    LoadTime:
    		ADCON0=%10000100 : Pause 10
    		ADCIN Poten,Time : Pause 10
    ;		Time=(Time/4)
    		DEBUG "TIME=",DEC Time,10
    Return
    
    
    LoadLight:
    		ADCON0=%10011100 : Pause 10
    		ADCIN Ldr,Ambient : Pause 10
    		DEBUG "AMBIENT=",DEC AMBIENT,10
    		While Setswitch=1 : Pause 330 : wend
    Return
    No Luck. Where am I going wrong. I did this before but only with 1 channel. That worked out well. Now never tried anything with two channels, so can't say much.
    ___________________
    WHY things get boring when they work just fine?

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: Code not working. Analog readings on 2 pins

    Also tried the following as well: (Still no luck)
    Code:
    LoadTime:		ANSEL=%00000010
    		ADCON0=%10000100 : Pause 10
    		ADCIN Poten,Time : Pause 10
    ;		Time=(Time/4)
    		DEBUG "TIME=",DEC Time,10
    Return
    
    
    LoadLight:
    		ANSEL=%10000000
    		ADCON0=%10011100 : Pause 10
    		ADCIN Ldr,Ambient : Pause 10
    		DEBUG "AMBIENT=",DEC AMBIENT,10
    		While Setswitch=1 : Pause 330 : wend
    Return
    ___________________
    WHY things get boring when they work just fine?

  5. #5
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default Re: Code not working. Analog readings on 2 pins

    Code:
    GOTO #2
    WHICH kind of parameter
    BTW ... Which version of PBP are you using ??? , < 3 ???

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: Code not working. Analog readings on 2 pins

    I am really sorry, I am not getting it yet what you are trying to say. I have read about 3 parameters, ANSEL, ADCON0 & 1. I have tried with different ways but
    I am using PBP version 2.5.
    ___________________
    WHY things get boring when they work just fine?

  7. #7
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default Re: Code not working. Analog readings on 2 pins

    IF you had upgraded to V 3.0 ...

    your code had worked fine !!! ( It's written in the holy manual !!! ... I just discovered it at this time ...)

    The error is in your channel selection parameter ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  8. #8


    Did you find this post helpful? Yes | No

    Default Re: Code not working. Analog readings on 2 pins

    Hi, Ok, I upgraded the whole PBP to 3 version. I even upgraded my MPLAB to Version 8.84 I am getting the following error:

    Executing: "C:\PBP3\pbpx.exe" -ampasmwin -oq -z -p16F676 "Light_676.BAS"
    PICBASIC PRO(TM) Compiler 3.0.5.4, (c) 1998, 2012 microEngineering Labs, Inc.
    All Rights Reserved.
    ERROR: Bad command parameter. -z
    BUILD FAILED: Thu May 31 10:18:03 2012
    ___________________
    WHY things get boring when they work just fine?

  9. #9


    Did you find this post helpful? Yes | No

    Default Re: Code not working. Analog readings on 2 pins

    Alain, here is the update. Updating to PBP3 didn't worked using MPLAB. But I copied the code to MicroCodeStudio. Compiled it and it compiled fine using PBP3 compiler. The result is the same:
    Code:
    ;-----------------PIC16F676---------------------------
    Include "modedefs.bas"
    DEFINE OSC 4       ' OSCCON defaults to 4MHz on reset
    DEFINE ADC_BITS 10
    DEFINE ADC_CLOCK 3
    DEFINE ADC_SAMPLEUS 50
    ;-------------CONFIGURATION FUSES SET HERE-------------------------
    
    
    @ __Config _XT_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _BODEN & _CP & _CPD
    
    
    '----------------------------------------
    DEFINE DEBUG_REG	PORTC
    DEFINE DEBUG_BIT	5
    DEFINE DEBUG_BAUD 2400
    DEFINE DEBUG_MODE 0
    
    
    '-------Analog Input--------------
    Poten		Var	PortA.1
    Ldr			Var	PortC.3
    '--------------------------------------
    
    
    Time		Var	Word
    Time1		Var	Word
    Counter	Var	Byte
    Ambient	Var	Word
    
    
    ;------REGISTERS SET --------
    PORTA=0
    PORTC=0
    TRISA = %000010
    TRISC = %001110
    OPTION_REG = 128
    PORTA=0
    PORTC=0
    CMCON=7
    ANSEL=%10000010
    ADCON1=%00110000
    ADCON0=%10000000
    '---------------------------------------
    
    
    
    
    	GoSub	LoadTime
    	Time1=Time
    	GoSub LoadLight
    	Counter=0
    
    
    Main:
    ; my code here
    Goto Main
    
    
    
    
    LoadTime:
    		ADCIN Poten,Time : Pause 10
    		DEBUG "TIME=",DEC Time,10
    Return
    
    
    LoadLight:
    		ADCIN Ldr,Ambient : Pause 10
    		DEBUG "AMBIENT=",DEC AMBIENT,10
    Return
    They both (Time & Ambient) are outputting as '0' on my screen. Seems the problem is something else.
    Both Time & Ambient pins are currently held high (5v)using 4.7k.
    ___________________
    WHY things get boring when they work just fine?

  10. #10


    Did you find this post helpful? Yes | No

    Default Re: Code not working. Analog readings on 2 pins

    Alain, thanks for your inputs. Got it working finally
    ___________________
    WHY things get boring when they work just fine?

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts