#include <LPC21xx.H>
#include"lcdsub.c"
unsigned char i2cread(unsigned char x);
void getdate();
void i2cint();
void i2cstart();
void i2cstop();
void i2cwrite(char);
unsigned char c2(int);
unsigned char c1(int);//
unsigned char i2cread(unsigned char x)
{
unsigned char data;
i2cstart();
delay(10);
i2cwrite(0xd0);
delay(10);
i2cwrite(x);
delay(10);
i2cstart();
delay(10);
i2cwrite(0xd1);
delay(10);
delay(10);
I2CONSET |=0X04;
while(!(I2STAT & 0x58));
delay(10);
data=I2DAT;
//lcddata(data);
//i2cstop();
//datq=data;
lcddata(data+0x30);
return data;
}
void i2cint()
{
PINSEL0 |=0X50; //SELECTING sda & scL MODES
I2CONSET=0X40;
I2CONCLR=0X08; //INITIALIZING I2C MASTXMODE
//I2CONCLR = 0xff;
I2SCLH=0X4B;
I2SCLL=0X4B;
I2CONCLR=0X08;
}
void i2cstart()
{
I2CONSET |=0X20;
I2CONSET |=0X08;// STA=1
while(!(I2STAT & 0X08)); //CHECKING START COPMLETED OR NOT
I2CONCLR=0X28;
//lcdstr(" in sta4rt "); //RESETTING
}
void i2cstop()
{
I2CONSET=0X14;// & ST0=1
I2CONCLR=0X08;
while(!(I2STAT & 0XA0)); //CHECKING Stop COPMLETED OR NOT
//lcdstr("in stop");
//I2CONCLR=0X18; //RESETTING
}
void i2cwrite(char data1)
{
//i2cstart();
I2DAT=data1;
while(!(I2STAT & 0x18)) ;
I2CONCLR=0X18;
// i2cstop();
// lcdstr("write");
}
/*unsigned char i2cread()
{
char rx;
//I2DAT=0xff;
while(!(I2STAT & 0x50));
rx=I2DAT;
//lcddata(rx);
return rx;
} */
void gettime()
{
i2cstart();
lcdcmd(0xc0);
lcddata(c1(i2cread(0x00)));
lcddata(c2(i2cread(0x00)));
lcdstr(":");
lcddata(c1(i2cread(0x01)));
lcddata(c1(i2cread(0x01)));
lcdstr(":");
lcddata(c1(i2cread(0x02)));
lcddata(c2(i2cread(0x02)));
}
unsigned char c1(int val)
{
return ( ((val&0xF0)>>4)+0x30);
}
unsigned char c2(int val2)
{
return ((val2&0x0F)+0x30);
}
int main()
{
//unsigned char data,datq;
//IO0DIR |= (1<<3) | (1<<4);
IO0DIR |= 0xFF<<16;
IO0DIR |= (rs);
IO0DIR |= (rw);
IO0DIR |= (en);
lcdint();
i2cint();
lcdstr("initializing");
delay(10);
lcdcmd(0x01);
while(1)
{
i2cstart();
gettime();
//i2cstop();
/* i2cstart();
i2cwrite(0x41);
datrx=i2cread();
lcddata(datrx);
i2cstop();
delay(105); */
}
}
#include"lcdsub.c"
unsigned char i2cread(unsigned char x);
void getdate();
void i2cint();
void i2cstart();
void i2cstop();
void i2cwrite(char);
unsigned char c2(int);
unsigned char c1(int);//
unsigned char i2cread(unsigned char x)
{
unsigned char data;
i2cstart();
delay(10);
i2cwrite(0xd0);
delay(10);
i2cwrite(x);
delay(10);
i2cstart();
delay(10);
i2cwrite(0xd1);
delay(10);
delay(10);
I2CONSET |=0X04;
while(!(I2STAT & 0x58));
delay(10);
data=I2DAT;
//lcddata(data);
//i2cstop();
//datq=data;
lcddata(data+0x30);
return data;
}
void i2cint()
{
PINSEL0 |=0X50; //SELECTING sda & scL MODES
I2CONSET=0X40;
I2CONCLR=0X08; //INITIALIZING I2C MASTXMODE
//I2CONCLR = 0xff;
I2SCLH=0X4B;
I2SCLL=0X4B;
I2CONCLR=0X08;
}
void i2cstart()
{
I2CONSET |=0X20;
I2CONSET |=0X08;// STA=1
while(!(I2STAT & 0X08)); //CHECKING START COPMLETED OR NOT
I2CONCLR=0X28;
//lcdstr(" in sta4rt "); //RESETTING
}
void i2cstop()
{
I2CONSET=0X14;// & ST0=1
I2CONCLR=0X08;
while(!(I2STAT & 0XA0)); //CHECKING Stop COPMLETED OR NOT
//lcdstr("in stop");
//I2CONCLR=0X18; //RESETTING
}
void i2cwrite(char data1)
{
//i2cstart();
I2DAT=data1;
while(!(I2STAT & 0x18)) ;
I2CONCLR=0X18;
// i2cstop();
// lcdstr("write");
}
/*unsigned char i2cread()
{
char rx;
//I2DAT=0xff;
while(!(I2STAT & 0x50));
rx=I2DAT;
//lcddata(rx);
return rx;
} */
void gettime()
{
i2cstart();
lcdcmd(0xc0);
lcddata(c1(i2cread(0x00)));
lcddata(c2(i2cread(0x00)));
lcdstr(":");
lcddata(c1(i2cread(0x01)));
lcddata(c1(i2cread(0x01)));
lcdstr(":");
lcddata(c1(i2cread(0x02)));
lcddata(c2(i2cread(0x02)));
}
unsigned char c1(int val)
{
return ( ((val&0xF0)>>4)+0x30);
}
unsigned char c2(int val2)
{
return ((val2&0x0F)+0x30);
}
int main()
{
//unsigned char data,datq;
//IO0DIR |= (1<<3) | (1<<4);
IO0DIR |= 0xFF<<16;
IO0DIR |= (rs);
IO0DIR |= (rw);
IO0DIR |= (en);
lcdint();
i2cint();
lcdstr("initializing");
delay(10);
lcdcmd(0x01);
while(1)
{
i2cstart();
gettime();
//i2cstop();
/* i2cstart();
i2cwrite(0x41);
datrx=i2cread();
lcddata(datrx);
i2cstop();
delay(105); */
}
}
//#include <LPC214X.H>
//#define data 0xFF<<16 //assinging values for po.16-p0.23
#define rs (1<<13)
#define rw (1<<14)
#define en (1<<15)
void delay(int);
void lcdint();
void lcdcmd(int);
void lcddata(char);
void lcdstr(unsigned char*);
void lcdstr(unsigned char *chr)
{
while(*chr)
{
lcddata(*chr++);
}
}
void lcddata(char xdata)
{
IO0PIN = (xdata<<16);
//IO0SET|=(xdata<<16);
IO0SET =(1<<13);
IO0CLR =(1<<14);
IO0SET = (1<<15);
delay(5);
IO0CLR = (1<<15);
}
void lcdcmd(int cmd)
{
IO0CLR =(1<<13);
IO0CLR =(1<<14);
//IO0SET|=(cmd<<16);
IO0PIN = (cmd<<16);
IO0SET = (1<<15);
delay(5);
IO0CLR = (1<<15);
}
void lcdint()
{
PINSEL0=0x00000000;
PINSEL1=0x00000000;
lcdcmd(0x0E);
delay(5);
lcdcmd(0x38);
delay(5);
lcdcmd(0x06);
delay(5);
lcdcmd(0x80);
delay(5);
//lcdstr("okay");
}
void delay(int t)
{
unsigned int i,j;
for(i=0;i<t;i++)
{
for(j=0;j<21275;j++);
}
}
/*IO0DIR |= 0xFF;
IO0DIR |= (rs);
IO0DIR |= (rw);
IO0DIR |= (en);
*/
No comments:
Post a Comment