Uploaded by Yizuo Chen

3305 lab1

advertisement
ELEX 3305: Microcontroller Systems
Lab 1
Lab 1 – Digital Input, Output and Timing
1 Objectives
1. Monitor and control digital inputs and outputs of the microcontroller.
2 Prelab Exercises
1. Study the schematic on page 20 of the TM4C123G LaunchPad Evaluation Board Users Guide
available in the Datasheets section under the course content in the Learning Hub and answer the
following questions:
a. Which pins on the TMC123G microcontroller are the Red LED, Green LED and Blue LED
connected to?
b. Are the Red, Green and Blue LED control signals active high or active low?
c. Which pins on the TMC123G microcontroller are the pushbuttons “switches” SW1 and
SW2 are connected to?
d. Are SW1 and SW2 useful without pull-up or pull-down resistors?
e. If an internal pull-up or pull-down resistor is necessary for SW1 and SW2 to be useful,
which register bits need to be set/cleared to configure the internal pull-up or pull-down
resistors (see section 10.5, Registers 15 & 16 in the Tiva TM4C123GH6PM
Microcontroller Data Sheet)?
2. Generate flowcharts or pseudocode for all parts of the lab.
3 Lab Activities
Open Code Compose Studio. Create a new Code Composer Studio project for Lab 1 following the
instructions from the Tutorial.
As you work through this lab, you will be changing your program for different parts of the lab. Make sure
that you save the code generated in each part of the lab as a separate file. As you go on to the next part of
the lab, while editing your main.c, select File→Save As , and save your code using another filename
such as main_2.c. Note that the CCS project can only have one main() function, so you must exclude the
old main.c from your project. You can exclude the old main.c from your project by right-clicking the
file in the Project Explorer window in Code Composer Studio and selecting Exclude from Build.
3.1 Write a msDelay() function
1. Using the delay loop from the tutorial lab, precisely measure the toggling interval on the output pin
connected to the blue LED using an oscilloscope. Use the result to determine the approximate
number of loop iterations required to generate a one millisecond delay. This does not need to be
precise.
// generate delay
for (loopCount = 0; lopCount < DELAY_LOOPS; loopCount++) {}
© Robert Trost 2023
1
ELEX 3305: Microcontroller Systems
Lab 1
2. Complete the following msDelay() function such that it will delay execution for the desired number
of milliseconds:
////////////////////////////////////////////////////////////
// msDelay - Function to generate a variable delay
// Arguments: ms - The number of milliseconds for the delay
// Return Value: none
////////////////////////////////////////////////////////////
void msDelay(unsigned int ms) {
// loop for the desired number of ms
while (ms > 0) {
// Add your code here to generate a 1 ms delay
???
ms--;
}
}
3.
Use the program given in the tutorial, but replace the loop that generates the delay with function call
to msDelay(_100MS). You will need to define the symbolic constant _100MS. Note that the
name of the constant starts with an underscore as names in C can not begin with a number.
4. Set the green LED to toggle at a 100 ms interval and run your code. Observe the output on an
oscilloscope to verify functionality.
5. Rewrite the traffic light program from the tutorial lab using the msDelay() function.
3.2 Monitoring Inputs
1. Write a program that continuously monitors the SW1 and SW2 pushbutton states and turns on the
LEDs as described in the table below.
Buttons Pressed
RGB LED Output
None
Off
SW1 only
Blue
SW2 only
Red
SW1 and SW2
Green
Note that SW1 will work as expected, however, bit 0 of Port F is “locked” and you will need to add
the following two lines of code before you initialize the Port F registers for SW2:
GPIO_PORTF_LOCK_R = GPIO_LOCK_KEY;
GPIO_PORTF_CR_R |= SW2;
3.3 Programming Problem
Write a program that simulates a pedestrian controlled traffic light. The light flashes green every half
second. If the SW1 button is pressed, the light changes to yellow for 1 second, then turns solid red for 5
seconds. After the 5 seconds, the light returns to the default state of flashing green. Note that if you
quickly press and release the button while the LED is flashing green it may not detect the button press and
© Robert Trost 2023
2
ELEX 3305: Microcontroller Systems
Lab 1
activate the signal. Why do you think that is? What is the minimum length of time you must hold the
button down to guarantee that the button press is detected?
© Robert Trost 2023
3
Download