【问题标题】:PIC16F887 Timer0 counting overflowsPIC16F887 Timer0 计数溢出
【发布时间】:2013-11-16 15:11:13
【问题描述】:

我正在使用 PIC16F887 44 引脚演示板。

我正在尝试这样做

  1. 用户按下开关
  2. 定时器启动
  3. 如果在 3 秒内再次按下开关,则打开 LED 0,否则打开 LED 1

由于 Timer0 的预分频器设置为 255,将在 65536 μs 后溢出,我需要计算它溢出的次数并检查它是否达到该数量。 那将是 46 次。

这些是我的代码的相关摘录

movlw     B'10000111'         ; configure Prescaler on Timer0, max prescale (/256)
movwf     OPTION_REG          ; configure
MOVLW     B'10100000'         ; enable Timer 0 and global interrupts
MOVWF     INTCON

MainLoop:
BTFSS     PORTB,0             ; is the switch pressed (0)
GOTO      EndMainLoop         ; Lights up LED 0
MOVF      TimerCount, w                 
XORLW     .46                 ; Check whether XOR TimerCount with 46
BTFSS     STATUS,Z            ; returns a 0
GOTO      MainLoop
GOTO      State2              ; Lights up LED 1

TimerCount 在代码的中断部分中递增,如下所示

org 4
ServiceTimer0:
 bcf       STATUS,RP0          ; Ensure ISR executes in Register Bank 0
 bcf       STATUS,RP1
 BCF       INTCON,T0IF         ; clear the interrupt flag.
 INCF      TimerCount,f        ; Increment TimerCount
 RETFIE                        ; Return from the interrupt

但是 Timer0 永远不会溢出,并且由于某种原因永远不会调用中断例程。 有谁知道我做错了什么?

【问题讨论】:

    标签: assembly pic microchip


    【解决方案1】:

    它不能工作,因为你没有选择正确的记忆库! 试试……

    movlw     B'10000001'         ; configure Prescaler on Timer0, max prescale (/256)
    BANKSEL   OPTION_REG
    movwf     OPTION_REG          ; configure
    movlw     B'10100000'         ; enable Timer 0 and global interrupts
    movwf     INTCON
    BANKSEL   0
    ...
    

    而且您的 ISR 不会保存和恢复 STATUS 标志,因此您的程序无法正常工作!

    【讨论】:

      猜你喜欢
      • 2011-08-04
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      • 2022-01-21
      • 2019-06-15
      • 2014-02-23
      相关资源
      最近更新 更多