完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
大家好,我正在做一个非常简单的程序,但是我遇到了一个问题。我想在我的端口看到一个下降沿时产生一个中断。我知道我必须使用RB0或RB1或RB2端口(我使用的是PIC18F480),但我不能在任何这些端口上进行。你能检查一下我的密码,告诉我问题在哪里吗?
以上来自于百度翻译 以下为原文 Hi everybody, I'm trying to make a very simple program but i've a problem. I want to generate an interrupt when my port see a falling edge. I know that I have to use RB0 or RB1 or RB2 port (i'm using a PIC18F4580), but i can't do it on any of these port. Can you check my code and tell me where the problem can be? #include #include "Config_Bits.h" long counter = 0; void interrupt my_isr() { counter++; if (counter%2==0){PORTBbits.RB2=1; } else {PORTBbits.RB2=0;} INTCONbits.INT0IF=0; } void main(void) { ADCON1=0b00111111; ADCON0bits.ADON=0; INTCON2bits.RBPU=0; TRISBbits.RB1=1; TRISBbits.RB0=1; TRISBbits.RB2=0; PORTBbits.RB2=0; OSCCONbits.IRCF0=1; OSCCONbits.IRCF1=1; OSCCONbits.IRCF2=1; INTCON=0b00000000; INTCON2=0b00000000; INTCON3=0b00000000; RCONbits.IPEN = 0; INTCON2bits.RBPU=1; INTCONbits.INT0IE = 1; INTCON2bits.INTEDG0 = 0; INTCONbits.PEIE=1; INTCONbits.GIE=1; while (1); } |
|
相关推荐
8个回答
|
|
|
在PIC18F上使用LATB而不是PORTB输出:Chaneto:避免可能的RMW错误。这:禁用PORTB上的内部拖动,在RB0上有外部拉出吗?也是一个好主意,在启用Its之前清除Itn0IF标志:
以上来自于百度翻译 以下为原文 On a pic18F use LATB instead of PORTB for output:change if (counter%2==0){PORTBbits.RB2=1; } else {PORTBbits.RB2=0;} TO: if (counter%2==0){LATBbits.RB2=1; else {LATBbits.RB2=0;} Avoids possible RMW errors. This: INTCON2bits.RBPU=1; disables the internal pull-ups on PORTB, do you have an external pullup on RB0? Also a good idea to clear the int0if flag before enabling the ints: INTCONbits.INT0IF = 0; |
|
|
|
|
|
我不确定你正在经历什么问题-你还没有真正告诉我们。根据数据表,你可以在Purb& lt;7:4& gt;(你提到的设备的数据表的第10.8部分)上设置一个中断,但是你似乎在端口上设置了其他引脚。11.2需要读取或写入端口以清除更改检测。当您编写到较低的PoBB位时,我也建议您在ISR中读取端口的习惯,因为ISR将被任何引脚(4到7)的更改触发,并且您通常需要找出它是哪一个。EST,你遵循的模式检查IE和IF位的潜在中断源。你有一个或两个(如果你正在使用优先级设置)ISRS的所有可能的中断。因此,调用一个ISR来处理多个中断触发器是非常普遍的,因此您需要在这个实例中找出哪个调用了。这通常会导致阿西这样的代码在ISR中更新变量,然后需要声明它是易失性的。虽然在代码示例中不关心,但易失性限定符告诉编译器不做任何假设,它知道变量何时会在代码的其余部分中发生变化。
以上来自于百度翻译 以下为原文 I'm not sure what problem you are experiencing - you have not really told us. According to the data sheet you can set up an Interrupt on Change on PORTB<7:4> (Section 10.8 of the data sheet for the device you mention) but you seem to be setting up other pins on the port. Also, as stated in Section 11.2 you need to read or write to the port to clear the change detection. While you do write to the lower PORTB bits, I would also recommend that you get into the habit of reading the port in the ISR as the ISR will be triggered by a change on any of the pins (4 to 7) and you typically need to work out which one it was. Also when you write an ISR for these devices, I suggest that you follow the pattern of checking both the IE and IF bits of the potential interrupt sources. You have one or two (if you are using the priority setup) ISRs for ALL of the possible interrupts. It is therefore very common to have the one ISR called to handle several interrupt triggers and so you need to work out which one called you in this instance. That would normally result in code such as void interrupt my_isr() { if( INTCONbits.INT0IF && INTCONbits.INT0IE) { // Put your interrupt handler code here INTCONbits.INT0IF = 0; } // Test for other interrupt triggers here } If you are updating a variable in an ISR then it needs to be declared volatile. While you don't care in your code example, the volatile qualifier tells the compiler not to make any assumptions that it knows when the variable will change in the rest of the code. Susan |
|
|
|
|
|
好的,我已经改正了我的代码,但是我的问题仍然存在。如果我使用相同的程序而不使用中断,它工作得很好,但是我需要产生一个中断,而我不能。
以上来自于百度翻译 以下为原文 Ok, I've correct my code, but my problem remains. If I make the same program without using the interrupt it works fine, but I need to generate an interrupt and I can't. The corrected code is #include #include "Config_Bits.h" volatile long counter = 0; int giri =0; void interrupt my_isr() { if( INTCONbits.INT0IF && INTCONbits.INT0IE) { counter++; if (counter%2==0){LATBbits.LATB2=1; } else {LATBbits.LATB2=0; } INTCONbits.INT0IF = 0; } } void main(void) { ADCON1=0b00111111; ADCON0bits.ADON=0; TRISBbits.RB1=1; TRISBbits.RB0=1; TRISBbits.RB2=0; PORTBbits.RB2=0; OSCCONbits.IRCF0=1; OSCCONbits.IRCF1=1; OSCCONbits.IRCF2=1; INTCON=0b00000000; INTCON2=0b00000000; INTCON3=0b00000000; RCONbits.IPEN = 0; INTCON2bits.RBPU=0; INTCONbits.INT0IF=0; INTCONbits.INT0IE = 1; INTCON2bits.INTEDG0 = 0; INTCONbits.PEIE=1; INTCONbits.GIE=1; while (1); } |
|
|
|
|
|
|
|
|
|
|
|
我仍然不清楚什么是“Works文件”,或者问题是什么仍然存在!苏珊
以上来自于百度翻译 以下为原文 I am still not clear on what "works file" means, or what the problem is that remains! Susan |
|
|
|
|
|
好,所以在没有中断的情况下发布“同一个程序”,这样我们就可以看到你想要做什么。
以上来自于百度翻译 以下为原文 Ok so post the "same program without interrupt" so we maybe can see what you are trying to do. |
|
|
|
|
|
您好,我想问题可能是您忘记将RB0/It0/AN10编程为数字PIN。AdCON0BIT。PCFG=0B0101;NB: PCFG的默认重置值取决于PBADEN配置位(参见手册页248中的注释1)BTW:如果您正在启动项目,则应考虑PIC18F45 K80而不是PIC。它是兼容的,但它具有更多的特性和灵活性,一个接一个地选择模拟输入。另外,它可以运行60%快(64 MHz,而不是40MHz)。
以上来自于百度翻译 以下为原文 Hi, I think maybe the issue is that you forgot to program RB0/INT0/AN10 as a digital pin. ADCON0bits.PCFG=0b0101; NB: the default reset value of PCFG depends upon PBADEN configuration bits (see note 1 in manual page 248) BTW : if you are starting a project you should consider PIC18F45K80 instead of PIC18F4580. It is compatible but it has much more features and flexibility espeically for selecting analog inputs one by one. Additionally it can run 60% faster (64MHz instead of 40MHz) Regards |
|
|
|
|
|
并且成本几乎为PIC18F480的1/2。
以上来自于百度翻译 以下为原文 And also costs almost 1/2 as much as the PIC18F4580. |
|
|
|
|
只有小组成员才能发言,加入小组>>
MPLAB X IDE V6.25版本怎么对bootloader和应用程序进行烧录
485 浏览 0 评论
5806 浏览 9 评论
2346 浏览 8 评论
2234 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3540 浏览 3 评论
1145浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
1114浏览 1评论
我是Microchip 的代理商,有PIC16F1829T-I/SS 技术问题可以咨询我,微信:A-chip-Ti
883浏览 1评论
MPLAB X IDE V6.25版本怎么对bootloader和应用程序进行烧录
487浏览 0评论
/9
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-12-9 06:25 , Processed in 1.064610 second(s), Total 86, Slave 69 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖
449