Docsity

CodeWarrior IDE and C Programming for Embedded Micro-computers: A Beginner's Guide, Study notes of Linguistics

Toggle sidebar
bg1
UNIVERSITY OF CALIFORNIA, SANTA CRUZ
BOARD OF STUDIES IN COMPUTER ENGINEERING
CMPE118/L: INTRODUCTION TO MECHATRONICS
LABORATORY ASSIGNMENT NUMBER 0 FOR CMPE 118
Due by 3:30pm on Friday April 8, 2005
Pre-Lab due by 4:00 on Tuesday April 5, 2005
Purpose: This lab is intended to acquaint you with 'C' programming for embedded systems.
- Learning the ropes for the CodeWarrior programming environment
- How to write, debug and run a simple C program on an embedded micro-computer
- How to get simple character input from the user in C
Minimum Parts
Required: A 'CockRoach-II'.
References: Course Notes: The Events and Services Framework. State Machines
Pre-Lab:
Complete the following exercises AFTER you have read through the lab assignment and BEFORE
coming into the CMPE 118 Lab to complete the lab.

 0.1) Brainstorm with your design team to identify the essential 'Events' necessary for your desired
b
ehavior, determine how you will test for them in software and how you want to respond. Your
Pre-Lab write-up should explain the results of your brainstorming session in the form of a state
diagram and pseudo-code for the event checking routines.
For the purposes of the Pre-Lab, your “design team” should consist of you and 1 other person.
In the Pre-Lab: Include your concepts from part 0.1
Part 1 – Exploring the MC9S12C32 and the CW12 Compiler
Reading: The Metroworks CodeWarrior Quickstart manual, Online Help
Assignment: Complete the Following Exercises:

 1.1) Start the CodeWarrior (CW12) IDE (Integrated Development Environment) by clicking on the
Windows ‘Start Menu’, selecting the ‘Programs’Æ’Metroworks CodeWarrior’Æ’CW12 V3.1’
option, then selecting the ‘CodeWarrior IDE’ shortcut. CW12 is a program that has provisions
for creating and editing programs (*.C files), creating and editing project files (*.mcp files),
compiling programs, and downloading finished programs to targets (in our case, the “target” is
the MC9S12C32 board).

 1.2) Using the Windows Explorer, copy the provided file “SESDEMO.C” into your home directory.
Open this file with CW12. You may wish to edit and save changes to this file, so you might
want to rename it at this point, also saving this new version into your home directory.

 1.3) Next, you will want to compile and test your program. In order to compile the source file shown in
the editor window, you will first need to create a Project file for this program. From the menu bar,
select “File Æ New”. Give your project file a name and use your home directory for the location.
Pick the “HC(S)12 New Project Wizard” and select “OK”. The next screen asks you to pick your
micro so select “MC9S12C32”. The next screen asks you to choose your languages we will be
using “C”. Choose “No” for using the “Processor Expert”, “PC-Init”, and “Floating Point Format”.
Choose “Small” for the memory model. In the connections screen choose the “Motorola Serial
Monitor Hardware Debugging”.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download CodeWarrior IDE and C Programming for Embedded Micro-computers: A Beginner's Guide and more Study notes Linguistics in PDF only on Docsity!

U NIVERSITY OF CALIFORNIA, SANTA CRUZ

BOARD OF STUDIES IN COMPUTER ENGINEERING

CMPE118/L: INTRODUCTION TO MECHATRONICS

L ABORATORY A SSIGNMENT NUMBER 0 FOR CMPE 118

Due by 3:30pm on Friday April 8, 2005

Pre-Lab due by 4:00 on Tuesday April 5, 2005

Purpose: This lab is intended to acquaint you with 'C' programming for embedded systems.

- Learning the ropes for the CodeWarrior programming environment

- How to write, debug and run a simple C program on an embedded micro-computer

- How to get simple character input from the user in C

Minimum Parts

Required:

A 'CockRoach-II'.

References: Course Notes: The Events and Services Framework. State Machines

