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

[复制链接]
 楼主| wfxsgsg 发表于 2017-10-25 09:29 | 显示全部楼层 |阅读模式
本帖最后由 wfxsgsg 于 2017-10-25 09:30 编辑

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

  7. byte ADDR;

  8. void GPIO_Init(void)
  9. {
  10.     PUCR=0x00;//PK/PE/BKGD都不使用上拉
  11.     DDRK=0x0f;//蜂鸣器和三色灯对应IO口:输出模式
  12.     DDRS=0x02;//485串口输入输出
  13.     DDRM=0x40;//485的DERE:输出
  14.     DDRP=0xff;//PWM输出
  15. }
  16. void main(void)
  17. {
  18.     DisableInterrupts;
  19.     PLL_Init();
  20.     GPIO_Init();
  21.     ADDR=PTM&0x0f;//限制地址最大到15
  22.     PORTK_PK0=1;//红灭
  23.     PORTK_PK1=0;//绿亮
  24.     PORTK_PK2=1;//蓝灭
  25.     UART0_Init();
  26.     PWM_Init();
  27.     ADC_Init();
  28.     RTI_Init();
  29.     EnableInterrupts;
  30.     for(;;)
  31.     {
  32.         _FEED_COP();
  33.         //UART0_Send_Byte(0x64);
  34.         UART0_Data_Handle();
  35.     }
  36. }
2、pll.c
  1. #include <hidef.h>
  2. #include "derivative.h"

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

  3. byte rxdcnt=0,framebit=0;
  4. byte rxdbuff[40];
  5. extern byte ADDR;

  6. void UART0_Complete_Frame(byte);
  7. void RTI_Init(void)
  8. {
  9.     CRGINT = 0x80;//使能实时中断
  10.     RTICTL = 0x8f;//设置分频:16*10^3,实时中断频率=fBUS/16*10^3=1kHz   
  11. }
  12. void UART0_Init(void)
  13. {
  14.     SCI0BDL=0x68;//波特率9600=fBUS/(16*0x68)=16M/(16*0x68)
  15.     SCI0CR1=0x00;//设置SCI0为正常模式,八位数据位,无奇偶校验
  16.     SCI0CR2=0x2c;//打开接收中断,允许发送和接收
  17.     PTM |= 0x40;//MAX485接收状态
  18.     //PTM &= ~0x40;//MAX485发送状态
  19. }
  20. #pragma CODE_SEG __NEAR_SEG NON_BANKED
  21. interrupt 20 void SCI_Interrupt(void)
  22. {
  23.     if(SCI0SR1_RDRF)
  24.         rxdbuff[rxdcnt++]=SCI0DRL;
  25. }
  26. interrupt 7 void RTI_Interrupt(void)
  27. {
  28.     if(CRG**_RTIF)
  29.     {
  30.         CRG**_RTIF = 0;
  31.         UART0_Complete_Frame(1);
  32.     }
  33. }
  34. #pragma CODE_SEG DEFAULT
  35. void UART0_Send_Byte(byte data)
  36. {
  37.     while(!SCI0SR1_TC);//等待发送器空闲
  38.     while(!SCI0SR1_TDRE);//等待发送保持器空闲
  39.     SCI0DRL=data;
  40. }
  41. void UART0_Send_Frame(byte *buf,byte len)
  42. {
  43.     PTM &= ~0x40;//MAX485发送状态
  44.     while(len--)
  45.     {
  46.         UART0_Send_Byte(*buf);
  47.         buf++;
  48.     }
  49.     PTM |= 0x40;//MAX485接收状态
  50. }
  51. void UART0_Data_Handle(void)
  52. {
  53.     if(framebit)
  54.     {
  55.         framebit=0;
  56.         UART0_Send_Frame(rxdbuff,rxdcnt);
  57.     }
  58. }
  59. void UART0_Complete_Frame(byte ms)//串口完整帧判断函数
  60. {
  61.     static byte rxdcntplus=0,idletime=0;
  62.     if(rxdcnt>0)//说明有数据接收
  63.     {
  64.         if(rxdcntplus != rxdcnt)//不相等说明rxdcnt的值变了
  65.         {
  66.             rxdcntplus = rxdcnt;//跟上去
  67.             idletime = 0;//待机时间清零
  68.         }
  69.         else if(idletime<5)//相等并且时间<5个ms,继续等
  70.         {
  71.             idletime+=ms;
  72.             if(idletime>=5)//再加一个ms之后>5,就判定为一帧接收完毕
  73.                 framebit=1;//接收一帧完成标志位
  74.         }
  75.     }
  76.     else//否则就清零rxdcntplus
  77.         rxdcntplus=0;
  78. }


您需要登录后才可以回帖 登录 | 注册

本版积分规则

4

主题

29

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部

4

主题

29

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部