fall 2015 hw12

12
ES52 Fall 2015 Homework 12 Due Tuesday, 11/24 at 11:59pm Section: _______ Name: ________________________________ Reading Practical Electronics for Inventors Chapter 12, 12.9 12.9.2: Three-State Buffers & Lathes Practical Electronics for Inventors Chapter 12, 12.1 12.2: Simple logic interfacing Practical Electronics for Inventors Chapter 12, 12.10.5 12.10.6: DACs & ADCs Practical Electronics for Inventors Chapter 16, 16.1 16.5: Audio Electronics Read the Arduino reference on the delay() function paying attention to the Caveat Article on avoiding the delay() function at: http://playground.arduino.cc/Code/AvoidDelay Always list collaborators and cite external references used in compliance with the course Collaboration and Academic Integrity Policy

Upload: nameofthedoctor

Post on 01-Feb-2016

9 views

Category:

Documents


0 download

DESCRIPTION

Advanced Circuitry Homework Problems covering microcontrollers, especially focusing on Arduino coding.

TRANSCRIPT

Page 1: Fall 2015 Hw12

ES52 Fall 2015

Homework 12 Due Tuesday, 11/24 at 11:59pm

Section: _______ Name: ________________________________

Reading

Practical Electronics for Inventors Chapter 12, 12.9 – 12.9.2: Three-State Buffers & Lathes

Practical Electronics for Inventors Chapter 12, 12.1 – 12.2: Simple logic interfacing

Practical Electronics for Inventors Chapter 12, 12.10.5 – 12.10.6: DACs & ADCs

Practical Electronics for Inventors Chapter 16, 16.1 – 16.5: Audio Electronics

Read the Arduino reference on the delay() function paying attention to the Caveat

Article on avoiding the delay() function at: http://playground.arduino.cc/Code/AvoidDelay

Always list collaborators and cite external references used in compliance with the course Collaboration and Academic Integrity Policy

Page 2: Fall 2015 Hw12

ES52 Homework 12 Fall 2015

Rev B 2 of 9

1) Finite State Machine Combination Lock (22 points) You are desiging a digital combination lock using a Finite State Machine (FSM). The hardware block

diagram looks like this:

The lock has two input buttons “LEFT” and “RIGHT” and two outputs which go high to light two indicator

LEDs. One LED indicates when the lock is ready for input, the other indicates when the lock is UNLOCKED.

The lock itself is a FSM consisting of flip-flops and the appropriate combinatorial logic to create the

next state and the output signal from the present state and the inputs. The FSM is clocked by a 4Hz

clock generator. The pushbuttons are normally open and short the contacts when pressed. You may

assume the pushbuttons have no mechanical bounce.

To use the lock:

Any time neither button is pressed the lock should be ready to accept a combination.

When ready to accept a combination, the READY LED should be on.

To unlock the lock, enter the combination by pressing the buttons as follows:

o Press both the LEFT and RIGHT button simultaneously to start combination entry

o Release the RIGHT button while continuing to hold the LEFT button down

o Press the RIGHT button again so both buttons are down

o Release the LEFT button while continuing to hold the RIGHT button down

o Press the LEFT button again so both buttons are down

o The lock is now unlocked1

When the lock is unlocked, the UNLOCKED LED should be lit. The lock should stay UNLOCKED as

long as both buttons are pressed. If either or both buttons are released, the lock should return

to READY.

While the combination is being entered the READY LED should be off

If you enter a wrong sequence the lock should return to the ready state

After a reset, the lock should be ready to accept a combination.

1 Ok, so it’s not the most secure combination in the world. Our problem is that if we make it more secure your

problem becomes much harder. So, be careful what you wish for.

Page 3: Fall 2015 Hw12

ES52 Homework 12 Fall 2015

Rev B 3 of 9

Hint: Be careful. It is likely that several FSM clock cycles will occur for each button entry in the

combination. Do not assume that there is only one FSM clock per combination sequence.