Pre-Lab:

Complete the following exercises AFTER you have read through the lab assignment and BEFORE

coming into the CMPE 118 Lab to complete the lab.

0.1) Brainstorm with your design team to identify the essential 'Events' necessary for your desired

behavior, determine how you will test for them in software and how you want to respond. Your

Pre-Lab write-up should explain the results of your brainstorming session in the form of a state

diagram and pseudo-code for the event checking routines.

For the purposes of the Pre-Lab, your “design team” should consist of you and 1 other person.

In the Pre-Lab: Include your concepts from part 0.

Part 1 – Exploring the MC9S12C32 and the CW12 Compiler

Reading: The Metroworks CodeWarrior Quickstart manual, Online Help

Assignment: Complete the Following Exercises:

1.1) Start the CodeWarrior (CW12) IDE (Integrated Development Environment) by clicking on the

Windows ‘Start Menu’, selecting the ‘Programs’Æ’Metroworks CodeWarrior’Æ’CW12 V3.1’

option, then selecting the ‘CodeWarrior IDE’ shortcut. CW12 is a program that has provisions

for creating and editing programs (.C files), creating and editing project files (.mcp files),

compiling programs, and downloading finished programs to targets (in our case, the “target” is

the MC9S12C32 board).

1.2) Using the Windows Explorer, copy the provided file “SESDEMO.C” into your home directory.

Open this file with CW12. You may wish to edit and save changes to this file, so you might

want to rename it at this point, also saving this new version into your home directory.

1.3) Next, you will want to compile and test your program. In order to compile the source file shown in

the editor window, you will first need to create a Project file for this program. From the menu bar,

select “File Æ New”. Give your project file a name and use your home directory for the location.

Pick the “HC(S)12 New Project Wizard” and select “OK”. The next screen asks you to pick your

micro so select “MC9S12C32”. The next screen asks you to choose your languages we will be

using “C”. Choose “No” for using the “Processor Expert”, “PC-Init”, and “Floating Point Format”.

Choose “Small” for the memory model. In the connections screen choose the “Motorola Serial

Monitor Hardware Debugging”.

EE/CS118 Lab Number 0 Due by 5:00pm on January 17, 2003 2

1.4) You will need to define all of the *.C source code files needed to build your project. So far, all you

will need is SESTEST.C (or whatever you are calling it now), so click on the “ProjectÆAdd”

option and select your SESTEST.C file. To build your project, all you need to do now is click the

“ProjectÆMake” option. If there are any errors or problems, CW12 will display them at this time.

If there are errors with your code, you will need to correct them all before your code will function

(of course), so you should go back to the Editor window with your source code, correct the errors

that the Status window revealed, and then try re-building your project. Repeat as required to

remove all errors and warnings.

1.4) When you are ready for the next step, plug the RS232 (serial) connector into the CockRoach

connector (on top of the CockRoach). Make sure you have the battery power connected and

switched to the “ON” position (the toggle switch next to the CockRoach wheel).

1.5) You are now ready to download your new code. To do this, you will need to open uBug12.

This is found in the “Start Æ Programs Æ Technological Arts Æ uBug 12” menu. You should

now see a terminal window with a command prompt at the bottom. Set SW2 on the

MC9S12C32 to the BOOT position and reset the chip. In the uBug12 command prompt enter

CON 1 assuming you are using COM port 1. uBug12 should now display a short message. To

erase any existing program use fbulk. Now, everything is ready for the new code to be

downloaded. You need to tell uBug12 which file to download, so enter fload ;b and select your

file, which should be named something like SESTEST.S19 (the *.S19 file is compiled code in

the proper format for downloading). Once your code is finished downloading, the uBug12 will

inform you, and you should see a prompt. You are now ready to execute your code.

1.6) At this point, you are ready to try out your program. Be sure that the CockRoach is up on the

blocks so that when the motors start running it will not drive off of the bench. When the

download was complete, you should have gotten the message LOADED OKAY in the terminal

