Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Project 1

Jean Mouawad
Elie Yaacoub

A report submitted Dr.Nicolas Haddad, in partial fulfillment to the requirement of the


Optimization Theory course for spring 2019

Faculty of Engineering
University of Balamand

April 2019
______________________________________________________________________________

Copyright 2019
All Right Reserved, Jean Mouawad, Elie Yaacoub
Introduction:

We were asked to write a program to implement the programmable timer with only 3 keyboard
buttons: “u” and” d” to set the time as up and down and “s" to start and initiate the countdown process.
• First it memorizes the set time in EEPROM.
• Then it turns on the device and displays a down counter running at a rate of 1 Hz.
• When the time elapses, the device is turned off and the program starts all over again. A flashing
display and intermittent beeps of a buzzer indicate timeout.

This system may be used in many applications such as timer applications in industrial
automation like machines. Also, it can be used in universities and classes as end of time alarm. It
can be used in games such as Basketball games to set the timers. It can be used in many other
applications.

For This Project I liked to make it as a Time Clock Violation in a Basketball Game. For this
purpose the timer is set to 24 seconds in each attack.

Fig 1. Proteus Drawing Schematics


Project Description:

The Project consists of a timer that is set manually using the computer keyboard and connecting
the pic18f45k22 microchip to the computer using RS-232 communication protocol. The
keyboard layout for the counter is provide in Fig 2. When the timer is set and when start is
pressed the timer value is saved to the EEPROM. The timer counts down until it reaches 0
seconds. A buzzer and LEDS are set on for 5 secs. The timer value can vary between 0 seconds
and 255 seconds.

Whenever the timer is interrupted by any other external cause or resetting the timer it is saved in
the EEPROM. So, after reset or ending the timer will be as last value set.

Special Case:

As mentioned in the introduction I assumed that this project Is a 24 seconds time clock violation
in a basketball game. The timer must be set manually for 24 seconds each time. Whenever a
team violates the rule and the 24 seconds ends the buzzer is set on for 5 seconds and the led are
on for 5 seconds also. Whenever the team scores a basket in less than 24 seconds the controller
must reset the timer and press start again.

Fig 2. Keyboard Layout


Fig 3. Timer LCD

Fig 4. Basketball timer with LEDS


Appendix A

Code:

typedef char far rom ROM;

#include <p18cxxx.h>

#include <LCD4PICDEM2.h>

#include <Hyperterm.h>

#include <delays.h>

#include <EEPROM.h>

#pragma config FOSC = INTIO67

#pragma config WDTEN = OFF

#pragma config MCLRE = EXTMCLR

#define eeSeconds 0x00 // variable address in EEP

#define Device PORTBbits.RB0 // device pin

#define Device PORTBbits.RB0 // device pin

#define Device1 PORTBbits.RB1 // device pin

#define Device2 PORTBbits.RB2 // device pin

#define Device3 PORTBbits.RB3 // device pin

void Setup(void);

void GetTime(void);

void TurnOn(void);
void Buzzer(unsigned char);

// Convert 8-bit N to ASCII characters and store in array a (starting with MSD)

void Bin2Asc(unsigned char N, char *a) // max of 425 cycles

a[2] = (N % 10) + '0'; // least significant ASCII digit

N = N / 10;

a[1] = (N % 10) + '0'; // middle significant ASCII digit

a[0] = (N / 10) + '0'; // most significant ASCII digit

enum {false, true};

int up = false, down = false;

int start = false;

char Digits[3];

unsigned char Seconds = 0, i, j; // i: loop counter

unsigned char receive;

void main(void) {

Setup();

GetTime();

void Setup(void)
{

OSCCON = 0b01010000;

InitUART ();

InitLCD(); // initialize LCD

TRISBbits.RB0 = ANSELBbits.ANSB0 = 0;

PORTBbits.RB0 = 0;

TRISBbits.RB1 = ANSELBbits.ANSB1 = 0;

PORTBbits.RB1 = 0;

TRISBbits.RB2 = ANSELBbits.ANSB2 = 0;

PORTBbits.RB2 = 0;

TRISBbits.RB3 = ANSELBbits.ANSB3 = 0;

PORTBbits.RB3 = 0;

TRISCbits.RC2 = ANSELCbits.ANSC2 = 0;

PR2 = 255;

T2CON = 0b00000110;

CCP1CON = 0x0C;

CCPR1L = 0;

T0CON = 0b01001111;
TMR0L = 256 - 256;

DispRomStr (Ln1Ch0, (ROM *)"Time = 000 s");

DispRomStr (Ln2Ch0, (ROM *)"'u'/'d'/'s'");

Seconds = ReadEE (eeSeconds); //Reads the value saved in the eeprom

Bin2Asc(Seconds, Digits); // convert to ASCII

DispVarStr(Digits, Ln1Ch7, 3); // send Digits to LCD

INTCONbits.TMR0IE = 1;

PIE1bits.RCIE = 1;

INTCONbits.PEIE = 1;

INTCONbits.GIE = 1;

void GetTime(void)

while (1) {

if (start) {

TurnOn(); // acknowledge START PB if (Seconds != 0)

start = false;

else if (down) // decrement

Bin2Asc(--Seconds, Digits); // convert to ASCII

DispVarStr(Digits, Ln1Ch7, 3); // send Digits to LCD


down = false;

else if (up) // increment

Bin2Asc(++Seconds, Digits); // convert to ASCII

DispVarStr(Digits, Ln1Ch7, 3); // send Digits to LCD

up = false;

Wrt2EE(Seconds, eeSeconds); // save in EEPROM

void TurnOn(void)

Device = 0; // turn device on

Device1= 0;

Device2 =0;

Device3 =0;

do {

Bin2Asc(Seconds, Digits); // convert to ASCII

DispVarStr(Digits, Ln1Ch7, 3); // send Digits to LCD

if (Seconds == 0)

break;

Seconds--;

Delay10KTCYx(100); // 1000 ms delay Seconds--;


} while (1);

start = false;

T0CONbits.TMR0ON = 1;

Device = 1;

Device1= 1;

Device2 =1;

Device3 =1;

Buzzer (10);

for (j = 0; j < 5; j ++) {

Delay10KTCYx (100);

T0CONbits.TMR0ON = 0;

Reset();

void Buzzer (unsigned char i){

CCPR1L = i;

#pragma code ISR = 0x0008

#pragma interrupt ISR


void ISR (void) {

if (PIR1bits.RCIF) {

receive = RCREG;

if (receive == 0x73) start = true;

else if (receive == 0x75) up = true;

else if (receive == 0x64) down = true;

else if (INTCONbits.TMR0IF) {

INTCONbits.TMR0IF = 0;

TMR0L = 256 - 256;

E = 0;

You might also like