久久ER99热精品一区二区-久久精品99国产精品日本-久久精品免费一区二区三区-久久综合九色综合欧美狠狠

新聞中心

EEPW首頁 > 嵌入式系統 > 設計應用 > LM3S9B96 的UART以中斷方式收發數據

LM3S9B96 的UART以中斷方式收發數據

作者: 時間:2016-11-11 來源:網絡 收藏
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));
}
}

本文引用地址:http://cqxgywz.com/article/201611/317010.htm如果想發送數據時,也進入中斷處理函數,則將:
UARTIntEnable(UART1_BASE, UART_INT_RX | UART_INT_RT);
改為
UARTIntEnable(UART1_BASE,UART_INT_TX| UART_INT_RX | UART_INT_RT);
按照前面講的中斷映射表的配置,將startup_ewarm.c文件中添加兩處代碼。編譯、運行即可。


評論


技術專區

關閉