PIC - RB7 有价值吗?
我读取了端口 RB7,并在 if a 中检查该值。如果 RB7 上有输出,我希望芯片上的 LED 亮起(LED D1),但即使没有任何东西连接到 RB7,它也会一直燃烧。我做错了什么?这就是 PIC 18F4550 它是用 mplab v8.63 和 C18 编译器编写的。
void main (void)
{
TRISD = 0x00; // PORTD als uitgang
TRISB = 0b00110000; // RB4 en RB5 als ingang
RCONbits.IPEN = 0; // prioriteit uit
INTCONbits.GIE = 1; // enable interrupt
INTCONbits.RBIE = 1; // interrupt portB aan
TRISBbits.TRISB7 = 0;
TRISBbits.TRISB6 = 0;
TRISBbits.TRISB3 = 0;
while(1)
{
_asm sleep _endasm
}
}
#pragma interrupt ISR
void ISR (void)
{
if (INTCONbits.RBIF==1)
{
if(LATBbits.LATB7 == 1) // value on RB7 ?
{
LATDbits.LATD1 ^= 1; // D2 togglen
}
}
INTCONbits.RBIF = 0;
}
I read the port RB7, and in the if a check the value. if there is output on RB7 I want that the led on my chip light up (led D1), but it burns all the time even when there is nothing connected to RB7. What i'm doing wrong? That's the PIC 18F4550 It's written in mplab v8.63 and the C18 compiler.
void main (void)
{
TRISD = 0x00; // PORTD als uitgang
TRISB = 0b00110000; // RB4 en RB5 als ingang
RCONbits.IPEN = 0; // prioriteit uit
INTCONbits.GIE = 1; // enable interrupt
INTCONbits.RBIE = 1; // interrupt portB aan
TRISBbits.TRISB7 = 0;
TRISBbits.TRISB6 = 0;
TRISBbits.TRISB3 = 0;
while(1)
{
_asm sleep _endasm
}
}
#pragma interrupt ISR
void ISR (void)
{
if (INTCONbits.RBIF==1)
{
if(LATBbits.LATB7 == 1) // value on RB7 ?
{
LATDbits.LATD1 ^= 1; // D2 togglen
}
}
INTCONbits.RBIF = 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
三件事:
Three things:
您正在读回 B7 上的输出值,这没有多大意义。 B7 将会被清除(低),除非您在代码中设置它,因此 B7 不会发生任何变化。在任何情况下,B7 输出锁存器的变化都不会触发 RBIF。
当RB7至RB4中的任何一个改变状态时,RBIF被置位。您已将 RB5 和 RB4 声明为输入,那么为什么不在 RBIF 触发时读取其中之一呢?
如果您在使用端口更改中断功能时遇到问题,只需尝试主循环代码中的以下行来证明您的 IO。
如果您的硬件没有上拉或下拉,则应使用 INTCON2 中的 RBPU 标志在端口 B 上启用弱上拉。请注意,当您将引脚声明为输出时,这些会自动关闭。
You are reading back the output value on B7, which doesn't make a lot of sense. B7 is will be clear (low) unless you set it in code so there are no changes going on with B7. In any case, changes to the output latch for B7 would not trigger the RBIF.
The RBIF is set when any of RB7 to RB4 changes state. You have declared RB5 and RB4 as inputs, so why don't you read one of those when the RBIF fires?
If you are having trouble with the interrupt-on-port-change functionality, just try the following line in main loop code to prove your IO.
If your hardware does not have pull ups or pull downs, you should enable the weak pull ups on port B using the RBPU flag in INTCON2. Note that these are automatically turned off when you declare the pin to be an output.