User manual

LPCXpresso Experiment Kit - User’s Guide
Page 75
Copyright 2013 © Embedded Artists AB
** Function name: playNote
**
** Descriptions: Initialize 16-bit timer #1 for PWM generation
**
** Parameters: noteInUs: Period time (in microseconds) for tone
** durationMs: Length of tone (in milliseconds)
**
** Returned value: None
**
*****************************************************************************/
void playNote(uint16_t noteInUs, uint16_t durationMs)
{
stopPWM();
initPWM(noteInUs); /* Setup to generate a PWM signal with cycle time = note */
updatePWM(0, 50); /* Update MAT0 to generate a 50% duty cycle */
startPWM();
delayMS(durationMs); /* Wait for the duration of the tone */
updatePWM(0, 100); /* Turn the signal off = signal constant high */
}
We start with a duty cycle of 50%. Half time the buffer is on and the other half it is off. It is actually
possible to adjust the volume by varying the duty cycle. The shorter time the buzzer is on, the lower
the volume is. It is not the duty cycle that controls the tone. It is the cycle time that controls this.
Recreate the breadboard setup from Figure 17 with one change. Connect the buzzer control to signal
GPIO_28-PWM, instead of to signal GPIO_7-BUZZ. That way you can create a PWM signal with the
functions that we created in the previous experiments.
Create an application that can play a song. In a song, notes can have different duration and there can
be pauses between notes. Design a system where you can specify songs in a string. Then let the
application decode this string and play the song.
7.11.3 Lab 10c: Control a Servo Motor
In order to complete this experiment you need an analog control servo, sometimes just called an RC
servo, and also an external power supply, 4-6 volt DC (about 1 ampere). These two parts are not
included in the LPCXpresso Experiment kit, but can easily be bought from electronic components
distributors and RC (Radio Control) hobby suppliers.
Servos are used in many different products. The smaller ones we focus on in this experiment are found
in toys. For example in small robots, rc cars, rc airplanes, etc. There is no unified color scheme for all
servos for the three wires you connect to: power (+5V), ground and control signal (PWM signal). Check
the datasheet of the servo that you will be using so you connect to the correct wires.
Figure 43 Typical Servo
There are many different types and models but in this experiment we will only focus on how to control
the position of the servo, which is done via a PWM signal. The cycle period can vary over a range, but
20 ms is an average value that will work on most servos. The position of the servo is controlled by the
on-time of the PWM signal. 1.5 ms will place the servo in the middle/neutral position. Increasing the
on-time to 2 ms will move the position to the right-most position. Decreasing the on-time to 1 ms will
move the position to the left-most position. Note that the corner values can differ between servos.