我写了一个程序,但是不知道为什么总是没法进入到定时器中断里面,大家帮我看一下哪里出错了
#include "p18f25j10.h"
unsigned char temp,time_count=0;
unsigned char receive_flag;
void send(unsigned char data);
void delay(unsigned char i);
//定时处理子程序
void dealtime(void)
{
for( time_count=0;time_count<=100;time_count++);
}
void init(void) //系统初始化
{
INTCON=0x00;
ADCON1=0x07;
TRISC=TRISC|0x80;
TRISC=TRISC&0xbf;
TRISB=0x00;
PIE1=0; //关闭所有中断
PIE2=0;
PIE3=0;
}
//串口初始化
void usart_init(void)
{
PIR1bits.RCIF=0; //中断标志位清零
PIE1bits.RCIE=1; //中断使能
IPR1bits.RCIP=1; //中断优先级设为高
INTCON=0xc0;
RCONbits.IPEN=1; //开总中断
TRISCbits.TRISC6=0;
TRISCbits.TRISC7=1;
TXSTA=0x24; //TXEN=1,
RCSTA=0x90; //SPEN=1,CREN=1,开放tx,RX,允许连续接受数据
BAUDCON=0x08; //波特率位16位
SPBRGH=0x00; //8M晶振,9600波特率
SPBRG=0xcf;
}
//定时器初始化
void time0_init(void)
{
//TMR0H=0x9e;
//TMR0L=0x58; //定时50ms
INTCONbits.GIE=1; //开中断和外围中断
INTCONbits.PEIE=1;
INTCONbits.TMR0IE=1; //开定时器中断允许位
INTCONbits.TMR0IF=0; //定时器标志位清零
T0CON=0x08; //16位定时器,无预分频
INTCON2bits.TMR0IP=0; //设定定时器中断为低优先级中断
RCONbits.IPEN=1; //开总中断
}
//串口终端服务程序
#pragma interrupt high_uart
void high_uart()
{
if(PIR1bits.RCIF==1)
{
temp=RCREG;
TXREG=temp;
receive_flag=1; //接收中断标志位置位
while(TXSTAbits.TRMT==1)break;
}
}
//定时器0中断服务程序
#pragma interrupt time0
void time0()
{
if(INTCONbits.TMR0IF==1)
{
INTCONbits.TMR0IF=0;
TMR0H=0x9e;
TMR0L=0x58;
dealtime();
}
}
#pragma code InterruptVectorHigh=0x08
void InterruptVectorHigh(void)
{
_asm
goto high_uart //跳到串口中断程序
_endasm
}
#pragma code
#pragma code InterruptVectorLow=0x18
void InterruptVectorLow(void)
{
_asm
goto time0 //跳到定时器0程序
_endasm
}
#pragma code
void main(void)
{
init();
usart_init();
time0_init();
while(1)
{
send(0x06); //发送06;
delay(10);//试用延时,额外附加
if(receive_flag==1) //判断是否有接收数据
{
receive_flag=0; //接收到数据,标志位清零
send(0x06);
_asm
nop
nop
_endasm //再次发送06
}
else 程序总是走到这边给调到结尾去了
{
T0CONbits.TMR0ON=1; //开定时器0
TMR0H=0x9e;
TMR0L=0x58;
if(time_count==50) //为接收到数据,定时2.5s
{
time_count=0;
send(0x53); //定时1s发送53
delay(10);//试用延时,额外附加
}
}
}
}
void send(unsigned char data)
{
TXREG=data;
while(TXSTAbits.TRMT==1)break;
}
void delay(unsigned char i)
{
unsigned char j,k;
for(j=i;j>0;j--)
for(k=100;k>0;k--);
} |