问题描述
我是 C 编码的新手,我无法找出将我的文件链接在一起的正确方法,以便我可以在另一个类中使用来自一个类的代码。我正在做一个可能会成为一个很长的程序的项目,所以我试图避免用一堆函数弄乱我的主类。相反,我希望将它们分开,以便将代码转移到任何未来的项目中。
通过查找类似的问题,我假设这有点简单,而且我可能为此苦苦挣扎,因为我缺少 C 中编码的基础知识。
> Executing task in folder Led-Pot_Circuit_Test: .\Build.cmd 16f887 <
C:\Users\raymu_000\Documents\Programming\VisualStudioCode\PIC_C++_on_VSCode\Led-Pot_Circuit_Test>mkdir output-files
A subdirectory or file output-files already exists.
C:\Users\raymu_000\Documents\Programming\VisualStudioCode\PIC_C++_on_VSCode\Led-Pot_Circuit_Test>C:\Progra~1\microchip\xc8\v2.32\bin\xc8.exe --chip=16f887 --outdir=".\output-files" ".\main.c"
C:\Progra~1\microchip\xc8\v2.32\pic\bin\picc --chip=16f887 --outdir=.\output-files .\main.c
microchip MPLAB XC8 C Compiler V2.32
Build date: Feb 1 2021
Part Support Version: 2.32
copyright (C) 2021 microchip Technology Inc.
License type: Node Configuration
0: (499) undefined symbol:
_adc_convert(output-files\main.obj)
(908) exit status = 1
(908) exit status = 1
C:\Users\raymu_000\Documents\Programming\VisualStudioCode\PIC_C++_on_VSCode\Led-Pot_Circuit_Test>C:\Progra~1\microchip\xc8\v2.32\bin\xc8.exe --chip=16f887 --outdir=".\output-files" ".\Project\InitApp.c"
C:\Progra~1\microchip\xc8\v2.32\pic\bin\picc --chip=16f887 --outdir=.\output-files .\Project\InitApp.c
microchip MPLAB XC8 C Compiler V2.32
Build date: Feb 1 2021
Part Support Version: 2.32
copyright (C) 2021 microchip Technology Inc.
License type: Node Configuration
Non line specific message: (1091) main function "_main" not defined
(908) exit status = 1
(908) exit status = 1
The terminal process "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -Command .\Build.cmd 16f887" terminated with exit code: 1.
Terminal will be reused by tasks,press any key to close it.
我的主要课程:
//main.c
// CONfig1
#pragma config FOSC = INTRC_NOCLKOUT// Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin,I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input,MCLR internally tied to VDD)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF // brown Out Reset Selection bits (BOR disabled)
#pragma config IESO = OFF // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FcmeN = OFF // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
#pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3 pin has digital I/O,HV on MCLR must be used for programming)
// CONfig2
#pragma config BOR4V = BOR21V // brown-out Reset Selection bit (brown-out Reset set to 2.1V)
#pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)
#define _XTAL_FREQ 8000000 // 8Mhz
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include <pic16f887.h>
#include "Project\user.h"
void main(void) {
//InitApp();
unsigned short adc_value; // variable to hold ADC conversion result in/* Configure the oscillator for the device
ANSELbits.ANS0 = 0; // disable analog on pin RA0
TRISAbits.TRISA0 = 0; // Set pin RA0 as output
while(1) {
PORTAbits.RA0 = 0; // Set RA0 to LOW
// calculate wait time
adc_value = adc_convert(1); // perform A/D conversion using input from channel 1 (AN1)
__delay_ms(500); // Right shift adc_value two spaces to convert to milliseconds
PORTAbits.RA0 = 1; // Set RA0 to HIGH
adc_value = adc_convert(1);
__delay_ms(500);
}
}
这是我尝试制作一个库,以便我以后可以“轻松”将一些有用的代码转移到另一个项目中:
//user.h
#ifndef USER_H
#define USER_H
void init_adc(void);
unsigned short adc_convert(unsigned char);
#endif
有用的代码:
//InitApp.c
#include <xc.h>
#include <pic16f887.h>
#include "user.h"
/*
Initialize the Analog to Digital Converter
*/
void init_adc(void) {
TRISAbits.TRISA1 = 0b1; // Set pin RA1 as Input
//ANCON0bits.ANSEL1 =0b1;
ADCON1bits.VCFG0 = 0; // set v+ reference to Vdd
ADCON1bits.VCFG1 = 0; // set v- reference to Vss (Gnd)
ADCON1bits.ADFM = 1; // right justify the output
ADCON0bits.ADCS = 0b01; // use Fosc/8 for clock source
ADCON0bits.ADON = 1; // turn on the ADC
}
/*
Preform an analog to digital conversion.
@param channel The ADC input channel to use.
@return The value of the conversion.
*/
unsigned short adc_convert(unsigned char channel) {
ADCON0bits.CHS = channel; // select the given channel
ADCON0bits.GO = 1; // start the conversion
while(ADCON0bits.GO_DONE); // wait for the conversion to finish
return(ADRESH<<8)|ADRESL; // return the result
}
图片供参考:
这是“输出文件”文件夹的内容,当我运行“Build.cmd”任务时,编译器中的所有文件都放在这里,但我缺少一些(如“.hex”文件),因为程序无法正确编译。
我需要在 InitApp.c 类中有一个主类吗? 如何正确编译和链接 C 文件?
感谢艾米的帮助和建议,谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)