Master Slave Clocks with PIC16F887

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Hello,
I have pic16f887 and i want to connect the RTC module with it, Module from provotech, it has scl and sda.
according to little research it say to connect the SQW to timer interput at 1Hz something like that!!


Thanks
 

ErnieM

Joined Apr 24, 2011
8,010
"scl" and "sda" sounds like the I2C bus, known as the MASTER SYNCHRONOUS SERIAL PORT (MSSP)MODULE and lives on pins 23 (SDA) and 24 (SCL).

To receive an interrupt signal tie it to a suitable pin such as pin 33 (INT).

I would also say a bit more research is required.
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Hello,
The data sheet says....

The address map for the DS1307 RTC and RAM registers. The RTC registers are located in address
locations 00h to 07h. The RAM registers are located in address locations 08h to 3Fh. During a multibyte access,
when the address pointer reaches 3Fh, the end of RAM space, it wraps around to location 00h, the beginning of
the clock space.
00 to 007h mean display register!
But what about 08h to 3fH mean here?
 

Papabravo

Joined Feb 24, 2006
12,695
It means that there are 56 bytes of RAM at location 08h to 03Fh. That statement was just about as clear as it could be. What did you think it meant?
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Hello,
I will use 7 segment LED display..
only min and hours.
so, how to start in Hi tech c i have done i2C based project in BLDC ESC motor.

thanks
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Hello,

Here is code i have updated how to read I2C function is not present, how to read from DS1307

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;
}
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Hello,
I have modified the code simple version.
please have a look i have write 0x0f in sec register @ 0x00h addr to watch the change after this i am reading the i2c buffer register to get value!
In MPLAB build successful, hanging at in debugger can we test code in this?
void i2c_Wait(void){
while((SSPCON2 & 0X1F || (SSPSTAT & 0X04)));
}


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;
    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);

I2C_write(0x00);
I2C_write(0x0f);

   
I2cSTOP();
 
while(1){
    I2C_start();
    I2C_write(0xd0);
    I2C_write(0x00);
    I2cSTOP();
    I2C_start();
    I2C_write(0xD1);
   i= SSPBUF;

    I2cSTOP();
}


RD0=0;
RD1=0;
RD2=0;
RD3=1;
PORTB=data[i];
__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;
}
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Hello,
Here is the codei want only min and hours but not working display and | | on segment!
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;
unsigned char i=0;

TRISB=0X00;
TRISD=0X00;
PORTB=0B00000000;
__delay_ms(500);
    I2C_init();
__delay_ms(50);

    I2C_start();
    I2C_write(0xd0);

I2C_write(0x00);
I2C_write(0x02);

I2cSTOP();

RD0=0;
RD1=0;
RD2=1;
RD3=0;

PORTB=data[5];
__delay_ms(800);
PORTB=0B00000000;
   
RD0=0;
RD1=0;
RD2=0;
RD3=0;

    I2C_start();
    I2C_write(0xd0);
    I2C_write(0x00);

    I2cSTOP();
while(1){
    I2C_start();
    I2C_write(0xD1);
   i= SSPBUF;

    I2cSTOP();


RD0=1;
RD1=0;
RD2=0;
RD3=0;
PORTB=data[i%16];

__delay_ms(500);
PORTB=0x00;

RD0=0;
RD1=1;
RD2=0;
RD3=0;
PORTB=data[i>>4];

__delay_ms(500);
PORTB=0x00;




}
}
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;
}
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Hello,
In this setting will ds1307 will work fine?
C:
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
}
 
Top