Little ADC Help PLease !!!


Results 1 to 2 of 2

Threaded View

  1. #2
    Join Date
    Aug 2005
    Posts
    42

    Default

    Here is a bit of code I am using at the moment that seems to work well.

    Wait for 255 to be sent from PC then samples and sends reply back.

    Code:
    '************************************
    
    'Define ADCIN parameters
    ' Set number of bits in result
    DEFINE ADC_BITS 10 
    
    ' Set clock source (3=rc)
    DEFINE ADC_CLOCK 3 
    
    ' Set sampling time in microseconds
    DEFINE ADC_SAMPLEUS 10 
    
    ' Set PORTA to all input
    TRISA = %11111111 
    
    ' Set up ADCON1 to tell it to right justify and enable A/D channel 0
    ADCON1 = %10000010 
    
    ' Maximum buffer size
    USBBufferSizeMax con 8
    
    ' Input buffer size
    USBBufferSizeTX con 8 
    
    ' Output buffer size
    USBBufferSizeRX con 8
    
    ' The USB buffer variables...
    USBBuffer Var ByteUSBBufferSizeMax 
    USBBufferCount Var Byte 
    
    ' Other variables
    datawait var byte
    
    ' Create variable to store result
    adcVar VAR WORD 
    
    
    'Wait Half a second let ADC settle
    pause 500
    
    
    
    ' Initialise USB...
    usbinit 
    
    ***' ***************************************************************
    ***' * Start of Main Routine ** **
    ***' ***************************************************************
    usbstart:
    
    'Clear main wait variable
    datawait=0
    
    'Go check for a USB packet
    gosub DoUSBIn
    
    ' Check data Received in and load variables
    datawait=usbbuffer(0)
    
    'Check if PC wants ADC sent back 
    if datawait=255 then
    
    'Gosub main ADC routine
    gosub main
    
    endif
    
    'Go back to the start and wait for the next command 
    goto usbstart
    
    
    *' *************************************************************
    *' * Main Sample Routine **
    *' *************************************************************
    main: 
    
    
    ' Read channel 0 
    ADCIN 0, adcVar 
    
    ' Wait.01 second
    Pause 10 
    
    'Shift the result down to 10 bits 
    ADcvar = adcvar >> 6
    
    
    'Set the Result
    usbbuffer(0)=adcvar.highbyte
    usbbuffer(1)=adcvar.Lowbyte 
    
    
    'Send it out to PC
    gosub dousbout
    
    
    return
    
    *' *************************************************************
    *' * Receive data from the USB bus **
    *' *************************************************************
    DoUSBIn:
    
    ' RX buffer size
    USBBufferCount = USBBufferSizeRX 
    
    ' Keep connection alive
    USBService 
    
    ' Read data, if available 
    USBIn 1, USBBuffer, USBBufferCount, DoUSBIn 
    
    return
    
    *' *************************************************************
    *' * Send Data out **
    *' *************************************************************
    DoUSBOut:
    
    ' TX buffer size 
    USBBufferCount = USBBufferSizeTX 
    
    ' Keep connection alive 
    USBService 
    
    ' If bus available, transmit data 
    USBOut 1, USBBuffer, USBBufferCount, DoUSBOut 
    
    return
    Cheers

    Sean.
    Last edited by sean-h; - 9th March 2009 at 16:03.
    *********************
    http://www.cncdudez.co.uk
    *********************

Similar Threads

  1. Stable Adc Reading Routine
    By gebillpap in forum General
    Replies: 27
    Last Post: - 13th May 2015, 03:18
  2. 10 bit ADC display on LCD using 16f873
    By pr2don in forum mel PIC BASIC
    Replies: 3
    Last Post: - 6th March 2010, 19:29
  3. Can't get ADC to loop
    By TravisM in forum mel PIC BASIC
    Replies: 2
    Last Post: - 11th October 2009, 16:33
  4. ADC value with 2 decimals on an LCD
    By Squibcakes in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 2nd December 2005, 16:54
  5. 12F675 ADC 'Issues'
    By harrisondp in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 31st March 2005, 02:55

Members who have read this thread : 0

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

Tags for this Thread

Posting Permissions

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