'****************************************************************

'*  Name    : UNTITLED.BAS                                      *

'*  Author  : [select VIEW...EDITOR OPTIONS]                    *

'*  Notice  : Copyright (c) 2004 [select VIEW...EDITOR OPTIONS] *

'*          : All Rights Reserved                               *

'*  Date    : 4/27/04                                           *

'*  Version : 1.0                                               *

'*  Notes   :                                                   *

'*          :                                                   *

'****************************************************************

' PicBasic Pro program to display result of

' 10-bit A/D conversion through serial at 9600 baud

' Connect analog input to channel 0 (RA0)

INCLUDE "modedefs.bas"

  ' Set Debug pin port

DEFINE DEBUG_REG PORTC

  ' Set Debug pin BIT

DEFINE DEBUG_BIT 6

  ' Set Debug baud rate

DEFINE DEBUG_BAUD 9600

  ' Set Debug mode: 0 = true, 1 = inverted

DEFINE DEBUG_MODE 1

 

  ' 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

 

 

AdcVar0  VAR WORD

AdcVar1  VAR WORD

AdcAverage VAR WORD

TimeOnVar VAR WORD

MaxTimeVar VAR WORD

SensorOnVar VAR WORD

 

 

OUTPUT portc.4

OUTPUT portb.4

OUTPUT portb.5

 

TRISA = %11111111       ' Set PORTA to all input

ADCON1 = %10000010      ' Set PORTA analog and right justify result

 

TImeonvar = 0 'initialize this variable

SensorOnVar=0

portc.4=0

 

Pause 500               ' Wait .5 second

 

main:

 

ADCIN 0, adcVar0 ' Read channel 0

ADCIN 1, adcVar1 ' Read channel 1

 

AdcAverage = (adcVar0+adcVar1)/2

 

 

 

if adcaverage > 100 and SensorOnVar=0 then

    pause 1000

    ADCIN 0, adcVar0 ' Read channel 0

    ADCIN 1, adcVar1 ' Read channel 1

    AdcAverage = (adcVar0+adcVar1)/2

    TimeOnVar=1

    portb.5=1 ' turn green led on

    portb.4=0 ' turn red led off

    MaxTimeVar = (1024-AdcAverage)*3

    SensorOnVar=1

 

    'determine millisecs based on adc val

    HIGH portc.4

endif

 

IF TimeOnVar>MaxTimeVar THEN

    'When max time for fsr value is

    'reached, turn off and reset all

    LOW portc.4

    timeOnVar = 0

ELSE

    TimeOnVar=TimeOnVar+1

ENDIF

 

IF AdcAverage<80 THEN

    SensorOnVar=0

    portb.5=0 ' turn green led off

    if timeOnVar = 0 then

        low portc.4

    endif

    portb.4=1 ' t urn red led on

ENDIF

 

 

 

Debug DEC AdcAverage, 10,13  ' print it to serial out, with linefeed

'and carriage return (10, 13)

Pause 30                ' Wait.03 second

GoTo main                   ' Do it forever