You are on page 1of 22

Võ Thị Phương An – 2070044

BÁO CÁO THỰC HÀNH


1. Interrupt and the Timer
1.1. Mục tiêu
- Thiết lập chế độ hoạt động của Timer và ngắt Timer.
- Xử lý ngắt để chớp tắt LED với tần số 10 Hz
1.2. Giải thích chương trình

#include <stdint.h> Thêm thư viện cần thiết


#include <stdbool.h>
#include "inc/tm4c123gh6pm.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/interrupt.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
SysCtlClockSet(SYSCTL_SYSDIV_5| Hàm tạo xung clock hệ thống. Hệ
SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ| thống sẽ hoạt động ở tần số 40Mhz,
SYSCTL_OSC_MAIN); do tham số SYSCTL_SYSDIV_5 sẽ
chia xung tổng 200Mhz/5=40Mhz
SysCtlPeripheralEnable(SYSCTL_PERIPH_GP Configure GPIO port F pin 1 2 3
IOF); cho LED
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE,
GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
SysCtlPeripheralEnable(SYSCTL_PERIPH_TI Configure Timer0
MER0);
TimerConfigure(TIMER0_BASE,
TIMER_CFG_PERIODIC);
ui32Period = (SysCtlClockGet() / 10) / Tham số SysCtlClockGet() sẽ
2; cho chu kỳ hệ thống là 1s, sau đó
TimerLoadSet(TIMER0_BASE, TIMER_A, SysCtlClockGet()/10 sẽ tạo
ui32Period -1); chu kỳ 0.1s=10Hz,
(SysCtlClockGet() / 10) /
2 sẽ tạo ngắt trong nửa chu kỳ 0.1s
TimerIntRegister(TIMER0_BASE, TIMER_A, Cho phép ngắt Timer0. Hàm
&Timer0IntHandler); TimerIntRegister khi ngặp
IntEnable(INT_TIMER0A); ngắt sẽ chạy chương trình ngắt
TimerIntEnable(TIMER0_BASE, Timer0: Timer0IntHandler
TIMER_TIMA_TIMEOUT);
IntMasterEnable(); Kích hoạt cho ngắt Timer0
TimerEnable(TIMER0_BASE, TIMER_A);
void Timer0IntHandler(void) Hàm ngắt Timer0
{ Đọc giá trị Pin 2, nếu Led Pin 2
// Clear the timer interrupt sáng (=1) sẽ tắt tất cả 3 Led, ngược
TimerIntClear(TIMER0_BASE, lại sẽ bật Led 2 lên
TIMER_TIMA_TIMEOUT);
// Read the current state of the
GPIO pin and
// write back the opposite state
if(GPIOPinRead(GPIO_PORTF_BASE,
GPIO_PIN_2))
{
GPIOPinWrite(GPIO_PORTF_BASE,
GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0);
}
else
{
GPIOPinWrite(GPIO_PORTF_BASE,
GPIO_PIN_2, 4);
}
}

1.3. Chương trình hoàn chỉnh

#include <stdint.h>
#include <stdbool.h>
#include "inc/tm4c123gh6pm.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/interrupt.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"

int main(void)
{
uint32_t ui32Period;
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|
SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|
GPIO_PIN_3);
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
ui32Period = (SysCtlClockGet() / 10) / 2;
TimerLoadSet(TIMER0_BASE, TIMER_A, ui32Period -1);
TimerIntRegister(TIMER0_BASE, TIMER_A, &Timer0IntHandler);
IntEnable(INT_TIMER0A);
TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
IntMasterEnable();
TimerEnable(TIMER0_BASE, TIMER_A);
while(1)
{}
}
void Timer0IntHandler(void)
{
// Clear the timer interrupt
TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
// Read the current state of the GPIO pin and
// write back the opposite state
if(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_2))
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|
GPIO_PIN_3, 0);
}
else
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 4);
}
}

1.4. Kết quả:


- Video đính kèm
2. ADC12
2.1. Giải thích chương trình

#include <stdint.h> Thêm thư viện cần thiết


