【问题标题】:Passing a Port as a variable - AVR将端口作为变量传递 - AVR
【发布时间】:2012-12-04 13:30:01
【问题描述】:

是否可以将 AVR 端口用作可以传递的变量?

例如

LED myLed(PORTA,7);   //myLED hooked to PORTA, Pin 7

我想让 LED 能够采用任何 PORT / Pin 组合,所以我宁愿不对其进行硬编码。

请注意,端口定义为:

#define PINA  _SFR_IO8(0x00)
#define DDRA  _SFR_IO8(0x01)
#define PORTA _SFR_IO8(0x02)    

PORTA 符号解析为 (*(volatile uint8_t *)((0x02) + 0x20))

我相信这将允许我执行以下操作,但我不确定是否需要 volatile 关键字,也不确定它是否会按预期工作

class LED{
public:
    LED(volatile uint8_t* port, uint8_t pin);
    {
        Port=port;
        Pin=pin;
    }
    void write(bool val)
    {
        if(val) (*Port) |= 1 << Pin;
        else    (*Port) &= ~(1 << Pin);
    }

private:
    uint8_t  Pin
    volatile uint8_t* Port;
}

最后,有没有办法将端口/引脚设置为 LED 构造器的输出? 这将涉及为 Given PORT# 找到相关的 DDR# 寄存器。 我可以假设 &DDR# 永远是 &PORT#-1 吗?

【问题讨论】:

  • 你也可以使用位结构...
  • 您介意对此进行扩展吗?我不确定你所说的位结构是什么意思
  • struct port{ UINT8 _port_1_0:1; UINT8 _port_1_2:1; ... }; - 这在 C 中更好/更好。但如果你有 C++ 和模板,avakar 示例会更好。

标签: c++ io avr


【解决方案1】:

寄存器宏基本上是指向适当寄存器所在的内存位置的指针,所以是的,您可以使用uint8_t volatile *。但是,编译器不会以这种方式生成最有效的代码——它将使用间接寻址而不是直接写入。

这就是我所做的,使用avrlib

#include <avrlib/porta.hpp>
#include <avrlib/pin.hpp>
using namespace avrlib;

typedef pin<porta, 4> led_pin;

然后你可以使用led_pin typedef,例如

led_pin::set();

【讨论】:

  • 图书馆的链接不再有效(网站已关闭)。
【解决方案2】:

我就是这样做的,我对AVR不是很熟悉,

#include <avr/io.h>

void LED(volatile uint8_t* port, uint8_t pin)
{
    // First set DDRx ; DDRx on ATmega32 is one address below port address
    *(port -1) |= (1<< pin);
    // Now set the pin high
    *port |= (1<< pin);
}

int main(void)
{
   LED(&PORTB,1);
   LED(&PORTC,2);
   LED(&PORTD,3);
    while (1) 
    {
    }
}

【讨论】:

    【解决方案3】:

    端口只不过是 I/O 地址,所以您只需将 I/O 端口的地址传递给 LED 构造函数即可:

    LED *light = new LED(&PORTA, 4);
    

    为什么会这样?正如您已经提到的,PORTA 解决了指针的取消引用:

    (*(volatile uint8_t *)((0x02) + 0x20))
    

    所以在前面添加一个操作符的地址就创建了

    &(*(volatile uint8_t *)((0x02) + 0x20))
    

    可以简化为

    (volatile uint8_t *)((0x02) + 0x20)
    

    【讨论】:

      猜你喜欢
      • 2023-03-11
      • 2020-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多