Advertisement
Guest User

Blue_Key

a guest
Sep 17th, 2008
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.61 KB | None | 0 0
  1. //experiment purpose: familiar how to use clock chip DS1302      
  2. //first set time and date:second(08),minute(58),hour(05),day(13),month(01),year(55)  
  3. //six LED display time and date,default display time,differentiate hour and min,min and second with decimal dot
  4. //when press RB1 not relax,display switch to date.    
  5. //hardware request: SW S9,S5,S6 all ON,S1 the seventh bit ON,the other bits OFF,the other SWS OFF.
  6.  
  7.  
  8.  
  9.  
  10. #include<pic.h>                        //include MCU head file    
  11. __CONFIG(0x1832);      
  12. //__CONFIG _DEBUG_OFF&_CP_ALL&_WRT_HALF&_CPD_ON&_LVP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_HS_OSC    
  13.  
  14. #define i_o   RB4                      //1302I_O          
  15. #define sclk  RB0                      //1302 clock        
  16. #define rst   RB5                      //1302 enable bit  
  17.  
  18. // unsigned char time_rx;
  19.   unsigned char time_rx @ 0x30;        //define receive reg.
  20.   static volatile bit time_rx7   @ (unsigned)&time_rx*8+7;   //receive reg highest.
  21. //static volatile bit temp0     @ (unsigned)&temp*8+0;
  22.  
  23. void port_init();                      //port initilize subroutine.
  24. void ds1302_init();                    //DS1302 initilize subroutine.
  25. void set_time();                       //set time subroutine.
  26. void get_time();                       //get time subroutine.
  27. void display();                        //display subroutine.
  28. void time_write_1(unsigned char time_tx);    //write one byte subroutine.
  29. unsigned char  time_read_1();          //read one byte subroutine.
  30. void delay();                          //delay subroutine.
  31. //define the time: sec,min,hour,day,month,week,year,control word.
  32. const char table[]={0x00,0x58,0x12,0x8,0x3,0x06,0x06,0x00};
  33. //define the read time and date save table.
  34. char table1[7];
  35. //define the display code for display 0-9
  36. const char table2[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
  37.  
  38. //----------------------------------------------
  39. //main routine.
  40. void main()
  41.   {
  42.      port_init();                     //port initilize.
  43.      ds1302_init();                   //DS1302 initilize.
  44.      set_time();                      //set time
  45.      while(1)
  46.         {
  47.           get_time();                
  48.           display();                  
  49.         }
  50.   }
  51.  
  52. //---------------------------------------------
  53. //DS1302 initilize.
  54. void ds1302_init()
  55.   {
  56.    sclk=0;                            //pull low clock
  57.    rst =0;                            //reset DS1302
  58.    rst=1;                             //enable DS1302
  59.    time_write_1(0x8e);                //send control command
  60.    time_write_1(0);                   //enable write DS1302
  61.    rst=0;                             //reset
  62.   }
  63.  
  64. //---------------------------------------------
  65. //set time.
  66. void set_time()
  67.   {
  68.    int i;                             //define the loop counter.
  69.    rst=1;                             //enable DS1302
  70.    time_write_1(0xbe);                //
  71.    for(i=0;i<8;i++)                   //continue to write 8 bytes.
  72.      {
  73.        time_write_1(table[i]);        //write one byte
  74.      }
  75.    rst=0;                             //reset
  76.    }
  77.  
  78. //---------------------------------------------
  79. //get time.
  80. void get_time()
  81.  {
  82.    int i;                             //set loop counter.
  83.    rst=1;                             //enable DS1302
  84.    time_write_1(0xbf);                //
  85.    for(i=0;i<7;i++)                   //continue to read 7 bytes.
  86.      {
  87.         table1[i]=time_read_1();      //
  88.      }
  89.     rst=0;                            //reset DS1302
  90.  }
  91.  
  92. //--------------------------------------------
  93. //write one byte
  94. void time_write_1(unsigned char time_tx)
  95.  {
  96.     int j;                            //set the loop counter.
  97.     for(j=0;j<8;j++)                  //continue to write 8bit
  98.       {
  99.         i_o=0;                        //
  100.         sclk=0;                       //pull low clk
  101.         if(time_tx&0x01)              //judge the send bit is 0 or 1.
  102.           {
  103.             i_o=1;                    //is 1
  104.           }
  105.         time_tx=time_tx>>1;           //rotate right 1 bit.
  106.         sclk=1;                       //pull high clk
  107.        }
  108.       sclk=0;                         //finished 1 byte,pull low clk
  109.   }
  110.  
  111. //---------------------------------------------
  112. //read one byte.
  113. unsigned char time_read_1()
  114.  {
  115.    int j;                            //set the loop counter.  
  116.    TRISB4=1;                         //continue to write 8bit
  117.    for(j=0;j<8;j++)                  
  118.       {
  119.         sclk=0;                       //pull low clk                  
  120.         time_rx=time_rx>>1;           //judge the send bit is 0 or 1.  
  121.         time_rx7=i_o;                //put the received bit into the reg's highest.
  122.        sclk=1;                       //pull high clk                
  123.       }                                                              
  124.     TRISB4=0;                        //finished 1 byte,pull low clk  
  125.     sclk=0;                          
  126.     return(time_rx);                
  127.   }
  128.  
  129. //--------------------------------------------
  130. //pin define func
  131. void port_init()
  132.   {
  133.     TRISA=0x00;                     //a port all output
  134.     TRISD=0X00;                     //d port all output
  135.     ADCON1=0X06;                    //a port all i/o
  136.     TRISB=0X02;                     //rb1 input, others output
  137.     OPTION=0X00;                    //open b port internal pull high.
  138.     PORTA=0XFF;              
  139.     PORTD=0XFF;                     //clear all display
  140.    }
  141.  
  142. //-------------------------------------------
  143. //display
  144. void display()
  145.    {
  146.      int i;                         //define table variable.
  147.      if(RB1==0)                     //judge rb1.
  148.        {
  149.           table1[0]=table1[3];    
  150.           table1[1]=table1[4];
  151.           table1[2]=table1[6];
  152.        }
  153.      i=table1[0]&0x0f;             //sec's low.
  154.      PORTD=table2[i];              //send to port d.
  155.      PORTA=0x1f;                   //light on sec's low.
  156.      delay();                      //delay some times.
  157.      i=table1[0]&0xf0;             //sec's high
  158.      i=i>>4;                       //rotate right for 4 bits.
  159.      PORTD=table2[i];              //send to port d.    
  160.      PORTA=0x2f;                   //light on sec's high.
  161.      delay();                      //delay some times.  
  162.      
  163.      i=table1[1]&0x0f;             //min's low.                
  164.      PORTD=table2[i]&0x7f;         //send to port d.            
  165.      PORTA=0x37;                   //light on min's low.        
  166.      delay();                      //delay some times.          
  167.      i=table1[1]&0xf0;             //min's high                
  168.      i=i>>4;                       //rotate right for 4 bits.  
  169.      PORTD=table2[i];              //send to port d.            
  170.      PORTA=0x3b;                   //light on min's high.      
  171.      delay();                      //delay some times.          
  172.  
  173.      i=table1[2]&0x0f;             //hour's low.                
  174.      PORTD=table2[i]&0x7f;         //send to port d.            
  175.      PORTA=0x3d;                   //light on hour's low.        
  176.      delay();                      //delay some times.          
  177.      i=table1[2]&0xf0;             //hour's high                
  178.      i=i>>4;                       //rotate right for 4 bits.  
  179.      PORTD=table2[i];              //send to port d.            
  180.      PORTA=0x3e;                   //light on hour's high.      
  181.      delay();                      //delay some times.          
  182.    }
  183.  
  184. //------------------------------------------------------------------
  185. //delay
  186. void  delay()              //
  187.     {
  188.      int i;                 //define variable
  189.      for(i=0x64;i--;);     //delay
  190.     }
  191.  
  192.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement