Anda di halaman 1dari 10

#include<reg51.

h>
#include<stdio.h>
#include<intrins.h>
#define ACK 1
#define NO_ACK 0
bit ack;
unsigned char reead,write,write2,i,j;
unsigned int temp;
int g ;

//Function declarations
void cct_init(void);
void delay(int);
void lcdinit(void);
void writecmd(int);
void writedata(char);
void writenum (int);
void ex0_isr (void);
void count (int);

// I2C Declarations
void InitSerial(void);
void DelayMs(unsigned int);
void aknowledge();
void start();
void stop();
void send_byte(unsigned char);
unsigned char read_byte();
void save();
void Read();
//*******************
//Pin description
/*
P2 is data bus
P1.0 is RS
P1.1 is E
*/
//********************

// Defines Pins
sbit RS = P1^0;
sbit E = P1^1;
sbit scl = P1^3; // connect to SCL pin (Clock)
sbit sda = P1^4; // connect to SDA pin (Data)

// ***********************************************************
// Main program
//
void main(void)
{
InitSerial();
DelayMs(5);
cct_init(); //Make all ports zero
lcdinit(); //Initilize LCD

writedata('C'); //write
writedata('o'); //write
writedata('u'); //write
writedata('n'); //write
writedata('t'); //write
writedata('s'); //write
writedata(' '); //write
writedata(':'); //write

EA=1; //For Enabling the interrupt register


EX0=1; //External interrupt set
IT0=1; //For Edge Triggered System
Read();
count();
while(1)
{
}
}
void cct_init(void)
{
P0 = 0x00; //not used
P1 = 0x00; //not used
P2 = 0x00; //used as data port
P3 = 0x04; //used for generating E and RS
}
void delay(int a)
{
int k;
for(k=0;k<a;k++); //null statement
}

void writedata(char t)
{
RS = 1; // => RS = 1
P2 = t; //Data transfer
E = 1; // => E = 1
delay(150);
E = 0; // => E = 0
delay(150);
}
void count(int j)
{
j=g;
writecmd(0x88);
if(j<10)
writenum(j+0x30);
else
{
writenum((j/10)+0x30);
writenum((j%10)+0x30);
}
}
void writenum (int i)
{
RS = 1; // => RS = 1
P2 = i; //Data transfer
E = 1; // => E = 1
delay(150);
E = 0; // => E = 0
delay(150);
}

void writecmd(int z)
{
RS = 0; // => RS = 0
P2 = z; //Data transfer
E = 1; // => E = 1
delay(150);
E = 0; // => E = 0
delay(150);
}

void lcdinit(void)
{
///////////// Reset process from datasheet /////////
delay(15000);
writecmd(0x30);
delay(4500);
writecmd(0x30);
delay(300);
writecmd(0x30);
delay(650);
/////////////////////////////////////////////////////
writecmd(0x38); //function set
writecmd(0x0c); //display on,cursor off,blink off
writecmd(0x01); //clear display
writecmd(0x06); //entry mode, set increment
}
void ex0_isr (void) interrupt 0
{
int f;
f=g;
f++;
if(f==100) // If Count reaches 100
{
f = 0;
writecmd(0x89);
writedata(' ');
}
g=f;
save();
count();
}

//---------------------------------------

// Initialize serial port

//---------------------------------------

void InitSerial(void)

SCON = 0x52; // setup serial port control

TMOD = 0x20; // hardware (9600 BAUD @11.0592MHZ)

TH1 = 0xFD; // TH1

TR1 = 1; // Timer 1 on

void aknowledge() //acknowledge condition


{
scl=1;
_nop_();
_nop_();
scl=0;
}
void start() //start condition
{
sda=1;
scl=1;
_nop_(); //No operation
_nop_();
sda=0;
scl=0;
}
void stop() //stop condition
{
sda=0;
scl=1;
_nop_();
_nop_();
sda=1;
scl=0;
}
void send_byte(unsigned char value) //send byte serially
{
unsigned int i;
unsigned char send;
send=value;
for(i=0;i<8;i++)
{
sda=send/128; //extracting MSB
send=send<<1; //shiftng left
scl=1;
_nop_();
_nop_();
scl=0;
}
ack=sda; //reading acknowledge
sda=0;
}
unsigned char read_byte() //reading from EEPROM serially
{
unsigned int i;
sda=1;
reead=0;
for(i=0;i<8;i++)
{
reead=reead<<1;
scl=1;
_nop_();
_nop_();
if(sda==1)
reead++;
scl=0;
}
sda=0;
return reead; //Returns 8 bit data here
}
void save() //save in EEPROM
{
start();
send_byte(0xA0); //device address
aknowledge();
send_byte(0x00); //word address
aknowledge();
send_byte(g); //send data
aknowledge();
stop();

}
void Read()
{
start();
send_byte(0xA0);
aknowledge();
send_byte(0x00);
aknowledge();
start();
send_byte(0xA1); //device address
aknowledge();
g=read_byte();
aknowledge();
stop();

}
//---------------------------------------

// Delay mS function

//---------------------------------------

void DelayMs(unsigned int count)

{ // mSec Delay 11.0592 Mhz

unsigned int i; // Keil v7.5a

while(count)

i = 115;
while(i>0) i--;

count--;