c - 如何修复错误消息 "__builtin_avr_delay_cycles expects a compile time integer constant make"?

标签 c adc

我的程序不是使用 GCC AVR 编译器编译的。这是一个在微处理器和 LCD 屏幕上的小游戏。

move_delay = 200;
_delay_ms( move_delay );

它不喜欢这个_delay_ms是一个变量,但它必须是一个变量,因为我可以通过一个ADC来调整屏幕 Action 。有什么方法可以使它保持不变但仍可与 ADC 一起使用?

最佳答案

如果在 #include 语句之前使用以下定义,则还可以使用变量而不是数字常量调用延迟函数:

#define __DELAY_BACKWARD_COMPATIBLE__
#include <util/delay.h>

/************************************************************************
 * Function: sound
 * Description: Toggle Port to generate sound with a given pulse width.
 * Parameter: duration, pulsewidth
 * Uses: _delay_us()
 ************************************************************************/
void sound(uint16_t duration, uint16_t pulsewidth) {
  uint16_t i;
  uint16_t j = 2270 * duration/pulsewidth;

  for(i=0; i<j; i++) {
    SPKR_PORT |= (1 << SPKR_PAD);   //Set port -> create bit mask and OR it
    //delay
    _delay_us(pulsewidth);

    SPKR_PORT &= ~(1 << SPKR_PAD);   //clear port -> negate mask and AND it
    //delay
    _delay_us(pulsewidth);
  }
}

关于c - 如何修复错误消息 "__builtin_avr_delay_cycles expects a compile time integer constant make"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30422367/

相关文章:

c - 如何用C写一个停止关机进程的程序?

c - 嵌入式 Linux C 代码,用于从 USB 驱动器执行 cp 到主机上的目录,保持目录结构完整

c - STM32 使用 CubeMX 和 STMStudio : 'int main()' doesn't work

c - 基于麦克风输入的 PWM 信号生成

c - 如何使用 HAL 库读取和显示 STM32F4 的 ADC 值

c++ - 用于链接 .lib 文件的代码内关键字

c++ - 位对齐 访问任意位长度字节对齐缓冲区

c++ - 带指针的 bitWrite 函数

c - AVR ATMega328P ADC channel 选择问题

c - STM32F746NG-发现: Program Stuck after first ADC read