hi all,
i have a problem with the analog values been recieved from another pic .well, i don't know exactly where is the problem it keeps recieving different values each time i open /close the switches in the other pic. here's the code attached below.

pic_sender:

INCLUDE "Modedefs.bas"

DEFINE OSC 4
DE_OR_RE VAR PORTC.5 ' DE and RE Pin of SN75176
RS232_DEBUG_PIN VAR PORTB.5 ' Debugging to PC
LEDPIN VAR PORTD.2 ' LED to confirm PIC is running
DATARECEIVED VAR BYTE


DEFINE ADC_BITS 8 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (rc = 3)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in microseconds

TRISA = 255 ' Set PORTA to all input
ADCON1 = 2 ' PORTA is analog
;ADCIN 0, B0 ' Read channel 0 to B0

'======== HSEROUT, HSERIN SETTINGS ==========
DEFINE HSER_RCSTA 90h
define HSER_TXSTA 20h
define HSER_SPBRG 25 'SPBRG 2400
DEFINE HSER_BAUD N2400
DEFINE HSER_CLROERR 1

TRISC = %10111000 ' Set TX (PortC.6) to out, rest in
SO VAR PORTC.6 ' SerOUT PORTC.6 TX
SI VAR PORTC.7 ' SerIN PORTC.7 RX
LED_C0 var PORTC.0 : LOW LED_C0
LED_C1 var PORTC.1 : LOW LED_C1
LED_C2 var PORTC.2 : LOW LED_C2
PORTC = 0
RCIF VAR PIR1.5
TXIF VAR PIR1.4
'======== Define LCD pins ========== RC.0 RC.1 RC.2
Define LCD_DREG PORTD
Define LCD_DBIT 4
Define LCD_RSREG PORTE
Define LCD_RSBIT 0
Define LCD_EREG PORTE
Define LCD_EBIT 1
DEFINE LCD_LINES 4
TRISB = %11111111
PORTB = %00000000

TARGET1 VAR BYTE
TARGET1 = $00

ADX1 VAR BYTE
ADY1 VAR BYTE
ADZ1 VAR BYTE
A VAR BYTE 'Working buffer 2 for button command
B VAR BYTE 'Working buffer 3 for button command
C VAR BYTE 'Working buffer 2 for button command
D VAR BYTE 'Working buffer 3 for button command
E VAR BYTE 'Working buffer 3 for button command

'---------- SW DEFINES
B0 VAR BYTE
SW1 VAR byte ;PORTA.0 ' TO DELECT SW 1
SW2 VAR byte ;PORTA.1 ' TO DELECT SW 2
SW3 VAR byte ;PORTA.2 ' TO DELECT SW 3
SW4 VAR byte ;PORTA.3 ' TO DELECT SW 4
SW5 VAR byte ;PORTA.5 ' TO DELECT SW 6

OPTION_REG.7 = 0
CMCON = 7
B0 = 0
PAUSE 50 ' WAS PAUSE 200
HIGH LEDPIN


mainloop:
TRISA = %00111111
Gosub getx ' Get x value
Gosub gety ' Get y value
Gosub getz ' Get z value
Gosub getM ' Get M value
Gosub getN ' Get N value
Lcdout $fe, 1
Lcdout $fe,$C0, "x=", DEC sW1, "y=", DEC sw2, " z=", DEC sw3
Lcdout $fe,$D4, "M=", DEC sw4 ," N=", DEC sw5 ' Send to LCD
Pause 1000 ' Do it about 10 times a second


Goto main
;;;-=====================================
main:
GOTO sel_slave1

sel_slave1:
GOSUB COMPARE
HIGH DE_OR_RE ' Make ready for TX
PAUSE 4

HSEROUT [ HEX SW1 , HEX sw2,HEX sw3 , HEX sw4 ,HEX sw5]


goto mainloop

;================================================= ================
;================================================= ================

' Subroutine to read a/d converter
getad:
Pauseus 50 ' Wait for channel to setup

ADCON0.2 = 1 ' Start conversion
Pauseus 50 ' Wait for conversion

Return

' Subroutine to get pot x value
getx:
ADCON0 = $41 ' Set A/D to Fosc/8, Channel 0, On 10 000 001
Gosub getad
SW1 = ADRESH
Return

' Subroutine to get pot y value
gety:
ADCON0 = $49 ' Set A/D to Fosc/8, Channel 1, On 01 001 001
Gosub getad
sw2 = ADRESH
Return

