Master Slave Clocks with PIC16F887

Discussion in 'Programmer's Corner' started by RRITESH KAKKAR, Mar 31, 2015.

  1. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    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
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    "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.
     
  3. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hello,
    The data sheet says....

    00 to 007h mean display register!
    But what about 08h to 3fH mean here?
     
  4. Papabravo

    Expert

    Feb 24, 2006
    10,152
    1,793
    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?
     
  5. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hello,
    Need of it where to use it?
    and after that how to work with DS1307 to get time and dates?
     
  6. Papabravo

    Expert

    Feb 24, 2006
    10,152
    1,793
    That's up to you. It's just another resource, you can use it or ignore it.
    The time and dates are in the datasheet
     
    RRITESH KAKKAR likes this.
  7. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,395
    497
  8. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    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
     
  9. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hello,
    How to use PIC16F887 AS DIGITAL INPUT OUT ?
    What is this?
    PBADEN = 0ff
     
  10. Papabravo

    Expert

    Feb 24, 2006
    10,152
    1,793
    What does the datasheet say?
     
  11. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hello,
    It is done by Ian
    The pic18 has the PBADEN.... On the pic16 you need to use " ANSELH = 0;"
     
  12. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hello,

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

    Code (C):
    1. #include <htc.h>
    2. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    3. #define _XTAL_FREQ 20000000
    4. #define SDATA RC4
    5. #define SCLK RC3
    6. void I2C_init(),I2C_start(void),I2C_write(char x), I2cSTOP(void);  
    7. void i2c_Wait(void);
    8. char data[13]={  0b00010000,0b01111101,0b00100011,0b00101000,0b01001101,0b10001000,
    9. 0b10000000, 0b00111101,0X00,0b00001000};
    10. void main(void){
    11. ANSELH = 0;
    12.     TRISC3=1; //direction to input have be changed
    13.     TRISC4=1;
    14. unsigned char ch=0;
    15. TRISC0=0;
    16. TRISB=0X00;
    17. TRISD=0X00;
    18. TRISC1=0;
    19. TRISC2=0;
    20. RC0=0;
    21. RC1=0;
    22. RC2=0;
    23. __delay_ms(500);
    24.     I2C_init();
    25. __delay_ms(50);
    26.  
    27.     I2C_start();
    28.     I2C_write(0xd0);//  DS1307 addr
    29.  
    30. I2C_write(0x00);//    ds1307 addr
    31. I2C_write(0x01);//   writing 1 sec
    32. I2cSTOP();
    33. while(1){
    34.     I2C_start();
    35.     I2C_write(0xd0);//  DS1307 addr
    36.     I2C_write(0x00);//ds1307 addr
    37.     I2C_start();
    38.     I2C_write(0xd1);//DS1307 addr bit high for reading
    39.     ch = I2C_Read();  //  how to read it??
    40.  
    41. I2cSTOP();
    42. RD0=0;
    43. RD1=0;
    44. RD2=0;
    45. RD3=1;
    46. PORTB=data[ch];
    47. __delay_ms(500);
    48. PORTB=0B00000000;
    49.  
    50.  
    51.  
    52.  
    53. }
    54. }
    55. void I2C_init(void)
    56.     {
    57.   SSPCON = 0x38;      // set I2C master mode
    58. SSPCON2 = 0x00;
    59. SSPADD = 0x0C;  //400KHZ  20MHz xtal
    60. SSPSTAT|=0X80;
    61. PSPIF=0;      // clear SSPIF interrupt flag
    62. BCLIF=0;      // clear bus collision flag
    63. }
    64. void I2C_start(void)
    65. {
    66.     i2c_Wait();
    67.     SEN=1;
    68. }
    69. void I2C_write(char x){
    70.     i2c_Wait();  
    71. SSPBUF=x;
    72.         }
    73. void i2c_Wait(void){
    74.     while((SSPCON2 & 0X1F || (SSPSTAT & 0X04)));
    75. }
    76. void I2cSTOP(void)
    77. {
    78.     i2c_Wait();
    79.     PEN=1;
    80. }
     
  13. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hello,

    Now i am understand the working of start and stop now i want to learn addr and read process!
     
  14. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hello,
    here is code of it for pic but incomplete..

    Code (C):
    1. http://www.8051projects.net/wiki/DS1307_I2C_RTC_Interfacing_Tutorial
     
  15. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    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)));
    }


    Code (C):
    1. #include <htc.h>
    2.  
    3. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    4. #define _XTAL_FREQ 20000000
    5. #define SDATA RC4
    6. #define SCLK RC3
    7. void I2C_init(),I2C_start(void),I2C_write(char x), I2cSTOP(void);  
    8. void i2c_Wait(void);
    9. char data[13]={  0b00010000,0b01111101,0b00100011,0b00101000,0b01001101,0b10001000,
    10. 0b10000000, 0b00111101,0X00,0b00001000};
    11.  
    12. void main(void){
    13. ANSELH = 0;
    14.     TRISC3=1; //direction to input have be changed
    15.     TRISC4=1;
    16. unsigned char ch=0;
    17.     unsigned char i;
    18. TRISC0=0;
    19. TRISB=0X00;
    20. TRISD=0X00;
    21. TRISC1=0;
    22. TRISC2=0;
    23. RC0=0;
    24. RC1=0;
    25. RC2=0;
    26. //__delay_ms(500);
    27.     I2C_init();
    28. //__delay_ms(50);
    29.  
    30.     I2C_start();
    31.     I2C_write(0xd0);
    32.  
    33. I2C_write(0x00);
    34. I2C_write(0x0f);
    35.  
    36.    
    37. I2cSTOP();
    38.  
    39. while(1){
    40.     I2C_start();
    41.     I2C_write(0xd0);
    42.     I2C_write(0x00);
    43.     I2cSTOP();
    44.     I2C_start();
    45.     I2C_write(0xD1);
    46.    i= SSPBUF;
    47.  
    48.     I2cSTOP();
    49. }
    50.  
    51.  
    52. RD0=0;
    53. RD1=0;
    54. RD2=0;
    55. RD3=1;
    56. PORTB=data[i];
    57. __delay_ms(500);
    58. PORTB=0B00000000;
    59.  
    60.  
    61. }
    62.  
    63. void I2C_init(void)
    64.     {
    65.   SSPCON = 0x38;      // set I2C master mode
    66. SSPCON2 = 0x00;
    67. SSPADD = 0x0C;  //400KHZ  20MHz xtal
    68. SSPSTAT|=0X80;
    69. PSPIF=0;      // clear SSPIF interrupt flag
    70. BCLIF=0;      // clear bus collision flag
    71. }
    72. void I2C_start(void)
    73. {
    74.     i2c_Wait();
    75.     SEN=1;
    76. }
    77. void I2C_write(char x){
    78.     i2c_Wait();  
    79. SSPBUF=x;
    80.         }
    81. void i2c_Wait(void){
    82.     while((SSPCON2 & 0X1F || (SSPSTAT & 0X04)));
    83. }
    84. void I2cSTOP(void)
    85. {
    86.     i2c_Wait();
    87.     PEN=1;
    88. }
    89.  
    90.  
    91.  
    92.  
    93.  
    94.  
    95.  
    96.  
    97.  
     
  16. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hello,
    Can any one help me about I2C with DS1307?
     
  17. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,395
    497
    I2C_write(0xd0);
    Is this where you get an error?
     
  18. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hello,
    Here is the codei want only min and hours but not working display and | | on segment!
    Code (C):
    1. #include <htc.h>
    2.  
    3. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    4. #define _XTAL_FREQ 20000000
    5. #define SDATA RC4
    6. #define SCLK RC3
    7. void I2C_init(),I2C_start(void),I2C_write(char x), I2cSTOP(void);  
    8. void i2c_Wait(void);
    9. char data[13]={  0b00010000,0b01111101,0b00100011,0b00101000,0b01001101,0b10001000,
    10. 0b10000000, 0b00111101,0X00,0b00001000};
    11.  
    12. void main(void){
    13. ANSELH = 0;
    14.     TRISC3=1; //direction to input have be changed
    15.     TRISC4=1;
    16. unsigned char ch=0;
    17. unsigned char i=0;
    18.  
    19. TRISB=0X00;
    20. TRISD=0X00;
    21. PORTB=0B00000000;
    22. __delay_ms(500);
    23.     I2C_init();
    24. __delay_ms(50);
    25.  
    26.     I2C_start();
    27.     I2C_write(0xd0);
    28.  
    29. I2C_write(0x00);
    30. I2C_write(0x02);
    31.  
    32. I2cSTOP();
    33.  
    34. RD0=0;
    35. RD1=0;
    36. RD2=1;
    37. RD3=0;
    38.  
    39. PORTB=data[5];
    40. __delay_ms(800);
    41. PORTB=0B00000000;
    42.    
    43. RD0=0;
    44. RD1=0;
    45. RD2=0;
    46. RD3=0;
    47.  
    48.     I2C_start();
    49.     I2C_write(0xd0);
    50.     I2C_write(0x00);
    51.  
    52.     I2cSTOP();
    53. while(1){
    54.     I2C_start();
    55.     I2C_write(0xD1);
    56.    i= SSPBUF;
    57.  
    58.     I2cSTOP();
    59.  
    60.  
    61. RD0=1;
    62. RD1=0;
    63. RD2=0;
    64. RD3=0;
    65. PORTB=data[i%16];
    66.  
    67. __delay_ms(500);
    68. PORTB=0x00;
    69.  
    70. RD0=0;
    71. RD1=1;
    72. RD2=0;
    73. RD3=0;
    74. PORTB=data[i>>4];
    75.  
    76. __delay_ms(500);
    77. PORTB=0x00;
    78.  
    79.  
    80.  
    81.  
    82. }
    83. }
    84. void I2C_init(void)
    85.     {
    86.   SSPCON = 0x38;      // set I2C master mode
    87. SSPCON2 = 0x00;
    88. SSPADD = 0x0C;  //400KHZ  20MHz xtal
    89. SSPSTAT|=0X80;
    90. PSPIF=0;      // clear SSPIF interrupt flag
    91. BCLIF=0;      // clear bus collision flag
    92. }
    93. void I2C_start(void)
    94. {
    95.     i2c_Wait();
    96.     SEN=1;
    97. }
    98. void I2C_write(char x){
    99.     i2c_Wait();  
    100. SSPBUF=x;
    101.         }
    102. void i2c_Wait(void){
    103.     while((SSPCON2 & 0X1F || (SSPSTAT & 0X04)));
    104. }
    105. void I2cSTOP(void)
    106. {
    107.     i2c_Wait();
    108.     PEN=1;
    109. }
    110.  
    111.  
    112.  
    113.  
    114.  
    115.  
    116.  
    117.  
    118.  
     
  19. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hello,
    In this setting will ds1307 will work fine?
    Code (C):
    1. void I2C_init(void)
    2.     {
    3.   SSPCON = 0x38;      // set I2C master mode
    4. SSPCON2 = 0x00;
    5. SSPADD = 0x0C;  //400KHZ  20MHz xtal
    6. SSPSTAT|=0X80;
    7. PSPIF=0;      // clear SSPIF interrupt flag
    8. BCLIF=0;      // clear bus collision flag
    9. }
    10.  
     
Loading...