main.c 2.24 KB
Newer Older
Matthias Wenzl's avatar
Matthias Wenzl committed
1
/*
2
3
 * Toggles LED on pin 1 on port N
 * Turns off LED on pin 0 on port N if button (USR_SW1 on port J pin 0) is pressed
Matthias Wenzl's avatar
Matthias Wenzl committed
4
 *
5
 * Already set build options of this project (no need to change anything):
6
 * Stack size set to 4096 byte
7
8
9
 * Heap size disabled - No malloc() available
 * No code optimization - Easier debugging
 * Strict floating point interrupt behavior
Matthias Wenzl's avatar
Matthias Wenzl committed
10
11
 * Hardware floating point unit activated
 */
12

Matthias Wenzl's avatar
Matthias Wenzl committed
13

14
15
16
17
18
19
20
21
22
23
24
#include <stdint.h> /* C99 header for uint*_t types */
#include <stdbool.h> /* Driverlib headers require stdbool.h to be included first */
#include <inc/tm4c1294ncpdt.h> /* Supplies SYSCTL_RCGCGPIO*, GPIO_PORT* macros */
#include <inc/hw_types.h> /* Supplies HWREG macro */



/* Controller is initially clocked with 16 MHz (via PIOSC) */
/* !!! Changing this macro does not change clock speed !!! */
#define F_CPU (16000000)

25
int main(void)
Matthias Wenzl's avatar
Matthias Wenzl committed
26
{
27

28
    uint32_t ui32Loop;
29
30
31
32
33
34
35
36
37
38
    uint8_t ui8button;
    uint8_t ui8toggle = (1<<1);

    /* Activate GPIO ports */
    SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R12 | SYSCTL_RCGCGPIO_R8;


    /* Set pin 0 of GPIO port J to digital input */
    GPIO_PORTJ_AHB_DEN_R |=  (1<<0);
    GPIO_PORTJ_AHB_DIR_R &= ~(1<<0);
Matthias Wenzl's avatar
Matthias Wenzl committed
39

40
41
    /* Enable pull-up for button on pin 0 of port J */
    GPIO_PORTJ_AHB_PUR_R |= (1<<0);
Matthias Wenzl's avatar
Matthias Wenzl committed
42
43
44



45
46
47
    /* Set pins 0 & 1 of GPIO port N to digital output */
    GPIO_PORTN_DEN_R |= (1<<1) | (1<<0);
    GPIO_PORTN_DIR_R |= (1<<1) | (1<<0);
Matthias Wenzl's avatar
Matthias Wenzl committed
48

49
50
51
52
53
    while(1) {
        /* Toggle bit 1 in variable ui8toggle */
        ui8toggle = ui8toggle ^ (1<<1);
        /* Write value of ui8toggle to pin 1 of port N */
        HWREG(GPIO_PORTN_DATA_BITS_R + (1<<1)) = ui8toggle;
Matthias Wenzl's avatar
Matthias Wenzl committed
54

55
56
57
58
        /* Read value of pin 0 on port j into variable */
        ui8button = HWREG(GPIO_PORTJ_AHB_DATA_BITS_R + (1<<0));
        /* Set pin 0 of port N accordingly */
        HWREG(GPIO_PORTN_DATA_BITS_R + (1<<0)) = ui8button;
Matthias Wenzl's avatar
Matthias Wenzl committed
59

60
61
62
63
64
65
        /* Delay for *approximately* 0.5 s by counting to 615385:
         * 615385 = 16M cycles per second / 2 / approximately(!) 13 CPU cycles per loop iteration on average.
         * The CPU cycles per iteration vary widely, e.g., due to compiler and hardware optimizations! */
        for(ui32Loop = 0; ui32Loop < 615385; ui32Loop++)
            __nop(); /* "No operation", prohibits possible loop elimination by the compiler. */
    }
Matthias Wenzl's avatar
Matthias Wenzl committed
66
}