Tutorial Week 5 Answers

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

TUTORIAL WEEK 5

1. The aim of this tutorial is to prepare you for the Week 5 Laboratory Test.
2. This tutorial will cover GPIO programming and UART programming.
3. Use the provided T5 template from the course website.
4. T5.h lists the available func�ons in the supplied library.
5. Use the datasheet and the lecture notes as reference material for your programs.
6. There may be more ques�ons than you can answer in the tutorial session. The tutorial is designed to give
extra ques�ons for you to use in your prepara�on.
7. These ques�ons have been designed so that you do not need any external circuitry or wiring. Everything you
need is on board – See Appendix A

1 GPIO

Copy the template folder and rename it to T5_Q1.

Write a program that will turn on LED (D4) when the SW1 buton is pressed. The LED will remain on un�l the SW2
buton is pressed.

Hint: you may need to use the msDelay() func�on to make sure that your ac�on is triggered only once per buton
press. This is known as Debouncing – preven�ng mul�ple triggers when a buton is pressed.

2 UART

Copy the template folder and rename it to T5_Q2.

Write a program that will echo the key that is entered in the terminal. (Echo means read the key that was pressed in
the terminal and then write it back out to the terminal so it can be seen). You must write the program for UART 0.
The Communica�ons se�ngs for the UART will be 115200 baud, 8 bits, No parity, 1 stop bit with a prescaler of divide
by 8.

You will need to use Teraterm to communicate with UART0 on the Tiva Board.

3 UART Extended

Copy the folder from Q2 and rename it to T5_Q3.

Modify Q2 so that when the A key is entered in the terminal, LED (D4) is turned on and when the Z key is entered in
the terminal, LED (D4) is turned off. The program should con�nue allowing the LED to be turned on and off un�l the
Escape (esc) key is pressed when the program will ternminate. See Appendix B for the Ascii value of the escape
character.
APPENDIX A: TIVA BOARD CONNECTIONS
APPENDIX B: ASCII TABLE