a) (12 points) You plan to implement the lock using a hardware Finite State Machine (FSM). Draw

a directed graph for this hardware. Be sure and show the inputs and outputs to your state

diagram and label each state to describe its function. Mark your reset state.

b) (8 points) Draw the Present State/Next State table for the Combination Lock FSM. Include

columns for the output signals. Indicate the reset state.

c) (2 points) How many flip-flops will you need to implement your Finite State Machine?

Page 4: Fall 2015 Hw12

ES52 Homework 12 Fall 2015

Rev B 4 of 9

2) What is Wrong with this Code? (12 points) Each of the following code examples have at least one show-stopping bug.

The bugs may be syntax errors or functional errors in the code. Assume that all

functions that aren't explicitly shown work as described. For each example, find and discuss

the bug. Hint: pay attention to the number of points for each question.

a) (5 points)

// Every hour, this program calls the function

// readDayofWeek(), which returns the day of the

// week (0 = Sunday). It prints to the serial port

// if the day is Friday.

const int FRIDAY 5 // 0 = Sunday, 6 = Saturday

const int milliseconds_in_an_hour = 1000 * 60 *60;

void setup (){

Serial.begin (9600);

}

void loop (){

if( readDayofWeek = FRIDAY ){

Serial.println ("Yeah! It’s Friday!");

}

delay(Milliseconds_in_an_hour);

}

Page 5: Fall 2015 Hw12

ES52 Homework 12 Fall 2015

Rev B 5 of 9

b) (3 points)

// This program checks to see if another day

// has elapsed since it was first started and

// prints a motivational message it if has.

const int milliseconds_in_a_day = 1000*60*60*24;

int time_last_printed = 0;

void setup (){

Serial.begin (9600);

}

void loop (){

if( millis () > (time_last_printed + milliseconds_in_a_day)){

Serial.println ("Today is the first day of the rest of your

life...");

}

time_last_printed = millis();

}

Page 6: Fall 2015 Hw12

ES52 Homework 12 Fall 2015

Rev B 6 of 9

The following program is intended to run on the following hardware:

c) (4 points)

// This program lights up the LED connected to

// Arduino pin 15 when the button is pressed

// and turns it off if the button is not pressed

const int MODE_BTN = 3;

const int OUTPUT_LED = 15;

void setup (){

pinMode(MODE_BTN, INPUT);

pinMode(OUTPUT_LED, OUTPUT);

}

void loop (){

if( digitalRead(MODE_BTN == 1){

OUTPUT_LED = true);

}

delay(100); // don’t need to check too often since waiting on a human

}

Page 7: Fall 2015 Hw12

ES52 Homework 12 Fall 2015

Rev B 7 of 9

3) Design an Interrupt-Driven Cat Counter (20 points) Your significant other is a crazy cat lady/man. S/he has nine cats that wander in and out

of the house all the time. S/he is very excited that you are taking ES52 because now you

can build a device that keeps track of how many cats are outside at any given time.

Your house has two cat doors, one that only opens inward and one that only opens out.

Each cat door has an electronic switch that shorts to ground when the door opens.2

When a cat goes through one of the doors, the normally open (n.o.) switch closes for

about 50mSec. (You may assume there is no bounce on the switch; i.e., there is only

one transition from closed to open and back to closed again when a cat goes out the

door.)

You design an Arduino Pro Micro powered cat counter which will allow the door

switches to interrupt the Arduino.3 The circuit can display a single value between “0” to

“9” on a Numitron seven-segment display to indicate how many cats are currently

outside. When you reset the cat counter the display shows “0,” so you always reset it

when you are sure that all the cats are in the house. Here is the completed schematic

for the cat counter:

2 Lucky for you none of the other cats in the neighborhood like the brand of cat food your S.O. serves so the only

cats that go in and out of the cat doors are hers/his. 3 The Arduino Pro Micro is essentially the same as the Arduino Micro except is has somewhat fewer I/O pins

