Introduction

This is a continuation of Laboratory III in which you will use some addtional features of Handel-C to add a visual effect to your seven segment display of ASCII codes. The visual effect is to have each character code fade away after it is first displayed. The Handel-C features you are to use are channels, macro procs, and libraries.

Procedure

  1. Configure a new library project.

    Start DK, and re-open the workspace you created in Laboratory I. Create a new project in that workspace named delayProcs. Select Library as the project type.

    Delete all the configurations for this project except Generic. You do not have to set any configuration options for this project.

  2. Write a millisecond delay macro proc.

    You can control time intervals in Handel-C if you know the speed of the clock you are working with, how long an interval you want, and the fact that each assignment statement takes exactly one clock cycle.

    Write a macro proc named msecDelay that takes two parameters: a number of milliseconds and the speed of the clock. The macro is to take exactly the specified number of milliseconds to execute. There are several ways to accomplish this, but here is the “standard” approach: The number of clock pulses per millisecond is the number of clock pulses per second (the clock rate passed as the second paramter) divided by 1,000. Multiply pulses per milliseconds by the desired number of milliseconds (the first parameter), and you have the number of pulses to use up. This value should be determined at compile time, so use a macro expr to compute it. Set up a register with enough bits to hold this value, using the log2ceil() from the standard library to establish its width. Initialize the register with a count of the number of pulses you need, and use a while loop to decrement the register until it reaches zero. Think about how to get exactly the correct number of statements executed by the macro — not that anyone will actually know if you are off by a one or two clock pulses.

    When you build this project, you will get a library file named delayProcs.hcl in the Generic subdirectory of the project. Later on you can add other procs to this library, for example to delay for a number of microseconds instead of milliseconds.

    To guarantee, as much as possible, that the code that uses your macro proc calls it correctly, you need to supply a function prototype for it that both the definition and the references both use. Add a Handel-C header file named delayProcs.hch to the delayProcs project, and put the following code in it:

    // delayProcs.hch #ifndef __DELAY_PROCS__ #define __DELAY_PROCS__ extern macro proc msecDelay(msec, clockRate); #endif

    Use a #include directive to include this header file in msecDelay.hcc. It seems strange to declare the procedure external in the file in which it is defined, but that’s the standard way to manage prototypes. Because this header file is not in a standard place, you have to put the file name in quotes rather than angle brackets.

  3. Test your delay procedure

    Modify your keybd2video.hcc from the Laboratory III project so that it includes the header file for your library, and add the library to the list of library modules for both the Debug and EDIF build configurations.

    Add a third endless loop to this project that reads from a channel, sets the foreground color to its default value, uses msecDelay() to execute for one second, and then sets the foreground color to match the background color. Add a line to the endless loop that reads from the keyboard so that it writes to the channel every time a character is typed. The value written to the channel and read from it is irrelevant for this project; I suggest you declare the channel’s data type as unsigned 1, and simply write either a 0 or a 1 to it, whichever you prefer.

    The effect should be that typing a character causes its ASCII code to display for one second and then disappear.

  4. Add the fade effect.

    Modify the loop you just added to the project so that instead of simply changing the foreground color in a single step at the end of one second, it changes the foreground color gradually, in at least 64 steps, spanning one or two seconds. To get a smooth transition, you will have to adjust the red, green, and blue components individually. Your algorthm must work independently of the actual foreground and background colors in effect, forcing you deal with the possibility that you will have to work with fractional intensity values, using simple fixed point numbers as described in class.

Submit Laboratories I through IV

If you have not already done so, be sure all the code you have written so far is “clean.” Things to check for are: proper indentation to show the logical structure with no tab charcters in the code, all lines of code narrow enough to display in a normal panel without wrapping (80 columns), and comments to show the logical structure of the code. Set off each macro proc and function defintion with some sort of horizontal line; likewise for each endless loop:

// main() // -------------------------------------------------------- /* * Meaningful comments. */ void main(void) { ... }

When your laboratories are ready for me to look at, send me an email message with “CS-345 First Laboratories” or “CS-780 First Laboratories” as the subject. Be sure to put at least your name inside your message. If you worked with someone else, be sure to list everyone’s name and to tell me which account has the code. (If a graduate student and undergraduate student worked together, the subject line can be either of the ones listed, or a mix.)