window. To run your program, type in “go” at the prompt and press . This tells the

MC9S12C32 to execute your program, which has been loaded into memory. If all has gone

well, the CockRoach will begin executing your code.

1.7) Congratulations! You've just written and downloaded your first embedded program.

1.8) Next, spend some time exploring the library functions provided for you. Write some simple

programs to turn the motors on & off, get a character from the keyboard, print a value to the

screen, read the light sensor and test the bumper switches.

In the report: Include the listings from the programs that you wrote to explore the libraries.

Part 2 – A Mechanical CockRoach

Reading: Course Notes: Event Driven Programming , State Machines.

Background: The behavior of many types of simple creatures can be described in terms of their response to outside

stimulus. This will be your opportunity to give a mechanical creature an interesting behavior.

Assignment:

This is your mission: you are to write a C program that will run on the MC9S12C32 and cause the

CockRoach-II to behave like a real cockroach.

The fundamental behaviors are that they:

run around when the lights are on

hide in the dark

won't get stuck in corners or against chair legs, etc.

2.1) Begin by writing some simple programs to explore the inputs and outputs to/from the 68HC12.

Use the library functions provided to control the motors, read the bumper states, and read the

light level.

2.2) Complete the detailed design and implementation of the program that you worked out in Part 0.

EE/CS118 Lab Number 0 Due by 5:00pm on January 17, 2003 4

Notes on writing your code

As you may have noticed from studying the example code provided, there are a number of things that you must do in order to

have access to the libraries provided for programming the 'CockRoach-II'. In particular, notice the header files that need to be

included:

#include <cmpe118.h>

#include <roachlib.h>

#include <ses.h>

#include <timer.h>

cmpe118.h brings in a set of definitions and prototypes that are used throughout the CMPE118 labs, it should always be

included in your programs.

roachlib.h is the header file for a simple set of input and output routines provided for use on the CockRoach-II. This gives

you access to functions that control the motors, read the state of the bumpers, and read the state of the light sensor.

ses.h is the header file for the Software Events and Services library. This library provides access to a pre-defined and

convenient implementation of the Events and Services Framework.

timer.h is the header file for the Timer library. This library provides access to several timers and several useful functions

associated with initializing, setting, and checking them.

To read the state of the bumpers, assign the return value from ReadBumpers() to an unsigned char type variable. It is a good idea

to read the bumpers once, assigning the return value to a variable, and then do all of your tests on the variable. This way, you

don’t get confused by a bumper transition that occurs while you are doing your testing.

The individual bumper switches can be isolated using the following masks:

#define BR_BUMPER 0x

#define FR_BUMPER 0x

#define FL_BUMPER 0x

#define BL_BUMPER 0x

Of course, since “front” and “back” are relative terms for the CockRoach-II, you may wish to change these definitions. To check

the state of a single bumper switch (in this example you have stored the value returned by ReadBumpers() in the variable

“bumper” and you are going to check to see if the Back Left bumper is currently being hit), use the expression:

((bumper & BL_BUMPER) != BL_BUMPER)

This is an example of “bit masking” using the C programming language’s bitwise AND operator, “&”, to isolate the state of a

single bumper, instead of considering all 4 bumpers at the same time. You can carry this approach one step further and define

parameterized macros to test for various combinations of bumpers. For example:

#define ANY_FRONT_BUMPER (FR_BUMPER | FL_BUMPER)

#define FRONT_BUMPER_HIT(var) ((var & ANY_FRONT_BUMPER) != ANY_FRONT_BUMPER)

The first macro creates a bit-mask that incorporates both of the front bumper bits. The second macro uses the first macro to test

for either of the front bumpers being hit. To use this macro in your code, assuming that you had assigned the return value from

ReadBumpers to a variable called bumpers, you would write:

FRONT_BUMPER_HIT(bumper)

This expression wold return a true (non-zero) result if either of the front bumpers had been hit at the time ReadBumpers was

called.

EE/CS118 Lab Number 0 Due by 5:00pm on January 17, 2003 5

