PDA

View Full Version : Newby with ADC problems.



Mark Scotford
- 3rd September 2004, 16:13
PicBasic Pro
17C756A
4 Mhz
Slow Learner at the wheel.

I am trying to use the ADC on a PIC for the first time and I have hit problems. I have spent several hours trying to work out the problem, but to no avail, your help would be most appreciated. I am trying to read the value of three different voltages on three different PIC pins of my 17C756A. The voltages to be measured are all in the range of 0 to 5V, and one of these to be measured, is simply a 10K Pot with its 2 ends connected to +5V and 0V and the wiper to PortG1. I have tied the A-VDD pin to the supply voltage +5V and tied the A-VSS pin to 0V. To experiment to see if I am reading anything or not, I have put a simple routine in so the the voltage on PortG1 is stored as value B21 (the other two values to be stored as B22 and B23). The value of these three variables is displayed on an LCD and re-read every 500. I am not reading the voltage correctly because the result (although stable if you leave the pot alone) is not 0-255 but something like either of the following:- 64, 128, 196, but not linear nor in the correct order, please would some kind soul look at my code and tell me where I am going wrong.

B21 VAR BYTE
B22 VAR BYTE
B23 VAR BYTE

TRISB = %00110000
TRISC = %00000100
TRISD = %11111111
TRISE = %1101
TRISF = %00000000
TRISG = %11101110

ADCON1 = %000000001110

ADCON1.7 = 1

DEFINE LCD_DREG PORTF
DEFINE LCD_DBIT 0
DEFINE LCD_RSREG PORTC
DEFINE LCD_RSBIT 7
DEFINE LCD_EREG PORTC
DEFINE LCD_EBIT 6
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50

DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50

GoTo MAIN

MAIN:

GOSUB LCDIT
PAUSE 500
GOTO MAIN


LCDIT: ADCIN 0, B23
ADCIN 1, B22
ADCIN 2, B21
LCDOut $FE,1,#B7
LCDOut $FE,$14,#B8
LCDOut $FE,$14,#B21
LCDOut $FE,$14,#B22
LCDOUT $FE,$14,#B23
LCDOUT $FE,$C0,"Readings "
RETURN

I have stripped out the code which I think is unecessary.

Dwayne
- 3rd September 2004, 16:44
hello Mark,

Mark try incorporating this in your program...


TRISIO=%00001010 'Pic 12F675

' Define ADCIN parameters
DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50

'adval VAR byte

'Pause 100
'Loop:
ADCIN 1, number ' Read channel 1 to adval (0-1023)
'number = (number */ 500)>>2 equates to: (adval * 500)/1024
number = number >> 6


Your values should be between 1 and 1023...

And use only ONE pin at first (until you get it working) then go to the using two pins.

Make darn sure your pin has a A/C converter on it. and make darn sure your TRIS is set to a "1" to represent the A/D converter.

Also, your reading will not be a "Voltage Reading" but a reading of a number between 0 and 1024... you must convert this number to a voltage reading.

if 0=0 volts.
if 5=1024

Voltage= 5 X ADvalue / 1024



Dwayne

Mark Scotford
- 4th September 2004, 12:16
Thanks Dwayne, you pointed me in the right direction. I did not realise how important the Microchip Data sheets are, I thought PBP did everything for you. I had to learn (by trial an error) how to set the ADCON manually, got there now, can read my Pot and output 0-255 just lovely (what was that about the Blinkey Light?)
Regards Mark

GEEZER
- 19th March 2005, 20:09
I have been looking at this post and may have picked up a little something. You guys are way ahead of me. I have only a few weeks experience.

I have an 16F877A which I have managed to interface with an LCD and KEYPAD.
All works great. Now I would like to read voltage through the ADC, A0 - A9.

Here is the code I am using.

DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50

X VAR BYTE
TRISA = %11111111
ADCON1 = % 10000010
PAUSE 500

MAIN:
ADCIN 1, X
LCDOUT X, " VOLTS"
PAUSE 500
GOTO MAIN
END

What I am getting now is random ascii characters for X. Sometimes they are sequential for a while.

Any help will be appreciated.

Geezer