RichardT1GATE2

Code:
'****************************************************************
'*  Name    : CAPSENSE.BAS                                      *
'*  Author  : RICHARD                                           *
'*  Notice  : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                               *
'*  Date    : 10/20/2011                                        *
'*  Version : 1.0                                               *
'*  Notes   :                                                   *
'*          :     16LF722A                                     *
 '                                                              *
'****************************************************************

'#DEFINE DBUG 1                            
#CONFIG
         __config _CONFIG1, _INTRC_OSC_NOCLKOUT & _WDTE_OFF & _BOR_OFF & _PLLEN_ON
#ENDCONFIG

define OSC 4
include "dt_ints-14.bas"
include "REENTERPBP.bas"
'      variables used by interrupt handler, comment out as per assembler
wsave   VAR BYTE    $20     SYSTEM       ' location for W if in bank0
'wsave   VAR BYTE    $70     SYSTEM      ' alternate save location for W 
                                         ' if using $70, comment wsave1-3
' --- IF any of these three lines cause an error ?? ------------------------
'       Comment them out to fix the problem ----
' -- Which variables are needed, depends on the Chip you are using -- 
wsave1  VAR BYTE    $A0     SYSTEM       ' location for W if in bank1
'wsave2  VAR BYTE    $120    SYSTEM      ' location for W if in bank2
'wsave3  VAR BYTE    $1A0    SYSTEM      ' location for W if in bank3
asm
INT_LIST macro
      INT_HANDLER T1GATE_INT, _TB1, PBP,YES
      endm
      INT_CREATE
ENDASM

 THRESH CON 8000   'needs to be established

 FLG     VAR BYTE    'GOT A KEY 
 KP      VAR BYTE     'WHICH KEY  pressed
 key_inx var byte     ;key index
 PW      VAR WORD   ; PULSEWIDTH

DEFINE DEBUG_REG PORTB
DEFINE DEBUG_BIT 7         ;pgd
DEFINE DEBUG_BAUD 9600
DEFINE DEBUG_MODE 0             


   OSCCON = %00010000
   CPSCON0 = %10001100      'high range
   CPSCON1 = 0 'cps ch 0
   ANSELB = %00000111      'SETS INPUTS B0-B2 AS ANALOG
   TRISB = %01111111       'SETS INPUTS B0-B5 AS INPUTS  b7 output
   WPUB = %00000000        'DISABLES WEAK PULL-UP BITS
   TRISC = %11111011        'ccp1 output portc.2

   OPTION_REG=$86      'edge int. prescale 1:128
   PIE1.7=1   'timer1 gate interrupt
   T1GCON=$f9    ' single shot timer0 overflow as input
   T1CON=%11000101   'capsense as input no prescale no sync
   INTCON= $C0       'peripheral interrupts
   PW=512
   t2con=7 
   LED VAR portc.2
   ;up                down              off
   ;key0=portb.0=ch0, key1=portb.1=ch1, key2=portb.2=ch2



#IFDEF  DBUG 

    portb.7=1
    LED=1 
    pause 2000
    Debug "Start",13 ,10
    LED=0
    pause 500 
#ENDIF 
   ccp1con=12
   CCPR1L=128
   FLG=0 
   key_inx=0



MAIN:
    IF FLG=1 THEN 
        IF KP = 0 THEN
           PW= (PW+64)  MIN 1023
        ELSEif  KP = 1 THEN     
           PW = PW-64
           IF PW.15 THEN PW=0 
         else  'MUST BE THE OTHER ONE 
            ccp1con=0       ;kill it
            led=0
        ENDIF
        CCPR1L = PW>>2;
        ccp1con=12|((PW&3)<<4);
        #IFDEF  DBUG 
            debug "KEY ",#KP,13,10 
            debug #PW,13,10 
        #ENDIF
        PAUSE 300
        FLG=0       'clr    key pressed flag
     ENDIF   

GOTO MAIN 


TB1: 
 #IFDEF  DBUG 
  debug #TMR1,13,10
 #ENDIF
 IF TMR1 <  THRESH  THEN    '   KEY DETECTED  
  if !flg then
   KP=key_inx 
   FLG=1         'set key pressed flag
  endif
 ENDIF
 key_inx=key_inx+1   ;check next key
 if key_inx > 2 then key_inx=0 
 CPSCON1  = key_inx
 TMR1=0
 T1GCON.3= 1 ;single shot reset


@ INT_RETURN
Page last modified on April 01, 2018, at 11:52 PM