what is code to interface DS1302 with Pic16f877a

Discussion in 'Embedded Systems and Microcontrollers' started by masoodiqbal, Mar 8, 2009.

  1. masoodiqbal

    Thread Starter New Member

    Mar 8, 2009
    3
    0
    I want to design a clock which set date,time with push button.
    how i define variables which store data in eprome and set date,time
    code define in C++
     
  2. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
  3. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    This is a bit banging routine for the 16F877A, cut out the display (to 7 segment) portions so it would fit in one post.



    Code ( (Unknown Language)):
    1.  
    2. #include<pic.h>                        //include MCU head file    
    3. __CONFIG(0x1832);      
    4. //__CONFIG _DEBUG_OFF&_CP_ALL&_WRT_HALF&_CPD_ON&_LVP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_HS_OSC    
    5.  
    6. #define i_o   RB4                      //1302I_O          
    7. #define sclk  RB0                      //1302 clock        
    8. #define rst   RB5                      //1302 enable bit  
    9.  
    10. // unsigned char time_rx;
    11.   unsigned char time_rx @ 0x30;        //define receive reg.
    12.   static volatile bit time_rx7   @ (unsigned)&time_rx*8+7;   //receive reg highest.
    13. //static volatile bit temp0     @ (unsigned)&temp*8+0;
    14.  
    15. void port_init();                      //port initilize subroutine.
    16. void ds1302_init();                    //DS1302 initilize subroutine.
    17. void set_time();                       //set time subroutine.
    18. void get_time();                       //get time subroutine.
    19. void display();                        //display subroutine.
    20. void time_write_1(unsigned char time_tx);    //write one byte subroutine.
    21. unsigned char  time_read_1();          //read one byte subroutine.
    22. void delay();                          //delay subroutine.
    23. //define the time: sec,min,hour,day,month,week,year,control word.
    24. const char table[]={0x00,0x58,0x12,0x8,0x3,0x06,0x06,0x00};
    25. //define the read time and date save table.
    26. char table1[7];
    27. //define the display code for display 0-9
    28. const char table2[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
    29.  
    30. //----------------------------------------------
    31. //main routine.
    32. void main()
    33.   {
    34.      port_init();                     //port initilize.
    35.      ds1302_init();                   //DS1302 initilize.
    36.      set_time();                      //set time
    37.      while(1)
    38.         {
    39.           get_time();                
    40.           display();                  
    41.         }
    42.   }
    43.  
    44. //---------------------------------------------
    45. //DS1302 initilize.
    46. void ds1302_init()
    47.   {
    48.    sclk=0;                            //pull low clock
    49.    rst =0;                            //reset DS1302
    50.    rst=1;                             //enable DS1302
    51.    time_write_1(0x8e);                //send control command
    52.    time_write_1(0);                   //enable write DS1302
    53.    rst=0;                             //reset
    54.   }
    55.  
    56. //---------------------------------------------
    57. //set time.
    58. void set_time()
    59.   {
    60.    int i;                             //define the loop counter.
    61.    rst=1;                             //enable DS1302
    62.    time_write_1(0xbe);                //
    63.    for(i=0;i<8;i++)                   //continue to write 8 bytes.
    64.      {
    65.        time_write_1(table[i]);        //write one byte
    66.      }
    67.    rst=0;                             //reset
    68.    }
    69.  
    70. //---------------------------------------------
    71. //get time.
    72. void get_time()
    73.  {
    74.    int i;                             //set loop counter.
    75.    rst=1;                             //enable DS1302
    76.    time_write_1(0xbf);                //
    77.    for(i=0;i<7;i++)                   //continue to read 7 bytes.
    78.      {
    79.         table1[i]=time_read_1();      //
    80.      }
    81.     rst=0;                            //reset DS1302
    82.  }
    83.  
    84. //--------------------------------------------
    85. //write one byte
    86. void time_write_1(unsigned char time_tx)
    87.  {
    88.     int j;                            //set the loop counter.
    89.     for(j=0;j<8;j++)                  //continue to write 8bit
    90.       {
    91.         i_o=0;                        //
    92.         sclk=0;                       //pull low clk
    93.         if(time_tx&0x01)              //judge the send bit is 0 or 1.
    94.           {
    95.             i_o=1;                    //is 1
    96.           }
    97.         time_tx=time_tx>>1;           //rotate right 1 bit.
    98.         sclk=1;                       //pull high clk
    99.        }
    100.       sclk=0;                         //finished 1 byte,pull low clk
    101.   }
    102.  
    103. //---------------------------------------------
    104. //read one byte.
    105. unsigned char time_read_1()
    106.  {
    107.    int j;                            //set the loop counter.  
    108.    TRISB4=1;                         //continue to write 8bit
    109.    for(j=0;j<8;j++)                  
    110.       {
    111.         sclk=0;                       //pull low clk                  
    112.         time_rx=time_rx>>1;           //judge the send bit is 0 or 1.  
    113.         time_rx7=i_o;                //put the received bit into the reg's highest.
    114.        sclk=1;                       //pull high clk                
    115.       }                                                              
    116.     TRISB4=0;                        //finished 1 byte,pull low clk  
    117.     sclk=0;                          
    118.     return(time_rx);                
    119.   }
    120.  
    121. //--------------------------------------------
    122. //pin define func
    123. void port_init()
    124.   {
    125.     TRISA=0x00;                     //a port all output
    126.     TRISD=0X00;                     //d port all output
    127.     ADCON1=0X06;                    //a port all i/o
    128.     TRISB=0X02;                     //rb1 input, others output
    129.     OPTION=0X00;                    //open b port internal pull high.
    130.     PORTA=0XFF;              
    131.     PORTD=0XFF;                     //clear all display
    132.    }
    133. [/i][/i]
     
  4. masoodiqbal

    Thread Starter New Member

    Mar 8, 2009
    3
    0
    I AM VERY THANKFULL TO YOU FOR GIVING GUIDE LINE.
     
Loading...