Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Wrong a_to_d value for input voltage

Status
Not open for further replies.

jerryd

New Member
Electro Tech,

I'm using a TMP36 device and a 16F88 to read the temperature.

I have one program that uses RB7 as an a_to_d and converts the
temperature to F. Then it uses the uart to send the results
to my laptop. Works great.

In this program portB is configured as:
TRISB = %11010001 'set portb 0,4,6,7 pins as input
PORTB = %00000000 'all portb outputs pins low
RCSTA = %10000000 'configures RB2 and RB5 as serial port pins
ANSEL = %01000000 'configure RB7 as analog input


In a very similar program portB is configured as:
TRISB = %11110001 'set portb 0, 4, 5, 6 & 7 as input
PORTB = %00000000 'all portb output pins low
INTCON = %10011000 'enable external interrupts INTF & RBIF
OPTION_REG = %00000000 'pull-up resistors on, falling edge on RB0
ANSEL = %01000000 'configure RB7 as analog input

Using the second program I can see the correct voltage level
at RB7 but the a_to_d reading is off by about 100mv.

The only read difference is the second program uses external
interrupts. Is there some kind of conflict?

jerryd
 
You show us a tiny bit of setup code and expect us to have a guess at what may be wrong with your code!!

Mike.
 
Pommie,

This is too large a program to post the whole thing.
I can post pieces of it.

Since I have one version working I suspect it's the way I'm using
the 16F88.

The code to read and convert the a_to_d value is identical.

Here's the status of the ports in the program that has the problem,
when I read the a_to_d value.

All RA ports are outputs and low. Except MCLR.
RB1, 2 and 3 are unused inputs and are low.
RB0 has a 0-5v 60Hz signal.
RB4 has an IR photo transistor which, when active, pulls the port low.
RB5 and RB6 are wired to switches which pull these ports low when pressed.
RB7 is the analog input.

So at the time I'm reading the a_to_d value RB0 is toggling,
and RB4, 5 and 6 are pulled high by the internal pullups the rest
are low.


On the program that works,
All RA ports are outputs and low. Except MCLR.
RB0, and 3 and are unused inputs and are low.
RB1 is an output and is high.
RB0, 4, 6 are inputs and low, pullups are off.
RB2 and 5 are used for uart.
RB7 is analog input.

Does any of this help?

Thanks,
jerryd
 
It's hard to say without seeing the code but the most common reasons for incorrect readings are,

Insufficient acquisition time.
Too much of a load on the supply thereby reducing the 5V line.

Mike.
 
Pommie,

I've checked the supply and it's 4.94 volts. Should be close enough to 5.00.

I thought the adc_read(ans6) command would wait for the conversion to
be complete.

In the working program I read the a_to_d value once every 5 seconds. On the
other program it's read once every 100ms.

How much time does the 16F88 take to read and convert?

jerryd
 
As I have no idea which compiler you are using or whether you are using built in libraries or your own code it's really hard to say if adc_read() waits the required acquisition time.

So, can you tell us the compiler and post the setup and read code for the adc.

Mike.
 
Last edited:
This is too large a program to post the whole thing.
I can post pieces of it.

Hi,
Its possible to attach a 'asm' file, using the following sequence.

Left click 'Reply', left click 'Go Advanced', this will show a 'Manage Attachments' option. Left click 'Manage Attachments' .

Use the pop up window to select the 'asm' file located in your PC.
Upload it and then Close the pop up window.

then left click 'Submit Reply' , [after you have entered any text to the post.]

Note: while the pop up file select window is open, it shows the file types and size which can be attached.
 
Mike,

I'm using mikroBasic Pro 4.60.0.0. All available libraries
were included when I created the project.

The program is all written in basic but here is the asm
for the line "value = adc_read(ans6)". Value is defined
as "dim value as float".

MOVLW 6
MOVWF FARG_ADC_Read_channel+0
CALL _ADC_Read+0
CALL _Word2Double+0
MOVF R0+0, 0
MOVWF _value+0
MOVF R0+1, 0
MOVWF _value+1
MOVF R0+2, 0
MOVWF _value+2
MOVF R0+3, 0
MOVWF _value+3

I understand this code but I don't see any thing that would wait
for acquisition.

I follow this with a "delay_ms(25).

I don't know how much of my setup you need? Here is how I
configured and initialized PORTB.

TRISB = %11110001 'set portb 0, 4, 5, 6 & 7 as input
PORTB = %00000000 'all portb output pins low
INTCON = %10011000 'enable external interrupts INTF & RBIF
OPTION_REG = %00000000 'pull-up resistors on, falling edge on RB0
ANSEL = %01000000 'configure RB7 as analog input

jerryd
 
Mike,

I forgot to add this code in my last reply.

value = adc_read(ans6)
delay_ms(25)

res = 5/1023 'voltage/a to d step resolution = .0048875
vout = (value*res)*1000 'convert a to d value to mv
Ctemp = (vout - 500)/10 '500 = 0 degrees C, 10mv/degree C
Ftemp = (9/5*Ctemp)+32 'convert c to f

jerryd
 
Try doing it the none library way,
Code:
ReadADC
	ANSEL=%01000000		'AN6 = analogue
        ADCON1=%10000000	'right justified
	ADCON0=%10110001	'Turn on A2D and select AN6
	Delay_us(100)		'wait for cap to charge
	ADCON0=%10110101	'or GO=1. Set the go bit
	while(ADCON1.GO=1)	'or while(GO). Wait for go to be zero
	Val=ADRESH*256+ADRESL	'get result

If the above fixes it then it was the acquisition time.

Mike.
 
Last edited:
Pommie,

Thanks for the code, however it will have to wait for a while.
I have 2 TMP36's and I've managed to blow up both.

jerryd
 
Electro Tech,

Finally got some new TMP36's.

I tried the code submitted by Mike Pommie and had the same
problem. The reading being about 10 degrees too low even though
I measure the correct input voltage at the AN6 pin.

The TMP36 is on a separate board and the wire carring the signal
to the 16F88 is about 10 inches long and goes through a slip ring.

I added a second ground to the TMP36 on the separate board,
same software. It didn't change the voltage out of the TMP36
but now I get the correct reading out of the 16F88.

Must be some kind of reference level problem.

Any suggestions?

jerryd
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top