#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/sysctl.h"
#include "driverlib/adc.h"
uint32_t ui32ADC0Value[4]; Tạo 1 mảng để lưu trữ data
volatile uint32_t ui32TempAvg; Các biến để lưu trữ giá trị
volatile uint32_t ui32TempValueC;
volatile uint32_t ui32TempValueF;
SysCtlClockSet(SYSCTL_SYSDIV_5| Hàm tạo xung clock hệ thống. Hệ
SYSCTL_USE_PLL|SYSCTL_OSC_MAIN| thống sẽ hoạt động ở tần số
SYSCTL_XTAL_16MHZ); 40Mhz, do tham số
SYSCTL_SYSDIV_5 sẽ chia xung
tổng 200Mhz/5=40Mhz
SysCtlPeripheralEnable(SYSCTL_PERIPH_A Configure ADC0
DC0);
ADCSequenceConfigure(ADC0_BASE, 1, Configure ADC sequencer và
ADC_TRIGGER_PROCESSOR, 0); enable ADC0
ADCSequenceEnable(ADC0_BASE, 1);
ADCSequenceStepConfigure(ADC0_BASE, 1, Ta sẽ đọc 4 lần và lấy giá trị trung
0, ADC_CTL_TS); bình
ADCSequenceStepConfigure(ADC0_BASE, 1,
1, ADC_CTL_TS);
ADCSequenceStepConfigure(ADC0_BASE, 1,
2, ADC_CTL_TS);
ADCSequenceStepConfigure(ADC0_BASE,1,3
,ADC_CTL_TS|ADC_CTL_IE|ADC_CTL_END);
ADCIntClear(ADC0_BASE, 1); Việc chuyển đổi ADC hoàn tất sẽ
ADCProcessorTrigger(ADC0_BASE, 1); là cờ ngắt ADC được bật. Thiết
while(!ADCIntStatus(ADC0_BASE, 1, lập xóa cờ ngắt trước khi sử dụng
false)) ADC
{} (ADCIntClear(ADC0_BASE,
ADCSequenceDataGet(ADC0_BASE, 1, 1))
ui32ADC0Value); Sau đó đọc giá trị ADC thông qua
ui32TempAvg = hàm ADCSequenceDataGet
(ui32ADC0Value[0] + ui32ADC0Value[1] +
ui32ADC0Value[2] + ui32ADC0Value[3] + và chuyển giá trị về 0C và 0F
2)/4;
ui32TempValueC = (1475 -
((2475 * ui32TempAvg)) / 4096)/10;
ui32TempValueF =
((ui32TempValueC * 9) + 160) / 5;
Đặt Debug tại
ADCIntClear(ADC0_BASE,
1); và chạy kết quả

2.2. Chương trình hoàn chỉnh

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/sysctl.h"
#include "driverlib/adc.h"

int main(void)
{
uint32_t ui32ADC0Value[4];
volatile uint32_t ui32TempAvg;
volatile uint32_t ui32TempValueC;
volatile uint32_t ui32TempValueF;
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|
SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_PROCESSOR, 0);
ADCSequenceStepConfigure(ADC0_BASE, 1, 0, ADC_CTL_TS);
ADCSequenceStepConfigure(ADC0_BASE, 1, 1, ADC_CTL_TS);
ADCSequenceStepConfigure(ADC0_BASE, 1, 2, ADC_CTL_TS);
ADCSequenceStepConfigure(ADC0_BASE,1,3,ADC_CTL_TS|ADC_CTL_IE|
ADC_CTL_END);
ADCSequenceEnable(ADC0_BASE, 1);
while(1)
{
ADCIntClear(ADC0_BASE, 1);
ADCProcessorTrigger(ADC0_BASE, 1);
while(!ADCIntStatus(ADC0_BASE, 1, false))
{}
ADCSequenceDataGet(ADC0_BASE, 1, ui32ADC0Value);
ui32TempAvg = (ui32ADC0Value[0] + ui32ADC0Value[1] +
ui32ADC0Value[2] + ui32ADC0Value[3] + 2)/4;
ui32TempValueC = (1475 - ((2475 * ui32TempAvg)) /
4096)/10;
ui32TempValueF = ((ui32TempValueC * 9) + 160) / 5;
}
}

2.3. Kết quả:


3. Memory
3.1. Flash

#include "include.h" Chạy đoạn code để xác


định vùng nhớ Flash đã
#include "inc/hw_types.h" dùng
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
int main(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_5|
SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|
SYSCTL_OSC_MAIN);

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE,
GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|
GPIO_PIN_2|GPIO_PIN_3, 0x00);
SysCtlDelay(20000000);
while(1)
{}
}
Flash đã sử dụng 0x13f4
(bắt đầu tại 0x00),
Ta sẽ chọn ghi vào địa
chỉ 0x10000

