在线时间6 小时
UID3588702
注册时间2020-9-29
NXP金币0
TA的每日心情 | 开心 2021-1-25 11:21 |
---|
签到天数: 3 天 [LV.2]偶尔看看I
注册会员
- 积分
- 53
- 最后登录
- 2021-8-27
|
发表于 2020-9-29 15:41:31
|
显示全部楼层
今天配置了LPC1788的UART2,用来调试程序,用的库开发的,代码如下,希望对你有帮助
#include "uart2_config.h"
uint8_t uart2_rec_table[uart2_rec_max_len];
uint8_t uart2_rec_len = 0;
uint8_t uart2_rec_complete_flag = 0;
uint8_t uart2_receiving_flag = 0;
uint8_t uart2_rec_counter = 0;
/**
* pin 123 P4[22] uart2 txd
* pin 129 P4[23] uart2 rxd
*/
void sys_uart2_config(void)
{
UART_CFG_Type UARTConfigStruct;
UART_FIFO_CFG_Type UART_FIFO_CFG_ConfigStruct;
CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCUART2,ENABLE);
/*
* Initialize UART2 pin connect
* P4.22: TXD
* P4.23: RXD
*/
PINSEL_ConfigPin(4, 22, 2);
PINSEL_ConfigPin(4, 23, 2);//P4[22] 、P4[23] function select <LPC178X_7X_UM.pdf> page 131
/* Initialize UART Configuration parameter structure to default state:
* Baudrate = 9600bps
* 8 data bit
* 1 Stop bit
* None parity
*/
// UARTConfigStruct.Baud_rate = 115200;
UARTConfigStruct.Baud_rate = 1000000;
UARTConfigStruct.Databits = UART_DATABIT_8;
UARTConfigStruct.Parity = UART_PARITY_NONE;
UARTConfigStruct.Stopbits = UART_STOPBIT_1;
// Initialize DEBUG_UART_PORT peripheral with given to corresponding parameter
UART_Init(LPC_UART2, &UARTConfigStruct);
// Enable UART Transmit
UART_TxCmd(LPC_UART2, ENABLE);
UART_FIFO_CFG_ConfigStruct.FIFO_DMAMode = DISABLE;
UART_FIFO_CFG_ConfigStruct.FIFO_Level = UART_FIFO_TRGLEV0;
UART_FIFO_CFG_ConfigStruct.FIFO_ResetRxBuf = ENABLE;
UART_FIFO_CFG_ConfigStruct.FIFO_ResetTxBuf = ENABLE;
UART_FIFOConfig(LPC_UART2,&UART_FIFO_CFG_ConfigStruct);
// UART_FIFOConfigStructInit(LPC_UART2);
UART_IntConfig(LPC_UART2,UART_INTCFG_RBR,ENABLE);
NVIC_EnableIRQ(UART2_IRQn);
}
void sys_uart2_send_byte(uint8_t tmep_data)
{
UART_SendByte(LPC_UART2,tmep_data);
while(SET == UART_CheckBusy(LPC_UART2));//wait busy
}
void sys_uart2_send_array_hex(uint8_t* pointer,uint8_t len)
{
uint8_t i;
for(i = 0;i < len;i++)
sys_uart2_send_byte(pointer);
}
void sys_uart2_send_array_ascii(uint8_t* pointer,uint8_t len)
{
uint8_t i;
for(i = 0;i < len;i++)
printf("%02x ",pointer);
}
void sys_uart2_send_string(uint8_t* s)
{
while(*s) //Check the end of the string
sys_uart2_send_byte(*s++); //Send current char and increment string ptr
}
void UART2_IRQHandler(void)
{
uint8_t Status;
Status=UART_GetLineStatus(LPC_UART2);
if(Status & 0x01)
{
uart2_rec_table[uart2_rec_len++] = UART_ReceiveByte(LPC_UART2);
uart2_receiving_flag = 1;
uart2_rec_counter = 0;
}
}
void uart2_data_process(void)
{
uint8_t command = 0;
uint16_t us_temp_data = 0;
command = uart2_rec_table[0];
if(uart2_rec_complete_flag)
{
sys_uart2_send_array_ascii(uart2_rec_table,uart2_rec_len);
sys_uart2_send_string("start!\r\n");
switch(command)
{
case 0x00:
break;
case 0x01:
break;
case 0x02:
break;
case 0x03:
break;
default:
break;
}
uart2_rec_complete_flag = 0;
uart2_rec_len = 0;
}
}
/*
串口重定向、使用printf功能。调试的时候使用
*/
#pragma import(__use_no_semihosting)
struct __FILE
{
int handle;
};
FILE __stdout;
_sys_exit(int x)
{
x = x;
}
int fputc(int ch, FILE *f)
{
/* 发送串口数据 */
sys_uart2_send_byte((uint8_t) ch);
return (ch);
}
|
|