16F616 A to D



THE BOOK of DT's INTERRUPTS is an organization of interrupt service routines and other works written by Darrel Taylor, RIP.

Interrupt routines are arranged per Darrel's original list. Darrel Taylor's Instant Interrupts are an extension of the work of Tim Box whom in October of 2002 wrote INT_CTRL.pbp

Many of the forum members felt this is a long overdue project.

THE BOOK of DT's INTERRUPTS
will be a valuable resource and a tribute to Darrel Taylor the "Fanatical Contributor".

THE BOOK of DT's INTERRUPTS is located at http://dt.picbasic.co.uk/

+ Reply to Thread
Results 1 to 4 of 4

Thread: 16F616 A to D

  1. #1

    Default 16F616 A to D

    I can't seem to get the A/D to work correctly. I have tried different configs yet no luck. The pin I am using to measure voltage is RC0/AN4/C2IN+. So I configure the pin to analogue with ANSEL.4=1, disable the comparator with CM2CON0.7=0 and select the analogue bit with ADCON0.5-2.

    I check voltage at the pin with my scope and its .650v. Yet debugging I get a value of '0' with any input from 0-5v. Any input is appreciated.

    Code:
    INCLUDE "modedefs.bas"
      
    FF           var PortC.0
    FrontOut     var PortC.1
    RearOut      var PortC.2
    
    Voltage      var word
    Perfect      var word
    
    define OSC 20
    DEFINE DEBUG_REG PortC          'SET DEBUG PIN PORT
    DEFINE DEBUG_BIT 5             'SET DEBUG PIN BIT
    DEFINE DEBUG_BAUD 9600         'SET DEBUG BAUD RATE
    DEFINE DEBUG_MODE 1            'SET DEBUG MODE 0=TRUE / 1=INVERTED
    
    Define ADC_BITS 10 ' Set number of bits in result
    Define ADC_CLOCK 3 ' Set clock source (3=rc)
    Define ADC_SAMPLEUS 10 ' Set sampling time in uSec
    
    TRISC.0 = 1  'FF (Flip/Flop)
    TRISC.1 = 0  'FrontOut
    TRISC.2 = 0  'RearOut
    
    '0 Digital 1 Analogue
    ANSEL.6=0 'RearOut
    ANSEL.5=0 'FrontOut
    ANSEL.4=1 'FF 
    
    CM2CON0.7=0 'Disable comparator
    
    ADCON0.7=1 'Right Justified
    ADCON0.6=0 ''Voltage reference is VDD
    ADCON0.5=0 'Turns on AN4 to analogue
    ADCON0.4=1
    ADCON0.3=0
    ADCON0.2=0
    ADCON0.0=1
    
    Perfect = 512
    
    Loop:
      GOSUB Get_Voltage
      gOSUB Compare
      debug "Voltage=", DEC VOltage, cr
    Goto Loop
    
    Get_Voltage:
    ADCIN FF, Voltage
    Return
    
    Compare:
    If Voltage < Perfect then
       Low FrontOut
    endif
    
    If Voltage > Perfect then
       Low RearOut
    endif
    Return

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


    Did you find this post helpful? Yes | No

    Default

    You have capture compare registers to deal with, and others. CCP1CON, CM1CON0,CM2CON0, CM2CON1, ADCON1
    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


    Did you find this post helpful? Yes | No

    Default

    Am I doing the ADCON0.x correctly?

    I added CM1CON0.7=0 so both comparators are disabled.

    I still get Voltage=0 when I debug.

    INCLUDE "modedefs.bas"

    FF var PortC.0
    FrontOut var PortC.1
    RearOut var PortC.2

    Voltage var word
    Perfect var word

    define OSC 20
    DEFINE DEBUG_REG PortC
    DEFINE DEBUG_BIT 5
    DEFINE DEBUG_BAUD 9600
    DEFINE DEBUG_MODE 1

    Define ADC_BITS 10
    Define ADC_CLOCK 3
    Define ADC_SAMPLEUS 10

    TRISC.0 = 1 'FF (Flip/Flop)
    TRISC.1 = 0 'FrontOut
    TRISC.2 = 0 'RearOut

    '0 Digital 1 Analogue
    ANSEL.6=0 'RearOut
    ANSEL.5=0 'FrontOut
    ANSEL.4=1 'FF
    CM1CON0.7=0
    CM2CON0.7=0 'Disable comparator

    ADCON0.7=1 'Right Justified
    ADCON0.6=0 'Voltage reference is VDD
    ADCON0.5=0 'Turns on AN4 to analogue
    ADCON0.4=1
    ADCON0.3=0
    ADCON0.2=0
    ADCON0.0=1

    CCP1CON.3=0
    CCP1CON.2=0
    CCP1CON.1=0
    CCP1CON.0=0

    Perfect = 512

    Loop:
    GOSUB Get_Voltage
    gOSUB Compare
    debug "Voltage=", DEC VOltage, cr
    Goto Loop

    Get_Voltage:
    ADCIN FF, Voltage
    Return

    Compare:
    If Voltage < Perfect then
    Low FrontOut
    endif

    If Voltage > Perfect then
    Low RearOut
    endif
    Return


  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    When you use ADCIN you must use the Channel #, not the PORT pin name.

    Code:
    ADCIN 4, Voltage ' PORTC.0 = AN4
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. PIC 16F616 Interrupt Enable/Disable?
    By Tobias in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 17th May 2009, 08:17
  2. Configuring 16F616
    By Tobias in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 16th February 2009, 16:28
  3. PIC16f628 to PIC16f616
    By JimA in forum General
    Replies: 0
    Last Post: - 22nd October 2007, 14:33
  4. HPWM of the port of two HPWM
    By ja2rqk in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 25th April 2007, 15:05

Posting Permissions

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