CSCE 236 Embedded Systems, Spring 2015
Lab 3
Thursday, February 19, 2015
Names of Group Members:
1 Instructions
This is a group assignment to work on during class. You only
need to hand in one copy of this, but make sure that the names of all
of your group members are on this sheet to receive credit. Complete
all of the sections below and make sure to get the
instructor or TA to sign off where required.
You should keep your own notes on what you complete since parts of future homework
will build on this lab.
2 Dimming LEDs
In this section you will take two different approaches to solving the
same problem. The goal is to dim one of the LEDs whenever the button
is pressed. You can dim an LED by only turning it on for a very short
amount of time and then turning it off for a period of time. If you
do this faster than about 30 Hz, you will not notice the flickering of
the LED turning on and off and it will appear dimmer than if it is
fully on (since the human eye acts as an integrator at this
frequency).
2.1 Manual
First, you will achieve this by using manual delays. Download the
sample code for this lab from the course website and examine it. Make
sure you connect your LEDs and buttons to the proper pins by examining
the code. This code dims the RED LED when the button is pressed. It
does so by using the function delay(ms). Try changing the
values of the delays and determine what values work well.
Approximately what is the frequency and duty cycle that it is being
dimmed at? If you increase the amount of time the LED is off, at what
frequency can you start to see the LED pulsing?
The problem with this approach is that the processor is always busy
doing the pulsing of the LED to dim it. If a function is called that
prevents this from occurring, the LED will not dim properly. To see
this, insert another delay of 100 milliseconds outside of the logic
that controls the LED. This simulates the execution of other code
that takes a while to complete. What happens when you press the
button now?
Checkoff: Show the LED dimming when the button is pressed with and without the 100ms delay. What frequency and duty cycle are you using with your LED?
2.2 PWM
To overcome the problem that you cannot easily do other actions while
dimming the LED, you will now implement LED dimming using PWM. To do
this, connect your LED to pin PB2 (pin 10 on Arduino, which
should have the blue LED connected to it already), which is also
OC1B (output compare B for timer1).
Download the core code from the course website.
Now, look at
section 16 (Timer/Counter1) of the datasheet (the description of fast
PWM mode in 16.9.3 and the register description in 16.11 are
especially useful). The code I gave you already configures it so:
- the timer is in fast PWM mode, with ICR1 as the top.
- set PB2 as an output pin to enable the PWM output (this
is already done in the LEDInit function).
You need to:
- configure OC1B so it is cleared on compare match and set at bottom (see table 16-2).
- set an appropriate frequency (1KHz works well) by configuring
the clock prescalar and the TOP value (stored in ICR1)
- change the duty cycle when the button is pressed or released by changing the value in register OCR1B
Checkoff: Show the code using the PWM system to dim the LED when the
button is pressed. Note how adding code (e.g. delay(100)) in the
main loop no longer impacts the dimming. Be sure to show us how you got 1Khz clock.
3 Servo Control
Servos are also controlled by PWM signals. Most servos enable angular
control of the output shaft (e.g. between 0 and 120 degrees). The
servos we are using in class are known as continuous rotation servos.
Instead of controlling position, we are able to control the forward
and backward rotational speed using PWM signals. Most servos use PWM
signals with a period of approximately 20 milliseconds. By varying
the pulse length between 1ms to 2ms the servo will go from full speed
forward to full speed backwards (on a traditional servo this would
mean rotating from 0 to 120 degrees).
You have seen how to manually configure the PWM channels to control
the frequency and duty cycle. However, instead of manually
configuring all the PWM channels, we will instead make use of an
Arduino servo control library. Read the information on how to use the
servo library here:
http://arduino.cc/en/Reference/Servo
To hook up the servos to your breadboard, you will need to first use
the 3-pin header to adapt the female servo connector to a male
connector. Then, connect the black wire to ground, red to power (5V),
and white to the PWM signal. Double check these connections before
powering your board. Also note that the servos can draw more power
than a USB port can supply. Therefore, you should not restrict the
output of the servo while using USB power, instead make use of the AC
adapter to power the servos whenever AC power is available.
Checkoff: Write code that reverses the direction of one servo whenever the button is pressed. If you are unsure about how to connect the servo, make sure to ask before powering everything on!
File translated from
TEX
by
TTH,
version 4.03.
On 18 Feb 2015, 11:53.