我试图使用CTC (Clear Timer on Compare)方法来使用计时器,但我无法写入寄存器以尝试启用该模式。完整的数据表可以在这里找到。P.144解释了这个想法是如何工作的,但没有说明如何在代码中实际实现。
我尝试过使用OCR2A(输出比较寄存器),试图设置一个值,但在试图设置其值时得到一个构建错误。
在尝试设置OCR2A或TCCR2A的值时,我得到了一些错误。错误为Operand 1 out of range
或Invalid Register
任何建议或指导都会很有帮助
.def AllOnes = R16
.def ChargeState = R17
.def AllZeros = R18
.def PushButn = R19
.def TimerValue = R20
MAIN:
LDI AllOnes, 0xFF ; assign 1 - make an output
LDI ChargeState, 0x00 ; start with all the LEDS ON; Holds the Light Pattern
LDI AllZeros, 0x00 ; assign 0 - make an input
LDI PushButn, 0x00 ; to hold the value read from PORTB0
SBI COM2A1,0 ; set the Output compare pin for
SBI TIFR2, 1 ; set the Output compare Flag; i.e OCF2A
;According to the breakout board, PORTB5 is connected on spot 13 on the board
OUT DDRD, AllOnes ;set PORT D as an output
;make PORTB an input
OUT DDRB, AllZeros ;set PORT B as an input
;Start by turning all LEDS OFF
OUT PORTD, ChargeState
OFFSTATE: ;state for when the lights are off
;check if the button has been pressed.
IN PushButn, PINB
SBRC PushButn, 0
JMP OFFSTATE
JMP CHARGING
CHARGING:
LDI ChargeState, 0x01
OUT PORTD, ChargeState
;display the state of the first charge state
;initialize the value for the Output Comare Register (OCR2A)
;SBI OCR2A, 0
LOOP:
CP TCCR0A, COM2A1
JMP LOOP
我想答案是:没有指示去做。参见数据表中的寄存器摘要和指令集摘要表。SBI OCR2A,0不能使用,因为OCR2A在上地址寄存器空间。与SBI TIFR2、1相比,TIFR1位于较低的空间。同样,CP TCCR0A, COM2A1也不能与IO寄存器进行比较。使用
MOV R0,TCCR0A
CPI R0,COM2A1
。对不起,用心写的,希望语法正确
原来TCCR0A有COM2A1,这是一个输出寄存器。要写入它,必须使用OUT