一字节触发 用定时器监控UART是否把数据全部接收,然后一起发送

#include "config.h"
#define BPS 9600
uint8 rcv_buf[];
volatile uint8 TimeOut;
uint32 SP485_DE=1<<17;
uint32 UartRcvPointer;

void DelayNS (uint32 dly);

void __irq IRQ_Uart0(void);
void __irq IRQ_Timer0(void);
void Init_Uart0(void);
void Init_Timer0(void);

void SendByte(uint8 dat);
void SendBuf(void);

int main (void)
{// add user source code
     TimeOut=0;
     PINSEL0=0x05;
     IO0DIR=SP485_DE;
     IO0CLR=SP485_DE;
       
     Init_Uart0();
     Init_Timer0();        
     IRQEnable();
     while(1)
     {
          if(TimeOut==1)
          {
               TimeOut=0;
               T0TCR=0x00; //关闭定时器
               IO0SET|=SP485_DE;
               SendBuf();
        
               IO0CLR|=SP485_DE;
               DelayNS(2);
          }
     }
    return 0;
}

void Init_Timer0(void)
{
 T0TC=0;
 T0PR=99;
 T0MCR=0x03;  //匹配后复位TC并产生中断标志
 T0MR0=Fpclk ;
 
 
 VICIntSelect = 0x00000000; //设置所有通道为IRQ中断
 VICVectCntl4 = 0x20 | 0x04;//Timer0  分配到IRQ  slot0,即最高中断
 VICVectAddr4 = (uint32 )IRQ_Timer0;   //设置Timer0向量地址
 VICIntEnable |= 1<<4;            //Timer0中断

void __irq IRQ_Timer0(void)
{
 TimeOut=1; //定时器标志
  
 T0IR=0x01;
 VICVectAddr=0x00;
}

void __irq IRQ_Uart0(void)//中断服务程序
{
  do
       {
           switch (U0IIR & 0x0E)
              {                   
                     case  0x04:                //接收中断,FIFO满中断     
                          {
                           rcv_buf[UartRcvPointer] = U0RBR;                          
                           UartRcvPointer=(UartRcvPointer+1) & 0x1FF; 
                           break;
                          }
                     case 0x0C:                 //接收超时中断                                 
                           
                           break;
                          
                     case  0x02:                //发送中断(THRE中断,主函数中UART0发送数据时中断)                    
                           break;
                          
                     case  0x06:                //接收线状态        
                           break;

                     default:
                           break;
              }
             
       }
     while ((U0IIR & 0x01) == 0);
    
     T0TCR=0x01; //启动定时器
     T0TC=0;  
     VICVectAddr = 0x00; 

   
}

void SendByte(uint8 dat)
{
    U0THR = dat;
    while((U0LSR & 0x40) == 0);

}

void SendBuf(void)
{
   
     uint8 i;
     for(i=0;i<UartRcvPointer;i++)
          SendByte(rcv_buf[i]);
     UartRcvPointer=0;
}

void Init_Uart0(void)
{

    uint16 Fdiv;
  
    U0LCR = 0x83 ;//允许设置波特率
    Fdiv = (Fpclk / 16) /BPS;
    U0DLM = Fdiv /256;
    U0DLL = Fdiv % 256;
    U0LCR = 0x03;
   
    U0FCR=0x01;//一字节触发
    U0IER=0x01;
   
 VICIntSelect = 0x00000000; //设置所有通道为IRQ中断
 VICVectCntl0 = 0x20 | 0x06;//UART0  分配到IRQ  slot0,即最高中断
 VICVectAddr0 = (uint32 )IRQ_Uart0;   //设置UART0向量地址
 VICIntEnable |= 1<<6;            //使能UART0、Timer0中断
}


void DelayNS (uint32 dly)
{
     uint32 i;
   
     for ( ; dly>0; dly--)
          for (i=0; i<50000; i++);
}

 

 

程序不通 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值