我的程序不是使用 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/