12F1840 ADCIN troubles, help needed :(


Closed Thread
Results 1 to 22 of 22

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    1,124

    Default 12F1840 ADCIN troubles, help needed :(

    Hello.

    I'm trying to use ADCIN on 12F1840, here's the code:

    Code:
    ;----[12F1840 Hardware Configuration]-------------------------------------------
    #IF __PROCESSOR__ = "12F1840"
      #DEFINE MCU_FOUND 1
    #CONFIG
    cfg1 = _FOSC_INTOSC           ; INTOSC oscillator: I/O function on CLKIN pin
    cfg1&= _WDTE_OFF              ; WDT disabled
    cfg1&= _PWRTE_OFF             ; PWRT disabled
    cfg1&= _MCLRE_OFF             ; MCLR/VPP pin function is digital input
    cfg1&= _CP_OFF                ; Program memory code protection is disabled
    cfg1&= _CPD_OFF               ; Data memory code protection is disabled
    cfg1&= _BOREN_ON              ; Brown-out Reset enabled
    cfg1&= _CLKOUTEN_OFF          ; CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin
    cfg1&= _IESO_ON               ; Internal/External Switchover mode is enabled
    cfg1&= _FCMEN_ON              ; Fail-Safe Clock Monitor is enabled
      __CONFIG _CONFIG1, cfg1
    
    cfg2 = _WRT_OFF               ; Write protection off
    cfg2&= _PLLEN_OFF             ; 4x PLL disabled
    cfg2&= _STVREN_ON             ; Stack Overflow or Underflow will cause a Reset
    cfg2&= _BORV_19               ; Brown-out Reset Voltage (Vbor), low trip point selected.
    cfg2&= _LVP_OFF               ; High-voltage on MCLR/VPP must be used for programming
      __CONFIG _CONFIG2, cfg2
    
    #ENDCONFIG
    
    #ENDIF
    
    ;----[Verify Configs have been specified for Selected Processor]----------------
    ;       Note: Only include this routine once, after all #CONFIG blocks
    #IFNDEF MCU_FOUND
      #ERROR "No CONFIGs found for [" + __PROCESSOR__ +"]"
    #ENDIF
    
    
    OSCCON = %11110000  'SET INTOSC TO 32MHZ
    
    TRISA=%00110000 'set LATAA.4 as input
    ANSELA=%00010000 'set LATAA.4 as analog
    ADCON0=%00001101  'ENABLE AND CONFIGURE ADC
    ADCON1=%11100011   'JUSTIFY
    CM1CON0=%00000000   'DISABLE COMPARATORS
    
    DEFINE OSC 32
    DEFINE ADC_BITS 10
    DEFINE ADC_SAMPLEUS 50
    DEFINE ADC_CLOCK 3
    VOLTAGE VAR WORD
    SHEMAVALI VAR LATA.4
    LED VAR LATA.2
    
    
    'PACUKA:
    'HIGH LED
    'PAUSE 200
    'LOW LED
    'PAUSE 200
    'GOTO PACUKA
    
    CIKLURI:
    ADCIN 3,VOLTAGE
    IF VOLTAGE=>1023 THEN 
    HIGH LED
    else
    low led
    endif
    pause 5
    GOTO CIKLURI
    The problem is that VOLTAGE variable always reads 1024, if input is not connected to ground.

    Any ideas?

  2. #2
    Join Date
    May 2009
    Posts
    40


    Did you find this post helpful? Yes | No

    Default Re: 12F1840 ADCIN troubles, help needed :(

    Firstly ADCON1=%11100011 'JUSTIFY statement is conflicting with DEFINE ADC_CLOCK 3.
    Secondly IF VOLTAGE=>1023 THEN statement can never be greater than 1023. DEFINE ADC_BITS 10 means maximum value of voltage =1023.

  3. #3
    Join Date
    Feb 2013
    Posts
    1,124


    Did you find this post helpful? Yes | No

    Default Re: 12F1840 ADCIN troubles, help needed :(

    well that IF is just experiment
    In real life, ADCIN returns 0 if input is grounded, if any other voltage is applied, it returns 1023.

  4. #4
    Join Date
    Feb 2013
    Posts
    1,124


    Did you find this post helpful? Yes | No

    Default Re: 12F1840 ADCIN troubles, help needed :(

    Found the problem!

    I was using Fixed internal voltage reference, but was not enabling it at all.

    added

    FVRCON=%11011111

    and now it works OK!

  5. #5
    Join Date
    Feb 2013
    Posts
    1,124


    Did you find this post helpful? Yes | No

    Default Re: 12F1840 ADCIN troubles, help needed :(

    Well, troubles continue.

    I've built a following circuitry. 10K resistor goes to LATA.2 pin and goes then to gnd.
    100k variable pot is connected one end to VCC, another to LATA.2
    LED is connected to GND via 1k resistor from LATA.1

    Wiring is 100% checked and corrected for shorts, misswire, etc.

    The code is as follows:

    Code:
    OSCCON = %11110000  'SET INTOSC TO 32MHZ
    TRISA=%00110000 'set PORt as input
    'ANSELA=%00010101 'set PORTA.4 as analog
    ADCON0=%00001111  'ENABLE AND CONFIGURE ADC
    ADCON1=%11000011   'JUSTIFY
    FVRCON=%11011111 'VREF ENABLE & set at 4.096v
    WPUA=%00000000 'pull up disable
    'CM1CON0=%00000000   'DISABLE COMPARATORS
    
    DEFINE OSC 32
    DEFINE ADC_BITS 10
    DEFINE ADC_SAMPLEUS 50
    DEFINE ADC_CLOCK 3
    VOLT VAR WORD
    LED VAR LATA.1
    
    greater:
    adcin 2,volt
    high led
    pause 1
    low led
    pause volt/10
    goto greater
    This code should do a simple thing - vary LED brightness according to pot setting. And sure it does, but the problem is that it works in very narrow range, because port sinks huge amount of current - up to 25mA via that pin. So say to achieve 2.5v on the pin, the pot resistance should be set somewhere to 40 ohms. I think this is not correct, right?

    Also I found a strange thing - if I set TRISA=%00110010, which means to set LATA.2 as input, the code does not works at all, but it also stops consuming current on that pin. What can be the reason?

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: 12F1840 ADCIN troubles, help needed :(

    Quote Originally Posted by CuriousOne View Post
    Well, troubles continue.

    Also I found a strange thing - if I set TRISA=%00110010, which means to set LATA.2 as input, the code does not works at all, but it also stops consuming current on that pin. What can be the reason?
    I think you have set porta.1 to input and not porta.2

    This sets PORTA.2 as input
    Code:
    %00110100
    Not sure about your other problems

Similar Threads

  1. 12F1840 config issues
    By enigma in forum PBP3
    Replies: 13
    Last Post: - 15th February 2015, 07:49
  2. Problem converting 12F683 code to 12F1840
    By RossWaddell in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 14th March 2013, 01:55
  3. Help needed with ADCIN Averaging Routine
    By Aussie Barry in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 24th December 2012, 16:05
  4. PBP Support for 12F1840 8-Pin Device?
    By springtank in forum mel PIC BASIC Pro
    Replies: 17
    Last Post: - 10th November 2012, 08:32
  5. troubles using more than one PCF8574
    By maus in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 28th July 2007, 19:37

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