微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > LPC21XX 串口的接收和发送中断(MDK)

LPC21XX 串口的接收和发送中断(MDK)

时间:11-09 来源:互联网 点击:
由于使用检测方式仅适合于数据量少的设备中使用,当需要发送或者接收大量数据时应该采用串口中断接收或发送的方式。

/*============================================================

LPC21XX 串口使用接收发送中断

==============================================================

本例程的CCLK=60M.

晶振采用12M

倍频系数为:5

分频系数为:2

以上设置在Startup.s中设置

*/

#include

#include "Config.H"

#define UART_BAUD(baud) (unsigned int)(Fpclk/(baud * 16))

uint8 buffer[100];

uint8 SendLen;//发送数据长度

uint8 SendCount;//数据发送计数

uint8 SendData[256];

void Init_Uart0(unsigned int Baud)

{

/* initialize the serial interface */

PINSEL0 = 0x00000005; /* Enable RxD0 and TxD0 */

U0LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */

U0DLM=(unsigned char)(Baud>>8);

U0DLL = (unsigned char)Baud;

U0LCR = 0x03; /* DLAB = 0 */

}

void delay (unsigned int i)

{ /* Delay function */

unsigned int n;

while(i>1)

{

for(n=65535;n>1;n--);

i--;

}

}

//发送普通二进制数

void Sent_Byte(uint8 *da,uint8 len)

{

uint8 i;

for(i=0;i

SendData[i]=da[i];

SendLen=len;

U0THR = SendData[0];

SendCount=1;

}

// 发送字符

void Sent_Str(unsigned char const *str)

{

uint8 i=0;

while(1)

{

SendData[i]=*str;

if( *str == /0 ) break;

str++;

i++;

}

SendLen=i;

U0THR = SendData[0];

SendCount=1;

}

//串口中断

void __irq uart_int()

{

uint8 i;

if((U0IIR&0x0f)==0x02) //发送中断

{

if(SendCount!=SendLen)

{

U0THR = SendData[SendCount];

SendCount++;

}

}

// 接收中断

else if(((U0IIR & 0x0F) == 0x0C)||((U0IIR & 0x0F) == 0x04))

{ // 如果中断是由FIFO数据达到触发点引起的

for (i=0; i<14; i++)

{

buffer[i] = U0RBR;

}

Sent_Byte(buffer,8);

}

/*

else if ((U0IIR & 0x0F) == 0x0C)

{ // 如果中断是由FIFO超时引起的

buffer[0] = U0RBR;

} */

VICVectAddr = 0; // 清中断标志,不是外部中断不必复位EXTINT

}

int main(void)

{

Init_Uart0(UART_BAUD(115200));

U0FCR = 0xc1; // 开启UART FIFO模式 1个字符触发0x01,4-0x41,8-0x81,14-0xc1

U0IER = 0x03; // 使能RBR中断,禁止THRE Rx线状态中断(DLAB=0时)

VICIntSelect = 0; // 选择所有中断为IRQ中断

VICVectCntl0 = 0x20 | 0x06; // slot0 对应中断源为UART

VICVectAddr0 = (uint32)uart_int; // slot0 中断服务程序

VICIntEnable = 1 < 0x06; // 使能UART中断源

Sent_Str("http://blog.csdn.net/cy757//n") ;

for(;;)

{

delay(200);

}

}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top