Arduino nano 上的 Atmega 4809 可以接收红外信号并通过 PWM 信号为 LED 的亮度供电

问题描述 投票:0回答:1

我目前正在做一项大学作业,涉及创建一个系统来识别和显示红外遥控器的十六进制代码。此外,系统应根据遥控器上的“+”和“-”按钮使用 PWM 调整 LED 的亮度。

我不可以使用除了Liquidcrystal之外的库用于液晶屏。

尝试使用 PlatformIO 编译此代码时遇到构建错误。我看到的具体错误消息是:

Indexing .pio\build\nano_every\libFrameworkArduino.a
Linking .pio\build\nano_every\firmware.elf
C:\Users\User\AppData\Local\Temp\ccWdZZlI.ltrans0.ltrans.o: In function `main':
<artificial>:(.text.startup+0xb2): undefined reference to `setup'
<artificial>:(.text.startup+0xba): undefined reference to `loop'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\nano_every\firmware.elf] Error 1

构建输出对我没有任何帮助,所以我无法理解是什么不允许程序构建。

由于我在 VSCode 中使用 PlatformIO 扩展,因此要在此处添加信息的是我的 platformio.ini conf 文件:

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:nano_every]
platform = atmelmegaavr
board = ATmega4809
framework = arduino
monitor_speed = 9600
lib_deps = 
    arduino-libraries/LiquidCrystal@^1.0.7

这是我的代码:

#include <LiquidCrystal.h>
#include <avr/io.h>
#include <avr/interrupt.h>

// Definition of IR codes for the "+" and "-" buttons
const unsigned long IR_BUTTON_PLUS = 0x40501505;
const unsigned long IR_BUTTON_MINUS = 0x44111144;

// Variables for managing time and IR code
volatile unsigned long irCode = 0;
volatile bool irReceived = false;
volatile int ledBrightness = 128; // Initial LED brightness
volatile unsigned long elapsedTime = 0; // Elapsed time in microseconds

// LCD display configuration (pin RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(12, 11, 14, 15, 16, 17);

// ISR for the timer that keeps track of elapsed time in microseconds
ISR(TCA0_OVF_vect) {
  elapsedTime++;
  TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm; // Clear the interrupt flag
}

// ISR for receiving the IR signal
ISR(PORTA_PORT_vect) {
  if (PORTA.INTFLAGS & PIN2_bm) {
    static unsigned long lastTime = 0;
    static unsigned long code = 0;
    static int bitCount = 0;

    // Use the timer to get the current time
    unsigned long time = elapsedTime; // Use elapsedTime to get the time in microseconds
    unsigned long duration = time - lastTime;
    lastTime = time;

    // IR signal decoding (simplified example)
    if (duration > 1000) { // Start of a new code
      code = 0;
      bitCount = 0;
    } else {
      code = (code << 1) | (duration > 500 ? 1 : 0);
      bitCount++;
      if (bitCount == 32) {
        irCode = code;
        irReceived = true;
      }
    }
    PORTA.INTFLAGS = PIN2_bm; // Clear the interrupt flag
  }
}

void setup() {
  // Initialize the LCD display
  lcd.begin(16, 2);
  lcd.print("IR Code:");

  // Configure the pin for the IR signal
  PORTA.DIRCLR = PIN2_bm; // Set PA2 as input
  PORTA.PIN2CTRL = PORT_ISC_BOTHEDGES_gc; // Enable interrupt on both edges

  // Configure the timer for PWM
  TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV64_gc | TCA_SINGLE_ENABLE_bm;
  TCA0.SINGLE.PER = 255; // PWM period
  TCA0.SINGLE.CTRLB = TCA_SINGLE_WGMODE_SINGLESLOPE_gc | TCA_SINGLE_CMP0EN_bm;
  TCA0.SINGLE.CMP0 = ledBrightness; // Set initial brightness

  // Configure the pin for the LED PWM
  PORTD.DIRSET = PIN6_bm; // Set PD6 as output

  // Enable interrupts
  sei();
}

void loop() {
  if (irReceived) {
    irReceived = false;
    lcd.setCursor(0, 1);
    lcd.print(irCode, HEX);

    if (irCode == IR_BUTTON_PLUS && ledBrightness < 255) {
      ledBrightness += 16;
    } else if (irCode == IR_BUTTON_MINUS && ledBrightness > 0) {
      ledBrightness -= 16;
    }
    TCA0.SINGLE.CMP0 = ledBrightness; // Update LED brightness
  }
}

这是我在 Tinkercad 上快速制作的 IRL 项目的代表,让您了解它是如何制作的

我已经验证我的代码符合 ATmega4809 的数据表,并且我使用了正确的寄存器和端口,但老实说,在这一点上我可能是错的。 自从从使用 arduino.h 切换到控制 I/O 以来,我一直被这个问题困扰,所以我认为问题出在我的 I/O 处理上。

谢谢你。

c++ arduino embedded platformio arduino-nano
1个回答
0
投票

您从未声明过

void loop()
void setup()
。编译器应该如何知道这些函数?

#include <Arduino.h>
添加到文件顶部。

© www.soinside.com 2019 - 2024. All rights reserved.