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
Bookmarks