简体   繁体   English

如何在 MICROCHIP XC8 的库中正确使用 __delay_ms()?

[英]How to properly use __delay_ms() in libraries in MICROCHIP XC8?

I'm facing some trouble trying to create a library that calls "__delay_ms()" inside a function.我在尝试创建一个在函数内调用“__delay_ms()”的库时遇到了一些麻烦。 Made extensive search but couldn't find the solution, nor some other explanations.进行了广泛的搜索,但找不到解决方案,也没有其他一些解释。 I'm using XC8 v2.30 MPLAB 5.45.我正在使用 XC8 v2.30 MPLAB 5.45。

I have a main function that includes the header "qc3.h":我有一个包含标题“qc3.h”的主函数:

#include <xc.h> // include standard header file

// set Config bits
#pragma config FOSC=INTOSC, PLLEN=OFF, WDTE=OFF, MCLRE=ON,
#pragma config CLKOUTEN=OFF, IESO=OFF, FCMEN=OFF,CP=OFF, CPD=OFF,BOREN=OFF
#pragma config WRT=OFF,STVREN=ON,BORV=LO,LVP=OFF

// Definitions
#define _XTAL_FREQ 500000 // this is used by the __delay_ms(xx) and __delay_us(xx) functions

#include "qc3.h"

#define LED PORTAbits.RA2




//**********************************************************************************
//***************** main routine ***********************************************
//**********************************************************************************
void main ( ) 
{
    // set up oscillator control register
    OSCCONbits.SPLLEN=0; // PLL is disabled
    OSCCONbits.IRCF=0x07; //set OSCCON IRCF bits to select OSC frequency=500Khz
    OSCCONbits.SCS=0x02; //set the SCS bits to select internal oscillator block
    // OSCON should be 0x7Ah now.

    // Set up I/O pins
    ANSELAbits.ANSELA=0; // set all analog pins to digital I/O
    ADCON0bits.ADON=0; // turn ADC off
    DACCON0bits.DACEN=0; // turn DAC off

    // PORT A Assignments (0 = OUTPUT, 1 = INPUT)
    TRISAbits.TRISA0 = 0; // RA0 = nc
    TRISAbits.TRISA1 = 0; // RA1 = nc
    TRISAbits.TRISA2 = 0; // RA2 = nc
    TRISAbits.TRISA3 = 0; // RA3 = nc (MCLR)
    TRISAbits.TRISA4 = 0; // RA4 = nc
    TRISAbits.TRISA5 = 0; // RA5 = nc
    
    
    QC3_Initialize(); // incia protocolo para handshake
         
    while(1)
    {
        __delay_ms(100);
        LED = 1;
        __delay_ms(100);
        LED = 0; 
    } 
}

qc3.h: qc3.h:

#ifndef QC3_H_ /* Include guard */
#define QC3_H_

/**
  Section: Included Files
*/

#include <xc.h>
#include <stdint.h>
#include <stdbool.h>

#ifdef __cplusplus // Provide C++ Compatibility

    extern "C" {

#endif

//Quick Charge 3 Pin defines
#define DP_HIGH PORTAbits.RA0
#define DM_HIGH PORTAbits.RA1

#define DP_LOW PORTAbits.RA5
#define DM_LOW PORTAbits.RA4

void QC3_Initialize(void);

//void onewireWriteBit(int b);
//unsigned char onewireReadBit();
//unsigned char onewireInit();
//unsigned char onewireReadByte();
//void onewireWriteByte(char data);
//unsigned char onewireCRC(unsigned char* addr, unsigned char len);

#ifdef __cplusplus // Provide C++ Compatibility

    }

#endif

#endif

qc3.c: qc3.c:

#include "qc3.h"


void QC3_Initialize(void) {
  __delay_ms(150);
 DP_HIGH = 0;
  
}

Error:错误:

