[英]XC8 compiler does not recognize __delay macros
I want to program a ATtiny85 microcontroller and have realized that my program needs a bit of delay to work properly.我想对 ATtiny85 微控制器进行编程,并意识到我的程序需要一些延迟才能正常工作。 I am using MPLabX and the XC8 compiler.我正在使用 MPLabX 和 XC8 编译器。 So I should be able to use the __delay_ms() macro and MPLab does recognize the macro and does NOT put the red line under the code.所以我应该能够使用 __delay_ms() 宏并且 MPLab 确实可以识别该宏并且不会将红线放在代码下方。 However when I try to compile the programm, there is an error thrown at each line where I use the macro.但是,当我尝试编译程序时,在我使用宏的每一行都会抛出一个错误。 "Undefined reference to __delay_ms" And also an additional "implicit declaration of __delay_ms" for the first line that uses the macro. “对 __delay_ms 的未定义引用”以及使用宏的第一行的附加“__delay_ms 的隐式声明”。 I do have _XTAL_FREQ defined correctly and xc.h included.我确实正确定义了 _XTAL_FREQ 并包含 xc.h。
The code also compiles just fine without the delay macros, but than it doesn't work correctly, so I need the delay.该代码在没有延迟宏的情况下也可以正常编译,但是它不能正常工作,所以我需要延迟。
I don't know what the problem is.我不知道问题是什么。 I have programmed other microcontrollers with the __delay_ms macro before.我之前用 __delay_ms 宏对其他微控制器进行了编程。 I am also using the latest version of the XC8 compiler.我也在使用最新版本的 XC8 编译器。 And according to the documentation of the compiler the version is fully compatible with the ATtiny85.并且根据编译器的文档,该版本与 ATtiny85 完全兼容。
I also tried to find a solution on the internet, but didn't find anything that helped with my problem...我也试图在互联网上找到解决方案,但没有找到任何有助于解决我的问题的方法......
Any ideas where the problem could be?问题可能出在哪里?
So I did some more digging in the include directories for xc8 and I found something.所以我在 xc8 的包含目录中进行了更多挖掘,我发现了一些东西。 If I include util/delay.h I can use the functions, but only using one underscore instead of two.如果我包含 util/delay.h 我可以使用这些函数,但只使用一个下划线而不是两个下划线。 So the command has to be _delay_ms().所以命令必须是_delay_ms()。 Then I can compile the code, but it is missing a definition for F_CPU.然后我可以编译代码,但它缺少 F_CPU 的定义。 This however only results in a warning and not an error.然而,这只会导致警告而不是错误。 I will try to find out where F_CPU is defined.我将尝试找出 F_CPU 的定义位置。 I am guessing it is defined relative to _XTAL_FREQ somewhere...我猜它是相对于某处的 _XTAL_FREQ 定义的......
I will keep you updated on what I can find out.我会及时通知您我能找到的信息。
Maybe this should be a comment, but I want to give you an example which compiles fine for me:也许这应该是一个评论,但我想给你一个对我来说很好编译的例子:
This is the way it works for a PIC这是它适用于 PIC 的方式
compiler: xc8 2.0编译器:xc8 2.0
#include <xc.h>
#define _XTAL_FREQ 4000000
void main (void)
{
while(1)
{
__delay_ms(1);
}
}
For an AVR it's a little bit different: (thats's crazy!)对于 AVR,它有点不同:(这太疯狂了!)
#define F_CPU 4000000UL
#include <xc.h>
#include <avr/delay.h>
int main(void)
{
while (1)
{
_delay_ms(1);
}
}
And.. Microchip has changed things again with AVR.. This is with MPLABX 5.5 and XC8 2.32.而且.. Microchip 用 AVR 再次改变了一切。 MPLABX 5.5 和 XC8 2.32 也是如此。
#define F_CPU 4000000UL
#include <xc.h>
#include <util/delay.h> // change to util from avr
int main(void)
{
while (1)
{
_delay_ms(1);
}
}
`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.