mcuxpresso ide串口发送10byte数据
时间: 2023-12-13 07:05:00 浏览: 144
通过串口发送数据
4星 · 用户满意度95%
以下是在MCUXpresso IDE中使用串口发送10字节数据的示例代码:
```c
#include "board.h"
#include "fsl_debug_console.h"
#include "fsl_lpuart.h"
#define EXAMPLE_LPUART_BASE (USART0_BASE)
#define EXAMPLE_LPUART_IRQ (USART0_IRQn)
#define EXAMPLE_LPUART_IRQHandler (USART0_IRQHandler)
#define EXAMPLE_LPUART_TX_BUFFER_LENGTH (10)
uint8_t lpuartTxBuffer[EXAMPLE_LPUART_TX_BUFFER_LENGTH] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A};
volatile bool txBufferEmpty = true;
void EXAMPLE_LPUART_IRQHandler(void)
{
/* If new data arrived. */
if ((kLPUART_RxDataRegFullFlag) & LPUART_GetStatusFlags(EXAMPLE_LPUART_BASE))
{
uint8_t ch;
ch = LPUART_ReadByte(EXAMPLE_LPUART_BASE);
PRINTF("Received %c\r\n", ch);
}
/* If there is data to be sent */
if ((kLPUART_TxDataRegEmptyFlag) & LPUART_GetStatusFlags(EXAMPLE_LPUART_BASE))
{
if (txBufferEmpty == false)
{
LPUART_WriteByte(EXAMPLE_LPUART_BASE, lpuartTxBuffer[0]);
txBufferEmpty = true;
for (uint8_t i = 1; i < EXAMPLE_LPUART_TX_BUFFER_LENGTH; i++)
{
lpuartTxBuffer[i - 1] = lpuartTxBuffer[i];
}
}
}
}
int main(void)
{
lpuart_config_t config;
uint32_t uartClkSrcFreq;
/* Init board hardware. */
BOARD_InitPins();
BOARD_BootClockRUN();
BOARD_InitDebugConsole();
/* Initialize LPUART0 pins below */
CLOCK_AttachClk(kFRO12M_to_FLEXCOMM0);
RESET_PeripheralReset(kFC0_RST_SHIFT_RSTn);
CLOCK_SetDiv(kCLOCK_DivLpuartClk, 1U);
/* Set up the LPUART config struct. */
LPUART_GetDefaultConfig(&config);
config.baudRate_Bps = 115200U;
config.enableTx = true;
config.enableRx = true;
/* Initialize the LPUART base. */
LPUART_Init(EXAMPLE_LPUART_BASE, &config, CLOCK_GetFreq(kCLOCK_CoreSysClk));
/* Send data */
LPUART_WriteBlocking(EXAMPLE_LPUART_BASE, lpuartTxBuffer, EXAMPLE_LPUART_TX_BUFFER_LENGTH);
txBufferEmpty = false;
/* Enable RX and TX interrupt. */
LPUART_EnableInterrupts(EXAMPLE_LPUART_BASE, kLPUART_RxDataRegFullInterruptEnable);
LPUART_EnableInterrupts(EXAMPLE_LPUART_BASE, kLPUART_TxDataRegEmptyInterruptEnable);
/* Enable interrupt in NVIC. */
NVIC_EnableIRQ(EXAMPLE_LPUART_IRQ);
while (1)
{
}
}
```
在这个示例代码中,我们使用了LPUART0作为示例,也可以根据实际情况选择其他的串口。在main函数中,首先初始化了串口,然后将10字节的数据通过LPUART_WriteBlocking函数发送出去。在发送数据之后,我们打开了接收和发送中断,并在中断处理函数中实现连续发送数据的功能。你可以根据自己的需求修改发送的数据和发送的长度。
阅读全文