make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory 'D:/Google Drive/Projetos/Pedais/Fonte para pedais isolada/QUICK CHARGE/PIC'
make -f nbproject/Makefile-default.mk dist/default/production/PIC.production.hex
make[2]: Entering directory 'D:/Google Drive/Projetos/Pedais/Fonte para pedais isolada/QUICK CHARGE/PIC'
"D:\Program Files\Microchip\xc8\v2.30\bin\xc8-cc.exe" -mcpu=12F1840 -c -mdfp="D:/Program Files/Microchip/MPLABX/v5.45/packs/Microchip/PIC12-16F1xxx_DFP/1.2.63/xc8" -fno-short-double -fno-short-float -O0 -fasmfile -maddrqual=ignore -xassembler-with-cpp -I"head_and_lib" -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx032 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall -std=c99 -gdwarf-3 -mstack=compiled:auto:auto -o build/default/production/head_and_lib/qc3.p1 head_and_lib/qc3.c 
"D:\Program Files\Microchip\xc8\v2.30\bin\xc8-cc.exe" -mcpu=12F1840 -c -mdfp="D:/Program Files/Microchip/MPLABX/v5.45/packs/Microchip/PIC12-16F1xxx_DFP/1.2.63/xc8" -fno-short-double -fno-short-float -O0 -fasmfile -maddrqual=ignore -xassembler-with-cpp -I"head_and_lib" -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx032 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall -std=c99 -gdwarf-3 -mstack=compiled:auto:auto -o build/default/production/source/ISOPOWER.p1 source/ISOPOWER.c 
make[2]: *** [build/default/production/head_and_lib/qc3.p1] Error 1
make[2]: *** Waiting for unfinished jobs....
head_and_lib/qc3.c:6:3: error: use of undeclared identifier '_XTAL_FREQ'
  __delay_ms(150);
  ^
D:/Program Files/Microchip/MPLABX/v5.45/packs/Microchip/PIC12-16F1xxx_DFP/1.2.63/xc8\pic\include\pic.h:101:51: note: expanded from macro '__delay_ms'
#define __delay_ms(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000.0)))
                                                  ^
1 error generated.
(908) exit status = 1
nbproject/Makefile-default.mk:123: recipe for target 'build/default/production/head_and_lib/qc3.p1' failed
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
make[2]: Leaving directory 'D:/Google Drive/Projetos/Pedais/Fonte para pedais isolada/QUICK CHARGE/PIC'
nbproject/Makefile-default.mk:91: recipe for target '.build-conf' failed
make[1]: Leaving directory 'D:/Google Drive/Projetos/Pedais/Fonte para pedais isolada/QUICK CHARGE/PIC'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed

BUILD FAILED (exit value 2, total time: 913ms)

The compiler complains (use of undeclared identifier '_XTAL_FREQ') but the header in declared AFTER the #define _XTAL_FREQ 500000. Why?编译器抱怨(使用未声明的标识符“_XTAL_FREQ”),但在#define _XTAL_FREQ 500000 之后声明的头文件。为什么?

If i remove the delay of the function:如果我删除函数的延迟:

void QC3_Initialize(void) {
  __delay_ms(150);
 DP_HIGH = 0;

it builds successfully它成功构建

So.所以。 "when" the QC3.c is "called" if it's never declared (even in the ".h" header)?如果 QC3.c 从未声明(即使在“.h”标头中),“何时”被“调用”?

Why I should declare the ".h" if is the ".c" that includes the ".h"?如果是包含“.h”的“.c”,为什么我应该声明“.h”? Shouldn't it be the other way around?不应该反过来吗? Or at least the ".h" to contain the ".c"?或者至少包含“.c”的“.h”?

Seems basic stuff but it's blowing my mind看起来很基本的东西,但它让我大吃一惊

Thank You谢谢你

You define _XTAL_FREQ in your C source file containing main() but, since you're calling __delay_ms() in qc3.c (a separate translation unit), that's where that definition needs to exist.您在包含main() C 源文件中定义了_XTAL_FREQ ,但是,由于您在qc3.c (一个单独的翻译单元)中调用__delay_ms() ),就是该定义需要存在的地方。

The easiest fix is probably to define it in qc3.h .最简单的解决方法可能是在qc3.h定义它。

您必须在 .c 文件中提供 _XTAL_FREQ,然后您可能会得到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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