<var id="fnfpo"><source id="fnfpo"></source></var>
<rp id="fnfpo"></rp>

<em id="fnfpo"><object id="fnfpo"><input id="fnfpo"></input></object></em>
<em id="fnfpo"><acronym id="fnfpo"></acronym></em>
  • <th id="fnfpo"><track id="fnfpo"></track></th>
  • <progress id="fnfpo"><track id="fnfpo"></track></progress>
  • <tbody id="fnfpo"><pre id="fnfpo"></pre></tbody>

  • x
    x

    利用單個PIC輸入監控15個觸點

    發布時間:2015-1-26 14:52    發布者:designapp
    關鍵詞: PIC微控制器 , LED顯示器 , 匯編

            作為一個簡單實用的示例,圖1中的電路利用一個8位PIC微控制器控制一個4位LED顯示器,顯示出按下了哪個按鈕。當任意按鈕被按下時,比較器中斷程序會立即作出響應。該程序會在VREF值之間進行循環,直至比較器輸出COUT返回高電平,表明該按鈕被按下。完整注釋的匯編程序源代碼總共不到100字。該代碼并未作過度優化,從而可方便理解或易于轉化到其他控制器上。




    匯編程序源代碼:
    ; MULTIBTN.ASM: sensing upto 15 pushbuttons with one I/O (pin6: GP1/CIN-)
    ; BENABADJI Noureddine - ORAN - Dec. 11...14th, 2013
    ;
            Errorlevel -302                ; avoid warning #302: Register in operand not in bank0.
                                                    ; Ensure that bank bits are correct.
            List P = 12F683                      
    #include "p12f683.inc"                        
            __CONFIG    _INTOSCIO&_MCLRE_OFF&_PWRTE_ON&_WDT_OFF&_FCMEN_OFF&_IESO_OFF&_BOD_ON&_CPD_OFF&_CP_OFF
    #define         LED1        GPIO, 0        ; output bit0 of the 4-bit binary LED display
    #define         LED2        GPIO, 2        ; output bit1 of the 4-bit binary LED display
    #define         LED3        GPIO, 4        ; output bit2 of the 4-bit binary LED display
    #define         LED4        GPIO, 5        ; output bit3 of the 4-bit binary LED display
    ;----------------------------- define variables ------------------------------
            CBLOCK        0x20        ; bank0 = [0x20...0x7F] = 94 bytes
    ;delay
            cnt1, cnt2, cnt3 ; counters
            ENDC
    ;------------------------------------------------------------------------------
    BANK0        macro
            BCF        STATUS, RP0                 ; bank0
            endm
    ;------------------------------------------------------------------------------
    BANK1        macro
            BSF        STATUS, RP0                 ; bank1
            endm
    ;------------------------------------------------------------------------------
    SIregGEval8b        macro        file, val, jmpOk        ; if (file >= val) goto jmpOk ;
            MOVLW        val
            SUBWF        file, w                ; w = file - val
            BTFSC        STATUS, C
            GOTO        jmpOk                ; yes
            endm
    ;/////////////////////////////////////////////////////////////////////////////
    ;        MAIN PROGRAM
    ;/////////////////////////////////////////////////////////////////////////////
            ORG                0x00                        ; reset vector
            GOTO        Setup
            ORG                0X04                        ; interrupt vector
            GOTO        IntCmp
    ;/////////////////////////////////////////////////////////////////////////////
    ;-----------------------------------------------------------------------------
    LEDsOFF
            CALL        Delay256ms
            CLRF        GPIO        ; all LEDs off
            RETLW        0
    ;-----------------------------------------------------------------------------
    Delay256ms
            CLRF        cnt2
            CLRF        cnt1
            NOP                                ; 1us
            DECFSZ        cnt1, f        ; 1us
            GOTO        $-2                ; 2us => 4*256 = 1024 us, approx. 1 ms internal delay loop
            DECFSZ        cnt2, f        ; approx. 256 ms external delay loop
            GOTO        $-4
            RETLW        0
    ;/////////////////////////////////////////////////////////////////////////////
    Setup
            BANK1
            CLRF        TRISIO                 ; config. all I/O as outputs
            BCF        OPTION_REG, T0CS ; use pin5 as GP2, not TOCKI
            CLRF        ANSEL                 ; use all AN as digital I/O
            BANK0
            CLRF        GPIO                ; all LEDs off
            MOVLW        b'00000111'
            MOVWF        CMCON0                 ;comparator off
    splash ; (initial test for LEDs)
            BSF                LED1
            CALL        LEDsOFF
            BSF                LED2
            CALL        LEDsOFF
            BSF                LED3
            CALL        LEDsOFF
            BSF                LED4
            CALL        LEDsOFF
    ;;;;;;;;;;
    initializeComparator
            BANK1
            MOVLW        b'00001010'         ;config. GP1 as input (will be CIN-)
            MOVWF        TRISIO
            ;BANK0
            MOVLW        b'10100001'
            ;BANK1
            MOVWF        VRCON                 ;Vref on, low range, VR=0001 => ratio = 1/24
            BANK0
            MOVLW        b'00000100'
            MOVWF        CMCON0                 ;comparator on: CIN- = GP1; CIN+ = Vref; Cout internal
    ;;;;;;;;;
           
    ;enable interrupt
            BANK1
            BSF        INTCON, PEIE         ; enable interrupt on Comparator trip
            BSF        PIE1, CMIE                 ; enable interrupt on Comparator trip
            BANK0
            BSF        INTCON, GIE                 ; set general interrupt enable
            goto $                                 ; infinite loop (waiting for an interrupt)
    ;-----------------------------------------------------------------------------
    ; Comparator trip interrupt routine
    ;-----------------------------------------------------------------------------
    IntCmp
    ;don't need to save any context, only interrupting a goto $
            BANK0
            MOVLW        .1
            MOVWF        cnt3
    nextBtn
            INCF        cnt3, F
            SIregGEval8b cnt3, .16, whichBtn ; if (cnt3 >= 16) goto whichBtn ;
            MOVLW        b'10100000'
            ADDWF        cnt3, W
            BANK1
            MOVWF        VRCON                 ;Vref on, low range, VR=cnt3
            BANK0
            BTFSS        CMCON0, COUT         ; Cout == 1 ?
            GOTO        nextBtn
    whichBtn
            DECF        cnt3, F
            BTFSC        cnt3, 0
            BSF                LED1
            BTFSC        cnt3, 1
            BSF                LED2
            BTFSC        cnt3, 2
            BSF                LED3
            BTFSC        cnt3, 3
            BSF                LED4
            CALL        LEDsOFF
           
    endIntCmp
            MOVLW        b'10100001'
            BANK1
            MOVWF        VRCON                 ;Vref on, low range, VR=0001 => ratio = 1/24
            BANK0
            BCF        PIR1, CMIF                 ; clear comparator interrupt flag
            RETFIE
    ;-----------------------------------------------------------------------------
            END



    本文地址:http://www.portaltwn.com/thread-145040-1-1.html     【打印本頁】

    本站部分文章為轉載或網友發布,目的在于傳遞和分享信息,并不代表本網贊同其觀點和對其真實性負責;文章版權歸原作者及原出處所有,如涉及作品內容、版權和其它問題,我們將根據著作權人的要求,第一時間更正或刪除。
    Zaxife 發表于 2015-1-27 09:27:20
    你這樣的等比例電阻分壓,多個按鈕同時按下時,會錯誤的認為是氣體按鈕按下的,沒啥意義。
    電阻分組必須按1:2:4:8:16:32:64:128:256:1024:2048......這樣的2的N次方來分配才能確定是哪幾個按鍵按下了。
    您需要登錄后才可以發表評論 登錄 | 立即注冊

    廠商推薦

    • Microchip視頻專區
    • 我們是Microchip
    • Cortex-M4外設 —— TC&TCC結合事件系統&DMA優化任務培訓教程
    • 你仿真過嗎?使用免費的MPLAB Mindi模擬仿真器降低設計風險
    • 更佳設計的解決方案——Microchip模擬開發生態系統
    • 貿澤電子(Mouser)專區

    相關視頻

    關于我們  -  服務條款  -  使用指南  -  站點地圖  -  友情鏈接  -  聯系我們
    電子工程網 © 版權所有   京ICP備16069177號 | 京公網安備11010502021702
    快速回復 返回頂部 返回列表
    精品一区二区三区自拍图片区_国产成人亚洲精品_亚洲Va欧美va国产综合888_久久亚洲国产精品五月天婷