PDA

View Full Version : (2) 10 Bit Inputs, Convert one to 8 bit and display both on LCD



jrudd
- 8th March 2012, 00:14
I have my x/y joystick control working with 8 Bit inputs for tank style control(i don't want to go through that algorithm again), but I want to use a temp sensor(LM35DT) with 10 Bit input for better resolution. I have tried the bit shift method before displaying the 8 Bit result without success. I would like to know for sure what the 8 bit value is by displaying on the LCD. Maybe that can't be done since the 8 bit must be left justified while the 10 bit value must be right justified. 16F877A Code below:

' PicBasic Pro program to display result of
' 10-bit and 8-bit A/D conversion on LCD
' Connect analog input to channel-0,8-bit and channel-1,10-bit (RA0)

' Define LCD registers and bits
@ DEVICE HS_OSC

DEFINE OSC 20
Define LCD_DREG PORTD
Define LCD_DBIT 4
Define LCD_RSREG PORTD
Define LCD_RSBIT 2
Define LCD_EREG PORTE
Define LCD_EBIT 1
define LCD_RWREG PORTD
define LCD_RWBIT 3
pause 500
' Define ADCIN parameters
Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS
'ADCON1 =000000 'Set PORTA analog and Right(1),Left(0) justify result
adval0 var word ' Create adval to store result
adval1 var word
duty var word
'************************************************* ***************
'Set up (2) Channel Analog input
TRISD=0
TRISE=0
TRISA = 000011 ' Set PORTA.0 & PortA.1 to input,ALL THE REST OUTPUTS
' ADCON1 =000000 'Set PORTA analog and right justify result
PORTE.2=0 ' Turn off leds so you can use the lcd display
PORTD.3=0 ' LCD R/W line low (W)
Pause 500 ' Wait .5 second
LCDOUT 254,1
PAUSE 500
'************************************************* *****************
'Set up PWM
PR2=9
TRISC.2=0 'Set CCP1 to Output
CCP1CON=001100 'SET CCP1 TO PWM
T2CON=000100 'SET TIMER2 TO "ON";PRESCALE=1

duty=5

loop:

adcon1=000000
ADCIN 0, adval0 ' Read channel 0 to adval
' adval0=adval0>>2
pause 50
Lcdout $fe, 1 ' Clear LCD
Lcdout "8 Bit=: ", dec adval0 ' Display the decimal value
' Pause 100 ' Wait .1 second

adcon1=000000
adcin 1,adval1
lcdout $fe,$c0
lcdout "10 Bit=: ",dec adval1
' pause 1000

Goto loop ' Do it forever
End

Fernando
- 12th March 2012, 23:34
Not sure watt you want to do.

As far i can see both your variables adval0 and adval1 are storing a 10 bit number in decimal range 0 to 1023
if you divide any of them by four they turn to 8bit ranging from 0 to 255 decimal number.

Fernando