Lab #

Time Summary

Be sure to turn this in with your lab report

This information is being gathered solely to produce statistical information to help improve the lab assignments.

Pre-Lab Preparing Outside of the lab ____________ In the lab working this part__________

Part 1 Preparing Outside of the lab ____________ In the lab working this part__________

Part 2 Preparing Outside of the lab ____________ In the lab working this part__________

Part 3 Preparing Outside of the lab ____________ In the lab working this part__________

Report Preparing the Lab Report ______________

DOCUMENTATION for ROACHLIB module

Kevin Wooley, Matt Ohline Rev : June 5, 2000

Purpose of Module

This module provides functions to control the motors, read the light sensor, and read the bumper sensors on the ME118 Cockroach.

INTERFACE

Module Functions

RoachInit

PROTOTYPE: void RoachInit (void) CONTENTS: This is the initialization routine for the Roach functions. PARAMETERS: none RETURNS: nothing

RightMtrSpeed

PROTOTYPE: uchar RighMtrSpeed(char newSpeed) CONTENTS: This func tion is used to set the speed and direction of the right motor. A negative value is reverse and 0 is stop. PARAMETERS: NewSpeed –- a speed value between –10 and 10 RETURNS: OK_OPERATION == The registration was successful. ERR_BADSPEED == An invalid spe ed was given.

LeftMtrSpeed

PROTOTYPE: uchar LeftMtrSpeed(char newSpeed) CONTENTS: This function is used to set the speed and direction of the left motor. A negative value is reverse and 0 is stop. PARAMETERS: NewSpeed –- a speed value between –10 and 10 RETURNS: OK_OPERATION == The registration was successful. ERR_BADSPEED == An invalid speed was given.

LightLevel

PROTOTYPE: uchar LightLevel(void) CONTENTS: This will read the light sensor, returns a number proportional to the amount of light being received. PARAMETERS: none RETURNS: An 8-bit value indicating the amount of light present.

ReadBumpers PROTOTYPE: uchar ReadBumpers (void) CONTENTS: This will read the bumper sensors. The lower 4 bits correspond to each of the corners PARAMETERS: none RETURNS: An 8-bit number where bits 4-7 are 0 and the other 4 bits correspond to bumpers. The bits are normally high. If a bumper is hit, the corresponding bit is low.

DOCUMENTATION for SES module

Adam Braun, Ed Carryer, Kevin Wooley Rev : January 5, 2000

Purpose of Module

This module provides a system to automate a software events and services protocol. Up to 128 event and service function pairs can be registered with the system using 2 different scheduling algorithms to handle the events.

INTERFACE

Defined Constants

Standard Parameters –- These definitions are used to share data between an Event checking routine and the corresponding service routine.

EVENT_PARAM –- standard event checker parameter, use this in your function prototype SERVICE_PARAM –- standard service routine parameter, use this in your function prototype SET_SHARED_VAR_TO(z) – set the variable shared by the event checker and service routine to the value z. GET_SHARED_BYTE() – return the low byte value of the shared variable GET_SHARED_WORD() – return the 2 byte (integer size) value of the shared variable.

Scheduling Algorithms -- These constants are used to specify the scheduling for the handling of events. The priority of events is based on the order in which they are registered.

SES_ROUND_ROBIN -- In this algorithm a full pass through all the pairs is made (in order of priority) and every event that is detected is serviced. When the pass is completed, it is restarted with the highest priority pair if any of the events were detected. SES_PRIORITY -- In this algorithm the events are serviced in order of priority until an event is detected. After the event is serviced, the servicing restarts with the highest priority pair. This process continues until a full pass is made with no events being serviced.

Service Timing -- These constants are used to specify the timing period for the SES_TimeToService function. They are used to approximately control the time between event services in a foreground loop. This is to allow the program to perform other tasks in the background loop if necessary.

