作为一个简单实用的示例,图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
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<3:0>=0001 => ratio = 1/24
BANK0
BCF PIR1, CMIF ; clear comparator interrupt flag
RETFIE
;-----------------------------------------------------------------------------
END |