CONTROLLING THE OUTPUT AND READING THE INPUT:EXERCISES

EXERCISES

Answers to these problems are not provided.

Since this is really all about input and output, a comprehensive set of exercises that focus specifically on input and output have been provided. We need to be completely comfortable with these I/O functions before we start on running motors, so you are encouraged to expand on these exercises on your own. These and similar techniques

will be used to control and respond to all the ancillary devices that we will use with our motors.

LED EXERCISES: CONTROLLING

THE LIGHT EMITTING DIODES (LEDS)

We will learn more about controlling the output from the LAB-X1 by writing a series of increasingly complicated programs that will control the ten-segment LED IC pro- vided on the LAB-X1. In these exercises we are controlling the LEDs, but the control strategies developed will apply to any kind of “on/off” devices that we will connect to the LAB-X1 or to any other device that we may design.

1. One at a time, light the eight LEDs on the right till they are all lit, and then turn them off one at a time. Time delay between actions is to be as close to one tenth of a second as you can get it.

2. Modify the preceding program so that the delay time is controlled by the top- most potentiometer on the LAB-X1. The time is to vary from 10 milliseconds to 200 milliseconds—no less, no more.

3. Write a program that will vary the glow on the rightmost LED from fully off to fully on once a second. Program the second LED to go dark and bright exactly 180 degrees out of phase with the first LED so that as one LED is getting brighter, the other LED gets dimmer, and vice versa.

4. Write a program that flashes the four leftmost LEDs on and off every 0.25 seconds and cycles the four LEDs on the right through a bright/dim cycle every two seconds.

5. Write a program that flashes the first LED 10 times a second, flashes the second one 9 times a second, and flashes the third LED whenever both LEDs are on at the same time. Display how many times the third LED has blinked on the LCD display. (Timing can be approximate but has to have a common divider so the third LED will give the beat frequency.)

LIQUID CRYSTAL DISPLAY EXERCISES: CONTROLLING THE LCD

The addresses of the memory locations used by the LCD have already been fixed, as has the instruction set that we use to write to the LCD. The description of the Hitachi HD44780U (LCD-II) controller instruction set, as well as its electronic characteristics, are given in the data sheet provided for the display. Here we will list only the codes that apply to our immediate use of the device.

There are two types of commands that can be sent to the display: the control codes and the set of actual characters to be displayed. Both uppercase and lowercase char- acters are supported, as are a number of special and graphic characters. The control codes allow you to control the display and set the position of the cursor and so on.

Each control code has to be preceded by decimal 254 or HEX$FE. (The controller also supports the display of a set of Japanese characters, which are not of interest to us.)

Command codes for the following actions are provided along with others. Go to the data sheet for the controller to learn what all these command codes are.

N Clear the LCD

N Return home

N Go to beginning of line 1

N Go to beginning of line 2

N Go to a specific position on line 1 N Go to a specific position on line 2 N Show the cursor

N Hide the cursor

N Use an underline cursor

N Turn on cursor blink

N Move cursor right one position

N Move cursor left one position

There are still other commands that you will discover in the data sheet. There are more memory locations within the LCD, and there are invisible locations beyond the end of the visible 20 characters.

It is also possible to design your own font for use with this particular display; all the information you need to do so is in the Hitachi HD44780U book/data sheet.

1. Write a program to put the 26 letters of the alphabet and the 10 numerals in the 40 spaces that are available on the display. Put four spaces between the numbers and the alphabet to fill in the four remaining spaces. Once all the characters have been entered, scroll the 40 characters back and forth endlessly though the two lines of the display.

2. Write a program to bubble the 26 capital letters of the alphabet through the numbers 0 to 9 on line two of the LCD. To do this, first put the numbers on line two. Then “A” takes the place of the “0” and all the numbers move over. Then the “A” takes the place of the “1” and the “0” moves to position 1. Then the “A” moves into place of the “2,” and so on till it gets past the 9. Then the “B” starts its way across the numbers and so on. Loop forever.

3. Write a program to write the numbers 0 to 9 upside down on line 1. Wait one second and then flip the numbers right side up. Loop.

4. Write a program to write “HELLO WORLD” to the display and then change it to lowercase one letter at a time with 50 milliseconds between letters. Wait one second and go back to uppercase one character at a time with negative letters (all dots on the display are reversed to show as dark background with white letters and on to lowercase). Loop.

ADVANCED EXERCISES

These exercises are designed to challenge your programming ability. Again, you will need access to the data sheet for the LC Display.

1. Editor: Write a program that displays 12 random numbers on line 1 of the LCD and displays a cursor that can be moved back and forth across the 20 spaces with poten- tiometer 0. The entire range of the potentiometer must be used to move across the 20 spaces. Allow the keypad to insert numbers 0 to 9 into the position that the cursor is on. Assign a delete switch and an insert space switch on the keyboard. A comprehen- sive number (plus decimal and space) editor is required.

2. Mirror: Write a program that puts a random set of letters and numbers on line 1 and then puts their mirror images on line 2. The mirror is between line 1 and line 2. To do this, you have to learn how to create the upside down numbers from the Hitachi data sheet for the display, and you have to learn how to read what is in the display from the display ROM.

3. Forty characters: The display ROM is capable of storing 40 characters on each line.

Design a program to allow you to scroll back and forth to see all 40 characters on both lines one line at a time. Use two potentiometers for scrolling, one for each line.

4. Four lines: Write a program to display four lines of random data on the LCD and to scroll up and down and side to side to see all four lines in their entirety. You have to store what is lost from the screen before it is lost so that you can re-create it when you need it.

5. Bar graphs: Create a three-bar graph display, with each bar 3 pixels high, that extends across both lines of the LCD. The lengths of the bar graphs are determined by the settings of the three potentiometers and changes as the potentiometers are manipulated.

Leave a comment

Your email address will not be published. Required fields are marked *