0% found this document useful (0 votes)
65 views11 pages

Code

This document contains code for interfacing with an I2C LCD display. It includes definitions for I2C pins and the display size. It also contains functions for initializing I2C communication, sending start/stop conditions, reading and writing bytes to the I2C bus. The code implements routines for writing individual bytes and blocks of bytes to the display's memory. It also contains character bitmap data and strings for displaying on the LCD.

Uploaded by

vls
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
65 views11 pages

Code

This document contains code for interfacing with an I2C LCD display. It includes definitions for I2C pins and the display size. It also contains functions for initializing I2C communication, sending start/stop conditions, reading and writing bytes to the I2C bus. The code implements routines for writing individual bytes and blocks of bytes to the display's memory. It also contains character bitmap data and strings for displaying on the LCD.

Uploaded by

vls
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 11

#include <REGX51.

H>
#include <intrins.h>
#define SDA
#define SCK
#define DATA
#define SH_CP
#define ST_CP
#define coloum
of coloums
#define speed
speed hi & vice versa
#define extend
size
#define Device_Address
#define status

P3_7
P3_6
P3_2
P3_3
P3_4
145
2

//value low then

208

//Maximum buffer

0xA0

code unsigned char font[95][5] = {


{0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x7a,0x00,0x00},
{0x00,0x70,0x00,0x70,0x00},
{0x14,0x3e,0x14,0x3e,0x14},
{0x10,0x2a,0x3e,0x2a,0x04},
{0x64,0x08,0x10,0x26,0x00},
{0x14,0x2a,0x14,0x02,0x00},
{0x00,0x00,0x70,0x00,0x00},
{0x00,0x3c,0x42,0x00,0x00},
{0x00,0x42,0x3c,0x00,0x00},
{0x00,0x2a,0x1c,0x2a,0x00},
{0x08,0x08,0x3e,0x08,0x08},
{0x00,0x01,0x06,0x04,0x00},
{0x08,0x08,0x08,0x08,0x00},
{0x00,0x06,0x06,0x00,0x00},
{0x04,0x08,0x10,0x20,0x00},
{0x00,0x3c,0x42,0x3c,0x00},
{0x00,0x22,0x7e,0x02,0x00},
{0x22,0x46,0x4a,0x32,0x00},
{0x44,0x52,0x52,0x6c,0x00},
{0x18,0x28,0x7e,0x08,0x00},
{0x74,0x52,0x52,0x4c,0x00},
{0x3c,0x52,0x52,0x0c,0x00},
{0x40,0x46,0x58,0x60,0x00},
{0x2c,0x52,0x52,0x2c,0x00},
{0x30,0x4a,0x4a,0x3c,0x00},
{0x00,0x36,0x36,0x00,0x00},
{0x01,0x36,0x34,0x00,0x00},
{0x00,0x08,0x14,0x22,0x00},
{0x14,0x14,0x14,0x14,0x00},
{0x00,0x22,0x14,0x08,0x00},
{0x00,0x20,0x4a,0x30,0x00},
{0x3c,0x42,0x5a,0x38,0x00},
{0x3e,0x48,0x48,0x3e,0x00},
{0x7e,0x52,0x52,0x2c,0x00},
{0x3c,0x42,0x42,0x24,0x00},
{0x7e,0x42,0x42,0x3c,0x00},
{0x7e,0x52,0x52,0x42,0x00},
{0x7e,0x50,0x50,0x40,0x00},
{0x3c,0x42,0x4a,0x2e,0x00},
{0x7e,0x10,0x10,0x7e,0x00},
{0x00,0x42,0x7e,0x42,0x00},

//Address of Slave Device


P1

//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//

// Data pin for i2c


// Clock pin for i2c
//DS pin of 74HC595
//SH_CP pin of 74HC595
//ST_CP pin of 74HC595
//Maximum number

!
"
#
$
%
&
'
(
)
*
+
,
.
/
0
1
2
3
4
5
6
7
8
9
:
;
<
=
>
?
@
A
B
C
D
E
F
G
H
I

{0x04,0x02,0x02,0x7c,0x00},
{0x7e,0x18,0x24,0x42,0x00},
{0x7e,0x02,0x02,0x02,0x00},
{0x7e,0x30,0x30,0x7e,0x00},
{0x7e,0x30,0x0c,0x7e,0x00},
{0x3c,0x42,0x42,0x3c,0x00},
{0x7e,0x48,0x48,0x30,0x00},
{0x3c,0x46,0x42,0x3d,0x00},
{0x7e,0x48,0x4c,0x32,0x00},
{0x24,0x52,0x4a,0x24,0x00},
{0x00,0x40,0x7e,0x40,0x00},
{0x7c,0x02,0x02,0x7c,0x00},
{0x78,0x06,0x06,0x78,0x00},
{0x7e,0x0c,0x0c,0x7e,0x00},
{0x66,0x18,0x18,0x66,0x00},
{0x00,0x70,0x0e,0x70,0x00},
{0x46,0x4a,0x52,0x62,0x00},
{0x00,0x7e,0x42,0x42,0x00},
{0x20,0x10,0x08,0x04,0x00},
{0x00,0x42,0x42,0x7e,0x00},
{0x00,0x20,0x40,0x20,0x00},
{0x02,0x02,0x02,0x02,0x00},
{0x00,0x40,0x20,0x00,0x00},
{0x0c,0x12,0x14,0x1e,0x00},
{0x7e,0x12,0x12,0x0c,0x00},
{0x0c,0x12,0x12,0x00,0x00},
{0x0c,0x12,0x12,0x7e,0x00},
{0x0c,0x16,0x1a,0x08,0x00},
{0x08,0x3e,0x48,0x20,0x00},
{0x0a,0x15,0x15,0x19,0x00},
{0x7e,0x10,0x10,0x0e,0x00},
{0x00,0x12,0x5e,0x02,0x00},
{0x00,0x02,0x01,0x5e,0x00},
{0x7e,0x08,0x14,0x02,0x00},
{0x00,0x42,0x7e,0x02,0x00},
{0x1e,0x08,0x18,0x0e,0x00},
{0x1e,0x10,0x10,0x0e,0x00},
{0x0c,0x12,0x12,0x0c,0x00},
{0x1f,0x12,0x12,0x0c,0x00},
{0x0c,0x12,0x12,0x1f,0x00},
{0x1e,0x10,0x10,0x08,0x00},
{0x0a,0x1a,0x16,0x14,0x00},
{0x10,0x7c,0x12,0x02,0x00},
{0x1c,0x02,0x02,0x1e,0x00},
{0x00,0x1c,0x02,0x1c,0x00},
{0x1e,0x06,0x06,0x1e,0x00},
{0x12,0x0c,0x0c,0x12,0x00},
{0x18,0x05,0x02,0x1c,0x00},
{0x12,0x16,0x1a,0x12,0x00},
{0x00,0x10,0x3c,0x42,0x00},
{0x00,0x00,0x7e,0x00,0x00},
{0x00,0x42,0x3c,0x10,0x00},
{0x20,0x40,0x20,0x40,0x00},

//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//

J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
[
"\"
]
^
_
`
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
{
|
}
~

};
code unsigned char reply[]="Press RESET button if display don't start automatica
lly\r\n\0";
code unsigned char prcs[]= "Processing...\r\n\0";
unsigned char pwcount;

idata unsigned char buffer[extend];


unsigned int eeprom,l;
void DSend(bit ds);
void InitI2C(void);
void I2C_Start(void);
void I2C_ReStart(void);
void I2C_Stop(void);
void I2C_Send_ACK(void);
void I2C_Send_NACK(void);
bit I2C_write_byte(unsigned char);
unsigned char I2C_read_byte(void);
void write_byte(unsigned int Address, unsigned char Data);
unsigned char read_byte(unsigned int Address);
void Write_Page(unsigned int Address,unsigned char* pData,unsigned char NoOfByte
s);
void delay(int itime)
{
int i2,j2;
for(i2=0;i2<itime;i2++)
for(j2=0;j2<10;j2++);
}
void InitI2C(void)
{
SDA = 1;
delay(1);
SCK = 1;
delay(1);
}
void I2C_Start(void)
{
SCK = 1;
delay(1);
SDA = 1;
delay(1);
SDA = 0;
delay(1);
}
void I2C_ReStart(void)
{
SCK = 0;
at SCK is low
delay(1);
SDA = 1;
delay(1);
SCK = 1;
delay(1);
SDA = 0;
delay(1);
}
void I2C_Stop(void)
{
SCK = 0;

// Make SDA and SCK pins input initially

// Make SCK pin high


// Make SDA pin High
// Make SDA Low

// Make SCK pin low


// when it is confirm th
// Make SDA pin High
// Make SCK pin high
// Make SDA Low

// Make SCK pin low

// when it is confirm th
at SCK is low
delay(1);
SDA = 0;
delay(1);
SCK = 1;
delay(1);
SDA = 1;
delay(1);
delay(100);
}
void I2C_Send_NACK(void)
{
SCK = 0;
at SCK is low
delay(1);
SDA = 1;
delay(1);
SCK = 1;
delay(1);
}

// Make SDA pin low


// Make SCK pin high
// Make SDA high

// Make SCK pin low


// when it is confirm th
// Make SDA high
// Make SCK pin high

bit I2C_write_byte(unsigned char Byte)


{
unsigned char i;
// Variable to be used in for loop
for(i=0;i<8;i++)
{
SCK = 0;

// Repeat for every bit


// Make SCK pin low
// when it is confirm th

at SCK is low
delay(1);
SDA=((Byte<<i)&0x80);
SCK = 1;

// So that slave can

}
SCK = 0;
delay(1);
SDA = 1;
delay(1);
SCK = 1;
delay(1);
return SDA;
}
unsigned char I2C_read_byte(void)
{
unsigned char i, d, RxData = 0;
for(i=0;i<8;i++)
{
SCK = 0;
SCK pin low
delay(1);
SDA = 1;
drive SDA
delay(1);

// Make
// Don't

SCK = 1;

// Make

SCK pin high


delay(1);
d = SDA;

// Capture R

eceived Bit
RxData = RxData|(d<<(7-i));
delay(1);

// Copy it in RxData

}
return RxData;
ed byte
}

// Return receiv

void write_byte(unsigned int Address, unsigned char Data)


{
I2C_Start();
// start i2c communication
while(I2C_write_byte((Device_Address|((Address>>7) & 0x0E))))
ntill device is free
{I2C_Start();}

//wait u

I2C_write_byte((unsigned char)Address);
//write address lower byte
I2C_write_byte(Data);
//write data byte
I2C_Stop();
//stop i2c communication
}

unsigned char read_byte(unsigned int Address)


{
unsigned char byte=0;
//variable to store received byte
I2C_Start();
while(I2C_write_byte((Device_Address|((Address>>7) & 0x000E)))) //wait u
ntill device is free
{I2C_Start();}
I2C_write_byte((unsigned char)Address);
I2C_ReStart();
I2C_write_byte((Device_Address | 1)|((Address>>7) & 0x000E));
byte=I2C_read_byte();
I2C_Send_NACK();
//indicate single byte read is complete
SDA = 0;
delay(1);
SDA = 1;
return byte;
}

void I2C_Send_ACK(void)
{
SCK = 0;
// Make SCK pin low
delay(1);
// when it is confirm that SCK is low
SDA = 0;
// Make SDA Low
delay(1);
SCK = 1;
// Make SCK pin high
delay(1);
}
void read_bytes(unsigned int Address, unsigned char pData[], unsigned int NoOfBy
tes)
{
unsigned int i;
I2C_Start();
// Start i2c communication
while(I2C_write_byte((Device_Address|((Address>>7) & 0x000E)))) //wait u
ntill device is free
{I2C_Start();}
I2C_write_byte((unsigned char)Address);
// Write Address lower byte
I2C_ReStart();
// Restart i2c
I2C_write_byte((Device_Address | 1)|((Address>>7) & 0x000E));
pData[0] = I2C_read_byte();
// Read First byte from EEPROM
for(i=1;i<NoOfBytes;i++)
// Read NoOfBytes
{
I2C_Send_ACK();
// Give Ack to slave to start receiving next byte
pData[i] = I2C_read_byte();
// Read next byte from EEPROM
}
I2C_Send_NACK();
SDA=0;
delay(1);
SDA=1;
delay(1);
}
void Write_data(unsigned int Address,unsigned char pData,unsigned char NoOfBytes
)
{
unsigned char i;
I2C_Start();
// Start i2c communication

while(I2C_write_byte((Device_Address|((Address>>7) & 0x000E)))) //wait u


ntill device is free
{I2C_Start();}
I2C_write_byte((unsigned char)Address);
// Write Address lower byte
for(i=0;i<NoOfBytes;i++)
// Write NoOfBytes
{
I2C_write_byte(font[pData][i]);
Address++;
if(Address%16==0)
{
I2C_Stop();
delay(5);
I2C_Start();
// Start i2c communication
while(I2C_write_byte((Device_Address|((Address>>7) & 0x0
00E)))) //wait untill device is free
{I2C_Start();}
I2C_write_byte((unsigned char)Address);
// Write Address lower byte
}
}
I2C_write_byte(0x00);
Address++;
// Write data byte
I2C_Stop();
delay(500);
// Stop i2c communication
}

void serial() interrupt 4


{
if(status!=2 && (RI==1))
{
status=3;
buffer[pwcount]=SBUF;
pwcount++;
if(pwcount==extend)
{
buffer[pwcount-1]='\r';
IE=0x00;
pwcount=0;
status=2;
}
else if(buffer[pwcount-1]==';')
{
if(buffer[pwcount-2]=='#')
{
IE=0x00;
buffer[pwcount-2]='\r';
pwcount=0;
status=2;

}
}
RI=0;
}
else RI=0;
}
void putc(unsigned char oho)
{
SBUF=oho;
while(TI==0);
TI=0;
}
void puts(unsigned char sdata[])
{
unsigned char i=0;
while(sdata[i]!='\0')
{
putc(sdata[i]);
i++;
}
}
void save_location()
{
l=eeprom;
write_byte(0x07FE,l>>8);
delay(50);
write_byte(0x07FF,(unsigned char)l);
delay(50);
}
void get_location()
{
l=read_byte(0x07FE);
l=(l<<8);
l=l+read_byte(0x07FF);
}
void eeprom_data(char eedata)
{
if(eedata<='~' && eedata>=' ')
{
Write_data(eeprom,eedata-' ',5);
eeprom=eeprom+6;
}
}
void eeprom_save(char edata[])
{
unsigned int i3=0;
while(edata[i3]!='\r')
{
eeprom_data(edata[i3]);
putc(edata[i3]);
i3++;
}
}

void DSend(bit ds)


{
DATA=(ds);
SH_CP=1;
SH_CP=0;

//DS set data to send


//Now data is writen
//Back to normal stage

}
void latch()
{
ST_CP=1;
ST_CP=0;
}
void blank(unsigned int Address)
{
unsigned char j;
I2C_Start();
// Start i2c communication
while(I2C_write_byte((Device_Address|((Address>>7) & 0x000E)))) //wait u
ntill device is free
{I2C_Start();}
I2C_write_byte((unsigned char)Address);
// Write Address lower byte
for(j=0;j<coloum;j++)
// Write NoOfBytes
{
I2C_write_byte(0x00);
Address++;
if(Address%16==0)
{
I2C_Stop();
delay(5);
I2C_Start();
// Start i2c communication
while(I2C_write_byte((Device_Address|((Address>>7) & 0x0
00E)))) //wait untill device is free
{I2C_Start();}
I2C_write_byte((unsigned char)Address);
// Write Address lower byte
}
}
// Write data byte
I2C_Stop();
delay(5);
eeprom=eeprom+coloum;
}
void display(char line,char ud)
{
unsigned char j;
for(j=0;j<coloum;j++)
{
DSend(buffer[j] & ud);
}
P2=0xFF;

latch();
P2=~line;
}
void serial_init()
//Initialization of serial commu
nication and interrups
{
SCON = 0x50;
/* mode 1, 8-bit uart, enable receiver *
/
TMOD = 0x20;
/* timer 1, mode 2, 8-bit reload */
TH1 = 0xFD;
/* reload value for 9600 baud */
TR1 = 1;
/* start the timer */
IE = 0x90;
/* 1001 0000 enable serial inter
rupt*/
}
void show_time()
{
unsigned char j;
for(j=0;j<speed;j++)
{
display(0x40,0x02);
display(0x20,0x04);
display(0x10,0x08);
display(0x08,0x10);
display(0x04,0x20);
display(0x02,0x40);
display(0x01,0x80);
}
buffer[coloum]=read_byte(eeprom+coloum);
eeprom++;
if(eeprom>=l)
{eeprom=0x0000;}
for(j=0;j<coloum;j++)
{buffer[j]=buffer[j+1];}
}
void main()
{
serial_init();
InitI2C();
puts("Author : P.J.Suthar\r\n\0");
status=0;
pwcount=0;
eeprom=0x0000;
delay(1000);
while(1)
{
while(status==0)
{
get_location();
read_bytes(eeprom,buffer,coloum);
eeprom=0x0000;
status=1;
}
while(status==10)
{
eeprom=0x0000;
blank(eeprom);

eeprom_save(reply);
blank(eeprom);
read_bytes(0x0000,buffer,coloum);
IE=0x90;
status=1;
eeprom=0;
}
while(status==2)
{
eeprom=0x0000;
blank(eeprom);
puts(prcs);
eeprom_save(buffer);
puts("\r\n\0");
save_location();
blank(eeprom);
read_bytes(0x0000,buffer,coloum);
eeprom=0;
puts("Done\r\n\0");
puts(reply);
IE=0x90;
status=1;
}
while(status==1)
{
show_time();
}
}
}

You might also like