Anda di halaman 1dari 9

MAN TECH 4RM3

Lab #1
Dr. Timber Yuen (Sept 2017) Rev 0
Working with a Micro-Controller

Lab Exercise #1 – Getting Started


Procedure:
1. Log onto the PC using your McMaster ID and your own password.
2. For ETB B103---- User:CAT; Password:Control3
3. Connect the small end of the USB cable to the microcontroller, and the large end of the USB to one of
the USB ports on the back of the PC monitor.

4. Run energia by clicking the <Shortcut to energia.exe> icon and reply <Run>. The Energia
screen should appear on the screen as shown above. (The link must be a short cut because energia
needs the other header files in the original directory to run.)

5. Copy the following program onto the Energia screen and click the key to download onto the
microcontroller.
/* Blink Red LED
This program turns on an LED on for one second, then off for one second, repeatedly.
Pin 2 has an LED connected on MSP430 boards, has a name 'RED_LED' in the code.
*/

// the setup routine runs once when you press the reset button
void setup() {
pinMode(RED_LED, OUTPUT);
// initialize the digital pin RED_LED as an output. RED_LED is a pre-defined variable name
// The names RED_LED and P1_0 can be used interchangeably
}
// the loop() routine runs over and over again forever
void loop() {
digitalWrite(RED_LED, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(RED_LED, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

6. If the program fails to download, make sure that there is no USB memory sticks connected to the
computer at the same time. Memory sticks could confuse the communication between the PC and
the Micro-Controller
7. Try changing the Blink rate by reducing delay(1000) to delay(100).
8. Try changing the Blink rate by reducing delay(100) to delay(10).
9. What do you observe when the delay(10) command is used?
10. Put the delay(1000) commands back.
11. Try changing the name RED_LED to GREEN_LED in all 3 statements. Download the program again and
observe what happens.
12. Try removing the line pinMode(GREEN_LED, OUTPUT) by adding // onto the front of the line,
download the program again and observe what happens.
13. Try downloading the program below and observe the results as you press the PUSH2 button.
14. Replace all HIGH keywords with 1 and LOW keywords with 0 and download the program again. Does
the system behave differently?
15. Change the INPUT_PULLUP command in the setup() to INPUT and download the program again.
/*
Blink Red or Green LED Based on PUSH2 Button State
*/

const int ButtonPin = P1_3; // P1_3 is the PUSH2 button


int ButtonState = 0; // since ButtonState is defined = 0 , it will allow the while loop to run.

// the setup routine runs once when you press reset:

void setup() {
pinMode(GREEN_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
pinMode(ButtonPin, INPUT_PULLUP); // Define Push Button as an Input (pushed is OFF)
}

void loop() {
while (ButtonState == LOW){
ButtonState = digitalRead(ButtonPin); // update the ButtonState by reading P1_3
digitalWrite(RED_LED, LOW); // Turn off Red LED
digitalWrite(GREEN_LED, HIGH); //Turn on Green LED
delay(100);
digitalWrite(GREEN_LED, LOW); //Turn off Green LED
delay(100);
}
while(ButtonState==HIGH){
ButtonState = digitalRead(ButtonPin); // update ButtonState
digitalWrite(GREEN_LED, LOW); //Turn off Green LED
digitalWrite(RED_LED, HIGH); // Turn on Red LED
delay(100);
digitalWrite(RED_LED, LOW); //Turn off Red LED
delay(100);
}
}

Lab Exercise #2 – Use a Pot to Control LED Blink Rate


Procedure:
1. Connect one gender changer to each of the I/O ports on the Micro Controller to change the pin
connectors to socket connector.
2. Connect a potentiometer to the TI board per the circuit diagram below.
a. Red (or first) Wire to Vcc
b. Blue (or middle) Wire to P1_3
c. Black (or last) Wire to GND

3. Cut and paste the program below onto energia, and download the program onto the micro
controller.
/* Use AnalogRead() to read a Pot and Control the Blink Rate of GREEN_LED
*/
int sensorValue = 0;

void setup() {
pinMode(GREEN_LED, OUTPUT);
}

void loop() {
// read the input from analog pin A3 (must use “A_something” for analogRead()
// Used as an analog input - cannot use the name P1_3 although it is the same pin:
sensorValue = analogRead(A3);
digitalWrite(GREEN_LED, LOW); //Turn off Green LED
delay(sensorValue);
digitalWrite(GREEN_LED, HIGH); // Turn on Red LED
delay(sensorValue);
}
4. Turn the knob on the pot and observe the blink rate of the GREEN_LED changes.
5. Modify the program to do the following:
a. If the PUSH2 button is pushed, turn on the GREEN_LED
b. If the PUSH2 button is not pushed, turn on the RED_LED.
c. In either of the cases above, use the pot to control the blink rate of the LED.
6. Hand in the program you created as part of the lab report for this lab. (if a lab report is required).

Lab Exercise #3 – Fade an on Board LED automatically Using AnalogWrite()


Procedure:
1. Remove the pot from lab exercise #2.
2. Copy the following program below in the energia and download it on to the microcontroller.
3. Change the delay from delay(2) to delay(20) and observe the effect.
4. Change the delay to delay(200) and observe the effect.

/* This sketch fades the on board GREEN_LED (pin 14) using the analogWrite capable pins.
*/
void setup()
{
pinMode(14, OUTPUT);
}
void loop() {
// fade the LED from off to brightest:
for (int brightness = 0; brightness < 255; brightness++)
{
analogWrite(14, brightness);
delay(2);
}
// fade the LED from brightest to off:
for (int brightness = 255; brightness >= 0; brightness--)
{
analogWrite(14, brightness);
delay(2);
}
delay(100);
}

Lab Exercise #4 – Fade an on Board LED with a Pot


Procedure:
 Re-connect the pot onto the microcontroller
 Download the following program and observe how the pot could change the brightness of the LED.
 What is the maximum value the variable “outputValue” will take on?
 What is the minimum value the variable “outputValue” will take on?

const int analogInPin = A3; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 14; // Analog output pin that the on board Green LED is attached to

int sensorValue = 0; // value read from the pot


int outputValue = 0; // value output to the analog out

void setup() {

void loop() {
sensorValue = analogRead(analogInPin); // read the analog in value: 10-bit resolution A/D

// map it to the range of the analog out: 8-bit resolution D/A (Max Brightness value is 255)
outputValue = map(sensorValue, 0, 1023, 0, 255);

for (int brightness = outputValue; brightness >= 0; brightness--)


{
analogWrite(analogOutPin, brightness);
delay(20); // use 20 msec delay to simulate blinking
}

// wait 1 second before the next loop


// for the analog-to-digital converter to settle
// after the last reading:
delay(1000);
}

Lab Exercise #5 – Working with a Stepper Motor


Procedure:
1. Use a Digital Multi Meter to check the continuity of the 2 phases of the stepper motor.

2. Set the Multi-meter to “2k Ω” range and connect the red and black probes as shown.
3. Measure the coil resistance between the White and Yellow wires:__________ Ω
4. Measure the coil resistance between the Red and Blue wires:__________ Ω
5. Install the green motor controller onto the top of the MicroController (make sure the white dot
on each of the cards is lined up)

Fold the copper wire onto the wire jacket and insert each of the wires into the sockets (the wire is
too thin, adding the jacket onto the wire diameter allows the screw to get a better grip of wire)

6. Copy the following program below in the energia and download.


7. Push the PUSH2 button and hold it for about 5 seconds to see the effect.
8. Why do we need to hold the button for 5 seconds to see the effect?
/* 2 Phase Stepper Motor Control - Connect the Stepper Motor to Roboteurs board

Phase 1 Wire 1 to M1+ & Phase 1 Wire 2 to M1-

Phase 2 Wire 1 to M2+ & Phase 2 Wire 2 to M2-

// This program will rotate the stepper motor through 4 commutations

To make the stepper rotate, we need to keep changing the polarity of the 2 phases.

*/

const int ButtonPin = P1_3; // P1_3 is the PUSH2 button


int ButtonState = 0; // since ButtonState is defined = 0 , it will allow the while loop to run.

void setup() {
pinMode(ButtonPin, INPUT_PULLUP); // Define Push Button as an Input (pushed is OFF)
// initialize the digital pin as an output.
// If your Roboteurs board is black instead of green – change P1_4 to P2_5 and P1_5 to P2_4

pinMode(P1_4, OUTPUT); // Define P1_4 as an Output (Phase 1)


pinMode(P1_5, OUTPUT); // We need to define P1_5 as an output port (Phase 1)
pinMode(P2_1, OUTPUT); // Define P2_1 as an Output (Phase 2)
pinMode(P2_2, OUTPUT); // We need to define P2_2 as an output port (Phase 2)
}
void loop() {
// If your Roboteurs board is black instead of green – change P1_4 to P2_5 and P1_5 to P2_4

while(ButtonState==HIGH) // button not pushed – CW Motion


{
ButtonState = digitalRead(ButtonPin); // update ButtonState
digitalWrite(P1_4, LOW); // turn the phase 1 off
digitalWrite(P1_5, LOW);
digitalWrite(P2_1, HIGH); // turn the phase 2 on
digitalWrite(P2_2, LOW);
delay(1000); // delay time in msec (1000 = 1 sec)
digitalWrite(P1_4, HIGH); // turn the phase 1 on
digitalWrite(P1_5, LOW);
digitalWrite(P2_1, LOW); // turn the phase 2 off
digitalWrite(P2_2, LOW);
delay(1000);
digitalWrite(P1_4, LOW); // turn the phase 1 off
digitalWrite(P1_5, LOW);
digitalWrite(P2_1, LOW); // turn the phase 2 on
digitalWrite(P2_2, HIGH);
delay(1000);
digitalWrite(P1_4, LOW); // turn the phase 1 on
digitalWrite(P1_5, HIGH);
digitalWrite(P2_1, LOW); // turn the phase 2 off
digitalWrite(P2_2, LOW);
delay(1000);
}
while(ButtonState==LOW) // button pushed – CCW motion
{
ButtonState = digitalRead(ButtonPin); // update ButtonState
digitalWrite(P1_4, HIGH); // turn the phase 1 on
digitalWrite(P1_5, LOW);
digitalWrite(P2_1, LOW); // turn the phase 2 off
digitalWrite(P2_2, LOW);
delay(1000);
digitalWrite(P1_4, LOW); // turn the phase 1 off
digitalWrite(P1_5, LOW);
digitalWrite(P2_1, HIGH); // turn the phase 2 on
digitalWrite(P2_2, LOW);
delay(1000); // delay time in msec (1000 = 1 sec)
digitalWrite(P1_4, LOW); // turn the phase 1 on
digitalWrite(P1_5, HIGH);
digitalWrite(P2_1, LOW); // turn the phase 2 off
digitalWrite(P2_2, LOW);
delay(1000);
digitalWrite(P1_4, LOW); // turn the phase 1 off
digitalWrite(P1_5, LOW);
digitalWrite(P2_1, LOW); // turn the phase 2 on
digitalWrite(P2_2, HIGH);
delay(1000);
}
}
9. The motor will behave like a clock with its shaft moving one commutation cycle per second.
10. Count the number commutation cycles required per 360 degrees rotation ___________
11. The resolution of the stepper motor is: _______________ degrees per commutation cycle.
12. Rewrite the program to make the stepping motor to rotate in the opposite direction.
13. Rewrite the program again to use the PUSH2 button to change the direction of motor. Show
Timber your results and hand in your program as part of the lab report.
Lab Exercise #6 – Stepper Motor Holding Torque & Dynamic Torque
Procedure:
1. Set one of the delays in the above program to delay(10000) (i.e. 10 sec)
2. Download the program onto the controller again
3. Let the program “run”. The motor should stop and hold the shaft position for 10 sec.
4. Turn the shaft by hand and get a feel for the amount torque that the motor can hold.
5. Use a C-clamp to gently hold the motor onto the lab table (Do not tighten C-clamp too much –
You will jam and damage the motor)
6. Put a small paper clip on the output shaft and wait until the clip goes on to the 9:00 o’clock
position and hang a 50 g hanger onto the clip add weight until the motor starts losing position.
Motor Load Holding Torque = torque
developed by the motor when it is
holding position (not moving).

Motor Dynamic Torque = torque


developed by the motor when it is
moving from one position to another.
7. Determine the Motor Load Holding Torque:_________[units = Nm]
8. (Remember torque = force x moment arm length)

9. Set the delay in the program back to delay(1000) – (i.e. 1 sec) and see how much load the motor
can turn while the motor is in motion dynamically.
10. Determine the Motor Dynamic Torque: __________[units = Nm]
11. (Note that torque = force x moment arm length)

---------------------- End of Lab 1 ----------------------

Anda mungkin juga menyukai