htps://commons.wikimedia.org/wiki/File:ASCII-Table.svg
1 // =========================== Question 1 ============================
2 #include "TM4C129.h"
3 #include "T5.h"
4
5 #define PORT_MASK ((1<<5)|(1<<8)) // Port F and Port J bits
6 #define PORTF_MASK (1<<0) // Port F pin 0
7 #define PORTJ_MASK ((1<<1)|(1<<0)) // Port J pins 0,1
8
9
10 #define LED_D4 (1<<0)
11 #define SW1 (1<<0)
12 #define SW2 (1<<1)
13
14
15 int main(void)
16 {
17 setSysClk80MHz() ; // Do NOT remove. Set the System clock to 80MHz
18
19 // Your code goes here
20
21 // Port F pin 0 for D4 (PF0) : Output
22 // Port J pin 0 for SW1 (PJ1) : Input - press (LOW)
23 // Port J pin 1 for SW2 (PJ1) : Input
24
25 // SYSCTL:
26 // enable port F(b5) & port J (b8)
27 SYSCTL->RCGCGPIO |= PORT_MASK ;
28 while( !(SYSCTL->PRGPIO & PORT_MASK) ) {}
29
30 // GPIO:
31 // minimal setup after board reset
32 // Configure GPIO N (PN1)
33 GPIOF_AHB->DIR |= PORTF_MASK ; // output
34 GPIOF_AHB->DEN |= PORTF_MASK ; // digital enable
35
36 // Configure GPIO J (PN1 PN0)
37 GPIOJ_AHB->DIR &= ~PORTJ_MASK ; // input
38 GPIOJ_AHB->PUR |= PORTJ_MASK ; // pull up resistor so SW1 SW2 high until pressed
39 GPIOJ_AHB->DEN |= PORTJ_MASK ; // all digital enable
40
41 // Make sure LED is OFF
42 GPIOF_AHB->DATA &= ~LED_D4 ;
43
44 // spinlock
45 volatile uint32_t sw;
46 while(true) {
47 sw=GPIOJ_AHB->DATA ;
48 if ( (sw & SW1) == 0 )
49 GPIOF_AHB->DATA |= LED_D4 ; // if SW1 turn led on
50
51 if ( (sw & SW2) == 0)
52 GPIOF_AHB->DATA &= ~LED_D4 ; // if SW2 turn led off
53
54 msDelay(100) ; // delay 0.1 seconds
55
56 }
57
58 } // end main
59
60
61
62
1 // ============== Question 2 ======================================
2
3 #include "TM4C129.h"
4 #include "T5.h"
5
6
7 void setup_GPIO_for_U0(void) {
8 // turn on the clock for GPIO A
9 SYSCTL->RCGCGPIO |= (1<<0); /* enable clock to PORTA */
10 while ((SYSCTL->PRGPIO&(1<<0)) == 0) { __asm("nop") ;}
11
12 // UART0 TX and RX use PA0 and PA1.
13 GPIOA_AHB->DEN |= 0x03; // Set PA0 and PA1 as digital
14 GPIOA_AHB->AFSEL |= 0x03; // Set PA0,PA1 as alternate function
15
16 // configure PP0 and PP1 for UART
17 GPIOA_AHB->PCTL = ((GPIOA_AHB->PCTL &=~ 0x000000FF)+0x00000011);
18
19 // delay a little - not alway necessary but i think it helps
20 msDelay(1) ;
21 }
22
23
24 void configure_UART0_parameters(void) {
25
26 // Turn on UART 6
27 SYSCTL->RCGCUART |= (1<<0); // provide clock to UART0
28 while ((SYSCTL->PRUART&(1<<0)) == 0) { __asm("nop") ;}
29
30 // UART0 initialization Thursday U6 115200,8,O,1, div8
31 UART0->CTL = 0; // disable UART0
32 UART0->IBRD = 86; // 80MHz/8=10MHz, 10MHz/115200= 86.80555556 IBRD=86
33 UART0->FBRD = 52; // ((0.80555556*64)+0.5=52.05555584) FBRD=52
34 UART0->LCRH = 0x60; // 8-bit, no parity, 1-stop bit
35 UART0->CC = 0; // use system clock
36 UART0->CTL = 0x321; // enable UART0, TXE, RXE, HSE=div8
37
38 // delay a little - not alway necessary but i think it helps
39 msDelay(1);
40 }
41
42 char read_UART0(void) {
43 char c;
44 while((UART0->FR&(1<<4)) != 0 ) {__asm("nop");} /* wait until the buffer is not
empty */
45 c = UART0->DR; /* read the received data */
46 return (c&0xFF); /* and return it */
47 }
48
49 void write_UART0(char c) {
50 while((UART0->FR & (1<<5) ) != 0) {__asm("nop");} /* wait until the buffer is not
full */
51 UART0->DR=c; /* write the data */
52 }
53
54
55 int main(void)
56 {
57 char ch ;
58
59 setSysClk80MHz() ; // Do NOT remove. Set the System clock to 80MHz
60
61 // Your code goes here
62
63 // Configure the UART0 for GPIO and communication parameters
64 setup_GPIO_for_U0() ;
65 configure_UART0_parameters() ;
66
67 // all ready for use, so echo keys pressed to terminal
68 while(true) {
69 ch = read_UART0() ;
70 write_UART0(ch) ;
71 }
72
73
74
75 } // end main
76
77
78
1 // ============== Question 3 ======================================
2
3 #include "TM4C129.h"
4 #include "T5.h"
5
6 #define PORT_MASK (1<<5) // for PORT F in SYSCTL
7 #define PORTF_MASK (1<<0) // for Port F pin 0 configuration in GPIO
8 #define LED_D4 (1<<0) // LED D4 pin
9
10 #define ESC 27 // ascii number for the escape <ESC> key (see Ascii
table)
11
12
13 // ================ UART 0 Functions ===============================
14 void setup_GPIO_for_U0(void) {
15 // turn on the clock for GPIO A
16 SYSCTL->RCGCGPIO |= (1<<0); /* enable clock to PORTA */
17 while ((SYSCTL->PRGPIO&(1<<0)) == 0) { __asm("nop") ;}
18
19 // UART0 TX and RX use PA0 and PA1.
20 GPIOA_AHB->DEN |= 0x03; // Set PA0 and PA1 as digital
21 GPIOA_AHB->AFSEL |= 0x03; // Set PA0,PA1 as alternate function
22
23 // configure PP0 and PP1 for UART
24 GPIOA_AHB->PCTL = ((GPIOA_AHB->PCTL &=~ 0x000000FF)+0x00000011);
25
26 // delay a little - not alway necessary but i think it helps
27 msDelay(1) ;
28 }
29
30
31 void configure_UART0_parameters(void) {
32
33 // Turn on UART 6
34 SYSCTL->RCGCUART |= (1<<0); // provide clock to UART0
35 while ((SYSCTL->PRUART&(1<<0)) == 0) { __asm("nop") ;}
36
37 // UART0 initialization Thursday U6 115200,8,O,1, div8
38 UART0->CTL = 0; // disable UART0
39 UART0->IBRD = 86; // 80MHz/8=10MHz, 10MHz/115200= 86.80555556 IBRD=86
40 UART0->FBRD = 52; // ((0.80555556*64)+0.5=52.05555584) FBRD=52
41 UART0->LCRH = 0x60; // 8-bit, no parity, 1-stop bit
42 UART0->CC = 0; // use system clock
43 UART0->CTL = 0x321; // enable UART0, TXE, RXE, HSE=div8
44
45 // delay a little - not alway necessary but i think it helps
46 msDelay(1);
47 }
48
49 char read_UART0(void) {
50 char c;
51 while((UART0->FR&(1<<4)) != 0 ) {__asm("nop");} /* wait until the buffer is not
empty */
52 c = UART0->DR; /* read the received data */
53 return (c&0xFF); /* and return it */
54 }
55
56 void write_UART0(char c) {
57 while((UART0->FR & (1<<5) ) != 0) {__asm("nop");} /* wait until the buffer is not
full */
58 UART0->DR=c; /* write the data */
59 }
60
61 // ================ main Function ===============================
62 int main(void)
63 {
64 char ch ;
65
66 setSysClk80MHz() ; // Do NOT remove. Set the System clock to 80MHz
67
68 // Your code goes here
69
70 // Configure the UART0 for GPIO and communication parameters
71 setup_GPIO_for_U0() ;
72 configure_UART0_parameters() ;
73
74 // Port F pin 0 for D4 (PF0) : Output
75
76
77 // SYSCTL:
78 // enable port N (b12)
79 SYSCTL->RCGCGPIO |= PORT_MASK ;
80 while( !(SYSCTL->PRGPIO & PORT_MASK) ) {}
81
82 // GPIO:
83 // minimal setup after board reset
84 // Configure GPIO N (PN1)
85 GPIOF_AHB->DIR |= PORTF_MASK ; // output
86 GPIOF_AHB->DEN |= PORTF_MASK ; // digital enable
87
88
89 // Make sure LED is OFF
90 GPIOF_AHB->DATA &= ~LED_D4 ;
91
92
93 // all ready for use, so echo keys pressed to terminal
94 while(true) {
95 // echo the character read so the user can see that a key was pressed
96 ch = read_UART0() ; // blocking read - wait until RX has receieved something
from the termianl
97 write_UART0(ch) ; // write it out to the terminal
98
99 // check for LED on key
100 if (ch=='A')
101 GPIOF_AHB->DATA |= LED_D4 ;
102
103 // check for LED off key
104 if (ch=='Z')
105 GPIOF_AHB->DATA &= ~LED_D4 ;
106
107 // check for LED off key
108 if (ch==ESC)
109 break ;
110 }
111
112 // now out of the key entry loop
113 // spinlock at end of embedded program as normal
114 while(true) ;
115
116
117
118 } // end main
119
120
121

You might also like