-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.c
71 lines (63 loc) · 3.62 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*!
* @author Yash Bansod
* @date 17th September 2017
*
* @brief Blink without delay
* @details The program controls the Green LED on the Tiva C board
* TM4C123G LaunchPad (with TM4C123GH6PM microcontroller) and
* blinks it with a specified time period.
* Timer0 is used to create periodic interrupts to control blinking.
* @note The tm4c123ghpm_startup_ccs.c contains the vector table for the
* microcontroller. It was modified to execute the specified ISR on
* Timer0A Interrupt.
*/
/* ----------------------- Include Files --------------------- */
#include <stdint.h> // Library of Standard Integer Types
#include <stdbool.h> // Library of Standard Boolean Types
#include "inc/tm4c123gh6pm.h" // Definitions for interrupt and register assignments on Tiva C
#include "inc/hw_memmap.h" // Macros defining the memory map of the Tiva C Series device
#include "inc/hw_types.h" // Defines common types and macros
#include "driverlib/sysctl.h" // Defines and macros for System Control API of DriverLib
#include "driverlib/interrupt.h" // Defines and macros for NVIC Controller API of DriverLib
#include "driverlib/gpio.h" // Defines and macros for GPIO API of DriverLib
#include "driverlib/timer.h" // Defines and macros for Timer API of driverLib
/* ----------------------- Global Variables --------------------- */
uint32_t ui32Period; // Variable to store the period to be inputted to Timer0
uint32_t ui32IntFrequency = 0x00000006; // Variable to determine the Blinking Frequency of LEDs
/* ----------------------- Function Prototypes --------------------- */
void Timer0IntHandler(void); // The prototype of the ISR for Timer0 Interrupt
/* ----------------------- Main Program --------------------- */
int main(void){
// Set the System clock to 80MHz and enable the clock for peripheral Port F and Timer0
SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
// Set the PF1, PF2, PF3 as output and configure Timer0 to run in periodic mode
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
// Calculate the Time period for 50% duty cycle while switching at "ui32IntFreqency" Hz.
ui32Period = (SysCtlClockGet() / ui32IntFrequency) / 2;
// Load the Timer with the calculated period.
TimerLoadSet(TIMER0_BASE, TIMER_A, ui32Period - 1);
// Enable the Interrupt specific vector associated with Timer0A
IntEnable(INT_TIMER0A);
// Enables a specific event within the timer to generate an interrupt
TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
// Master interrupt enable API for all interrupts
IntMasterEnable();
// Enable the Timer
TimerEnable(TIMER0_BASE, TIMER_A);
while (1);
}
/* ----------------------- Function Definition --------------------- */
void Timer0IntHandler(void)
{ // The ISR for Timer0 Interrupt Handling
// 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_3)){
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, 0);
} else{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, GPIO_PIN_3);
}
}