Friday, 11 December 2015

STOPWATCH WITH LAPTIMES VIDEO

Produced by Hanif Razali and Nasrul Fadli.
Directed by Sir Zulkhairi.
                  Link Video Youtube

Coding In Simplicity Studio Software

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <SI_C8051F850_Register_Enums.h>
#include "InitDevice.h"
#include "stdio.h"

//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------

sbit SW0 = P2^1;
const unsigned int str[10];
int milisec,sec,minute;
char flag=0;
//-----------------------------------------------------------------------------
//------------lcd header----------------------------------------

sbit EN = P0^0;
sbit RW = P0^1;
sbit RS = P0^2;

#define DATA P0

void EN2HIGH(void);
void EN2LOW(void);
//void init_usart(void);
void Delay_MS(unsigned int x);
unsigned char check_busy(void);      //function declaration'
char read_data(void);
void send_control(char x);
void send_data(char x);
void Initialize_LCD(void);
void puts_lcd(char *pointer);

void puts_lcd(char *pointer){
     while(*pointer)     // print till null encountered
send_data(*pointer++);
}

void Delay_MS(unsigned int x){
unsigned int i, j;
for(i=0; i<x; i++){
for(j=0; j<20; j++);  //adjust delay faster
 }
}

unsigned char check_busy(void){
  RS = 0; // Logic ‘0’
  RW = 1; // Logic ‘1’
DATA|=0xf0;   // Set data line as input
  Delay_MS(1);
EN2HIGH();
if(DATA&0x80){
EN2LOW();
EN2HIGH();
EN2LOW();
DATA&=0x0f;   // Set data line as output
return 1;
}
else{
EN2LOW();
EN2HIGH();
EN2LOW();;
DATA&=0x0f;   // Set data line as output
return 0;
}
}

char read_data(void){
  char x;
  while(check_busy());

DATA|=0xf0;   // Set data line as input
  RS = 1; // Logic ‘1’
  RW = 1; // Logic ‘1’
  Delay_MS(1);
EN2HIGH();
x = DATA;
EN2LOW();
EN2HIGH();
x |= DATA>>4;
EN2LOW();
DATA&=0x0f;   // Set data line as output
return x;
}

void send_control(char x){
  while(check_busy());
DATA&=0x0f;   // Set data line as output
  RS = 0; // Logic ‘0’
  RW = 0; // Logic ‘0’

DATA&=0x0f;   // clear previous data
DATA |= x&0xf0;
  Delay_MS(1);
EN2HIGH();
EN2LOW();
DATA&=0x0f;   // clear previous data
DATA |= x<<4;
Delay_MS(1);
EN2HIGH();
EN2LOW();
}

void send_data(char x){
while(check_busy());
DATA&=0x0f;   // Set data line as output
  RS = 1; // Logic ‘0’
  RW = 0; // Logic ‘0’

DATA&=0x0f;   // clear previous data
DATA |= x&0xf0;
  Delay_MS(1);
EN2HIGH();
EN2LOW();
DATA&=0x0f;   // clear previous data
DATA |= x<<4;
Delay_MS(1);
EN2HIGH();
EN2LOW();
}

void Initialize_LCD(void){
DATA = 0x00; // Set data line as output
Delay_MS(20);     // 20ms @ 16MHz
send_control(0x33);
send_control(0x33);
send_control(0x32);
send_control(0x28); // 4 bit mode
send_control(0x10); // clear the screen
send_control(0x06);
send_control(0x0C);
send_control(0x01); // display on cursor on
send_control(0x06); // increment cursor
send_control(0x80); // row 1 column 1
}

void EN2HIGH(void){
EN=1;
  Delay_MS(1);
}

void EN2LOW(void){
EN=0;
Delay_MS(1);
}

//--------------------------delay---------------------------------

void delay_10us(unsigned int x){
unsigned int i;
for(i=0; i<x; i++);
 }



void delay_0dot1ms(unsigned int x){
unsigned int i, j;
for(i=0; i<x; i++){
 for(j=0; j<11; j++);
   }
 }

void delay_1ms(unsigned int x){
unsigned int i, j;
for(i=0; i<x; i++){
 for(j=0; j<1111; j++);
   }
 }
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void){
   //Sysclk - 24.5Mhz/8 Timer2 overflow set to 1
   enter_DefaultMode_from_RESET();

   Initialize_LCD();

   send_control(0x80);
   puts_lcd("NASRUL HANIF");

   send_control(0xC0);
puts_lcd("STOP WATCH TIMER");

   while(SW0);
   delay_1ms(10);

send_control(0x01);
    delay_1ms(100);


while(1){

milisec++;
delay_1ms(50);

if (milisec>=10){
milisec=0;
sec++;
if (sec>=60){
sec=0;
minute++;
}
}

send_control(0x80);
sprintf(str,"    %d:%d:%d   ",minute,sec,milisec);
puts_lcd(str);

if (!SW0 && flag==0){
delay_1ms(100);
send_control(0xC0);
sprintf(str,"LAP1:    %d:%d:%d   ",minute,sec,milisec);
puts_lcd(str);
flag=1;

}

if (!SW0&&flag==1){
delay_1ms(100);
send_control(0x80);
sprintf(str,"LAP2:    %d:%d:%d   ",minute,sec,milisec);
puts_lcd(str);
milisec=sec=minute=0;
IE_EA = 0; //stop timer


while(SW0);
delay_1ms(100);   //debouncing
milisec=sec=minute=0;
flag=0;
send_control(0x01);
puts_lcd("");
IE_EA =1;  //start timer
}

}
}
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------

No comments:

Post a Comment