available. Since you use so few I/O pins in this circuit, the differences are unimportant. The Pro Micro does require and external reset switch, which you have included in your design.

Page 8: Fall 2015 Hw12

ES52 Homework 12 Fall 2015

Rev B 8 of 9

Page 9: Fall 2015 Hw12

ES52 Homework 12 Fall 2015

Rev B 9 of 9

As a reminder, here are the pins on the Arduino Pro Micro that can be used to interrupt the

microcontroller:

Arduino Interrupt # ProMicro Pin Number Notes 0 D3 (SCL) Asynchronous on Edge Trigger4 1 D2 (SDA) Asynchronous on Edge Trigger 2 D0 (RXI) Asynchronous on Edge Trigger 3 D1 (TXO) Asynchronous on Edge Trigger 4 D7 Synchronous

(a) (20 points) Attached is skeleton Arduino code for you to complete to create your interrupt

driven cat counter. You will need to complete the function to display a number from 0 to 9

on the Numitron:

void updateDisplay(int digit_to_display)

the interrupt routines:

void catOutISR()

void catInISR()

as well as the main program loop().

You do not have to worry about there being more than 9 or less than 0 cats in the house.

Also, you do not have to write the code as a FSM but your code does have to be clear and

understandable.

Make sure you properly initialize all the I/O pins you are using and declare any variables used

in your Interrupt Service Routines properly. You will also need to set up the Interrupt Service

Routine(s) which is/are called when a cat goes through either one of the doors. The

foreground FSM should do what is necessary so that the cat counter always displays the

number of cats that are currently outside. Use comments to explain your code.5

4 Asynchronous interrupts can wake the uController from sleep modes other than Idle mode.

5 You may download the skeleton code from Canvas if you prefer but please make it easy for us to tell what you

have changed/added.

Page 10: Fall 2015 Hw12

D:\users\david\Documents\Teaching\Harvard\ES-52\2015_2 Fall\Homework\HW12 p2 CODE.no FSMc Sunday, November 15, 2015 3:55 PM

/************************************* Partially Complete Cat Counter Code Skeleton for Homework 12 Question 2

<Your Name Goes Here> 2015-11-25 ************************************/

// declare any constant values you need

const int SSD_A = 2; // least significant bit of the 74LS247 Seven Segment Decoderconst int SSD_B = 3;const int SSD_C = 4;const int SSD_D = 5; // most significant bit of seven segment decoder

const int inSw = 0; // define pins used by cat door switchesconst int outSw = 1;

// Declare Program Variables

int cats_outside; // this is the variable that keeps track of the cat count

void setup() { // put your setup code here, to run once:

}

-1-

Page 11: Fall 2015 Hw12

D:\users\david\Documents\Teaching\Harvard\ES-52\2015_2 Fall\Homework\HW12 p2 CODE.no FSMc Sunday, November 15, 2015 3:55 PM

// **************************************************// void updateDisplay(int digit_to_display)//// Function to display a digit between "0" and "9"// on the Numitron. //// Arguments: digit_to_display = 0 to 9//// The 74HC247 is connected to the uController as// follows:// D2 -> A ('LS247 bit 0 input)// D3 -> B ('LS247 bit 1 input)// D4 -> C ('LS247 bit 2 input// D5 -> D ('LS247 bit 3 input)//// You may assume that you can update the 74LS247// inputs quickly enough that the Numitron will// not display intermediate values.// **************************************************

void updateDisplay(int digit_to_display) {

}

// ****************************************************// The following two Interrupt Service routines are// called when a cat leaves and enters the house // respectively. They should do the minimum necessary// to let the foreground FSM know that a cat has// exited or entered.// ****************************************************

void catOutISR() {

}

-2-

Page 12: Fall 2015 Hw12

D:\users\david\Documents\Teaching\Harvard\ES-52\2015_2 Fall\Homework\HW12 p2 CODE.no FSMc Sunday, November 15, 2015 3:55 PM

void catInISR() {

}

void loop(){

}

-3-