PDA

View Full Version : Little ADC Help PLease !!!



Francisco M
- 13th February 2009, 03:21
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

__________________________________________________ ________

sean-h
- 9th March 2009, 14:59
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.



'************************************

'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.