12F683 ADcin = binary outcome!


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2009
    Posts
    653

    Default 12F683 ADcin = binary outcome!

    I'm a struggling learner who took a few months out of all this - I wish I hadn't now!

    Anyway, I'm re-starting with a different (simpler!) chip - a 12f683 - just getting back to the basics right now....AtoD.

    I found some code posted by Bruce on here that I shamelessly used (changing the pins to suit my setup)...

    Code:
    @MyConfig = _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON  
    @MyConfig = MyConfig & _MCLRE_OFF 
    
        CMCON0 = 7          ' Disable comparator
        ANSEL = %00000010   ' Set GPIO.1 to A/D
        ADCON0.7 = 1        ' Right justify for 10-bit
        TRISIO = %00000010  ' GP.0 = serial out, GPIO.1 = A/D in, rest outputs
    
        DEFINE OSC 8            ' Internal 8MHz 
    
        DEFINE DEBUG_REG GPIO   ' Define DEBUG port
        DEFINE DEBUG_BIT 0      ' Set GPIO.0 as DEBUG serial output pin
        DEFINE DEBUG_BAUD 9600  ' DEBUG baud rate = 9600 bps
        DEFINE DEBUG_MODE 0     ' 1 = inverted, 0 = true
        
        DEFINE  ADC_BITS 10     ' 10-bit resolution
        DEFINE  ADC_CLOCK 5     ' Set clock source to Frc/16
        DEFINE  ADC_SAMPLEUS 50 ' Sample time in uS before A/D is started
        Quanta  CON 1251        ' For 10-bit A/D +Vref = 5V : 5V/1023*256=1.251=Quanta
    
    ' PIN# NAME     USE & CONNECTION
    '  1   Vdd      +5VDC power supply
    '  2   GPIO.5   N/C (N/C = NO CONNECTION)
    '  3   GPIO.4   N/C
    '  4   GPIO.3   N/C
    '  5   GPIO.2   N/C
    '  6   GPIO.1   A/D input pin
    '  7   GPIO.0   Serial output pin to PC serial port RX pin
    '  8   Vss      Power supply ground (also connect to DB-9 pin #5)
    '-------------------------------------------------------------------------
    
    ADval  VAR WORD	' A/D result
    
    
    MAIN:
        ADCIN 1, ADval      ' Read A/D
        DEBUG DEC ADval DIG 3,".",DEC3 ADval,"V",13, 10
        PAUSE 500
        GOTO MAIN
        END
    the thing is it'll read 5V for 0V presented on pin 6 GP1 (I view the AtoD result using the PICkit 2 Usart tool)- but nothing in between (only two states!)

    Any ideas please before I just give this all up as a bad job and turn to constructing replica stately homes out of discarded matchsticks.

  2. #2
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Hi Hank. . .
    try ADCON0 = %10000111
    it POR defaults to 0 0 0 0 0 0 0 0

    Bits 3:2
    00 = GPIO.0 AN0
    01 = GPIO.1 AN1
    10 = GPIO.2 AN2
    11 = GPIO.3 AN3
    Bits 1:0
    Bit 0 , 0 = disabled 1 = enabled
    Bit 1, 0 = A/D completed or not in progress 1 = ready to do for you
    Last edited by Archangel; - 4th March 2010 at 01:49.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  3. #3
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Hi Joe,

    I tried your recommendation - couldn't get it to work.

    I think it might be something to do with leaving the Pickit2 programmer attached as a 'virtual' serial connnection to my PC - so I moved the AtoD over to AN3 (pin 3)..

    This code seems to work...

    Code:
    @MyConfig = _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON  
    @MyConfig = MyConfig & _MCLRE_OFF 
    
    DEFINE OSC 4           ' Internal 8MHz
    DEFINE ADC_BITS 8      ' 8-bit resolution
    DEFINE ADC_CLOCK 2     ' Set clock source to Frc/32
    DEFINE ADC_SAMPLEUS 50 ' Sample time in uS before A/D conversion is started
    
    CMCON0 = 7   			'Comparators off
    ADCON0 = %00000001		'ADC enabled and right justified
    ANSEL = %00001000		'GPIO.0 and GPIO.1 analog input
    INTCON = 0				'INTERRUPTS off
    TRISIO = %111011		'GPIO2 output
    GPIO = %00010000 		' All outputs = 0 on boot
    
        Quanta  CON 1251        ' For 10-bit A/D +Vref = 5V : 5V/1023*256=1.251=Quanta
        ADval  VAR WORD	    ' A/D conversion result
    
        DEFINE DEBUG_REG GPIO   ' Define DEBUG port
        DEFINE DEBUG_BIT 0      ' Set GPIO.0 as DEBUG serial output pin
        DEFINE DEBUG_BAUD 9600  ' DEBUG baud rate = 9600 bps
        DEFINE DEBUG_MODE 0     ' 1 = inverted, 0 = true
        
        MAIN:
        ADCIN 3, ADval      ' Read A/D
        DEBUG DEC ADvAL,13 ,10
        PAUSE 200
        GOTO MAIN
    
        END
    Many thanks for your help.
    Last edited by HankMcSpank; - 4th March 2010 at 22:00.

  4. #4
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by HankMcSpank View Post
    Hi Joe,

    I tried your recommendation - couldn't get it to work.

    I think it might be something to do with leaving the Pickit2 programmer attached as a 'virtual' serial connnection to my PC - so I moved the AtoD over to AN3 (pin 3)..

    This code seems to work...

    Code:
    @MyConfig = _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON  
    @MyConfig = MyConfig & _MCLRE_OFF 
    
    DEFINE OSC 4           ' Internal 8MHz
    DEFINE ADC_BITS 8      ' 8-bit resolution
    DEFINE ADC_CLOCK 2     ' Set clock source to Frc/32
    DEFINE ADC_SAMPLEUS 50 ' Sample time in uS before A/D conversion is started
    
    CMCON0 = 7               'Comparators off
    ADCON0 = %00000001        'ADC enabled and right justified
    ANSEL = %00001000        'GPIO.0 and GPIO.1 analog input
    INTCON = 0                'INTERRUPTS off
    TRISIO = %111011        'GPIO2 output
    GPIO = %00010000         ' All outputs = 0 on boot
    
        Quanta  CON 1251        ' For 10-bit A/D +Vref = 5V : 5V/1023*256=1.251=Quanta
        ADval  VAR WORD        ' A/D conversion result
    
        DEFINE DEBUG_REG GPIO   ' Define DEBUG port
        DEFINE DEBUG_BIT 0      ' Set GPIO.0 as DEBUG serial output pin
        DEFINE DEBUG_BAUD 9600  ' DEBUG baud rate = 9600 bps
        DEFINE DEBUG_MODE 0     ' 1 = inverted, 0 = true
        
        MAIN:
        ADCIN 3, ADval      ' Read A/D
        DEBUG DEC ADvAL,13 ,10
        PAUSE 200
        GOTO MAIN
    
        END
    Many thanks for your help.
    OK, if you are going to use @MyConfig, you are missing the most important piece, under @myConfig you must have the config statement directing it to use MYConfig
    like so:
    Code:
    @_MyConfig = _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON  
    @_MyConfig = MyConfig & _MCLRE_OFF 
    @ __config MyConfig
    And @ _MyConfig must have 1 underscore and no spaces between the @ symbol and the underscore.
    I suspect if that worked for you then doing it this way will give an overwriting previous address error, because your code is still using the default config.
    Last edited by Archangel; - 5th March 2010 at 04:50.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

Similar Threads

  1. Conway's Game Of Life
    By wellyboot in forum mel PIC BASIC Pro
    Replies: 45
    Last Post: - 28th May 2020, 06:14
  2. Thermo 7 segments - little problem
    By fratello in forum mel PIC BASIC Pro
    Replies: 49
    Last Post: - 27th July 2013, 07:31
  3. Delayed output 10 secs
    By lilimike in forum mel PIC BASIC Pro
    Replies: 37
    Last Post: - 14th October 2011, 06:28
  4. RS485 bus - starting probem
    By wurm in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 27th January 2010, 13:35
  5. Working with indivividual bytes of an array
    By J_norrie in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 22nd January 2007, 19:16

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