简体   繁体   中英

PIC and XC8 Compiler issue

Im sure Im missing something simple, and obvious, but I am tired of searching for the answer. Im using a PIC16F688 and XC8 compiler.

The compiler user manual says that there is a delay function __delay_ms() . It says that _XTAL_FREQ must be defined.

Here is my code, but it does not accept the command. What is wrong?

#include <stdio.h>
#include <stdlib.h>

#define _XTAL_FREQ 20000000

#include<xc.h>

    int main(int argc, char** argv) {


       _delay_ms(4);

    return (EXIT_SUCCESS);

Please include "htc.h"

#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <htc.h>

#define _XTAL_FREQ 20000000

int main(int argc, char** argv) {


       _delay_ms(4);

    return (EXIT_SUCCESS);
}

They are right, the problem was experienced by older versions of the IDE's. I have found it helpful to use:

while(1){
//Invert LED state
LED = !LED;
//Delay ~1 second (4MHz Internal Clock)
_delay(1000000); //specify clock cycles directly
}

To solve the problem.

What does it mean "it does not accept the command"? Compiler cannot find function _delay_ms() ? Maybe you should use proper name with two underscores __delay_ms() ?

Moreover, why you do not close main function with } ? It is only a typo in your post or in your real code?

May be you have to include or enable pic Controller library file in your compiler.

In some compiler meed to give controller info like controller series and clock frequency using ect.,

It seems problem with compiler setting.

I discovered that the IDE wasnt correctly reading the include file xc.h, so it was red lining a line that was actually correct. It wasnt compiling due to another issue earlier in the program.

Thank you for the responses.

It seems like you used only ONE UNDERSCORE for the function. Use 2 underscore, __delay_ms(1000);

#include <stdio.h>
#include <stdlib.h>
#include <xc.h>


#define _XTAL_FREQ 20000000

int main(int argc, char** argv) {


       __delay_ms(4);

    return (EXIT_SUCCESS);
}

I hope that the following link will help you to learn MPLAB XC8.

PIC Microcontroller Tutorials using MPLAB XC8

maybe try this directly

     //Delay Definitions
     #define __delay_us(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000000.0)))
     #define __delay_ms(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000.0)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM