这真是件好事。不理解为什么某些东西(神奇地或看起来如此)被固定了,几乎肯定会在代码被添加或修改时导致后来的问题。或者,当你继续进行其他项目时,也许我能消除一些困扰你的疑虑,即使是在一些更有经验的助手中,关于编译器头文件和其他编译器“问题”,这些代码确实是你的代码中的问题。代码:问题不在于你给我们展示了什么,而是在于你没有给我们展示什么。当我们呈现整个EngILADA时,我们可以看到它不是编译器或IDE配置“问题”。您的代码(在稍后的帖子中给了它一个链接)还包括“LCD.H”和LCD。H在BePr.h中有这个。是向编译器提交的行,这就是编译器抱怨的原因。(这就是为什么当您将中断函数移到另一个不包括LCD.H的文件时,它不会产生错误消息)也许您现在可以看到为什么下面的行也会触发编译器错误消息:注意宏。在程序中使用更可能是单色的东西,而不是在LCD中定义EN。H,也许是定义LCDGEN和使用LCDGEN,无论你想操纵LCD使能信号。还有一个重要的编程注意:输出信号应该总是使用LAT位,而不是端口位,所以FO。LLWIN将是合适的:在开云(中国)官方上搜索RMW或阅读修改,在网站上的其他地方搜索解释。这个(读修改写现象)在800页数据表的第16部分中提到,但是它的重要性没有被强调。这是非常,非常(非常)重要的。所以,同样地,在您的LCDGPORT()函数中,使用LAT位,而不是端口位:底线:即使您的代码似乎正在工作(或者在几乎工作和几乎不工作之间波动),使用LAT位来输出会节省大量的头痛,并且很难调试。最后的注意事项:当你获得K42的经验时,你会发现矢量中断,如在第9页中的1and 0所建议的那样,可以使代码更加高效和可维护。这是8位PICS体系结构的一大进步,也是K42系列的一个突出特点。但这里的事情是:为了使用矢量化中断函数,必须改变MVECCENPREMA:问候,戴夫。
以上来自于百度翻译
以下为原文
It is a really Good Thing to be bothered about. Not understanding why something got (magically, or so it seems) fixed will almost certainly result in later problems as code is added or modified. Or as you go on to other projects.
Maybe I can clear up a couple of nagging doubts, even among some more experienced helpers, concerning the compiler header file and other compiler "issues" that are really problems with your code.
From your original post:
Here's why it is important to give all of your code: The problem is not in what you showed us, but in what you didn't show us. When we are presented with the whole enchilada, we can see that it is not a compiler or IDE configuration "issue."
Your code (to which you gave a link to in a later post) also includes "lcd.h" and lcd.h has this
#define EN RD3
In beeper.h, BEEPER1 is defined as
#define BEEPER1 PWM5CONbits.EN
With this in mind, consider
BEEPER1 = 0;
Now, after preprocessing, this line is presented to the compiler as
PWM5CONbits.RD3 = 0;
That's why the compiler complains. (And that's why it did not generate an error message when you moved your interrupt function to another file that did not include lcd.h)
Maybe you can now see why the following line also triggers a compiler error message
T0CON0bits.EN = 0
Bottom line: Beware of MACROS. Use something that is more likely to be unique
So, in your program, instead of defining
EN in lcd.h, maybe
#define LCD_EN and use
LCD_EN wherever you want to manipulate the LCD Enable signal.
Also, an important programming note: Output signals should always use the LAT bit, not the PORT bit,
so the following would be appropriate:
#define LCD_EN LATDbits.LATD3
Search for RMW or read-modify-write in this forum and in about a gazillion other places on the web for explanations.
This (read-modify-write phenomenon) is mentioned in section 16.0 of the 800+ page Data Sheet, but its significance is not emphasized. It's really, really (really) important.
So, similarly, in your Lcd_Port() function, use LAT bits, not PORT bits:
void Lcd_Port(char a)
{
if(a & 1)
LATDbits.LATD4 = 1;
else
LATDbits.LATD4 = 0;
// etc
Bottom line:
Even if your code seems to be working (or fluctuating between almost working and barely working), use of LAT bits for outputs will save lots of headaches and difficult-to-debug problems later on.
Final note: As you gain experience with the K42, you may find that vectored interrupts, as suggested by 1and0 in post #9, can make for vastly more efficient and maintainable code. This is a Big Deal advance in the architecture of 8-bit PICs, and is one of the outstanding features of the K42 family.
But here's the thing:
In order to use the vectored interrupt functions you must change the MVECEN pragma:
//#pragma config MVECEN = OFF // Multi-vector enable bit->Interrupt contoller does not use vector table to prioritze interrupts
#pragma config MVECEN = ON // Multi-vector enable bit (Multi-vector enabled, Vector table used for interrupts)
Regards,
Dave