Handy Board Lecture 26 Engineering H192 - Computer Programming

advertisement
Engineering H192 - Computer Programming
Handy Board
Lecture 26
Winter Quarter
Gateway Engineering Education Coalition
Lect 26
P. 1
Engineering H192 - Computer Programming
Hands-on Lab #6 - The Traffic Light
• Hands-On Lab #6 will use the MIT Handy Board
controller.
• First part of exercise is to write a C (not C++)
program which performs functions to simulate
the operation of a traffic light.
• Second part of exercise will be to convert
program to run on Interactive C, the compiler for
the Handy Board.
• Hand-outs will be provided for Handy Board
basics, description of required C program, and
laboratory exercise.
Winter Quarter
Gateway Engineering Education Coalition
Lect 26
P. 2
Engineering H192 - Computer Programming
Programming in Real Time - Time of Day
• Programs which run in real time make frequent
use of system timing functions and system clock.
• On the Unix system there are some useful timing
routines in <time.h> and <unistd.h>
• We will need to use at least:
– time
– sleep
Winter Quarter
defined in <time.h>
defined in <unistd.h>
Gateway Engineering Education Coalition
Lect 26
P. 3
Engineering H192 - Computer Programming
Programming in Real Time - Time of Day
• From the time routine in <time.h> we can look up
the current time of day:
• NAME
time - get time
• SYNOPSIS
#include <sys/types.h> /* Not usually required */
#include <time.h>
time_t time (time_t *tloc) ;
• DESCRIPTION
time returns the value of time in seconds since 00:00:00
UTC, January 1,1970. If tloc is non-zero, the return value is
also stored in the location to which tloc points.
Winter Quarter
Gateway Engineering Education Coalition
Lect 26
P. 4
Engineering H192 - Computer Programming
Programming in Real Time - Time of Day
• To get the elapsed time from some starting point:
#include <time.h>
int time_start, time_now, delta_t ;
time_start = time (NULL) ;
time_now = time (NULL) ;
delta_t = time_now - time_start ;
• More rigorously, one would probably use the difftime
function:
delta_t = difftime ( time_now, time_start) ;
Winter Quarter
Gateway Engineering Education Coalition
Lect 26
P. 5
Engineering H192 - Computer Programming
A sleep function
void sleep (float n)
{
long start, now ;
now=time (NULL) ;
start=now ;
while (now - start < n)
now = time (NULL) ;
}
Winter Quarter
Gateway Engineering Education Coalition
Lect 26
P. 6
Engineering H192 - Computer Programming
Programming in Real Time - Waiting
• From the sleep routine in <unistd.h> we can wait
for time to elapse (i.e., "take a nap"):
• NAME
sleep - suspend execution for interval
• SYNOPSIS
#include <unistd.h>
unsigned sleep (unsigned seconds);
• DESCRIPTION
The current thread is suspended from execution for the
number of seconds specified by the argument. The
suspension time may be longer than requested by an
arbitrary amount due to the scheduling of other activity in
the system. See "man sleep" for more details on
SLEEP(3C).
Winter Quarter
Gateway Engineering Education Coalition
Lect 26
P. 7
Engineering H192 - Computer Programming
Programming in Real Time - Waiting
• To wait a specified amount of time between two
statements in a program:
#include <unistd.h>
int color1, color2, direction1, direction2;
change_lites (color1, direction1) ;
sleep (6) ;
change_lites (color2, direction2) ;
Winter Quarter
Gateway Engineering Education Coalition
Lect 26
P. 8
Engineering H192 - Computer Programming
The Handy Board
•
The Handy Board features:
•
•
•
•
•
•
•
•
52–pin Motorola 6811 microprocessor with system clock at 2 MHz.
32K of battery-backed CMOS static RAM.
Two L293D chips capable of driving four DC motors.
16 x 2 character LCD screen.
Two user-programmable buttons, one knob, and piezo beeper.
Powered header inputs for 7 analog sensors and 9 digital sensors.
Internal 9.6v nicad battery with built-in recharging circuit.
Hardware 38 kHz oscillator and drive transistor for IR output and
on-board 38 kHz IR receiver.
Board size of 4.25 x 3.15 inches, designed for a commercial, high
grade plastic enclosure which holds battery pack beneath the
board.
•
Winter Quarter
Gateway Engineering Education Coalition
Lect 26
P. 9
Engineering H192 - Computer Programming
Interactive C
• Loosely based on ANSI C
• Two versions available:
– IC version 3.2 - installed in lab, available for
$35 per educational user from Newton Labs.
This is the most up-to-date version with some
local (OSU) enhancements.
– IC Version 2.8x - available for free as a
download from the Handy Board web site.
Does not support the use of structs, #define,
multi-dimensional arrays, and some other
improvements found only in Version 3.2.
Winter Quarter
Gateway Engineering Education Coalition
Lect 26
P. 10
Engineering H192 - Computer Programming
Interactive C
• Things that are different in IC (compared to ANSI C):
– No function prototypes
– No pointer arithmetic
– Limited formats possible in printf
– Array bounds checked at run time
– Arrays can not be converted to pointers
– No #include statements
– Smaller set of allowable data types (only int, long, float,
char, and struct (v3.2 only)
– Character strings are supported, but simple char
variables are not
Winter Quarter
Gateway Engineering Education Coalition
Lect 26
P. 11
Engineering H192 - Computer Programming
Interactive C
• More things that are different in IC (compared to
ANSI C):
– No switch/case construct
– Can only do +, -, and * on long int
– All source files must be compiled together
– No extern statement
– #define has global scope
– User functions come first in source file
– No do/while construct
Winter Quarter
Gateway Engineering Education Coalition
Lect 26
P. 12
Engineering H192 - Computer Programming
Traffic Light States
1
Winter Quarter
2
Gateway Engineering Education Coalition
3
Lect 26
P. 13
Engineering H192 - Computer Programming
Traffic Light States
4
Winter Quarter
1
Gateway Engineering Education Coalition
Lect 26
P. 14
Engineering H192 - Computer Programming
Skeleton Program for Traffic Light
void change( /*parameters*/ )
{
/* motor function calls (or display light status) */
}
void main( )
{
/* declarations */
/* set initial light conditions */
/* while loop with timing and function calls */
}
Winter Quarter
Gateway Engineering Education Coalition
Lect 26
P. 15
Download