Long / short press button...but reading ADC


Closed Thread
Results 1 to 38 of 38

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default Re: Long / short press button...but reading ADC

    I've done multi-button with long press (and timed no-button auto-backout) on a '88 using ADC to save pins, and it worked fine, but field EMF noise can wreak absolute havoc on your results (fluorescent lights much?) if you're not using twisted, and preferably shielded wire. That could be what's throwing you out of your "consistent" results needed to qualify for a long-press. Ceradiodes or double-zeners can bleed off freak transients, and a capacitor at the chip A/D input pin would help smooth things out.

  2. #2
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default Re: Long / short press button...but reading ADC

    @Mr.Richard : Tried to figure out how your soft work ... But in Proteus nothing happens.
    I do something wrong ? I "addapted" the "elseif" command, since is not present in PBP 2.6 (?).
    Thanks in advance for support !
    Code:
    ;pic12f683
    
    DEFINE OSC  8
    include "dt_ints-14.bas"
    include "REENTERPBP.bas"
    
    @timer1 =TMR1L
    wsave   VAR BYTE    $20     SYSTEM      ' location for W if in bank0
    wsave1  VAR BYTE    $A0     SYSTEM      ' location for W if in bank1
    
    ASM
    INT_LIST  macro    ; IntSource,        Label,           Type,   ResetFlag?
            INT_Handler   TMR1_INT,   _chk_sw,      PBP,  yes
         endm
        INT_CREATE      
    ENDASM     
    
    long_press_threshold   con 200   ;1 sec       200*5( time const)=1000mS
    short_press_threshold  con 5 
    btn_tol                con 25   ;ADC READING TOLORACE
    number_of_buttons      con 3    ;MAX 7       
    ;4K7 TO 5V                                         
    '                               3k3            /  
    '                         ___  /\  /\  /\  ___/ ^____ btn1
    '                        |   \/  \/  \/  \/          |
    '   5v     4k7           |       2k2           /     |
    '    ___  /\  /\  /\  ___!___  /\  /\  /\  ___/ ^____!btn2
    '       \/  \/  \/  \/   |   \/  \/  \/  \/          |
    '                        |       1k            /     |
    '    ____________________!___  /\  /\  /\  ___/ ^____!btn3
    '    an3  gpio4              \/  \/  \/  \/          |
    '                                                    |
    '    ________________________________________________! 
    '    gnd                                             |
    '    ___________|\|__led_____________________________|
    '    gpio2      |/|
    
    OSCCON=$70
    CMCON0    = 7
    TRISIO   = %111011
    ANSEL    = %01011000        ;fosc/16 ,an3
    btn_adc var word[number_of_buttons]
    b_cnt   var byte[number_of_buttons]
    b_state var byte[number_of_buttons]     ;0 not pressed ,1 short ,2 long
    button_index  var byte      ;which button
    led           var gpio.2    ;led
    btn_flg       var byte 
    abp           var BIT  
    adcr          var WORD 	    ;adc reading
    timer1        VAR WORD EXT
    tmp           var word     
    FLASH_ON      VAR WORD      ;led
    FLASH_OFF     VAR WORD      ;led
    FLASH_DUTY    VAR WORD      ;led
    led = 0 
    clear
    
    btn_adc[0]=209           ;adc read for buttons
    btn_adc[1]=327           ;adc read for buttons
    btn_adc[2]=422           ;adc read for buttons
    
    timer1=55543             ;5mS  time const
    T1CON=1
    @ INT_ENABLE  TMR1_INT
    
    FLASH_OFF=200
    FLASH_On=0
    ADCON0 = %10001101     ;warm up the adc  ,right shift   ,an3
    ADCON0.1 = 1            ' Start a conversion
    
    Main:
       Pause 5   ;loop time const  
        IF ABP THEN 
           button_index= 0
           tmp=20
          
           while abp     
               if  B_STATE[button_index]=2 then
                   FLASH_OFF= tmp
                   abp=0
                   led=0
                   FLASH_DUTY=0
               else 
                   if   B_STATE[button_index]=1 then
                   FLASH_On=tmp
                   abp=0
                   led=0
                   FLASH_DUTY=0   
                   endif
               endif
               tmp=tmp+100
               B_STATE[button_index]=0
               button_index=button_index+1
               if  button_index= number_of_buttons then abp=0  ;clr error  if req 
            wend          
        ENDIF  
         IF  FLASH_ON THEN
           IF FLASH_DUTY=0 THEN
           led = ! LED     ; flash led
            	IF LED THEN
             	FLASH_DUTY = FLASH_ON
            	ELSE
             	FLASH_DUTY = FLASH_OFF
            	ENDIF
           ELSE
           FLASH_DUTY=FLASH_DUTY-1
           ENDIF       
         ENDIF        
        
    Goto Main     
       
    chk_sw:
        T1CON.0=0
        timer1=timer1+55543 
        T1CON.0=1
        ;ADCON0 = %10001101 ; NO NEED since ch never changes and ADC is left ON 
        ;Pauseus 50		'  aqusition  time  if req
        ;ADCON0.1 = 1		' Start conversion
        ;While ADCON0.1=1:Wend	' Wait for conversion
        adcr.HighByte = ADRESH	' Read variable from ADC 
        adcr.LowByte  = ADRESL
        FOR button_index = 0 TO 2
              if ! B_STATE[button_index] then
                  IF    abs (adcr- btn_adc[button_index] )< btn_tol  then
                       if  btn_flg.0[button_index]  THEN
                            IF b_cnt[button_index] < 250 THEN  b_cnt[button_index]=b_cnt[button_index]+1   
                            ELSE   ;FIRST PRESS  FOR  THIS BUTTON 
                            b_cnt[button_index]=0
                            btn_flg.0[button_index]=1
                            ENDIF            
                       ELSE
                       IF   btn_flg.0[button_index]  THEN    ;  HAD   THIS BUTTON  BEEN PRESSED  ?
                           IF   b_cnt[button_index] > long_press_threshold THEN                                           
                           B_STATE[button_index]=2
                           abp=1 
                           ELSE
                           IF   b_cnt[button_index] > short_press_threshold THEN  
                           B_STATE[button_index] =1
                           abp=1
                           ENDIF 
                       endif
                       btn_flg.0[button_index]=0   
                  ENDIF
              ENDIF
              endif
        next 
        ADCON0.1 = 1     ' Start conversion  for next pass
    @ INT_RETURN
    Name:  TEST.jpg
