×
单片机 > 单片机程序设计 > 详情

LM3S9B96 的UART以中断方式收发数据

发布时间:2020-06-20 发布时间:
|
lm3s9b96的uart发送和接收都可以进入用户编写的中断处理函数。

uart的配置无非是设置:波特率、数据位、停止位、奇偶校验位等。
 
下面是用uart1为例,配置成中断方式收发数据

#include "inc/lm3s9b96.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_ints.h"
#include "driverlib/interrupt.h"
#include "driverlib/gpio.h"
#include "driverlib/uart.h"
#include "driverlib/sysctl.h"

//*****************************************************************************
//
// 延时函数
//
//*****************************************************************************
void Delay(volatile signed long nCount)
{
for(; nCount != 0; nCount--);
}

//*****************************************************************************
//
// Send a string to the UART.
//
//*****************************************************************************
void UARTSend(const unsigned char *pucBuffer, unsigned long ulCount)
{
// Loop while there are more characters to send.
while (ulCount--)
{
// Write the next character to the UART.
UARTCharPutNonBlocking(UART1_BASE, *pucBuffer++);
}
}

//*****************************************************************************
//
// UART1初始化函数
//
//*****************************************************************************
void UART1_Init(void)
{
// 使能UART1外设
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART1);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

// Set GPIO B0 and B1 as UART pins
GPIOPinTypeUART(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1);
GPIOPinConfigure(GPIO_PB0_U1RX);
GPIOPinConfigure(GPIO_PB1_U1TX);

// Configure the UART1 for 115200, 8-N-1 operation
UARTConfigSetExpClk(UART1_BASE, SysCtlClockGet(), 115200, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE));

// Enable the UART interrupt
IntEnable(INT_UART1);
UARTIntEnable(UART1_BASE, UART_INT_RX | UART_INT_RT);

// Prompt for text to be entered
//UARTSend((unsigned char *)"hello world", 11);
}


//*****************************************************************************
//
// 主函数
//
//*****************************************************************************
int main(void)
{
// Set the clocking to run directly from the crystal.
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);

UART1_Init();
IntMasterEnable(); // 开总中断

while (1)
{
}
}

//*****************************************************************************
//
// The UART1 interrupt handler.
//
//*****************************************************************************
void UART1IntHandler(void)
{
unsigned long ulStatus;

// Get the interrrupt status.
ulStatus = UARTIntStatus(UART1_BASE, true);

// Clear the asserted interrupts.
UARTIntClear(UART1_BASE, ulStatus);

// Loop while there are characters in the receive FIFO.
while (UARTCharsAvail(UART1_BASE))
{
// Read the next character from the UART and write it back to the UART.
UARTCharPutNonBlocking(UART1_BASE, UARTCharGetNonBlocking(UART1_BASE));
}
}

如果想发送数据时,也进入中断处理函数,则将:
          UARTIntEnable(UART1_BASE, UART_INT_RX  |  UART_INT_RT);
改为
UARTIntEnable(UART1_BASE, UART_INT_TX |  UART_INT_RX  |  UART_INT_RT );
 
按照前面讲的中断映射表的配置,将startup_ewarm.c文件中添加两处代码。编译、运行即可。
关键字:LM3S9B96  UART  中断方式  收发数据 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
TQ210天嵌开发板S5PV210 LED闪烁程序C语言代码记录