#include <stdint.h>
#include "LPC17xx.h"
#include "bmboard.h"
#include "led.h"
#include "lpc1768_uart.h"
void UART2_SendString(unsigned char *s, uint32_t len)
{
while(len--)
UART2_SendByte(*s++);
}
void UART2_SendByte(unsigned char ucData)
{
while(!(LPC_UART2->LSR&LSR_THRE));
LPC_UART2->THR=ucData;
}
RX_BUF_TypeDef RxBuf; //声明 RxBuf lpc1768_uart 中可见
void UART2_Init(uint32_t baud)
{
LPC_UART_TypeDef *pUart;
pUart=LPC_UART2;
RxBuf.available=0;
RxBuf.len=0;
LPC_PINCON->PINSEL0|=(1<<20);
LPC_PINCON->PINSEL0|=(1<<22);
LPC_SC->PCONP=LPC_SC->PCONP|(1<<24);
uart_set_divisors(pUart,baud);
LPC_UART2->LCR=LCR_LENGTH_8|\ //数据位8位
LCR_STOP_1; //停止位1位
LPC_UART2->FCR=(FCR_FIFO_ENABLE|FCR_RX_FIFO|FCR_TX_FIFO);
LPC_UART2->IER=(IER_RBR_ENABLED|IER_THRE_ENABLED);
NVIC_EnableIRQ(UART2_IRQn);
}
UART2_IRQHandler(void)
{
uint32_t int_status;
uint32_t lsr_status;
char ch;
int_status=LPC_UART2->IIR;
switch(int_status&0x0F)
{
case IIR_RLS:
lsr_status=LPC_UART2->LSR;
break;
case IIR_RDA:
if(LPC_UART2->LSR&LSR_RDR)
{
if((ch=LPC_UART2->RBR)!='\r')
RxBuf.RxData[RxBuf.len++]=ch;
else{
RxBuf.available=1;
}
}
break;
case IIR_THRE:
LPC_UART2->IER&=~IER_THRE_ENABLED;
break;
}
}
喵喔喔
慕勒3428872