Multi-Button CSM with Pic16LF722A


Closed Thread
Results 1 to 32 of 32

Hybrid View

  1. #1
    Join Date
    Apr 2016
    Location
    Mi, USA
    Posts
    24


    Did you find this post helpful? Yes | No

    Default Re: Multi-Button CSM with Pic16LF722A

    Hello again!

    If I want to change the order of the keys so that key0 = 1/0 (RB2), key1 = down(RB1), and key2 = up(RB0) do I need to un-remark the
    ;key0=portb.0=ch0, key1=portb.1=ch1, key2=portb.2=ch2 statement and change it, or do I need to change the code in the main portion of the program?

    Is there any way I can get the 1/0 button to toggle between off and full pwm, right now I am only getting a momentary break in the pwm when it is touched.

    In the debug setup, all I need to do is monitor the pin specified with Serial Communicator or Hyperterminal?

    I still can't believe how simple you made this look compared to my listing posted above. Thanks again!

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,644


    Did you find this post helpful? Yes | No

    Default Re: Multi-Button CSM with Pic16LF722A

    If I want to change the order of the keys so that key0 = 1/0 (RB2), key1 = down(RB1), and key2 = up(RB0) do I need to un-remark the
    ;key0=portb.0=ch0, key1=portb.1=ch1, key2=portb.2=ch2 statement and change it, or do I need to change the code in the main portion of the program?
    comments are just that ,they only serve to show what it is that the programmer decides need an explanation . they don't get compiled or participate in the execution of the code in any way. to change the way the keys are interpreted requires a change the code .


    right now I am only getting a momentary break in the pwm when it is touched
    my mistake the pw var should also be set to zero too when killing the light

    In the debug setup, all I need to do is monitor the pin specified with Serial Communicator or Hyperterminal
    I use a pickit2 to pgm/debug with ,the debug statements suit my setup . if you use a real rs232 port to debug then a rs232/ttl level converter needs to be used and the logic level may need to be inverted [rtfm]

    Is there any way I can get the 1/0 button to toggle between off and full pwm,
    should be easy , hint "FINITE STATE MACHINE"


    what did your "threshold" count need to be ?
    why not explore a "limited slew rate filter" to automatically track and adjust the threshold value . or more reliable yet, automatically track and adjust the threshold value for each individual key
    Warning I'm not a teacher

  3. #3
    Join Date
    Apr 2016
    Location
    Mi, USA
    Posts
    24


    Did you find this post helpful? Yes | No

    Default Re: Multi-Button CSM with Pic16LF722A

    Hi,

    Thanks for the info. Right now I am using 16000 as a threshold, and it seems to be working well. I have a pickit2, maybe I'll try that. I also have a usb to serial in both 3v and 5v versions. I've been looking at the Microchip application notes concerning touch applications and ran across what you are talking about concerning the threshold value for each key. Maybe once I get into the debugging, I can determine if I will need to do more than a fixed threshold. Thanks again!

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,644


    Did you find this post helpful? Yes | No

    Default Re: Multi-Button CSM with Pic16LF722A

    There is a "pause 300" that you have in the MAIN program area that delays the led indicator change from the time that the button is pressed. I tried to move the code
    the pause is there to slow the key action to human speeds ,the indicator leds can go in the key pressed loop ok as far as I can see


    What is "if PW.15 then PW=0" doing? Is it checking bit 15 of PW to be high?

    with any subtraction if the result is negative (ie overflowed) then the highest order bit of the result is always set.


    ps can porta.3 be an output ?



    have a try like this

    Code:
    '#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 16000 '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
     p_level var byte
    
     DEFINE DEBUG_REG PORTB
     DEFINE DEBUG_BIT 7 ;pgd
     DEFINE DEBUG_BAUD 9600
     DEFINE DEBUG_MODE 0 
    
    
     OSCCON = %00010000 '4 mhz
     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=1023
     p_level=5
     t2con=7 
     LED1 VAR PORTA.1 ' SET LED LEVEL INDICATORS
     LED2 VAR PORTA.2
     LED3 VAR PORTA.3  ;?????????????????????????????????
     LED4 VAR PORTA.4
     LED5 VAR PORTA.5
    
     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=255 'SET TO MAX BRIGHTNESS
     FLG=0 
     key_inx=0
    
    
    
     MAIN:
     IF FLG=1 THEN 
     IF KP = 0 THEN '1/0 BUTTON PRESSED
     ccp1con=0 ;kill it
     led=0
     p_level=0
     ELSEif KP = 1 THEN 'UP BUTTON PRESSED
     p_level= (p_level+1) min 5 
     ;PW= (PW+256) MIN 1023 
     else 'DOWN BUTTON PRESSED
     p_level= p_level-1
     ;PW = PW-256 
     IF p_level.7 THEN p_level=0 
     ;IF PW.15 THEN PW=0 
     
     
     
     lookup p_level, [0,12,48,218,586,1023],pw  'gamma 1.35,5 levels plus OFF
     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
     
     porta=0  ;all leds off
     IF p_level= 5 THEN
     HIGH LED5
     ENDIF
     IF p_level=4 THEN
     HIGH LED4
     ENDIF
     IF p_level=3 THEN
     HIGH LED3
     ENDIF
     IF p_level=2 THEN
     HIGH LED2
     ENDIF
     if p_level=1 THEN 
     high LED1
     endif
    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
    Last edited by richard; - 19th May 2016 at 00:12. Reason: ps
    Warning I'm not a teacher

  5. #5
    Join Date
    Apr 2016
    Location
    Mi, USA
    Posts
    24


    Did you find this post helpful? Yes | No

    Default Re: Multi-Button CSM with Pic16LF722A

    Hello again,

    Okay I got the buttons to work as i wished, and I changed to 4 pwm levels. However, I have added code to indicate which of the 4 levels I am currently at.
    There is a "pause 300" that you have in the MAIN program area that delays the led indicator change from the time that the button is pressed. I tried to move the code for the led indicators to a point before the "pause 300", but then the program quits working. Is this pause what determines the time between steps if a[ button is continuously pressed, or does it serve another purpose? Another dumb thing, if I want 5 levels plus off, as shown in my previously posted example, how do I do it? Currently I am using a value of 256 from the original 16 as you had to get 4 steps, but that works out pretty evenly.

    What is "if PW.15 then PW=0" doing? Is it checking bit 15 of PW to be high? Please see cod attached.

    Thanks.


    [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 16000 '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 '4 mhz
    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=1023
    t2con=7
    LED1 VAR PORTA.1 ' SET LED LEVEL INDICATORS
    LED2 VAR PORTA.2
    LED3 VAR PORTA.3
    LED4 VAR PORTA.4
    LED5 VAR PORTA.5

    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=255 'SET TO MAX BRIGHTNESS
    FLG=0
    key_inx=0



    MAIN:
    IF FLG=1 THEN
    IF KP = 0 THEN '1/0 BUTTON PRESSED
    ccp1con=0 ;kill it
    led=0
    PW=0
    ELSEif KP = 1 THEN 'UP BUTTON PRESSED
    PW= (PW+256) MIN 1023
    else 'DOWN BUTTON PRESSED
    PW = PW-256
    IF PW.15 THEN PW=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

    IF pw = 1023 THEN
    HIGH LED5
    ENDIF
    IF pw = 768 or PW = 767 THEN
    HIGH LED4
    ENDIF
    IF pw = 512 or PW = 511 THEN
    HIGH LED3
    ENDIF
    IF pw = 256 or PW = 255 THEN
    HIGH LED2
    ENDIF
    if pw = 0 THEN
    high LED1
    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][/CODE]

Similar Threads

  1. Replies: 5
    Last Post: - 18th March 2012, 21:40
  2. multi functions button press
    By malwww in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 27th May 2009, 00:12
  3. Multi-Threading
    By Ted's in forum mel PIC BASIC Pro
    Replies: 22
    Last Post: - 2nd September 2008, 05:55
  4. Multi-tasking
    By malc-c in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 13th July 2006, 18:50
  5. Multi Interrupt How To ?
    By capitano in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 3rd February 2005, 14:48

Members who have read this thread : 3

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts