Advertisement
Guest User

Blue_Key

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