Chương trình hoàn chỉnh:


#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/flash.h"
int main(void)
{
unsigned long pulData[2];
unsigned long pulRead[2];
pulData[0] = 0x12345678;
pulData[1] = 0x56789abc;
SysCtlClockSet(SYSCTL_SYSDIV_5|
SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|
SYSCTL_OSC_MAIN);

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE,
GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|
GPIO_PIN_2|GPIO_PIN_3, 0x00);
SysCtlDelay(20000000);
FlashErase(0x10000);
FlashProgram(pulData, 0x10000,
sizeof(pulData));
GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|
GPIO_PIN_2|GPIO_PIN_3, 0x02);
SysCtlDelay(20000000);
while(1)
{ }
}
SysCtlClockSet(SYSCTL_SYSDIV_5| Hàm tạo xung clock hệ
SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ| thống. Hệ thống sẽ hoạt
SYSCTL_OSC_MAIN); động ở tần số 40Mhz,
do tham số
SYSCTL_SYSDIV_5 sẽ
chia xung tổng
200Mhz/5=40Mhz
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); Configure GPIO port F
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, pin 1 2 3 ở chế độ
GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3); output

FlashErase(0x10000); Xóa khối flash 0x10000,


FlashProgram(pulData, 0x10000, giá trị sau khi xóa là
sizeof(pulData)); 0xFFFFFFFF
FlashProgram sẽ lấy
dữ liệu địa chỉ và kích
cỡ của pulData để bắt
đầu ghi vào địa chỉ
0x10000
Sau đó sẽ có đèn đỏ
sáng báo hiệu
3.2. Reading and Writing EEPROM

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/flash.h"
#include "driverlib/eeprom.h"
int main(void)
{
uint32_t pui32Data[2];
uint32_t pui32Read[2];
pui32Data[0] = 0x12345678;
pui32Data[1] = 0x56789abc;
SysCtlClockSet(SYSCTL_SYSDIV_5|
SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|
SYSCTL_OSC_MAIN);

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE,
GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|
GPIO_PIN_2|GPIO_PIN_3, 0x00);
SysCtlDelay(20000000);
FlashErase(0x10000);
FlashProgram(pui32Data, 0x10000,
sizeof(pui32Data));

GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|
GPIO_PIN_2|GPIO_PIN_3, 0x02);
SysCtlDelay(20000000);

SysCtlPeripheralEnable(SYSCTL_PERIPH_EEPROM0)
;
EEPROMInit();
EEPROMMassErase();
EEPROMRead(pui32Read, 0x0,
sizeof(pui32Read));
EEPROMProgram(pui32Data, 0x0,
sizeof(pui32Data));
EEPROMRead(pui32Read, 0x0,
sizeof(pui32Read));
GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|
GPIO_PIN_2|GPIO_PIN_3, 0x04);
while(1)
{}
}
SysCtlClockSet(SYSCTL_SYSDIV_5| Hàm tạo xung clock hệ
SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ| thống. Hệ thống sẽ hoạt
SYSCTL_OSC_MAIN); động ở tần số 40Mhz,
do tham số
SYSCTL_SYSDIV_5 sẽ
chia xung tổng
200Mhz/5=40Mhz
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); Configure GPIO port F
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, pin 1 2 3 ở chế độ
GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3); output

FlashErase(0x10000); Xóa khối flash 0x10000,


FlashProgram(pulData, 0x10000, giá trị sau khi xóa là
sizeof(pulData)); 0xFFFFFFFF
FlashProgram sẽ lấy
dữ liệu địa chỉ và kích
cỡ của pulData để bắt
đầu ghi vào địa chỉ
0x10000
SysCtlPeripheralEnable(SYSCTL_PERIPH_EEPROM0) Cấu hình EEROM,
; EEPROMMassErase();
EEPROMInit(); Sẽ xóa EEROM và đưa
EEPROMMassErase(); về mặc định, nên khi đặt
EEPROMRead(pui32Read, 0x0, breakpoint và chạy thì
sizeof(pui32Read)); sẽ thấy giá trị đọc tại
EEPROMProgram(pui32Data, 0x0, pui32Read sẽ là
sizeof(pui32Data)); 0xFFFFFFFF,
EEPROMRead(pui32Read, 0x0, EEPROMProgram sẽ đọc
sizeof(pui32Read)); dữ liệu từ pui32Data
GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1| để lưu vào EEROM nên
GPIO_PIN_2|GPIO_PIN_3, 0x04); khi đọc EEROM lại
bằng lệnh EEPROMRead
sẽ chứa dữ liệu giống
pui32Data và sau đó
đèn xanh được bật
Đèn đỏ sáng

