PDA

View Full Version : 10 bit ADC display on LCD using 16f873



pr2don
- 6th March 2010, 08:19
I have written a code for ADC conversion from 0-5v, but seem not to work. Can someone help me where I am wrong. Here is the code attached is the circuit diagram Cheers

Define LCD_DBIT 4
Define LCD_RSREG PORTB
Define LCD_RSBIT 3
Define LCD_EREG PORTB
Define LCD_EBIT 2
Define LCD_EBITS 4
Define LCD_LINES 2

' Initialise ADC
ADCON1 = %10000100 ' Set PORTA analog and RIGHT justify result
ADCON0 = %01000001 ' Configure and turn on A/D Module channel o Fosc 8
'Pause 500 ' wait .5 second
'Declear variables
val VAR WORD 'final calculated adc valuesWORD' final calculated adc values
volt VAR WORD ' scaled value real part
voltd1 VAR WORD ' scaled value first decimal place
voltd2 VAR WORD ' scaled value second decimal place
i var byte ' counter
'************************************************* ******************
' Main program code
Start:
GoSub ADC
pause 500
GoTo Start
'************************************************* *****************
ADC
Val = 0 ' Value returned is the average of 5 conversions
For i = 0 TO 4
ADCON0.2 = 1 ' Start convertion
NoteDone:
pause 1
IF ADCON0.2 = 1 Then NoteDone
val = val + ((ADRESH * 256)+(ADRESL))
nEXT I
VAL = VAL/5
' Calculate Volatage to 2 decimal places
volt = val * 5/1024
voltd1 = ( val * 5//1024) * 10/1024
Voltd2 = ((val *5 //1024) * 10//1024) * 10/1024

LCDOut $FE,$C0, # Val, "=", #volt, ".", # voltd1, # voltd2, "Vdc"
Return

mark_s
- 6th March 2010, 16:22
Hi pr2don,

Looks like a few typos and omissions

Your defines:

Define LCD_DBIT 4
Define LCD_RSREG PORTB
Define LCD_RSBIT 3
Define LCD_EREG PORTB
Define LCD_EBIT 2
Define LCD_EBITS 4
Define LCD_LINES 2

Try changing your defines to these

Define LCD_DREG PORTB
Define LCD_DBIT 4
Define LCD_RSREG PORTB
Define LCD_RSBIT 3
Define LCD_EREG PORTB
Define LCD_EBIT 2
Define LCD_BITS 4
Define LCD_LINES 2
Define LCD_COMMANDUS 2000
Define LCD_DATAUS 50

Also on your subroutine "ADC" you need to add a colon after ADC:

Hope this helps

mark_s
- 6th March 2010, 18:09
pr2don

Are you using pic basic pro or pic basic? No LCDout command in pic basic. If using pic basic pro, look up the "adcin" command

ScaleRobotics
- 6th March 2010, 18:29
You might want to initialize the LCD too.



PAUSE 500 : LCDOUT $FE,1 : PAUSE 250 ; Initialize LCD (You may not need this,
; but some displays are picky)