| 
 | 
 
我用PICC18 + MPLAB + ICD2. 我做了一个小测试程序,这是把rb0每500ms闪烁一次。我发现如果把所有代码写成一个c文件,就可以;但分成几个c文件, 就不行了: 
(方法1) 如果把所有的代码放入一个c文件,什么都可以。把icd2当成debugger在板子上转程序,可以;把icd2当成烧录器把程序烧到板子上,断开icd2,让板子独立运行,也可以。 
(方法2)把闪烁那句话 (RB0 = !RB0)做成一个独立的c文件,加入到project中。这时候把icd2当成debugger在板子上转程序,可以;把icd2当成烧录器把程序烧到板子上,断开icd2,让板子独立运行,不行。灯闪的特别快,有时还停。 
我的问题是:在一个project中,写成一个c文件和分割成多个c文件有什么区别?有时候你必须放到多个c文件里,那怎么办? 
 
刚才说的两种方法, 我都用的compiler, linker and assambler的缺省设置. 我同时也试了以下的设置:  
 
compiler: Global Optimization level = 9  
enable assembly optimization = UNCHECKED  
assembler: Enable optimization = checked  
linker: warning level = -9  
 
结果是相同的。 
 
烧片的时候enable了 WDT. 也试过disable WDT, 更不行。 
 
我的程序:  
 
方法1: (one big main.c file, and the isr):  
 
/* main.c */  
#include  
#include "mainPLC.h"  
 
volatile unsigned char flashyCounter = 0;  
 
void TaskFlashy(void)  
{  
RB0 = !RB0;  
}  
 
 
void main(void)  
{  
/*** timer0, 10 ms tick ***/  
T0CON=0b01000110; //preload=178. 78*128*1us=10ms  
TMR0IF=0;  
TMR0L = 178;  
TMR0IE=1;  
PEIE = 1;  
GIE = 1;  
TMR0ON = 1;  
 
/**** Port Init ***/  
TRISB = 0x00; /* PortB: digital output */  
 
for(;;)  
{  
if(flashyCounter >= 50)  
{  
flashyCounter = 0;  
TaskFlashy();  
}  
}  
}  
 
/* isr.c */  
#include  
#include "mainPLC.h"  
void interrupt ISR(void)  
{  
if(TMR0IE && TMR0IF) // Timer0 interrupt  
{  
TMR0IF = 0;  
TMR0L = 178;  
flashyCounter = flashyCounter + 1;  
}  
}  
 
方法 2: (main.c file, taskFlashy.c file and the isr):  
 
/* main.c */  
#include  
#include "mainPLC.h"  
 
volatile unsigned char flashyCounter = 0;  
 
extern void TaskFlashy(void);  
 
 
void main(void)  
{  
/*** timer0, 10 ms tick ***/  
T0CON=0b01000110; //preload=178. 78*128*1us=10ms  
TMR0IF=0;  
TMR0L = 178;  
TMR0IE=1;  
PEIE = 1;  
GIE = 1;  
TMR0ON = 1;  
 
/**** Port Init ***/  
TRISB = 0x00; /* PortB: digital output */  
 
for(;;)  
{  
if(flashyCounter >= 50)  
{  
flashyCounter = 0;  
TaskFlashy();  
}  
}  
}  
 
/* isr.c */  
#include  
#include "mainPLC.h"  
void interrupt ISR(void)  
{  
if(TMR0IE && TMR0IF) // Timer0 interrupt  
{  
TMR0IF = 0;  
TMR0L = 178;  
flashyCounter = flashyCounter + 1;  
}  
}  
 
 
/* taskFlashy.c */  
#include  
#include "mainPLC.h"  
 
void TaskFlashy(void)  
{  
RB0 = !RB0; //toggle RB0  
}  
;  
 
我的问题是:在一个project中,写成一个c文件和分割成多个c文件有什么区别?有时候你必须放到多个c文件里,那怎么办? |   
 
 
 
 |