使用 Hi Tech PICC 设置 PIC16F876 中的寄存器值
我正在使用 MPLABx 和 HI Tech PICC 编译器。我的目标芯片是PIC16F876。通过查看 pic16f876.h 包含文件,似乎应该可以通过名称引用来设置芯片的系统寄存器。
例如,在 CCP1CON 寄存器中,位 0 至 3 设置 CCP 和 PWM 模块的工作方式。通过查看 pic16f876.h 文件,看起来应该可以单独引用这 4 位,而无需更改 CCP1CON 寄存器其余部分的值。
然而,我尝试以多种方式引用这 4 位,但没有成功。
我已经尝试过了;
CCP1CON.CCP1M=0xC0; this results in "error: struct/union required
CCP1CON:CCP1M=0xC0; this results in "error: undefined identifier "CCP1M"
但两者都失败了。我已经阅读了 Hi Tech PICC 编译器手册,但不知道如何执行此操作。
从 pic16f876.h 文件中,我认为我应该能够按名称引用系统寄存器中的这些子集,因为它们是在 .h 文件中定义的。 有谁知道如何做到这一点?
摘自 pic16f876.h
// Register: CCP1CON
volatile unsigned char CCP1CON @ 0x017;
// bit and bitfield definitions
volatile bit CCP1Y @ ((unsigned)&CCP1CON*8)+4;
volatile bit CCP1X @ ((unsigned)&CCP1CON*8)+5;
volatile bit CCP1M0 @ ((unsigned)&CCP1CON*8)+0;
volatile bit CCP1M1 @ ((unsigned)&CCP1CON*8)+1;
volatile bit CCP1M2 @ ((unsigned)&CCP1CON*8)+2;
volatile bit CCP1M3 @ ((unsigned)&CCP1CON*8)+3;
#ifndef _LIB_BUILD
volatile union {
struct {
unsigned CCP1M : 4;
unsigned CCP1Y : 1;
unsigned CCP1X : 1;
};
struct {
unsigned CCP1M0 : 1;
unsigned CCP1M1 : 1;
unsigned CCP1M2 : 1;
unsigned CCP1M3 : 1;
};
} CCP1CONbits @ 0x017;
#endif
I am using MPLABx and the HI Tech PICC compiler. My target chip is a PIC16F876. By looking at the pic16f876.h include file, it appears that it should be possible to set the system registers of the chip by referring to them by name.
For example, within the CCP1CON register, bits 0 to 3 set how the CCP and PWM modules work. By looking at the pic16f876.h file, it looks like it should be possible to refer to these 4 bits alone, without change the value of the rest of the CCP1CON register.
However, I have tried to refer to these 4 bits in a variety of ways with no success.
I have tried;
CCP1CON.CCP1M=0xC0; this results in "error: struct/union required
CCP1CON:CCP1M=0xC0; this results in "error: undefined identifier "CCP1M"
but both have failed. I have read through the Hi Tech PICC compiler manual, but cannot see how to do this.
From the pic16f876.h file, it looks to me as though I should be able to refer to these subsets within the system registers by name, as they are defined in the .h file.
Does anyone know how to accomplish this?
Excerpt from pic16f876.h
// Register: CCP1CON
volatile unsigned char CCP1CON @ 0x017;
// bit and bitfield definitions
volatile bit CCP1Y @ ((unsigned)&CCP1CON*8)+4;
volatile bit CCP1X @ ((unsigned)&CCP1CON*8)+5;
volatile bit CCP1M0 @ ((unsigned)&CCP1CON*8)+0;
volatile bit CCP1M1 @ ((unsigned)&CCP1CON*8)+1;
volatile bit CCP1M2 @ ((unsigned)&CCP1CON*8)+2;
volatile bit CCP1M3 @ ((unsigned)&CCP1CON*8)+3;
#ifndef _LIB_BUILD
volatile union {
struct {
unsigned CCP1M : 4;
unsigned CCP1Y : 1;
unsigned CCP1X : 1;
};
struct {
unsigned CCP1M0 : 1;
unsigned CCP1M1 : 1;
unsigned CCP1M2 : 1;
unsigned CCP1M3 : 1;
};
} CCP1CONbits @ 0x017;
#endif
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要通过结构体的实例访问位域成员。在本例中,即
CCP1CONbits
。因为它是一个位字段,所以您只需要具有位字段中定义的有效位数,而不是代码中的完整八位。所以:
应该相当于你正在尝试做的事情。如果您想一次设置所有八位,可以使用
CCP1CON = 0xc0
。这会将 CCP1M 位设置为 0x0c,并将所有其他位设置为零。您提供的标头也有单独的位符号,因此您也可以这样做:
尽管位域方法更干净。
You need to access the bitfield members through an instance of a struct. In this case, that is
CCP1CONbits
. Because it is a bitfield, you only need to have the number of significant bits as defined in the bitfield, not the full eight bits in your code.So:
Should be the equivalent of what you are trying to do. If you want to set all eight bits at once you can use
CCP1CON = 0xc0
. That would set the CCP1M bits to 0x0c and all the other bits to zero.The header you gave also has individual bit symbols, so you could do this too:
Although the bitfield approach is cleaner.