#include"uart.h"
static uint8 UART0_Rec_Buffer[UART0_LEN];
volatile static uint8 UART0_Rec_pWrite = 0;
volatile static uint8 UART0_Rec_pRead = 0;
static uint8 UART0_Send_Buffer[UART0_LEN];
volatile static uint8 UART0_Send_Num = 0;
volatile static uint8 UART0_Send_pRead = 0;
volatile static uint8 UART0_Send_pWrite = 0;
void UART0_Init(void)
{
uint16 temp;
U0LCR |= 0X80;
temp = 11059200/(8*9600);
U0DLL = temp &0xff;
U0DLM = temp >> 8;
U0LCR = 0x1B; //0001 1011
U0IER = 0x03;
//U0FCR = 0X80 A(本想先置为触发,然后开中断,然后使能uart0,中断进入错误)
NVIC_EnableIRQ(UART0_IRQn);
//U0FCR = 0X80 B(调整到开中断后设置触发,使能UART0,中断进入错误)
//U0FCR |= 0X01;
U0FCR = 0x87; 一次性写入触发和使能中断,正确的进入中断
}
uint8 UART0_Send(uint8* pData,uint8 num)
{
uint8 i=0;
if(pData == NULL || num == 0)
return 0;
while(UART0_Send_Num0)
{
UART0_Send_Buffer[UART0_Send_pWrite++] = *pData++;
if(UART0_Send_pWrite >= UART0_LEN)
{
UART0_Send_pWrite = 0;
}
UART0_Send_Num++;
num--;
i++;
}
if((U0LSR & 0X20)&&UART0_Send_Num>0)
{
U0THR = UART0_Send_Buffer[UART0_Send_pRead++];
if(UART0_Send_pRead >= UART0_LEN)
{
UART0_Send_pRead = 0;
}
UART0_Send_Num--;
}
return i;
}
uint8 UART0_Read(uint8* pData,uint8 num,uint8 offset)
{
return 0;
}
uint8 UART0_Copy(uint8* pData,uint8 num)
{
uint8 i = 0;
if(pData == NULL || num == 0 || UART0_Rec_pWrite == UART0_Rec_pRead)
return 0;
for(;num>0;num--)
{
*pData++ = UART0_Rec_Buffer[UART0_Rec_pRead++];
if(UART0_Rec_pRead >= UART0_LEN)
{
UART0_Rec_pRead = 0;
}
i++;
if(UART0_Rec_pRead == UART0_Rec_pWrite)
break;
}
return i;
}
void UART0_IRQHandler(void)
{
uint8 list = (U0IIR & 0X0F)>>1;
uint8 i;
switch(list)
{
case 0x02:
{
for(i=0;i<8;i++)
{
UART0_Rec_Buffer[UART0_Rec_pWrite++] = U0RBR;
if(UART0_Rec_pWrite >= UART0_LEN)
{
UART0_Rec_pWrite = 0;
}
}
}break;
case 0x06:
{
while(U0LSR &0X01)
{
UART0_Rec_Buffer[UART0_Rec_pWrite++] = U0RBR;
if(UART0_Rec_pWrite >= UART0_LEN)
{
UART0_Rec_pWrite = 0;
}
}
}break;
case 0x01:
{
if(UART0_Send_Num == 0)
break;
for(i=0;i<16;i++)
{
U0THR = UART0_Send_Buffer[UART0_Send_pRead++];
if(UART0_Send_pRead >= UART0_LEN)
{
UART0_Send_pRead = 0;
}
UART0_Send_Num--;
if(UART0_Send_Num == 0)
break;
}
}break;
default:break;
}
}
中断错误的主要现象是进入RDA和CTI错误,无论发送几个字节数据,总是进入RDA中断;
这是为什么啊?哪位高手给指点下,分两次操作UOFCR为何会错误,我也只是在不经意间发现的这个问题