Master Slave Clocks with PIC16F887

Status
Not open for further replies.
Hello,
In mp lab can we see how it is working/communicating with DS1307?


Hello, main objective to production of it in market!
so, we are not doing it for homework or college student project, that why i am keeping it on top of all.!
 
Hello,

Here is code i have updated how to read i2c function i snot present.


C:
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
#define SDATA RC4
#define SCLK RC3
void I2C_init(),I2C_start(void),I2C_write(char x), I2cSTOP(void);   
void i2c_Wait(void);
char data[13]={  0b00010000,0b01111101,0b00100011,0b00101000,0b01001101,0b10001000,
0b10000000, 0b00111101,0X00,0b00001000};
void main(void){
ANSELH = 0;
    TRISC3=1; //direction to input have be changed
    TRISC4=1;
unsigned char ch=0;
TRISC0=0;
TRISB=0X00;
TRISD=0X00;
TRISC1=0;
TRISC2=0;
RC0=0;
RC1=0;
RC2=0;
__delay_ms(500);
    I2C_init();
__delay_ms(50);

    I2C_start();
    I2C_write(0xd0);//  DS1307 addr

I2C_write(0x00);//    ds1307 addr
I2C_write(0x01);//   writing 1 sec
I2cSTOP();
 
while(1){
    I2C_start();
    I2C_write(0xd0);//  DS1307 addr
    I2C_write(0x00);//ds1307 addr
    I2C_start();
    I2C_write(0xd1);//DS1307 addr bit high for reading
    ch = I2C_Read();  //  how to read it??

I2cSTOP();
RD0=0;
RD1=0;
RD2=0;
RD3=1;
PORTB=data[ch];
__delay_ms(500);
PORTB=0B00000000;




}
}
void I2C_init(void)
    {
  SSPCON = 0x38;      // set I2C master mode
SSPCON2 = 0x00;
SSPADD = 0x0C;  //400KHZ  20MHz xtal
SSPSTAT|=0X80;
PSPIF=0;      // clear SSPIF interrupt flag
BCLIF=0;      // clear bus collision flag
}
void I2C_start(void)
{
    i2c_Wait();
    SEN=1;
}
void I2C_write(char x){
    i2c_Wait();   
SSPBUF=x;
        }
void i2c_Wait(void){
    while((SSPCON2 & 0X1F || (SSPSTAT & 0X04)));
}
void I2cSTOP(void)
{
    i2c_Wait();
    PEN=1;
}
 
If you want average timekeeping then use the chips xtal to keep time, if you want good timekeeping use a watch xtal in conjunction with the chip, and if you want very good timekeeping which continues even with power off use a real time clock chip.
This decision is the first step.
 
I see your code is in C, I cant help you this time as I code in .asm.
I suggest you read up on i2c, its fairly simple but needs to be understood before you try to communicate with a i2c device.
 
Hello,
I have understated start and stop condition, please tell further how addr and read the data with ackn.
i have no idea how it give data from SCL and SDA.
 
Why don't you use my routines.. Just add I2C.h and EEprom.h to your project and you can access the RTC!!
 
Then you need to go through the software routines I posted.... The software bit banging is the best to understand how it all works
 
Anyway, I have modified this code to my need on segment display on one digit right now to see its working or not?

C:
#include <htc.h>

__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
#define SDATA RC4
#define SCLK RC3
void I2C_init(),I2C_start(void),I2C_write(char x), I2cSTOP(void);   
void i2c_Wait(void);
char data[13]={  0b00010000,0b01111101,0b00100011,0b00101000,0b01001101,0b10001000,
0b10000000, 0b00111101,0X00,0b00001000};
unsigned char I2CData[] = {0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x09, 0x00};    // Buffer where we will read/write our data
void main(void){
ANSELH = 0;
    TRISC3=1; //direction to input have be changed
    TRISC4=1;
unsigned char ch=0;
    unsigned char i;
TRISC0=0;
TRISB=0X00;
TRISD=0X00;
TRISC1=0;
TRISC2=0;
RC0=0;
RC1=0;
RC2=0;
__delay_ms(500);
    I2C_init();
__delay_ms(50);

    I2C_start();
    I2C_write(0xd0);//esc default addr

I2C_write(0x00);//esc default addr

    /* Loop to write 8 bytes */
    for (i = 0; i < 8; i++) {
        /* send I2C data one by one */
        I2C_write(I2CData[i]);
    }
I2cSTOP();
 
while(1){
    I2C_start();
    I2C_write(0xd0);//esc default addr
    I2C_write(0x00);//esc default addr
   
    I2C_start();
    I2C_write(0xd1);//esc default addr
    I2C_write(0xD1);
    for (i = 8; i > 0; i--) {
       
        I2CData[i]= I2CRead();
        /* ACK if its not the last byte to read */
        /* if its the last byte then send a NAK */
        if (i - 1)
            I2CAck();
        else
            I2CNak();
    }
   
    I2CStop();
}
  //ch = I2C_Read();


RD0=0;
RD1=0;
RD2=0;
RD3=1;
PORTB=data[ch];
__delay_ms(500);
PORTB=0B00000000;


}

void I2C_init(void)
    {
  SSPCON = 0x38;      // set I2C master mode
SSPCON2 = 0x00;
SSPADD = 0x0C;  //400KHZ  20MHz xtal
SSPSTAT|=0X80;
PSPIF=0;      // clear SSPIF interrupt flag
BCLIF=0;      // clear bus collision flag
}
void I2C_start(void)
{
    i2c_Wait();
    SEN=1;
}
void I2C_write(char x){
    i2c_Wait();   
SSPBUF=x;
        }
void i2c_Wait(void){
    while((SSPCON2 & 0X1F || (SSPSTAT & 0X04)));
}
void I2cSTOP(void)
{
    i2c_Wait();
    PEN=1;
}
 
  1. Bit banging is a technique for serial communications using software instead of dedicated hardware. Software directly sets and samples the state of pins on the microcontroller, and is responsible for all parameters of the signal: timing, levels, synchronization, etc.
Ok, but how to use this code you posted that the main problem to move forward!
 
The code you posted has two functions Ack() and Nack() but don't get defined anywhere???
 
Hello,
In your code you are using portb why? i2c work on portc .
#define I2C_PORT PORTB // I2C port configuration
#define I2C_TRIS TRISB
#define SDAI RB7
#define SCLI RB6
 
Hello,
How DS1307 has internal addresses set to 0xD0, mean how hardware is set for this, for better understanding i am making image of it!
 
I have watched this animation of i2C, it as very nice.
but in code there is difficulty!
 
Hello,
In your code you are using portb why? i2c work on portc .
#define I2C_PORT PORTB // I2C port configuration
#define I2C_TRIS TRISB
#define SDAI RB7
#define SCLI RB6
The chip in my code didn't have a port c... It was a pic16f628a.... Thats the only reason... If I need I2C on the bigger chips I use the hardware port..
Hello,
How DS1307 has internal addresses set to 0xD0, mean how hardware is set for this, for better understanding i am making image of it!
Its done internally.... If you send a different address then the DS1307 will not respond.... As I2C is a bus.. many different devices can talk on it!! Some devices have A0~A2 so several of the same can talk..
I have watched this animation of i2C, it as very nice.
but in code there is difficulty!
How would you like me to go through it with you...
 
Hello,
That why your code was long!
If I need I2C on the bigger chips I use the hardware port..

So, i have pic16f887 it has hardware i2C.
can we do code simple on it.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…