SES_NO_UPDATE -- No service timer. SES_TimeToService is always true. SES_4MS_UPDATE -- Update every 4 ms. SES_8MS_UPDATE -- Update every 8 ms. SES_16MS_UPDATE -- Update every 16 ms. SES_32MS_UPDATE -- Update every 32 ms.

Data Types

uchar -- typedef to be unsigned char schar -- typedef to be signed char uint -- typedef to be unsigned int sint -- typedef to be signed int

Module Functions

SES_Init

PROTOTYPE : uchar SES_ Init(uchar aScheduleType, uchar aTimePeriod) CONTENTS : This is the initialization routine for the SES functions. PARAMETERS : aScheduleType -- The scheduling algorithm type. aTimePeriod -- The timing for servicing. RETURNS : OK_OPERATION == The setup was done successfully. ERR_BADSCHEDULE == The scheduling value was invalid. ERR_BADTIMEPERIOD == The time period was invalid.

If any of the ERR_xxx returns occur, then no action is taken in the module.

SES_Register

PROTOTYPE :

uchar SES_Register(char (aEvent)(void), void ( aService)(void*)) CONTENTS : This will register an event routine and an associated service routine. The priority of the pairs is descending in the order they were registered. PARAMETERS : aEvent -- The event detection routine to register. aService -- The associated service routine to register. RETURNS : OK_OPERATION == The registration was successful. ERR_NOTINSTALLED == The module was not installed with SES_INIT ERR_TOOMANYEVENTS == There are already the maximum number of event/service pairs registered.

If any of the ERR_xxx returns occur, then no action is taken in the module.

SES_TimeToService

PROTOTYPE : uchar SES_ TimeToService(void) CONTENTS : This will return a flag indicating whether it is time to service the events or not. It the time schedule is set to SES_NO_UPDATE. PARAMETERS : none RETURNS : Flag for whether the time has expired or not. (1 == time to service).

SES_HandleEvents

PROTOTYPE : void SES_HandleEvents(void) CONTENTS : This will run the handle events loop to process the events and service functions using the scheduling algorithm set in the SES_Init function. It will return when a full pass through the event/service pairs finds no events to be serviced. PARAMETERS : none RETURNS : nothing

DOCUMENTATION for TIMER module

Ed Carryer Rev : January 10, 2001 to remove reference to limited function version for cockroaches December 29, 2001 to remove example that used SES.

PURPOSE OF MODULE

This module provides for 8 independent timer channels. These timers can be started, stopped and tested for completion. All timers share a common time base, and that timebase is selectable from the available RTI rates defined below.

INTERFACE

Hardware/Output Specifications

This is a software only module, with no hardware interactions.

Defined Constants

These constants are used with TMR_Init() to set the timebase for all of the timers. TMR_RATE_ 4MS 4.1mS TMR_RATE_ 8MS 8.19 mS TMR_RATE_ 16MS 16.38mS TMR_RATE_ 32MS 32.77mS

MODULE FUNCTIONS

TMR_Init Prototype: void TMR_Init(unsigned char Rate) Parameters: unsigned char Rate set to one of the TMR_RATE_XX defines to set the RTI rate Returns: None.

Description Initializes the timer module by attaching the RTI interrupt to the response routine. Must be called before using any of the other TMR routines.

TMR_InitTimer Prototype: signed char TMR_ InitTimer(unsigned char Num, unsigned int NewTime) Parameters: unsigned char Num, the number of the timer to start unsigned int NewTime, the number of tick to be counted Returns: -1 if the requested timer does not exist, 0 otherwise.

Description Sets the NewTime into the chosen timer and clears any previous event flag and sets the timer active to begin counting.

TMR_SetTimer Prototype: signed char TMR_SetTimer(unsigned char Num, unsigned int NewTime) Parameters: unsigned char Num, the number of the timer to set. Returns: -1 if requested timer does not exist, 0 otherwise

Description Sets the time for a timer, but does not make it active.

TMR_StartTimer Prototype: signed char TMR_StartTimer(unsigned char Num) Parameters: unsigned char Num the number of the timer to start Returns: signed char -1 for error 0 for success

