LPC1788学习之串口学习-寄存器操作
接收数据使用中断接收---发送暂时是直接发送--简单的寄存器操作-记下不忘记!
#include "uart_lpc1788.h"
uint32_t Receive_Sign; //接收数据下标--接收数据个数
uint8_t Receive_Buf[UART_RING_BUFSIZE]; //接收数组
/*-----------------------串口0初始化------------------*/
void Uart0_Init(uint32_t clk, uint32_t baudrate)
{
uint32_t tmp;
uint16_t divisorInt;
LPC_IOCON->P0_2=0X21;
LPC_IOCON->P0_3=0X21;
LPC_SC->PCONP |= (1<<3); //外设时钟使能
//---------------------------------------------
LPC_UART0->FCR=0X07;
LPC_UART0->FCR=0X00;
while (LPC_UART0->LSR & 0X01)
{
tmp = LPC_UART0->RBR; //假读取去清空
}
LPC_UART0->TER = 0x80;
while (!(LPC_UART0->LSR & 0x20));
LPC_UART0->TER = 0;
LPC_UART0->IER = 0;
LPC_UART0->LCR = 0;
LPC_UART0->ACR = 0;
LPC_UART0->RS485CTRL = 0;
LPC_UART0->RS485DLY = 0;
LPC_UART0->ADRMATCH = 0;
tmp = LPC_UART0->LSR; //获取线状态寄存器
//-------------------------------------
divisorInt =Uart0GetDivisorByBaudrate(clk,baudrate);
LPC_UART0->LCR |= 0x80; //使能访问除数寄存器
LPC_UART0->DLM = (divisorInt>>8) & 0xFF;
LPC_UART0->DLL = divisorInt & 0xFF;
LPC_UART0->LCR &= (~0x80) & 0xFF; //禁止访问
//------------------------------------------
tmp = (LPC_UART0->LCR & (0x80 | 0x40)) & 0xFF;
tmp |= 0x03; //8位数据-1个停止位-禁止奇偶校验
LPC_UART0->LCR = (uint8_t)(tmp & 0xFF);
//----------------------------------------------
tmp=0;
tmp |= 0x01; //使能FIFO
tmp |= 0x00; //触发点0-一个字符触发中断
tmp |= 0x04; //发送缓存
tmp |= 0x02; //接收缓存
LPC_UART0->FCR = tmp & 0xFF;
//------------------------------------------------
LPC_UART0->TER |= 0x80;//发送使能
LPC_UART0->IER|= 0x01& 0x307; //使能RD数据接收中断
LPC_UART0->IER|= 0x04& 0x307; //使能RX线状态中断
//----------------------------------------------------
Receive_Sign=0;
//--------------------------------------
NVIC_SetPriority(UART0_IRQn, ((0x01<<3)|0x01));
NVIC_EnableIRQ(UART0_IRQn);
}
/*-------------------串口0中断函数----------------------*/
void UART0_IRQHandler(void)
{
uint32_t tmp, tmp1;
tmp = ((LPC_UART0->IIR) & 0x3CF) & (7<<1); //获取中断标识
if (tmp == (3<<1)) //线状态中断
{
tmp1 = ((LPC_UART0->LSR) & 0xFF);
tmp1 &= ((1<<1) | (1<<2) | (1<<3)| (1<<4) | (1<<7));
if (tmp1) //有错误
{
while (1){}
}
}
if ((tmp == (2<<1)) || (tmp == (6<<1)))//接收数据中断--超时中断
{
Receive_Buf[Receive_Sign]= (LPC_UART0->RBR & 0xFF); //读取数据
Receive_Sign++;
}
if (tmp == (1<<1))//THRE发送中断
{
//UART_IntTransmit(); //发送数据--未使用
}
}
/*-----------发送指定数组----------------*/
void UART0_SendStr(uint8_t *txbuf)
{
uint8_t *pChar = txbuf;
while(*pChar!='