wfxsgsg 发表于 2017-10-25 09:29

中断问题 MC9S12XS128 串口中断不能接收数据,查询可以

本帖最后由 wfxsgsg 于 2017-10-25 09:30 编辑

目的:用MC9S12XS128进行485通讯
问题:使用中断函数,使能中断,板子连接电脑串口助手,收不到数据;但是,不用中断,用查询等待的方式接收数据,完全没问题,串口助手都能收到。初步断定是中断出了问题,但又不知怎么改,以下是我的代码,请大神帮忙看看,代码哪里出了问题。
1、main.c
#include <hidef.h>
#include "derivative.h"
#include "pwm.h"
#include "adc.h"
#include "uart.h"
#include "pll.h"

byte ADDR;

void GPIO_Init(void)
{
    PUCR=0x00;//PK/PE/BKGD都不使用上拉
    DDRK=0x0f;//蜂鸣器和三色灯对应IO口:输出模式
    DDRS=0x02;//485串口输入输出
    DDRM=0x40;//485的DERE:输出
    DDRP=0xff;//PWM输出
}
void main(void)
{
    DisableInterrupts;
    PLL_Init();
    GPIO_Init();
    ADDR=PTM&0x0f;//限制地址最大到15
    PORTK_PK0=1;//红灭
    PORTK_PK1=0;//绿亮
    PORTK_PK2=1;//蓝灭
    UART0_Init();
    PWM_Init();
    ADC_Init();
    RTI_Init();
    EnableInterrupts;
    for(;;)
    {
      _FEED_COP();
      //UART0_Send_Byte(0x64);
      UART0_Data_Handle();
    }
}2、pll.c
#include <hidef.h>
#include "derivative.h"

void PLL_Init(void)
{
    CLKSEL &= ~0x80;
    PLLCTL &= ~0x70;//失能锁相环
    REFDV = 0xc0;//fREF=fOSC=16MHz,fBUS=fOSC/2=8MHz
    PLLCTL |= 0x70;//使能锁相环
    asm NOP;
    asm NOP;
    while(!(CRG**&0x08));
    CLKSEL |= 0x80;//fBUS=fPLL/2=16MHz
}3、uart.c
#include <hidef.h>
#include "derivative.h"

byte rxdcnt=0,framebit=0;
byte rxdbuff;
extern byte ADDR;

void UART0_Complete_Frame(byte);
void RTI_Init(void)
{
    CRGINT = 0x80;//使能实时中断
    RTICTL = 0x8f;//设置分频:16*10^3,实时中断频率=fBUS/16*10^3=1kHz   
}
void UART0_Init(void)
{
    SCI0BDL=0x68;//波特率9600=fBUS/(16*0x68)=16M/(16*0x68)
    SCI0CR1=0x00;//设置SCI0为正常模式,八位数据位,无奇偶校验
    SCI0CR2=0x2c;//打开接收中断,允许发送和接收
    PTM |= 0x40;//MAX485接收状态
    //PTM &= ~0x40;//MAX485发送状态
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED
interrupt 20 void SCI_Interrupt(void)
{
    if(SCI0SR1_RDRF)
      rxdbuff=SCI0DRL;
}
interrupt 7 void RTI_Interrupt(void)
{
    if(CRG**_RTIF)
    {
      CRG**_RTIF = 0;
      UART0_Complete_Frame(1);
    }
}
#pragma CODE_SEG DEFAULT
void UART0_Send_Byte(byte data)
{
    while(!SCI0SR1_TC);//等待发送器空闲
    while(!SCI0SR1_TDRE);//等待发送保持器空闲
    SCI0DRL=data;
}
void UART0_Send_Frame(byte *buf,byte len)
{
    PTM &= ~0x40;//MAX485发送状态
    while(len--)
    {
      UART0_Send_Byte(*buf);
      buf++;
    }
    PTM |= 0x40;//MAX485接收状态
}
void UART0_Data_Handle(void)
{
    if(framebit)
    {
      framebit=0;
      UART0_Send_Frame(rxdbuff,rxdcnt);
    }
}
void UART0_Complete_Frame(byte ms)//串口完整帧判断函数
{
    static byte rxdcntplus=0,idletime=0;
    if(rxdcnt>0)//说明有数据接收
    {
      if(rxdcntplus != rxdcnt)//不相等说明rxdcnt的值变了
      {
            rxdcntplus = rxdcnt;//跟上去
            idletime = 0;//待机时间清零
      }
      else if(idletime<5)//相等并且时间<5个ms,继续等
      {
            idletime+=ms;
            if(idletime>=5)//再加一个ms之后>5,就判定为一帧接收完毕
                framebit=1;//接收一帧完成标志位
      }
    }
    else//否则就清零rxdcntplus
      rxdcntplus=0;
}

页: [1]
查看完整版本: 中断问题 MC9S12XS128 串口中断不能接收数据,查询可以