#define GPIO_PORTB_AFSEL_R (*((volatile unsigned long *)0x40005420))
#define GPIO_PORTB_AMSEL_R (*((volatile unsigned long *)0x40005528))
#define GPIO_PORTB_PCTL_R (*((volatile unsigned long *)0x4000552C))
#define GPIO_PORTB_DEN_R (*((volatile unsigned long *)0x4000551C))
#define UART1_DR_R (*((volatile unsigned long *)0x4000D000))
#define UART1_FR_R (*((volatile unsigned long *)0x4000D018))
#define UART1_IBRD_R (*((volatile unsigned long *)0x4000D024))
#define UART1_FBRD_R (*((volatile unsigned long *)0x4000D028))
#define UART1_LCRH_R (*((volatile unsigned long *)0x4000D02C))
#define UART1_CTL_R (*((volatile unsigned long *)0x4000D030))
#define UART_FR_TXFF 0x00000020 // UART Transmit FIFO Full
#define UART_FR_RXFE 0x00000010 // UART Receive FIFO Empty
#define UART_LCRH_WLEN_8 0x00000060 // 8 bit word length
#define UART_LCRH_FEN 0x00000010 // UART Enable FIFOs
#define UART_CTL_UARTEN 0x00000001 // UART Enable
#define SYSCTL_RCGC1_R (*((volatile unsigned long *)0x400FE104))
#define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108))
#define SYSCTL_RCGC1_UART1 0x00000002 // UART0 Clock Gating Control
#define SYSCTL_RCGC2_GPIOB 0x00000002 // port B Clock Gating Control
//------------UART_Init------------
// Wait for new serial port input
// Initialize the UART for 115,200 baud rate (assuming 80 MHz UART clock),
// 8 bit word length, no parity bits, one stop bit, FIFOs enabled
// Input: none
// Output: none
void UART_Init(void){
//GPIOPinConfigure(GPIO_PB0_U1RX);
//GPIOPinConfigure(GPIO_PB1_U1TX);
SYSCTL_RCGC1_R |= SYSCTL_RCGC1_UART1; // activate UART1
SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOB; // activate port B
GPIO_PORTB_AFSEL_R |= 0x03; // enable alt funct on PB1-0
GPIO_PORTB_DEN_R |= 0x03; // enable digital I/O on PB1-0
// configure PB1-0 as UART
GPIO_PORTB_PCTL_R = (GPIO_PORTB_PCTL_R&0xFFFFFF00)+0x00000011;
GPIO_PORTB_AMSEL_R &= ~0x03; // disable analog functionality on PB
}
//------------UART_InChar------------
// Wait for new serial port input
// Input: none
// Output: ASCII code for key typed
unsigned char UART_InChar(void){
// while((UART1_FR_R&UART_FR_RXFE) != 0);
//return((unsigned char)(UART1_DR_R&0xFF));
int ret;
char rdata;
while((UART1_FR_R & 0x10)!=0);
ret = UART1_DR_R;
if(!(ret & 0xF00)){
rdata = (char)(UART1_DR_R & 0xFF);
}
return rdata;
}
//------------UART_OutChar------------
// Output 8-bit to serial port
// Input: letter is an 8-bit ASCII character to be transferred
// Output: none
void UART_OutChar(unsigned char data){
while((UART1_FR_R&UART_FR_TXFF) != 0);
UART1_DR_R = data;
}
// Print a character to UART0.
主函数:
#include // standard C library
#include "UART.h" // functions to implment input/output
#include "TExaS.h" // Lab grader functions