PA3040
Advanced Member level 3
Dear All,
My RTC is ds1307
my pic is 16f877a
compiler Hi-Tech C
It is working fine for Proteus but it dose not work for my actual hardware please advice
Thanks in advance
My RTC is ds1307
my pic is 16f877a
compiler Hi-Tech C
It is working fine for Proteus but it dose not work for my actual hardware please advice
Thanks in advance
Code:
#include <htc.h>
#define _XTAL_FREQ 4000000 // 4 MHz clock
__CONFIG(0X3F39);
#define LCD_EN RB3
#define LCD_RS RB5
#define LCD_RW RB4
#define LCD_DATA PORTD
#define LCD_STROBE() ((LCD_EN = 1),(LCD_EN=0))
unsigned char date [5] = {'D','a','t','e',':'};
unsigned char time [5] = {'T','i','m','e',':'};
unsigned char I2CData[8] = {0x00 , 0x00, 0x00, 0x01, 0x01, 0x01, 0x09, 0x00};
unsigned char tmp;
void I2CInit(void){
TRISC3 = 1; /* SDA and SCL as input pin */
TRISC4 = 1; /* these pins can be configured either i/p or o/p */
SSPSTAT |= 0x80; /* Slew rate disabled */
SSPCON = 0x28; /* SSPEN = 1, I2C Master mode, clock = FOSC/(4 * (SSPADD + 1)) */
SSPADD = 0x28; /* 100Khz @ 4Mhz Fosc */
}
void I2CStart(){
SEN = 1; /* Start condition enabled */
while(SEN); /* automatically cleared by hardware */
/* wait for start condition to finish */
}
void I2CStop(){
PEN = 1; /* Stop condition enabled */
while(PEN); /* Wait for stop condition to finish */
/* PEN automatically cleared by hardware */
}
void I2CRestart(){
RSEN = 1; /* Repeated start enabled */
while(RSEN); /* wait for condition to finish */
}
void I2CAck(){
ACKDT = 0; /* Acknowledge data bit, 0 = ACK */
ACKEN = 1; /* Ack data enabled */
while(ACKEN); /* wait for ack data to send on bus */
}
void I2CNak(){
ACKDT = 1; /* Acknowledge data bit, 1 = NAK */
ACKEN = 1; /* Ack data enabled */
while(ACKEN); /* wait for ack data to send on bus */
}
void I2CWait(){
while ( ( SSPCON2 & 0x1F ) || ( SSPSTAT & 0x04 ) ); /* wait for any pending transfer */
}
void I2CSend(unsigned char dat){
SSPBUF = dat; /* Move data to SSPBUF */
while(BF); /* wait till complete data is sent from buffer */
I2CWait(); /* wait for any pending transfer */
}
unsigned char I2CRead(void){
unsigned char temp;
/* Reception works if transfer is initiated in read mode */
RCEN = 1; /* Enable data reception */
while(!BF); /* wait for buffer full */
temp = SSPBUF; /* Read serial buffer and store in temp register */
I2CWait(); /* wait to check any pending transfer */
return temp; /* Return the read data from bus */
}
void lcddata(unsigned char value)
{
LCD_DATA = value;
LCD_RS = 1;
LCD_RW = 0;
LCD_EN = 1;
__delay_ms (1);
LCD_EN = 0;
}
void lcdcmd(unsigned char value)
{
LCD_DATA = value;
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 1;
__delay_ms (1);
LCD_EN = 0;
}
void bcdToascii(unsigned char value){
tmp = value;
tmp = tmp & 0xf0;
tmp = tmp >> 4;
tmp = tmp | 0x30;
lcddata(tmp);
tmp = value;
tmp = tmp & 0x0f;
tmp = tmp | 0x30;
lcddata(tmp);
}
//-------------------------------------
void display (){
lcdcmd(0x80);
for (char i=0;i<5;i++)
{
lcddata(time[i]);
}
bcdToascii(I2CData[3]);
lcddata('-');
bcdToascii(I2CData[2]);
lcddata('-');
bcdToascii(I2CData[1]);
lcdcmd(0xc0);
for (char j=0;j<5;j++)
{
lcddata(date[j]);
}
bcdToascii(I2CData[5]);
lcddata('-');
bcdToascii(I2CData[6]);
lcddata('-');
bcdToascii(I2CData[0]);
}
//-------------------------------------
void lcd_init(){
TRISD = 0;
TRISB = 0;
LCD_EN =0;
__delay_ms(175);
lcdcmd(0x38);
__delay_ms(175);
lcdcmd(0x0e);
__delay_ms(15);
lcdcmd(0x01);
__delay_ms(10);
lcdcmd(0x06);
__delay_ms(10);
lcdcmd(0x80);
__delay_ms(10);
lcdcmd(0x0c);
__delay_ms(10);
}
//-------------------------------------
void main ()
{
lcd_init();
while(1){
i2c();
display();
}
}
i2c(){
I2CInit(); /* Initialize I2C Port */
I2CStart(); /* Send Start condition */
I2CSend(0xD0); /* Send DS1307 slave address with write operation */
I2CAck();
I2CSend(0x00); /* Send subaddress 0x00, we are writing to this location */
I2CAck();
I2CRestart(); /* send slave address with read bit set */
I2CSend(0xD1); /* Loop to read 8 bytes from I2C slave */
I2CData[0]=I2CRead();
I2CAck();
I2CData[1]=I2CRead();
I2CAck();
I2CData[2]=I2CRead();
I2CAck();
I2CData[3]=I2CRead();
I2CAck();
I2CData[4]=I2CRead();
I2CAck();
I2CData[5]=I2CRead();
I2CAck();
I2CData[6]=I2CRead();
//I2CNak();
I2CStop(); /* Send stop */
/* end of program */
}
Last edited: