'****************************************************************
'* Name : Receive.BAS *
'* Author : Jan Kordylewski*
'* Notice : Copyright (c) 2004 freely distributable *
'* : All Rights Reserved *
'* Date : 12/12/2004 *
'* Version : 1.0 *
'* Notes : *
'* : *
'****************************************************************
DEFINE OSC 20

DEFINE HSER_RXSTA 90h ' enable the receive register
DEFINE HSER_TXSTA 20h ' enable the transmit register
DEFINE HSER_BAUD 31250 ' set the baud rate

' 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

ADCvar VAR WORD ' Create variable to store result
Counter Var Word
Target Var word
Change var word
hit var word

output portd.2
OUTPUT portb.6
output portd.1
input portc.1
input portc.2

TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %10000010 ' Set PORTA analog and right justify result
Pause 500 ' Wait .5 second

portd.2 = 1
counter = 127
target = 127
hit = 0

hserout [$90, $3,counter]
hserout [$90, $2,$00]

serout2 portd.1, 16468, ["hello", 10, 13]
pause 500

main:

ADCIN 0, ADCvar ' Read channel 0 to adval
serout2 portd.1, 16468, ["counter = ", DEC counter, 10, 13]
'serout2 PORTC.6, 16468, [DEC ADCvar, 13, 10] ' print it to serial out,
' with linefeed and carriage return (10, 13)
'if portc.1 = 1 then


 
if ((portc.1 = 1) and (portc.2 = 1)) then
hserout [$90, $3,$0F]
hserout [$90, $2,$7F]
if hit = 0 then
hit = 1
endif
'portd.2 = 0
'pause 1000
'portd.2 = 1
serout2 portd.1, 16468, ["both", 10, 13]
endif


if ((portc.1 = 1) and (portc.2 = 0)) then
serout2 portd.1, 16468, ["one", 10, 13]
target = 40
'if counter > 30 then
'counter = counter - 1
'endif
'hserout [$90, $3,counter]
'portd.2 = 1
'hserout [$90, $2,$0]
ELSE
if ((portc.1 = 0) and (portc.2 = 0)) then
Target = 127
hserout [$90, $2,$0]
'portd.2 = 1
endif
endIF

if counter < target then
change = 2
endif

if counter > target then
change = -2
endif

If (counter != target) then
'serout2 portd.1, 16468, ["neither", 10, 13]
serout2 portd.1, 16468, ["my counter=",DEC counter, 10, 13]
'portd.2 = 1
'hserout [$90, $2,$0]
counter = counter + change
hserout [$90, $3,counter]
pause 100
endIF

if hit > 0 then
portd.2 = 0
hit = hit +1
if hit > 1000 then
hit = 0
portd.2 = 1
endif
endif

GoTo main ' Do it forever