Đèn xanh sáng


4. Floating-Point Unit
4.1. Mục tiêu
- Vẽ hình sin
4.2. Chương trình thực hiện

#include <stdint.h> Include thư viện


#include <stdbool.h>
#include <math.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/fpu.h"
#include "driverlib/sysctl.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#ifndef M_PI Định nghĩa số Pi
#define M_PI 3.14159265358979323846 có độ dài phần
#endif thập phân là 20.
#define SERIES_LENGTH 100 Tạo mảng
float gSeriesData[SERIES_LENGTH]; gSeriesData gồm
int32_t i32DataCount = 0; 100 phần tử để
lưu các giá trị của
sin
float fRadians; Cấu hình FPU
FPULazyStackingEnable(); Biến fRadians sẽ
FPUEnable(); chia chu kỳ 2 PI
SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | ra thành 100 phần
SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN); bằng nhau
fRadians = ((2 * M_PI) / SERIES_LENGTH);
while(i32DataCount < SERIES_LENGTH) Lấy giá từ 0 ->
{ 2PI cách nhau
gSeriesData[i32DataCount] = sinf(fRadians * một khoảng
i32DataCount); fRadians để tính
i32DataCount++; hàm sin
}
Chương trình hoàn chỉnh:

#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/fpu.h"
#include "driverlib/sysctl.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define SERIES_LENGTH 100
float gSeriesData[SERIES_LENGTH];
int32_t i32DataCount = 0;
int main(void)
{
float fRadians;
FPULazyStackingEnable();
FPUEnable();
SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL |
SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);
fRadians = ((2 * M_PI) / SERIES_LENGTH);
while(i32DataCount < SERIES_LENGTH)
{
gSeriesData[i32DataCount] = sinf(fRadians *
i32DataCount);
i32DataCount++;
}
while(1)
{}
}
Các giá trị sin
đã tính được
lưu trong
mảng
gSeriesData

Hình sin
được vẽ
Số xung
clock trước
và sau khi
chạy
Floating-
Point Unit
5. UART
5.1. Giải thích chương trình

#include <stdint.h> Include thư viện cần thiết


#include <stdbool.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
SysCtlPeripheralEnable(SYSCTL_PERIPH_UAR Configure chân UART0 là chân
T0); 0 và 1 của port A

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPI
OA);
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
GPIOPinTypeUART(GPIO_PORTA_BASE,
GPIO_PIN_0 | GPIO_PIN_1);
UARTConfigSetExpClk(UART0_BASE, Lấy clock nội, tốc độ baudrate =
SysCtlClockGet(), 115200, 115200, 8 bit data, stop bit =1
(UART_CONFIG_WLEN_8 | và không có parity
UART_CONFIG_STOP_ONE |
UART_CONFIG_PAR_NONE));
UARTCharPut(UART0_BASE, 'E'); Gửi ký tự lên máy tính, mỗi
UARTCharPut(UART0_BASE, 'n'); lệnh UARTCharPut sẽ gửi 1 ký
UARTCharPut(UART0_BASE, 't'); tự
UARTCharPut(UART0_BASE, 'e');
UARTCharPut(UART0_BASE, 'r');
UARTCharPut(UART0_BASE, ' ');
UARTCharPut(UART0_BASE, 'T');
UARTCharPut(UART0_BASE, 'e');
UARTCharPut(UART0_BASE, 'x');
UARTCharPut(UART0_BASE, 't');
UARTCharPut(UART0_BASE, ':');
UARTCharPut(UART0_BASE, ' ');
if (UARTCharsAvail(UART0_BASE)) UARTCharsAvail(UART0_BA
UARTCharPut(UART0_BASE, SE) sẽ kiểm tra xem có ký tự
UARTCharGet(UART0_BASE)); nào trong bộ nhớ đệm FIFO
không. Nếu có sẽ lấy ký tự nhận
được
UARTCharGet(UART0_BASE)
và gửi lại
UARTCharPut(UART0_BASE,
UARTCharGet(UART0_BASE)
);
5.2. Chương trình hoàn chỉnh

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
int main(void) {
SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL |
SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 115200,
(UART_CONFIG_WLEN_8 |
UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE));
UARTCharPut(UART0_BASE, 'E');
UARTCharPut(UART0_BASE, 'n');
UARTCharPut(UART0_BASE, 't');
UARTCharPut(UART0_BASE, 'e');
UARTCharPut(UART0_BASE, 'r');
UARTCharPut(UART0_BASE, ' ');
UARTCharPut(UART0_BASE, 'T');
UARTCharPut(UART0_BASE, 'e');
UARTCharPut(UART0_BASE, 'x');
UARTCharPut(UART0_BASE, 't');
UARTCharPut(UART0_BASE, ':');
UARTCharPut(UART0_BASE, ' ');
while (1)
{
if (UARTCharsAvail(UART0_BASE)) UARTCharPut(UART0_BASE,
UARTCharGet(UART0_BASE));
}
}
5.3. Kết quả:

