hello sir
The problem was solve but in lcd error output diaplay this rtc program using i2c pic16f877a, i have problem in i2c "write" function please see my program
#include<htc.h>
#include<stdio.h>
//#include<main.h>
//#include<intrins.h>
#pragma config WDT=OFF
#define ACK 1
#define NO_ACK 0
#define SLAVE 0xD0
#define WRITE 0x00
#define READ 0x01
#define ERR_ACK 0x01
#define DATA PORTB
//LCD Control Lines
#define RS RB0
#define lcd_RW RB1
#define lcd_e RB2
//Display messages
unsigned char msg[] = ("REAL TIME CLOCK ");
unsigned char msg1[] = ("*** DEMO ***");
unsigned char msg2[] = {"DATE: / / "};
unsigned char msg3[] = {"TIME: : : "};
unsigned int i;
unsigned char j,msb,lsb;
//void i2c_init();
void i2c_waitForIdle();
//void stop1();
//void start1();
//LCD Functions
void lcd_init(void);
void lcd_cmd(unsigned char);
void lcd_display(unsigned char);
void delay(void);
void disp_date_time(void);
void lcd_display1(unsigned char);
void lcd_display2(unsigned char);
void lcd_display3(unsigned char);
void lcd_display4(unsigned char);
void lcd_display5(unsigned char);
void lcd_display6(unsigned char);
void lcd_display7(unsigned char);
void lcd_display8(unsigned char);
void lcd_display9(unsigned char);
void lcd_display10(unsigned char);
void lcd_display11(unsigned char);
void lcd_display12(unsigned char);
void InitSerial(void);
void DelayMs(unsigned int a);
void hex_decimal(unsigned char);
void bcd_hex(unsigned char);
//---------------------------------------
// DS1307 driver
//---------------------------------------
unsigned char RTC_ARR[7]; // Buffer for second,minute,.....,year
unsigned char p;
void lcd_cmd(unsigned char cmnd)
{
DATA = 0xf0&cmnd; //Masking lower 4 Bits
RS = 0; lcd_RW = 0;
lcd_e = 1;
DelayMs(35);
lcd_e = 0;
DATA = cmnd*16; //Masking lower 4 Bits
RS = 0; lcd_RW = 0;
lcd_e = 1;
DelayMs(35);
lcd_e = 0;
}
//----------------------------------
// LCD Data Function
//----------------------------------
void lcd_display(unsigned char dat)
{
DATA = 0xf0&dat; //Masking lower 4 Bits
RS = 1; lcd_RW = 0;
lcd_e = 1;
DelayMs(15);
lcd_e = 0;
DATA = dat*16; //Masking lower 4 Bits
RS = 1; lcd_RW = 0;
lcd_e = 1;
DelayMs(15);
lcd_e = 0;
}
//----------------------------------
// LCD Initialization
//----------------------------------
void lcd_init(void)
{
unsigned char i;
lcd_cmd(0x28);
DelayMs(15);
lcd_cmd(0x0c);
DelayMs(15);
lcd_cmd(0x06);
DelayMs(15);
lcd_cmd(0x01);
DelayMs(15);
//-------------------------------------------
// First Line Message Display
//-------------------------------------------
lcd_cmd(0x80); //First Line Initialization
DelayMs(35);
i=0;
while(msg!='\0')
{
lcd_display(msg);
i++;
}
DelayMs(50);
//-------------------------------------------
// Second Line Message Display
//-------------------------------------------
lcd_cmd(0xc0); //Second Line Initialization
DelayMs(35);
i=0;
while(msg1!='\0')
{
lcd_display(msg1);
i++;
}
DelayMs(50);
}
//LCD Delay function
void delay(void)
{
unsigned int a;
for(a=0;a<=1000;a++);
}
//DIsplay DATE and Time
//Display Message in First line
void disp_date_time()
{
lcd_cmd(0x80);
delay();
delay();
for(i=0;msg2 != '\0';i++)
lcd_display(msg2);
delay();
//Display Message in First line
lcd_cmd(0xC0);
delay();
delay();
for(i=0;msg3 != '\0';i++)
lcd_display(msg3);
delay();
}
//void i2c_waitForIdle()
//{
//while (( SSPCON2 & 0x1F ) | RW ) {}; // wait for idle and not writing
//}
// start I2C
//void Start(void)
//void st1()
//{
// i2c_waitForIdle();
// SSPCON2=0x01;
//}
void i2c_start()
{
//i2c_waitForIdle();
SSPCON2=0x1b;
//delay();
}
// stop I2C
//void Stop(void)
//void sp1()
//{
//i2c_waitForIdle();
//SSPCON2=0x04;
//}
void i2c_stop()
{
//i2c_waitForIdle();
PEN=1;
//delay();
}
// Write I2C
//void WriteI2C(unsigned char Data)
unsigned char WriteI2C( unsigned char i2cWriteData )
{
//i2c_waitForIdle();
SSPBUF = i2cWriteData;
//if(ACKSTAT)
{
//while(ACKSTAT);
}
return ( ! ACKSTAT ); // function returns '1' if transmission is acknowledged
}
// Read I2C
//unsigned char ReadI2C(bit ACK_Bit)
int ReadI2C( unsigned char ack )
{
unsigned char i2cReadData;
//i2c_waitForIdle();
RCEN=1;
//i2c_waitForIdle();
i2cReadData = SSPBUF;
//i2c_waitForIdle();
if ( ack )
{
ACKDT=0;
}
else
{
ACKDT=1;
}
ACKEN=1; // send acknowledge sequence
return( i2cReadData );
}
// Read RTC (all real time)
void ReadRTC(unsigned char * buff)
{
void i2c_start();
WriteI2C(0xD0);
WriteI2C(0x00);
void i2c_start();
WriteI2C(0xD1);
*(buff+0)=ReadI2C(ACK); // Second
*(buff+1)=ReadI2C(ACK); // Minute
*(buff+2)=ReadI2C(ACK); // hour
*(buff+3)=ReadI2C(ACK); // Day
*(buff+4)=ReadI2C(ACK); // date
*(buff+5)=ReadI2C(ACK); // month
*(buff+6)=ReadI2C(NO_ACK); // year
void i2c_stop();
}
// Write RTC
void WriteRTC(unsigned char *buff)
{
void i2c_start();
WriteI2C(0xD0);
WriteI2C(0x00);
WriteI2C(*(buff+0));
WriteI2C(*(buff+1));
WriteI2C(*(buff+2));
WriteI2C(*(buff+3));
WriteI2C(*(buff+4));
WriteI2C(*(buff+5));
WriteI2C(*(buff+6));
void i2c_stop();
}
// Delay mS function
void DelayMs(unsigned int count)
{ // mSec Delay 11.0592 Mhz
unsigned int i;
while(count)
{
i = 115;
while(i>0) i--;
count--;
}
}
//SECONDS DISPLAY
void lcd_display1(unsigned char dat)
{
lcd_cmd(0xcd);
delay();
lcd_display(dat);
delay();
}
void lcd_display2(unsigned char dat)
{
lcd_cmd(0xcc);
delay();
lcd_display(dat);
}
//Minutes DISPLAY
void lcd_display3(unsigned char dat)
{
lcd_cmd(0xca);
delay();
lcd_display(dat);
}
void lcd_display4(unsigned char dat)
{
lcd_cmd(0xc9);
delay();
lcd_display(dat);
}
//Hours DISPLAY
void lcd_display5(unsigned char dat)
{
lcd_cmd(0xc7);
DelayMs(2);
lcd_display(dat);
}
void lcd_display6(unsigned char dat)
{
lcd_cmd(0xc6);
delay();
lcd_display(dat);
delay();
}
//DATE DISPLAY
void lcd_display7(unsigned char dat)
{
lcd_cmd(0x87);
delay();
lcd_display(dat);
}
void lcd_display8(unsigned char dat)
{
lcd_cmd(0x86);
delay();
lcd_display(dat);
}
//Month DISPLAY
void lcd_display9(unsigned char dat)
{
lcd_cmd(0x8a);
delay();
lcd_display(dat);
}
void lcd_display10(unsigned char dat)
{
lcd_cmd(0x89);
delay();
lcd_display(dat);
}
//YEAR DISPLAY
void lcd_display11(unsigned char dat)
{
lcd_cmd(0x8d);
delay();
delay();
lcd_display(dat);
delay();
}
void lcd_display12(unsigned char dat)
{
lcd_cmd(0x8c);
delay();
delay();
lcd_display(dat);
delay();
}
//unsigned char bcd_hex(unsigned char hex)
void bcd_hex(unsigned char hex)
{
unsigned char bcd;
bcd = hex;
bcd = bcd & 0x0F;
bcd = bcd | 0x30;
lsb = bcd;
bcd = hex;
bcd>>=4;
bcd = bcd & 0x0F;
bcd = bcd | 0x30;
msb=bcd;
}
//---------------------------------------
// Main program
//---------------------------------------
void main(void)
{
// ptr=day;
TRISB=0;
//void i2c_init();
TRISC=0x18; // set SCL and SDA pins as inputs
//SSPADD=0x18;
SSPCON = 0x38; // set I2C master mode
SSPCON2 = 0x00;
//file://SSPADD = 0x6; // 400kHz bus with 10MHz xtal - use 0x0C with 20MHz xtal
SSPADD = 9; // 100k at 4Mhz clock
CKE=0; // use I2C levels worked also with '0'
SMP=1; // disable slew rate control worked also with '0'
//PSPIF=0; // clear SSPIF interrupt flag
//BCLIF=0;
lcd_init();
delay();
disp_date_time();
// Setup time and enable oscillator
ReadRTC(&RTC_ARR[0]);
RTC_ARR[0] = RTC_ARR[0] & 0x7F; // enable oscillator (bit 7=0)
RTC_ARR[1] = 0x45; // minute = 59
RTC_ARR[2] = 0x11; // hour = 05 ,24-hour mode(bit 6=0)
RTC_ARR[3] = 0x03; // Day = 1 or sunday
RTC_ARR[4] = 0x25; // Date = 30
RTC_ARR[5] = 0x11; // month = August
RTC_ARR[6] = 0x09; // year = 05 or 2005
WriteRTC(&RTC_ARR[0]); // Set RTC
while(1)
{
ReadRTC(&RTC_ARR[0]);
bcd_hex(RTC_ARR[0]);//seconds
lcd_display1(lsb);
lcd_display2(msb);
bcd_hex(RTC_ARR[1]);//minutes
lcd_display3(lsb);
lcd_display4(msb);
bcd_hex(RTC_ARR[2]);//Hours
lcd_display5(lsb);
lcd_display6(msb);
bcd_hex(RTC_ARR[4]);//Date
lcd_display7(lsb);
lcd_display8(msb);
bcd_hex(RTC_ARR[5]);//Month
lcd_display9(lsb);
lcd_display10(msb);
bcd_hex(RTC_ARR[6]);//Year
lcd_display11(lsb);
lcd_display12(msb);
}
}