Lab6: Building your Dream Alarm Clock Using the PIC18

Here is the grading form we will use.

Goal

In this lab you will use C language to implement an alarm clock. You will learn how to write "C" programs in the PIC18 microcontroller. Also you will learn interrupts,  how to use digital to analog conversion, and how to play music in the PIC18.

This is a team project. Each team should have at most two members. You may decide to work individually but the same grading criteria will be used for all the projects.

Step 1 .Intial Setup

Remove the Full Color LED and store it safely. You may decide to use it later for this project.

Initial Setup

Move the button cables 2 positions to the right, that is they should be now in A28, A29, and A30

Button cables

Now look for the temperature sensor. It is the black semi-cylinder with three legs and it is labeled LM60. Do not confuse it with the other transistors.Now insert the temperature sensor in E19, E20, and E21 in the position shown. Make sure that the flat face faces you. Connect A19 to +5V and A21 to 0V. Also add a wire that goes to the center of the sensor to A26 that is the analog input. Make sure the temperature sensor  is connected correctly to the power to avoid burning the sensor.

temperature sensor

Now add the 20MHz crystal to A37 and A38 to give a more precise clock to the microcontroller. In previous projects the CPU used the internal clock that is not as accurate as the crystal.

crystal

Now connect the speaker to terminal A41 and ground. The speaker will play the tones using the PWM ouput from RC2.

speaker

Now you will connect a resistor to the segment that corresponds to the decimal point. Connect a 470ohm resistor (Yellow-Violet-Brown) between B62 and B52.  This resistor is similaro to the ones used for the other segments. Alxso add a cable from A52 to I35.

decimal point segment




Now conect a wire for the cahode of the ":" segment that appears in the middle of the seven segment display. Connect a wire between A59 and I28.

colon cathode

Step 2. Building your First C Program

Now launch MPLAB IDE and connect the Pickit2 to your breadboard.

1. Select Project->Project Wizard and the Project Wizard will appear. Select Next.

2. In "Step One: Select a Device" make sure that PIC18F4550 is selected and click next.

3. In "Step Two: Select a language toolsuite" select MPLAB C18 C Compiler and click Next.

4. In "Step Three: Create a New Project..." click in Browse and select the place where your lab6 files will be stored. In File Name type "hello" and click Next.

5. In "Step Four: Add existing..." click Next.

6. In "Summary" Click Finish.

7. Select Project->Build Optons->Project. In the Directories Tab select Show Directories for: "Include Search Path" . Then click in "New". Click the "..." button and browse for C:\MCC18\h Then click "OK".

8.  Now select Show Directories for: "Library Search Path" . Then click in "New". Click the "..." button and brows for C:\MCC18\lib. Then click "OK".

9. Now Select File->New and copy the following file:

#include <p18cxxx.h>

#pragma config WDT=OFF // disable watchdog timer
#pragma config MCLRE = ON// MCLEAR Pin on
#pragma config DEBUG = ON// Enable Debug Mode
#pragma config LVP = OFF// Low-Voltage programming disabled (necessary for debugging)
#pragma config FOSC = INTOSC_EC // High Speed

void main()
{
    // Make all bits of PORTD digital output
    TRISD = 0x0;
    PORTD = 0xFF;
   
    // Make all bits of PORTB digital output
    TRISB = 0x0;
    PORTB = 0x0;

    while (1) {
        // Delay
        unsigned int i = 0xFFFF;
        while (i>0) i--;

        // Turn on and off all segments of the seven segment display
        PORTD = ~PORTD;
    }
}


10. Select File->Save As and type "hello.c".

11. In the Project Window (the one titled hello.mcw ) right click in "Source Files" and select "Add Files". Then select hello.c and click Open.

12. Now build the project  by selecting Project->Build All

13. To program the PIC18, select Programmer->PicKit2

14. Now run the program. Select Programmer->Program and then Programmer->Release From Reset.

You should see all the segments of the display flashing.

Step 3. Running the Base Program

1. Download the lab6-src.zip file into your working directory. and unzip it.

2. Exit MPLAB and reopen it again. Then select Project->Open and then select lab6-src directory you unzipped and then select the "clock" project.

3. Select Project->Build All. Then select Programmer->Program and Programmer->Release from Reset.

At this point you should hear a piece of "Happy Birthday to You" and also the display counting in seconds. You can increase the counter in the first digit by pressing the first button.

You will use this program as the base for your alarm clock.

All the special register variable names are listed in the file C:\MCC18\h\p18f4550.h

Step 4. The Alarm Clock

You will implement your alarm clock with the following basic functionality:

Basic Functionality (70% of total grade)

This is the basic functionality that your clock is expected to have. It will count for 70% of the grade.

Advanced Functionality (30% of total grade)

This is the functionality that you will add to your clock. It will count for 30% of the grade. Some ideas are:
We will also grade how easy and intuitive it is using the clock.

Extra: Connecting the Servo

The servo is a motor an gear box combination that allows angular movements to a specific number of degrees. The angle is selected by changing the width of pulses sent to the servo  A pulse of 1500us (us = 1 micro second) will move the servo to 0 degrees. Pulses of about 1000us will move the servo about -90 degrees and pulses of about 2000us will ove the servo to +90 degrees. The pulses are sent continuously about every 20 milliseconds.

If you decide to use the servo this is the way you can connect it. Cut three wires of about 2 inches each. Then connect the brown side of the servo to ground (0V) and the red wire connect it to +5V. The orange wire should be connected to RE0 or A32 in the protoboard.

The program in lab6-src includes a procedure drive_servo(angle) that shows how to drive the servo. The servo will move from -90 to 90 degrees after playing the "Happy Birthday To You" song. The code produces the pulses in a very simple way that are not very precise. however, they are precise enough to make the servo move and for example ring a bell. If you want more precise movements, you can generate the pulses inside a timer interrupt routine.


servo1

servo2

Turning in your project.

You will show your project to your lab instructor during your lab time in April 8th, and 9th, 2010

Also, that day you will turnin the code of your project. Login to lore and then turn it in with the command: