Professional Documents
Culture Documents
#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);
}
}
#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;
}
}
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
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
#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
#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
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
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);
}
}
#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