本帖最后由 香莲清风 于 2014-8-20 21:30 编辑
void Uart2Init(void)
{
uint16_t usFdiv;
LPC_GPIO1->FIODIR |= ((unsigned int)1 << 31);
LPC_GPIO1->FIOPIN &= ~((unsigned int)1<<31); /* 485_DIR = 0 */
LPC_PINCON->PINSEL0 |= (((unsigned int)1<<20)|((unsigned int)1<<22));
LPC_SC->PCONP |= (1<<24);
usFdiv = (FPCLK / 16) / 2400;
LPC_UART2->DLM = usFdiv / 256;
LPC_UART2->DLL = usFdiv % 256;
LPC_UART2->LCR = 0x1b;
LPC_UART2->FCR = 0x07;
LPC_UART2->IER = 0x01;
NVIC_EnableIRQ(UART2_IRQn); /* enable irq in nvic */
}
void Uart2SendByte(int8_t buf)
{
LPC_UART2->THR = buf;
while (!(LPC_UART2->LSR & 0x20));
}
void uart2SendStr (uint8_t *pucStr, uint32_t ulNum)
{
uint32_t i;
LPC_GPIO1->FIOCLR |= ((unsigned int)1 << 31);
Delay(5);
for (i = 0; i < ulNum; i++)
{
Uart2SendByte (*pucStr++);
}
Delay(5);
LPC_GPIO1->FIOSET |= ((unsigned int)1 << 31);
}
void UART2_IRQHandler(void)
{
while ((LPC_UART2->IIR & 0x01) == 0)
{
switch (LPC_UART2->IIR & 0x0E)
{
case 0x04:
for (GulNum = 0; GulNum < 8; GulNum++)
{
GucRcvBuf[GulNum+RcvLen] = LPC_UART2->RBR;
}
break;
case 0x0C:
while ((LPC_UART2->LSR & 0x01) == 0x01)
{
GucRcvBuf[RcvLen] = LPC_UART2->RBR;
}
break;
default:
break;
}
}
}
这是程序部分,不知道有没有大侠遇到过呀 |