我正在使用Atmel Studio 6.1和带有Atmega328P微控制器的Arduino Uno板。下面是我的代码和我硬件的图片。我无法弄清楚这是硬件还是软件问题......所有LED都会启动。按下按钮时,应该改变为确定的模式。再次按下按钮会导致显示不同的图案。我不能使用C而我无法使用实时调试,因为我没有JTAG或其他一些调试支持的接口。最终发生的事情是按下按钮,我正在缩短电路并重置电路板的电源。奇怪的是,模式改变了一次,但后来再也没有改变。
.def counter = R23
.def TimeLoopMax = R24
.def AllOnes = R16
.def DisplayPattern = R17
.def AllZeros = R18
MAIN:
LDI AllOnes, 0xFF ; assign 1 - make an output
LDI DisplayPattern, 0x00 ; start with all the LEDS ON; Holds the Light Pattern
LDI AllZeros, 0x00 ; assign 0 - make an input
LDI r19, 0x00 ; to hold the value read from PORTB0
LDI counter, 0x00 ; value for counter
LDI TimeLoopMax, 0x70
;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, DisplayPattern
LOOP:
;read in the value from PORTB0 (ie the push button)
IN r19, PORTB0
;CPI r19, 0x05 ; might need to do a compare; compare with 5 since we're using 5V
CP r19, AllZeros
BRNE LOOP
JMP IncreasePatternCounter
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Blinking Pattern Definitions ;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;This defines a blinking sequence for Pattern 0
P0:
;P0, start at a value of 1, and shift the bit to the MSB
LDI DisplayPattern, 0x01
P0_LOOP:
OUT PORTD, DisplayPattern
LSL DisplayPattern ;Logic shift Left
;need to check butotn press every time the light switches
; since we are not using interrupts
/*IN r19, PORTB0
SBRC r19, 0 ;skip if Bit in Reg. is clear
JMP IncreasePatternCounter*/
JMP TimeWastingLoop
;This defines a blinking sequence for Pattern 1
P1:
;P1, start at a value of AA, and shift the bit to the MSB
LDI DisplayPattern, 0xAA
P1_LOOP:
OUT PORTD, DisplayPattern
LSL DisplayPattern
/*IN r19, PORTB0
SBRC r19, 0
JMP IncreasePatternCounter*/
JMP TimeWastingLoop
;This defines a blinking sequence for Pattern 1
P2:
;P1, start at a value of AA, and shift the bit to the MSB
LDI DisplayPattern, 0xFF
P2_LOOP:
OUT PORTD, DisplayPattern
LSR DisplayPattern
/*IN r19, PORTB0
SBRC r19, 0
JMP IncreasePatternCounter*/
JMP TimeWastingLoop
;This defines a blinking sequence for Pattern 1
P3:
;P1, start at a value of AA, and shift the bit to the MSB
LDI DisplayPattern, 0x00
P3_LOOP:
OUT PORTD, DisplayPattern
INC DisplayPattern
/*IN r19, PORTB0
SBRC r19, 0
JMP IncreasePatternCounter*/
JMP TimeWastingLoop
IncreasePatternCounter:
INC counter
SequenceSelect:
CPI counter, 0x01
BREQ P0;
CPI counter, 0x02
BREQ P1;
CPI counter, 0x03
BREQ P2
CPI counter, 0x04
BREQ P3
JMP P0
; Time wasting loop registers: r20,21,22
; R16 is all 1's
TimeWastingLoop:
LDI R20, 0x05
LDI R21, 0x05
LDI R22, 0x04
OutMostLoop:
CP TimeLoopMax, R20;(TLM - R20)
BREQ EndLoop
FirstInnerLoop:
/*IN r19, PORTB0
SBRC r19, 0
JMP IncreasePatternCounter*/
CP TimeLoopMax, R21; (TLM - R21)
BREQ EndOutMostLoop
SecondInnerLoop:
IN r19, PORTB0
SBRC r19, 0
JMP IncreasePatternCounter
CP TimeLoopMax, R22; (TLM - R22)
BREQ EndSecondInnerLoop
INC R22
JMP SecondInnerLoop
EndSecondInnerLoop:
CLR R22 ;reset the register for the next pass
INC R21 ; increment 1st inner loop counter
JMP FirstInnerLoop
EndOutMostLoop:
CLR R21
INC R20
JMP OutMostLoop
EndLoop:
;this is where we do the compare statements with counter
;Need to perform a compare here to see which loop we bounce back in
CPI counter, 0x01
BREQ P0_TWL ; Pattern0 Time Wasting Loop
CPI counter, 0x02
BREQ P1_TWL
CPI counter, 0x03
BREQ P2_TWL
;OUT PORTD, counter
P0_TWL:
CPI DisplayPattern, 0b00000000
BREQ BtnPressCheckP0
BRNE P0_LOOP
P1_TWL:
CPI DisplayPattern, 0b00000000
BREQ P1
BRNE P1_LOOP
P2_TWL:
CPI DisplayPattern, 0b00000000
BREQ P2
BRNE P2_LOOP
BtnPressCheckP0:
IN r19, PORTB0
SBRC r19, 0
JMP IncreasePatternCounter
JMP LOOP