Views: 7069
Size:  119.3 KB

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,645


    Did you find this post helpful? Yes | No

    Default Re: Long / short press button...but reading ADC

    Tried to figure out how your soft work ... But in Proteus nothing happens
    did you try it on a real chip ?
    Warning I'm not a teacher

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


    Did you find this post helpful? Yes | No

    Default Re: Long / short press button...but reading ADC

    the led should have a series resistor to limit current , I assumed that would be obvious and omitted it from the sketch
    @ 5v try 180 ohm for a yellow led
    Warning I'm not a teacher

  5. #5
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default Re: Long / short press button...but reading ADC

    Thanks for reply ! Proteus work fine even without resistor for LED ... I wonder if my "adaptations" of code are corect.

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,645


    Did you find this post helpful? Yes | No

    Smile Re: Long / short press button...but reading ADC

    your adaptation works fine when complied with pbp3 [on a real chip , none of this simulator nonsense ], can't vouch for pbp2.6
    would recommend an upgrade
    Warning I'm not a teacher

  7. #7
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default Re: Long / short press button...but reading ADC

    I see ... Thank you !
    LE : Please, can share asm & hex file, as results of PBP3 compiled ?

Similar Threads

  1. Replies: 5
    Last Post: - 26th February 2011, 05:51
  2. Button press or not
    By lerameur in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 24th November 2010, 20:37
  3. 4 Bytes one button press
    By Dennis in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 15th January 2010, 22:36
  4. Sleep until button press?
    By kevj in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 9th October 2007, 03:47
  5. Button press and press & hold how to ?
    By GrandPa in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 22nd August 2007, 03:37

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