3

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);
AJ.
  • 4,526
  • 5
  • 29
  • 41
Terryl
  • 143
  • 2
  • 2
  • 10
  • Try to define after #include – Chinna Dec 24 '13 at 07:17
  • tried, but no go. thanks for the suggestion. – Terryl Dec 24 '13 at 07:44
  • How you are testing that your delay is happening? 4 milli seconds delay is too less. – Chinna Dec 24 '13 at 07:49
  • I havent gotten that far. That was just a random number I threw in until I know better. Im writing the code, but the actual delay depends on some experiments Im doing. The code will charge a capacitor bank and then release the charge into some Nychrome wire. – Terryl Dec 25 '13 at 17:04

7 Answers7

2

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);
}
2

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.

1

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?

vmario
  • 411
  • 3
  • 15
  • hi, thank you for your response. Both things you have mentioned are typos in the above code. The actual ode does have __ and it is closed. The error is that it doesnt compile, the actual error from the IDE (MPLab X) reads "unable to resolve identifier". – Terryl Dec 25 '13 at 16:59
1

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

Ligo George
  • 819
  • 2
  • 9
  • 21
0

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.

Jagadesan
  • 11
  • 2
0

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.

Terryl
  • 143
  • 2
  • 2
  • 10
0

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)))
Aldanis Vigo
  • 159
  • 2
  • 6