' Subroutine to get pot z value
getz:
ADCON0 = $59 ' Set A/D to Fosc/8, Channel 3, On 01 011 001
Gosub getad
sw3 = ADRESH
Return
' Subroutine to get pot z value
getM:
;ADCON0 = %01010001 ' Set A/D to Fosc/8, Channel 2, On 01 010 001 $51
ADCON0 = $51
Gosub getad
sw4 = ADRESH
Return
' Subroutine to get pot z value
getN:
; DCON0 = %01010001 ' Set A/D to Fosc/8, Channel 2, On 01 100 001 $51
ADCON0 = $61
Gosub getad
sw5 = ADRESH
Return
End



COMPARE:

If sw1=127 Then
sw1="A"
ELSE
;If sw1=0 Then
sw1="B"
ENDIF
;ENDIF


If sw2=127 Then
sw2="C"
ELSE
;If sw2=0 Then
sw2="D"
;ENDIF
ENDIF


If sw3=127 Then
sw3="E"
ELSE
;If sw3=0 Then
sw3="F"
;ENDIF
ENDIF


If sw4=127 Then
sw4="G"
ELSE
;If sw4=0 Then
sw4="H"
;ENDIF
ENDIF


If sw5=127 Then
sw5="I"
ELSE
;If sw5=0 Then
sw5="J"
;ENDIF
ENDIF
return
pic_reciever:


INCLUDE "Modedefs.bas"

DEFINE OSC 4
DE_OR_RE VAR PORTC.5 ' DE and RE Pin of SN75176
RS232_DEBUG_PIN VAR PORTB.5 ' Debugging to PC
LEDPIN VAR PORTD.2 ' LED to confirm PIC is running
DATARECEIVED VAR BYTE

DEFINE ADC_BITS 8 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (rc = 3)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in microseconds

TRISA = 255 ' Set PORTA to all input
ADCON1 = 2 ' PORTA is analog



'======== HSEROUT, HSERIN SETTINGS ==========
DEFINE HSER_BAUD N2400
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_SPBRG 129
DEFINE HSER_CLROERR 1
DEFINE HSER_PORT 2


TRISC = %10100000 ' Set TX (PortC.6) to out, rest in
SO VAR PORTC.6 ' SerOUT PORTC.6 TX
SI VAR PORTC.7 ' SerIN PORTC.7 RX
LED_B7 var PORTB.7
low LED_B7



PORTC = 0
RCIF VAR PIR1.5
TXIF VAR PIR1.4
'======== Define LCD pins ========== RC.0 RC.1 RC.2
Define LCD_DREG PORTD
Define LCD_DBIT 4
Define LCD_RSREG PORTE
Define LCD_RSBIT 0
Define LCD_EREG PORTE
Define LCD_EBIT 1
DEFINE LCD_LINES 4
TRISB = %00000000
;PORTB = %00000000
TRISA = %00111111

TARGET VAR BYTE
TARGET="T"

ADX1 VAR BYTE
ADY1 VAR BYTE
ADZ1 VAR BYTE
ADM1 VAR BYTE
ADN1 VAR BYTE


B0 VAR BYTE
B0 = 0
OPTION_REG.7 = 0
CMCON = 0
;ADCON1 = 4
;ADCON1 = %00000010

PAUSE 100
HIGH LEDPIN

MAIN:


LCDOUT $fe,1
Lcdout $fe,2, "START WORKING"
PAUSE 100

LCDOUT $fe,1
GOTO GET_TARGET


GET_TARGET:
LOW DE_OR_RE
HSERIN [ adx1, ady1, adz1, adm1, adn1]
HIGH LED_B7
GOsub COMPARE

Lcdout $fe,$C0, "x= ", DEC ADX1 , "y= ", DEC ADY1, " z=", DEC ADZ1
Lcdout $fe,$D4, "M= ", DEC ADM1 , "N= ",DEC ADN1 ' Send to LCD

END


COMPARE:

If adx1= 127 Then
high portB.0
ELSE
If adx1= 127 Then
low portB.0
ENDIF
ENDIF


If ady1=127 Then
high portB.1
ELSE
If ady1=0 Then
low portB.1
ENDIF
ENDIF

If adz1=127 Then
high portB.2
ELSE
If adz1=0 Then
low portB.2
ENDIF
ENDIF

If adm1=127 Then
high portB.3
ELSE
If adm1= 0 Then
low portB.3
ENDIF
ENDIF

If adn1=127 Then
high portB.5
ELSE
If adn1=0 Then
low portB.5
ENDIF
ENDIF
return