engr.waqas
Full Member level 3
![Full Member level 3](/styles/images/ranks/elevel3.gif)
- Joined
- Jul 21, 2009
- Messages
- 172
- Helped
- 13
- Reputation
- 26
- Reaction score
- 10
- Trophy points
- 1,298
- Location
- karachi,Pakistan
- Activity points
- 2,342
I want to interface DS1307 with Pic 18f452 using I2C interface.
I am not able to read data properly and only "??" is displaying on LCD.
My Coding in MPLAB C18 is provided below
NOTE: This coding is only for "second halting" (enabeling CH bit in second register of DS1307)
#include<p18f452.h>
#include <i2c.h>
#define rs PORTBbits.RB0
#define rw PORTBbits.RB1
#define en PORTBbits.RB2
void MSDelay(unsigned int itime)
{
unsigned int i,j;
for(i=0;i<itime;i++)
for(j=0;j<135;j++);
}
void lcdcmd(unsigned char value)
{
PORTD=value;
rs=0;
rw=0;
en=1;
MSDelay(1);
en=0;
}
void BCDtoASCIIandSEND(unsigned char myvalue)
{
unsigned char tmp=myvalue;
//LCD initilization
TRISD=0;
TRISB=0;
en=0;
MSDelay(15);
lcdcmd(0x38);
MSDelay(15);
lcdcmd(0x0C);
MSDelay(15);
lcdcmd(0x01);
MSDelay(15);
lcdcmd(0x06);
MSDelay(15);
lcdcmd(0x86);
MSDelay(15);
tmp=tmp&0xF0;//mask lower nibble
tmp=tmp>>4;//swap it
tmp=tmp|0x30;//make it ASCII
PORTD=tmp;
rs=1;
rw=0;
en=1;
MSDelay(1);
en=0;
MSDelay(1);
tmp=myvalue;//for other digit
tmp=tmp&0x0F;//mask upper nibble
tmp=tmp|0x30;//make it ASCII
PORTD=tmp;
rs=1;
rw=0;
en=1;
MSDelay(1);
en=0;
MSDelay(1);
PORTD=':';
rs=1;
rw=0;
en=1;
MSDelay(1);
en=0;
MSDelay(1);
}
void SDELAY(int ms)
{
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<135;j++);
}
void init(void)
{
TRISCbits.TRISC3 =1; //RC3 as INPUT (SCL)
TRISCbits.TRISC4 =1; //RC4 as INPUT (SDA)
//MSSP Configuration
SSPCON1 =0x28; //Enable MSSP in Master mode
SSPCON2 =0;
SSPSTATbits.SMP =1; //Disable slew rate control
SSPSTATbits.CKE =0; //I2C compatible
SSPADD =0x09; //Fscl = 100kHz pour Fosc=4MHz
//Fscl = Fosc/(4*(SSPADD+1))
TRISD=0;
TRISB=0;
en=0;
MSDelay(15);
lcdcmd(0x38);
MSDelay(15);
lcdcmd(0x0C);
MSDelay(15);
lcdcmd(0x01);
MSDelay(15);
lcdcmd(0x06);
MSDelay(15);
lcdcmd(0x86);
MSDelay(15);
}
void main (void)
{
unsigned char temp1=0,i;
init();
while (1) {
StartI2C();
if (!DataRdyI2C())
WriteI2C(0x00); //Address
IdleI2C();
if (!DataRdyI2C())
WriteI2C(0x80); //DATA
IdleI2C();
StopI2C();
IdleI2C();
if (!DataRdyI2C())
temp1 = ReadI2C();
StopI2C();
if (i==4)
NotAckI2C(); //NACK before STOP
else
AckI2C(); //ACK for next read i2c_idle();
BCDtoASCIIandSEND(temp1);
}
StopI2C();
}
I am not able to read data properly and only "??" is displaying on LCD.
My Coding in MPLAB C18 is provided below
NOTE: This coding is only for "second halting" (enabeling CH bit in second register of DS1307)
#include<p18f452.h>
#include <i2c.h>
#define rs PORTBbits.RB0
#define rw PORTBbits.RB1
#define en PORTBbits.RB2
void MSDelay(unsigned int itime)
{
unsigned int i,j;
for(i=0;i<itime;i++)
for(j=0;j<135;j++);
}
void lcdcmd(unsigned char value)
{
PORTD=value;
rs=0;
rw=0;
en=1;
MSDelay(1);
en=0;
}
void BCDtoASCIIandSEND(unsigned char myvalue)
{
unsigned char tmp=myvalue;
//LCD initilization
TRISD=0;
TRISB=0;
en=0;
MSDelay(15);
lcdcmd(0x38);
MSDelay(15);
lcdcmd(0x0C);
MSDelay(15);
lcdcmd(0x01);
MSDelay(15);
lcdcmd(0x06);
MSDelay(15);
lcdcmd(0x86);
MSDelay(15);
tmp=tmp&0xF0;//mask lower nibble
tmp=tmp>>4;//swap it
tmp=tmp|0x30;//make it ASCII
PORTD=tmp;
rs=1;
rw=0;
en=1;
MSDelay(1);
en=0;
MSDelay(1);
tmp=myvalue;//for other digit
tmp=tmp&0x0F;//mask upper nibble
tmp=tmp|0x30;//make it ASCII
PORTD=tmp;
rs=1;
rw=0;
en=1;
MSDelay(1);
en=0;
MSDelay(1);
PORTD=':';
rs=1;
rw=0;
en=1;
MSDelay(1);
en=0;
MSDelay(1);
}
void SDELAY(int ms)
{
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<135;j++);
}
void init(void)
{
TRISCbits.TRISC3 =1; //RC3 as INPUT (SCL)
TRISCbits.TRISC4 =1; //RC4 as INPUT (SDA)
//MSSP Configuration
SSPCON1 =0x28; //Enable MSSP in Master mode
SSPCON2 =0;
SSPSTATbits.SMP =1; //Disable slew rate control
SSPSTATbits.CKE =0; //I2C compatible
SSPADD =0x09; //Fscl = 100kHz pour Fosc=4MHz
//Fscl = Fosc/(4*(SSPADD+1))
TRISD=0;
TRISB=0;
en=0;
MSDelay(15);
lcdcmd(0x38);
MSDelay(15);
lcdcmd(0x0C);
MSDelay(15);
lcdcmd(0x01);
MSDelay(15);
lcdcmd(0x06);
MSDelay(15);
lcdcmd(0x86);
MSDelay(15);
}
void main (void)
{
unsigned char temp1=0,i;
init();
while (1) {
StartI2C();
if (!DataRdyI2C())
WriteI2C(0x00); //Address
IdleI2C();
if (!DataRdyI2C())
WriteI2C(0x80); //DATA
IdleI2C();
StopI2C();
IdleI2C();
if (!DataRdyI2C())
temp1 = ReadI2C();
StopI2C();
if (i==4)
NotAckI2C(); //NACK before STOP
else
AckI2C(); //ACK for next read i2c_idle();
BCDtoASCIIandSEND(temp1);
}
StopI2C();
}