完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
|
相关推荐
1个回答
|
|
作为一个简单实用的示例,图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 ;----------------------------------------------------------------------------- |
|
|
|
只有小组成员才能发言,加入小组>>
722 浏览 0 评论
1123 浏览 1 评论
2505 浏览 5 评论
2840 浏览 9 评论
移植了freeRTOS到STMf103之后显示没有定义的原因?
2685 浏览 6 评论
使用eim外接fpga可是端口一点反应都没有有没有大哥指点一下啊
664浏览 9评论
679浏览 7评论
682浏览 5评论
666浏览 5评论
imx6dl芯片与imx6q芯片互换启动无反应请问是什么问题呢
287浏览 5评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-30 23:38 , Processed in 1.133950 second(s), Total 79, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号