6. UART có ngắt
6.1. Giải thích chương trình

IntMasterEnable(); Thêm cấu hình ngắt cho UART.


IntEnable(INT_UART0); Khi ngắt xảy ra, chương trình sẽ
UARTIntEnable(UART0_BASE, UART_INT_RX | thực hiện ngắt trong hàm
UART_INT_RT); UARTIntHandler
UARTIntRegister(UART0_BASE,&UARTIntHandl
er);
void UARTIntHandler(void) Vào chương trình ngắt ta sẽ xóa
{ cờ báo ngắt. Sau đó kiểm tra dữ
uint32_t ui32Status; liệu trong bộ nhớ đệm bằng hàm
ui32Status = UARTCharsAvail(UART0_BA
UARTIntStatus(UART0_BASE, true); SE) nếu nhận được dữ liệu sẽ
UARTIntClear(UART0_BASE, gửi lại dữ liệu đã nhận đồng
ui32Status); thời nhớp tắt led để báo hiệu
while(UARTCharsAvail(UART0_BASE))
{

UARTCharPutNonBlocking(UART0_BASE,
UARTCharGetNonBlocking(UART0_BASE));
GPIOPinWrite(GPIO_PORTF_BASE,
GPIO_PIN_2, GPIO_PIN_2);
SysCtlDelay(SysCtlClockGet() /
(1000 * 3));
GPIOPinWrite(GPIO_PORTF_BASE,
GPIO_PIN_2, 0);
}
}

6.2. Chương trình hoàn chỉnh:

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
void UARTIntHandler(void)
{
uint32_t ui32Status;
ui32Status = UARTIntStatus(UART0_BASE, true); //get interrupt
status
UARTIntClear(UART0_BASE, ui32Status); //clear the asserted
interrupts
while(UARTCharsAvail(UART0_BASE)) //loop while there are
chars
{
UARTCharPutNonBlocking(UART0_BASE,
UARTCharGetNonBlocking(UART0_BASE)); //echo character
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);
//blink LED
SysCtlDelay(SysCtlClockGet() / (1000 * 3)); //delay ~1
msec
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0); //turn off
LED
}
}
int main(void) {
SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL |
SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); //enable GPIO
port for LED
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2); //enable
pin for LED PF2
UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 115200,
(UART_CONFIG_WLEN_8 |
UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE));
IntMasterEnable(); //enable processor interrupts
IntEnable(INT_UART0); //enable the UART interrupt
UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT); //only
enable RX and TX interrupts
UARTIntRegister(UART0_BASE,&UARTIntHandler);
UARTCharPut(UART0_BASE, 'E');
UARTCharPut(UART0_BASE, 'n');
UARTCharPut(UART0_BASE, 't');
UARTCharPut(UART0_BASE, 'e');
UARTCharPut(UART0_BASE, 'r');
UARTCharPut(UART0_BASE, ' ');
UARTCharPut(UART0_BASE, 'T');
UARTCharPut(UART0_BASE, 'e');
UARTCharPut(UART0_BASE, 'x');
UARTCharPut(UART0_BASE, 't');
UARTCharPut(UART0_BASE, ':');
UARTCharPut(UART0_BASE, ' ');
while (1) //let interrupt handler do the UART echo function
{
}
}
6.3. Kết quả:
- Video đính kèm

You might also like