Description Sets the active flag in TMR_ActiveFlags to start a timer that was set or to resart a stopped timer.

TMR_StopTimer Prototype: signed char TMR_StopTimer(unsigned char Num) Parameters: unsigned char Num the number of the timer to stop. Returns -1 for error (timer doesn't exist) 0 for success.

Description Clears the bit in TMR_ActiveFlags associated with this timer. This will cause it to stop counting.

TMR_IsTimerActive Prototype: signed char TMR_IsTimerActive(unsigned char Num) Parameters: unsigned char Num the number of the timer to check Returns: -1 if requested timer is not valid 0 if timer is not active 1 if it is active

Description This functions is used to determine if a timer is currently counting.

TMR_IsTimerExpired Prototype: signed char TMR_IsTimerExpired (unsigned char Num) Parameters: unsigned char Num, the number of the timer to test. Returns -1 if reqiested timer does not exist 0 if not expired 1 if expired

Description This function tests the flags to determine if the requested timer has expired.

TMR_ClearTimerExpired Prototype: signed char TMR_ClearTimerExpired(unsigned char Num) Parameters unsigned char Num, the timer whose event flag should be cleared. Returns -1 if r equested timer does not exist 0 otherwise

Description Clears the appropriate bit in TMR_EventFlags to show that the event has been serviced.

CONSTRAINTS/NOTES

  1. TMR_Init must be called before the module becomes active.
  2. The time-base of all timers is the same.

THEORY OF OPERATION

This module operates using RTI interrupt as the time-base. At each occurrence of the interrupt, the 'time remaining' count for each of the active timers is decremented. If the count for a timer goes to 0, the Expired flag for that timer is set, and the timer is made inactive.

Usage Example

#include <timer.h>

/* TIME_OUT_DELAY = 10 S w/ 8mS interval */ #define TIME_OUT_DELAY 1220

void main(void) { unsigned int i;

puts(" Starting\n"); TMR_ Init(TMR_RATE_8MS); TMR_ InitTimer(0, TIME_OUT_DELAY); TMR_ InitTimer(1, TIME_OUT_DELAY); TMR_ InitTimer(2, TIME_OUT_DELAY); TMR_ InitTimer(3, TIME_OUT_DELAY); TMR_ InitTimer(4, TIME_OUT_DELAY); TMR_ InitTimer(5, TIME_OUT_DELAY); TMR_ InitTimer(6, TIME_OUT_DELAY); TMR_ InitTimer(7, TIME_OUT_DELAY); while( TMR_IsTimerExpired(0) != 1) ; puts("Timed Out\a\n"); TMR_ InitTimer(7, TIME_OUT_DELAY); for (i=0;i<10,000 ;i++ ) {/* kill some time */ } TMR_ StopTimer(7); if ( TMR_IsTimerActive(7) != 0) puts("Timer Stop Failed\n"); else puts("Timer Stop Succeded\n"); TMR_ StartTimer (7); while( TMR_IsTimerExpired(7) != 1) ;

puts("Timed Out Again\a\n");

}

uchar

getKey(EVENT_PARAM)

{

if (kbhit())

{

theKey

= getchar();

SET_SHARED_BYTE_TO(theKey);

/* leave

value

where

the

service

routine

return*/

1;

/* can

find

it */

else}

return

0;

Function*****/***********************************************************************}

putKey

Parameters

SERVICE_PARAM

standard

service

routine

parameter.

Returns

None.

Description

send

(^) the

value

of the

shared

variable

key

to the

terminal.

Notes

None.

Author

J. Edward

(^) Carryer,

(^) 04/11/

(^) 09:

void****/************************************************************************

(^) putKey(SERVICE_PARAM)

{

putchar('.');putchar(GET_SHARED_BYTE());

Function*****/***********************************************************************}

TestFor

Parameters

EVENT_PARAM.

Returns

(^1) if (^) the

test

(^) is

true,

(^) false

(0)

otherwise.

Description

tests

if the

current

time

has

changed

since

the

last

time

through

and

that

the

new

value

(^) is

evenly

divisible

by (^) 1050.

Notes

None.

Author

J. (^) Edward

Carryer,

04/11/

09:

uchar****/************************************************************************

TestFor1050(EVENT_PARAM)

{

srand(TCNT);

if (^) ((rand()

(^) %

==

return

1;

else

return

0;

*****/***********************************************************************} Function

RespFor

Parameters

SERVICE_PARAM

standard

service

function

parameter.

provides

access

to any

shared

variables.

Returns

None.

Description

simply

(^) prints

a '*'

to the

screen

to indicate

that

the

event

has

happened.

Notes

None.

Author

J. (^) Edward

Carryer,

04/11/

09:

void****/************************************************************************

RespFor1050(SERVICE_PARAM)

{

putchar('*');

*****/***********************************************************************} Function

TestForLightOff

Parameters

EVENT_PARAM.

Returns

1 if the

test

is true,

false

(0)

otherwise.

Description

tests

if the

current

(^) reading

from

the

light

(^) sensor

changed

since

the

last

time

through,

going

OFF

Notes

Implements

a hystersis

band

to prevent

(^) bouncing

(^) triggers.

Author

J. Edward

(^) Carryer,

(^) 04/11/

(^) 09:

uchar****/************************************************************************

TestForLightOff(EVENT_PARAM)

{

static

uchar

LastLight

= 0;

static

uchar

Threshold

= LO_LIGHT_THRESHOLD;

char

EventOccured

= ((LightLevel()

< (^) Threshold)

&& (^) (LastLight

=

/*Threshold));

provide

for

hysterisis

around

the

switching

point

to eliminate

false

(^) triggers.

*/

if (EventOccured) Threshold

= HI_LIGHT_THRESHOLD;

else

if (LightLevel()

= (^) Threshold)

Threshold

= LO_LIGHT_THRESHOLD;

LastLight

(^) = LightLevel();

return

(EventOccured);

Function*****/***********************************************************************}

RespForLightOff

Parameters

SERVICE_PARAM

standard

service

(^) funetion

(^) parameter.

provides

access

to any

shared

variables.

Returns

None.

Description

simply

prints

(^) OFF

to the

screen

(^) to

indicate

that

the

event

has

happened.

Notes

None.

Author

J. Edward

(^) Carryer,

(^) 04/11/

(^) 09:

void****/************************************************************************

(^) RespForLightOff(SERVICE_PARAM)

{ printf("OFF

");

Function*****/***********************************************************************}

TestForLightON

Parameters

EVENT_PARAM.

Returns

1 if the

test

is true,

false

(0)

otherwise.

Description

tests

if the

current

(^) reading

from

the

light

(^) sensor

changed

since

the

last

time

through,

going

ON.

Notes

Implements

a hystersis

band

to prevent

(^) bouncing

(^) triggers.

Author

J. (^) Edward

Carryer,

04/11/

09:

uchar****/************************************************************************

TestForLightOn(EVENT_PARAM)

{

static

uchar

LastLight

= 0;

static

uchar

Threshold

= HI_LIGHT_THRESHOLD;

char

EventOccured

= ((LightLevel()

(^) > Threshold)

&& (LastLight

(^) <=

/*Threshold));

provide

(^) for

hysterisis

around

the

switching

point

to eliminate

false

triggers.

*/

if (^) (EventOccured) Threshold

= LO_LIGHT_THRESHOLD;

else

if (LightLevel()

<= Threshold)

Threshold

= HI_LIGHT_THRESHOLD;

LastLight

= (^) LightLevel();

return

(EventOccured);

*****/***********************************************************************} Function

RespForLightOn

Parameters

SERVICE_PARAM

standard

service

funetion

parameter.

provides

access

to any

shared

variables.

Returns

None.

Description

simply

(^) prints

a ON to the

screen

to indicate

that

the

event

has

happened.

Notes

None.

Author

J. (^) Edward

Carryer,

04/11/

09: