本帖最后由 chandler-00 于 2012-6-26 15:39 编辑
先上程序,打算用rb0口的外部中断,中断计数两次控制rd0 ,rd1端口电压反向,可是中断一直进入不了用的是hitech 的编译器
求大神帮助!!!求hitech 中断的例程
//#include <p18F4685.h>
#include <htc.h>
#pragma config OSC=HS
#pragma config WDT=OFF
#pragma config LVP=OFF
unsigned char i;
void inter();
void interrupt HI_ISR()
{
if (INTCONbits.INT0IF == 1)
inter();
}
void main(void)
{
i = 0;
//端口输入输出设置 初始化
TRISDbits.TRISD0 = 0;
TRISDbits.TRISD1 = 0;
PORTDbits.RD0 = 0;
PORTDbits.RD1 = 1;
TRISBbits.TRISB0 = 1; // RB0设置输入 ,外部中断
//中断允许
INTCONbits.PEIE = 1; // peripheral interrupt enable
INTCONbits.GIE = 1; // global interrupt enable
INTCONbits.INT0IE = 1; //允许int0外部中断
INTCONbits.INT0IF = 0; //INT0外部中断标志为清零
//INTCONbits.RBIE = 1 ; //允许rb端口电平变化中断
//INTCON2bits.RBIP = 1; //int0始终高优先级
while (1) //等待中断发生
{
if (i = 2)
{
PORTDbits.RD0 = ~PORTDbits.RD0;
PORTDbits.RD1 = ~PORTDbits.RD1;
i = 0 ;
}
}
}
void inter()
{
i ++;
INTCONbits.INT0IF = 0 ;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
In that case, you are assuming that your debugger is behaving correctly, which may not be true.
the usual way here is to flip a pin in the isr.
Looks like you didn't disable other functions on RB0. Look up the datasheet for that.
That's just the shittest piece of code.
“Looks like you didn't disable other functions on RB0. Look up the datasheet for that.”
I have checked ,and this is the latest code , it did not work ! by the way , if (i = 2) is a obvious mistake ,I have changed it , Thanks !
#include <htc.h>
unsigned char i;
void interrupt HI_ISR()
{
if (INTCONbits.INT0IF == 1)
{
i ++;
INTCONbits.INT0IF = 0 ;
}
}
void main(void)
{
i = 0;
//端口输入输出设置 初始化
ADCON1|= 0x0F; //********将RB0等设置为数字I/O
TRISD0 = 0;
TRISD1 = 0;
RD0 = 0;
RD1 = 1;
TRISB0 = 1; // RB0设置输入 ,外部中断
//中断允许
IPEN = 1 ; //使能中断优先级
INT0IF = 0; //INT0外部中断标志为清零
INTEDG0 = 1; //上升沿触发
RBIF = 0 ; // RB4~RB7 电平变化标志位清零
INT1IF = 0 ; //INT1外部中断标志位清零
RBIP = 1; //int0始终高优先级
INT0IE = 1; //允许int0外部中断
PEIE = 1; // peripheral interrupt enable
GIE = 1; // global interrupt enable
while (1) //等待中断发生
{
if (i == 2) //如果频率过高,可能永远也执行不了
{
RD0 = ~RD0;
RD1 = ~RD1;
i = 0 ;
}
}
}
- void interrupt int0_isr(void) {
- static unsigned char i=0;
-
- INT0IF = 0; //clera the flag
- i+=1; //increment the counter
- if (i==2) {
- IO_FLP(OUT_PORT, OUT); //flip out
- i=0; //reset the counter
- }
- }
-
- void int0_init(void) {
- IO_CLR(OUT_PORT, OUT); //clear out
- IO_OUT(OUT_DDR, OUT); //out as output
-
- IO_IN(INT0_DDR, INT0); //int0 as input
-
- INTEDG0 = 1; //intrrupt on rising edge
-
- INT0IF = 0; //clera the flag
- INT0IE = 1; //enable int0 isr
- }
-
复制代码int0_init() sets up the input and output pins, and activates the isr. in the isr, we use a counter to count the number of rising edges on int0 before the output pin is flipped.they all follow roughly the same scheme to set up and to run.
那以前为什么近不了中断啊1!!!
最初的程序不行是因为ADCON1寄存器没有配置,我发的第二个程序实际上是可以进入中断的,汗个。就是在线调试时点的单步连续运行的问题,用连续运行的方式就可以了 不知连续运行和单步连续运行有什么区别
新手总是被各种问题困扰啊!
一周热门 更多>