Little ADC Help PLease !!!


Closed Thread
Results 1 to 2 of 2
  1. #1
    Francisco M's Avatar
    Francisco M Guest

    Question Little ADC Help PLease !!!

    Hello !!! i have a little doubt about the ADC ... can anyone help me ??

    i have a USB interface (Hard) with a 18F2550 pic, on wich i can turn on/off some leds by the Soft in Visual Net... i want to use the Inputs and read some data, i read the input data in the Digital Input, and that was correct, but i still can not read the input data in the Analogic Input .. can anyone help me and tell me if im writing bad the code of the ADC or something like that ?? all the comunication is fine, and in the terminal input i can read with the multimeter the voltage (2.5 volts) that im sending to the pic, so there is a signal !!!

    thks a lot !!!! really a lot !!!

    Frank

    ______________________________________________


    DEFINE OSC 12

    USBBufferSizeMax con 8 ' maximum buffer size
    USBBufferSizeTX con 8 ' input
    USBBufferSizeRX con 8 ' output

    ' the USB buffer...
    USBBuffer Var Byte[USBBufferSizeMax]
    USBBufferCount Var Byte

    ADCON1 = $0D ' inicializar el registro hace que adc0 - 3 sean entradas analogicas.
    TRISA = $0F ' el Puerto A lo declara como Entradas
    TRISB = 0 ' el Puerto B lo declara como Salidas
    CMCON = 7 ' deshabilitar los comparadores

    ' ************************************************** **********
    ' * main program loop - remember, you must keep the USB *
    ' * connection alive with a call to USBService every couple *
    ' * of milliseconds or so... *
    ' ************************************************** **********
    usbinit ' initialise USB...
    ProgramStart:
    gosub DoUSbiN
    gosub DoUSBOut

    GOSUB DoTemp
    GOSUB DoAna

    PORTB=USBBuffer[7]
    goto ProgramStart

    ' ************************************************** **********
    ' * receive data from the USB bus *
    ' ************************************************** **********
    DoUSBIn:
    USBBufferCount = USBBufferSizeRX ' RX buffer size
    USBService ' keep connection alive
    USBIn 1, USBBuffer, USBBufferCount, DoUSBIn ' read data, if available
    return

    ' ************************************************** **********
    ' * wait for USB interface to attach *
    ' ************************************************** **********
    DoUSBOut:
    USBBufferCount = USBBufferSizeTX ' TX buffer size
    USBService ' keep connection alive
    USBOut 1, USBBuffer, USBBufferCount, DoUSBOut ' if bus available, transmit data
    return

    ' ************************************************** **********
    ' * Parte nueva del codigo para medir la Temperatura *
    ' ************************************************** **********

    DoTemp:
    'Configurar como input (Puerto A, Entrada 4, Pin 6 como Entrada)
    TRISA.4 = 1

    ' Declara las Variables para la Temperatura
    FREC VAR WORD
    ZERO VAR WORD
    RES VAR WORD
    RES1 VAR WORD
    RES2 VAR WORD
    RES3 VAR WORD

    ' Hace la medición de la Frecuencia de entrada al Pic y realiza
    ' operaciones para dar la Temperatura
    COUNT PORTA.4, 1000, FREC
    ZERO = 30750
    RES = FREC * 10
    RES1 = RES - ZERO
    RES2 = 65535 - RES1
    RES3 = RES1 / 140

    ' Cada Variable la asigna a un bit del buffer para poder ser leido
    ' por el sistema
    USBBuffer[0] = "T"
    USBBuffer[1] = FREC
    USBBuffer[2] = ZERO
    USBBuffer[3] = RES
    USBBuffer[4] = RES1
    USBBuffer[5] = RES2
    USBBuffer[6] = RES3

    ' Envia a subrutina de USBOut y regresa al programa
    GOSUB DoUSBOut
    PORTB=USBBuffer
    RETURN


    ' ************************************************** **********
    ' * Parte nueva del codigo para probar entrada Analoga *
    ' ************************************************** **********

    DoAna:
    'Configurar como input (Puerto A, Entrada 1, Pin3 como Entrada)
    TRISA.2 = 1

    ' Declara las Variables para la Medicion del Voltaje del Pot
    VOLTPOT VAR WORD
    MIL VAR WORD
    RESANA VAR WORD

    ' Hace la medición del Voltaje de entrada al Pic y realiza
    ' operaciones
    COUNT PORTA.2, 1000, VOLTPOT
    MIL = 1000
    RESANA = VOLTPOT * MIL

    ' Cada Variable la asigna a un bit del buffer para poder ser leido
    ' por el sistema
    USBBuffer[0] = "V"
    USBBuffer[1] = VOLTPOT
    USBBuffer[2] = MIL
    USBBuffer[3] = RESANA

    ' Envia a subrutina de USBOut y regresa al programa
    GOSUB DoUSBOut
    PORTB=USBBuffer
    RETURN

    __________________________________________________ ________

  2. #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 15: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, 02:18
  2. 10 bit ADC display on LCD using 16f873
    By pr2don in forum mel PIC BASIC
    Replies: 3
    Last Post: - 6th March 2010, 18:29
  3. Can't get ADC to loop
    By TravisM in forum mel PIC BASIC
    Replies: 2
    Last Post: - 11th October 2009, 15: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, 15:54
  5. 12F675 ADC 'Issues'
    By harrisondp in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 31st March 2005, 01: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