CPR E 381x/382x - Lab 9a

Sprayer Lock

 

1. Objectives

This lab continues to provide practice with C programming, since C programming is a tool that we use in the laboratory to gain hands-on experience with microcontrollers. However, the lab experience goes beyond the syntax of C to address program design and introduce a more complicated input device than the switches used so far – i.e., a keypad. Although advanced I/O devices are the focus of the latter part of the course, we start with the keypad to expand the programmer’s view of an I/O device. In addition, the lab underscores that software needs to understand the hardware that it is interfacing with.

1.1 Reference Files for Lab

Lab Evaluation Form

 

2. Prelab

Read all of the lab sections, there is a lot of info, which will take awhile to read through if you wait until lab time.  Begin to design your software, even if only pseudo-code.

3. Setup

Create the folder in your home directory U:\CPRE381\Lab9a to save all your work from this lab.

 

4. Sprayer Lock

Write a C program that implements a digital lock, e.g., to open a tractor's sprayer unit (i.e., give the farmer access to gauges, valves, chemicals used in treatments, etc.), having the following specifications and behavior.

 

BEFORE YOU START: As of Spring 2004, a hardware modification has been made to the PowerBoxes to accommodate this lab. Please make sure that you are sitting at a PowerBox that has a silver sticker on the circuit board (lower-left corner) reading "CAP MOD".


 

The digital lock uses the keypad input and LCD display output. The keypad is at the lower left of the PowerBox, as shown in the following diagram. The keypad has four rows and four columns of keys.

The layout of the keys on the keypad is:

1 - 2 - 3 - A
4 - 5 - 6 - B
7 - 8 - 9 - C
* - 0 - # - D

 

As given in the file defines.h, the address of the keypad input register, or port, is:

#define IO_DIGITAL_INPUT_KEYPAD 0x40000013

 

This is an 8-bit port, and the bits have the following interpretation:

Bit

7-5

4

3-0

Description

Not used

1 – Key pressed

0 - No key

Position of key pressed (0-15):
0 - 1 - 2 - 3
4 - 5 - 6 - 7
8 - 9 - 10-11
12-13-14-15

 

For example, key '#' is at position 14. Notice that the position field indicates a position number, with the key values (1, 2, 3, A, ... D) mapping directly to a position number (0, 1, 2, 3, ... 15).

 

Create your design to meet the following specifications:

·        The digital lock is either open or closed. The keypad is used to enter the password for the digital lock. The password is written to the LCD display. The correct password opens the lock, which in turn, allows the sprayer unit to be opened.

·        The initial password will be #06C382.  Upon successfully reading and identifying the sequence, the digital lock will open.  Any sequence may be entered provided that the last sequence is the password (e.g., ABCD#06C382 is valid).

·        For each key pressed, print the character that the key represents to the LCD screen. Use the LCD_PutChar(char) function. Remember to initialize the LCD at the beginning of the program.

·        Pressing the * key should let the user change the password.  Implement and document this feature using your own ideas.

·        In addition to the keypad input, the digital lock has one other input:

o       Reset digital lock - bit 0 of DIP Switch 1: If bit equals 0, reset the digital lock to closed.  After the sprayer has been locked, you should be able to enter the password for the lock again without having to restart the program.

·        In addition to the LCD display, the digital lock has one other output:

o       Status of digital lock - bit 0 of LED Bargraph 1: Turn on the LED when the digital lock is open.  Remember that writing a ‘0’ to the LED will turn it on.

 

The design of your program needs to address two interesting behaviors:

 

Password Identification

How is the password sequence identified? How does the program keep track of which keys have been pressed in order to check them against the password? Select one of the following options to design your program:

·        State Machine approach: Recall that a state machine can implement a sequence detector, not just sequences of 0 and 1, but sequences of any values. Design a FSM that uses the key pressed as an input to determine present state and next state transitions. At each state, it checks whether the key values match the password and acts appropriately. A challenge in this design approach is implementing the change of password (hint: each state may use a variable for the key expected).

·        Buffer approach: A buffer is a place to store data. It can be a hardware device, such as a buffer chip. However, it can also refer to a software data structure, such as an array. Declare a buffer to store the characters as keys are pressed, and then check whether it matches the password. (Hint: see the strstr string handling function in C.  What does the function do? How is the function used? Note: you should not need to include the string.h file.)

 

Key Debouncing

The keypad is set up to read the immediate value of the digital input.  However, a phenomenon known as bouncing may occur.  Due to the mechanical nature of the keypad, the input will bounce as it makes intermittent contact and finally settles on the contact.  Thus, you will get a waveform similar to the following graphic, where time is on the horizontal axis and logic value, vertical axis:

If you take the first value and assume that the next different value is the user releasing the key, your input will behave as if every key press causes the user to tap the key several times.  Of course, this is incorrect, as one key press by the user should be detected as only one key press in your program.  There are two solutions to this problem, one in hardware and another in software. 

Hardware: For a hardware-based solution, you would use a resistor/capacitor combination, below, resulting in the waveform shown.

Software: In software, you will simply ignore the input for a pre-specified amount of time (i.e. the bounce period). To wait a specified number of milliseconds, use the msleep function, which takes number of milliseconds to wait as a parameter.

msleep(xx);   // Waits xx ms 

In this lab, you should experiment with the software solution to debouncing to find the value for xx which waits long enough to keep the keys from bouncing, but does not wait so long that it keeps you from pressing keys quickly.

 

Please treat this project as if you were building a real lock -- add anything a real user might want.