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

来源:互联网 发布:淘宝大学电话 编辑:程序博客网 时间:2024/05/22 17:32

#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++);
}

 

 

程序不通 

原创粉丝点击