【问题标题】:Reading state of push button to turn on LED in AVR using Atmega32使用 Atmega32 读取 AVR 中打开 LED 的按钮状态
【发布时间】:2020-07-01 01:11:09
【问题描述】:

我有一个程序应该从连接到 PORTA 的 DIP 开关读取输入值,当按下 PC0 上的按钮时,它会将位输出到 PORTB 上的 LED,或者如果按下 PC7,它会显示在港口。当按下 PC3 时,它应该将两个字节相乘以存储为高字节和低字节的 16 位值。

问题是,一旦我按下按钮一次,结果就会显示在 LED 中。但是,在第一次按下按钮后,逻辑停止工作。例如,在 PORTA 上切换输入时,无论是否按下按钮,它仍会显示在 LED 中。

我无法确定问题的确切位置,我真的需要帮助来调试它。

我正在使用 atmega32,在 Atmel studio 中编程,并在 Proteus 中进行模拟。

感谢您的帮助。

.cseg
.org 0x0000

; set stack pointer
ldi r29, low(ramend)
out spl, r29
ldi r29, high(ramend)
out sph, r29

start:
    ser r16
    out ddrb, r16   ; portb output  
    out ddrd, r16   ; portd output
    clr r16
    out ddra, r16   ;porta input
    out ddrc, r16   ;portc input
    ser r16
    out portc, r16  ;pull-up resistor on PORTC
    rjmp main

main:
    sbic pinc, 0    ;skip if button is not pressed on PC0
    call Load_Low   ;call subroutine function to load the lower bit
    sbic pinc, 7
    call Load_High  ;call subroutine function to load the higher bit
    sbic pinc, 3
    call Multiply   ;call subroutine function to multiply both stored values
    rjmp main

Load_High:
    in r20, pina    ;read bits in PINA to R20
    mov r30, r20    ;store copy
    out portb, r30  ;output to LEDs on PORTB
    cbi portc, 0    ;clear bit
    ret

Load_High:
    in r20, pina
    mov r31, r20
    out portd, r31
    cbi portc, 7
        ret

Multiply:   
    mul r31, r30
    out portd, r0
    out portb, r1
    cbi portc, 3
        ret

【问题讨论】:

  • 我投票结束这个问题,因为电子设计问题。

标签: avr atmega microprocessors atmega32


【解决方案1】:

在原理图中没有下拉电阻可以在释放按钮时产生低逻辑电平。因此电压是不确定的,在现实生活中电压会浮动,从而导致输入端出现意外的逻辑电平变化。

您可以将按钮连接到“GND”并使用内部上拉电阻,而不是添加外部下拉电阻(将PORTC 的相应位设置为 1,而在DDRC 中这些位为 0)

在你所有的例程中都有奇怪的行没有解释:

cbi portc, 0    ;clear bit
cbi portc, 7
cbi portc, 3

这些是按钮输入,为什么每次都清除PORTC 位?

【讨论】:

  • 我知道那些行没有做任何事情。我刚刚注意到在不按下按钮的情况下输出会改变的行为。所以,我想可能是因为它在按一下按钮后保持高状态。
  • 感谢您的回答。我会尝试实施它,并将结果与​​您联系。
  • 我编辑代码我必须添加行out portc, r16 来设置拉电阻。我将按钮接地,但这似乎不起作用。
  • @SegFaulter 您还必须反转逻辑。现在,当按下按钮时,输入的逻辑电平为低电平(零),释放时为高电平(一)。 IE。 sbic pinc, 0sbic 更改为 sbis
  • 是的,我明白了,谢谢。我现在正在研究为什么不能覆盖这些位。 0 位变为 1,但在第二次按下时不是相反
猜你喜欢
  • 1970-01-01
  • 2012-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-07
  • 1970-01-01
  • 2016-12-02
  • 2020-09-25
相关资源
最近更新 更多