PDA

View Full Version : I can not a reading sensor lm35



cesar35
- 6th January 2016, 01:20
I can not a reading sensor lm35
could someone help me out here the code I'm using
I must read 4 sensor lm35

'USB CDC DEMO sample program for PIC18F4550 CDC serial port emulation
'Compilation of this program requires that specific support files be
'available in the source directory.You may also need to modify the
'file USBDESC.ASM so that the proper descriptor files are included. For
'detailed information, see the file PBP\USB18\USB.TXT.
asm
__CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L

endasm
DEFINE OSC 48
DEFINE LOADER_USED 1
DEFINE RESET_ORG 1000h 'For Microchip USB Bootloader

buffer Var Byte[16]
cnt Var Byte
sw var byte
ch var byte
adval var word 'ADC values 10 bit
led var portb.0

'ADC parameters
DEFINE ADC_BITS 10 'Number of bits in ADCIN result
DEFINE ADC_CLOCK 3 'ADC clock source (rc = 3)
DEFINE ADC_SAMPLEUS 50 'ADC sampling time in microseconds

TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %00001010 ' Set PORTA analog and right justify result, PORTE digital

'***** MAIN PROGRAM ******
START:
'Initialize
USBInit
'Wait for USB input
idleloop:
USBService 'Must service USB regularly
cnt = 16 'Specify input buffer size
USBIn 3, buffer, cnt, idleloop
sw = buffer[0]
'Message received
Toggle LED

select case sw
case "1"
'Read and send ADC values
ADCIN 0,adval
adval = adval*48
adval = adval/100+1
pause 50
ch = "1"
gosub adc2buff
case "2"
'Read and send ADC values
ADCIN 1,adval
adval = adval*48
adval = adval/100+1
pause 50
ch = "2"
gosub adc2buff
case "3"
'Read and send ADC values
ADCIN 2,adval
adval = adval*48
adval = adval/100+1
pause 50
ch = "3"
gosub adc2buff
case "4"
'Read and send ADC values
ADCIN 3,adval
adval = adval*48
adval = adval/100+1
pause 50
ch = "4"
gosub adc2buff
end select
outloop:
USBService 'Must service USB regularly
USBOut 3,buffer,15,outloop
Goto idleloop 'Wait for next buffer
'*************************************************
'Subroutines
Adc2Buff:
buffer[0]="A"
buffer[1]="d"
buffer[2]="c"
buffer[3]= ch
buffer[4]="="
buffer[5]=(adval dig 2)
buffer[6]=(adval dig 1)
buffer[7]=(adval dig 0)
buffer[8]=13
buffer[9]=10
buffer[10]=0
return

HenrikOlsson
- 7th January 2016, 06:40
So what's the problem, more specifically?
Does the program and communication work but the ADC does not?
Is it the math that's the problem?
Is it a binary/ASCII thing when you're sending the data?

Posting a chunk of code with a problem description basically saying "it doesn't work" isn't of much use to those taking their time trying to help you - of course it doesn't work, if it did you wouldn't be posting, right?

So, I'm going to take one shot at it:
The documentation for USBService says that it needs to be, and I quote, executed at least every 10ms throughout the program. In your Select Case block you have 50ms pauses which violates that requirement straight up.

But then again, perhaps the communication is working fine and the problem is with the math - what do I know...

/Henrik.

cesar35
- 7th January 2016, 15:00
Hello
I think my problem is in the upstream buffer will modify the codes
Thank you

cesar35
- 8th January 2016, 14:13
Please tell me how to deal with the problem. I can not figure out how to transfer data from USB via variables.As a result, data is received as such, irrespective of the position of the variable resistor connected to the ADC input.

HenrikOlsson
- 8th January 2016, 16:41
So at this point you know that the USB coomunication is working and that the PIC is sending something, just not the correct stuff? Can you send "Hello World" and have it show up in whatever application it is you're using on the PC?

I'm not sure the DIG operator returns the ASCII code for the digit. I'd try ArrayWrite Buffer, ["Adc", Ch, "=", DEC3 adval, 13,10,0]

/Henrik.

cesar35
- 8th January 2016, 19:38
Thanks for the answer
more still send wrong data
the PC is receiving a yes "Adc1=[05][06][04]
[00]|[00]ÿÿ"

Adc2Buff:
ArrayWrite Buffer,["Adc","=",DEC3 adval,13,10,0]
return