Advertisement
Guest User

carlhako

a guest
Mar 23rd, 2010
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <avr/io.h>
  4.  
  5. #define CE  PD4     //definitions
  6. #define SCLK  PD2
  7. #define IO  PD3
  8.  
  9. #define READ 1
  10. #define WRITE 0
  11.  
  12. // 0x80 seconds write
  13. // 0x81 seconds read
  14. // 0x82 minutes write
  15. // 0x83 minutes read
  16. // 0x84 hour write
  17. // 0x85 hour read
  18. // 0x86 date write
  19. // 0x87 date read
  20. // 0x88 month write
  21. // 0x89 month read
  22. // 0x90 trick charger write
  23. // 0x91 trickle charger read
  24. // 0x8A day write
  25. // 0x8B day read
  26. // 0x8C day write
  27. // 0x8D day read
  28. // 0x8E write protect bit write
  29.  
  30. void reset(void);       //define the functions
  31. void send(unsigned char);
  32. unsigned char read(void);
  33. void write_byte(unsigned char,unsigned char);
  34. unsigned char read_byte(unsigned char);
  35. unsigned char seconds(unsigned char,unsigned char);
  36. unsigned char minutes(unsigned char,unsigned char);
  37. unsigned char hours(unsigned char, unsigned char);
  38. unsigned char get_day(unsigned char, unsigned char);
  39. void rtc_init(void);
  40. unsigned char get_seconds(void);
  41.  
  42. void trickle() {
  43.     reset();
  44.     send(0x90); // tell ds1302 what we want to read/write
  45.     send(0xA9); // turn on trickle charger, 1 diode, 2kohm resistor.
  46. }
  47.  
  48. void rtc_init() {
  49.     unsigned char tmp; // temp variable
  50.     unsigned char tmp2; // temp variable
  51.     reset();
  52.     send(0x8E); // tell ds1302 what we want to read/write
  53.     send(0x00); // turn off write protect
  54.    
  55.     reset(); // read seconds then write them back. this gets the clock ticking
  56.     send(0x81);
  57.     tmp = read();
  58.     //tmp &= ~(1<<7);
  59.     reset();
  60.     send(0x80);
  61.     send(tmp);
  62.    
  63.     reset(); // read hours and make sure mode is on 24hour
  64.     send(0x85);
  65.     tmp = read();
  66.     tmp2 = (tmp & (1<<7));
  67.     if (tmp2) {
  68.         reset();
  69.         send(0x84);
  70.         tmp &= ~(1<<7);
  71.         send(tmp);
  72.     }
  73.    
  74. }
  75.  
  76.  
  77. unsigned char write_seconds(unsigned char BYTE) {
  78.     reset(); // reset pins ready for communication
  79.     send(0x80); //tell ds1302 what we want to read/write
  80.     if (BYTE > 0x09) {
  81.         unsigned char tmp; // temp variable
  82.         tmp = BYTE / 0x0A; // copy tens of minutes to tmp
  83.         BYTE %= 0x0A; // remove tens of minutes from BYTE
  84.         BYTE |= (tmp<<4); //move tens of minutes across 4 bytes
  85.     }
  86.     send(BYTE); // write BYTE
  87. }
  88.  
  89. unsigned char read_seconds(unsigned char units) {
  90.     reset(); // reset pins ready for communication
  91.     unsigned char tmp; // temp variable
  92.     send(0x81); // tell ds1302 what we want to read minutes
  93.     tmp = read(); // read byte previously requested
  94.     if (units == 1) tmp = (tmp & 0x0F); //  grab first 4 bits
  95.     if (units == 10) tmp = ((tmp & 0x70) >> 4); // grab bits 5-7 and shift right
  96.     return tmp;
  97. }
  98.  
  99. unsigned char write_minutes(unsigned char BYTE) {
  100.     reset(); // reset pins ready for communication
  101.     send(0x82); //tell ds1302 what we want to read/write
  102.     if (BYTE > 0x09) {
  103.         unsigned char tmp; // temp variable
  104.         tmp = BYTE / 0x0A; // copy tens of minutes to tmp
  105.         BYTE %= 0x0A; // remove tens of minutes from BYTE
  106.         BYTE |= (tmp<<4); //move tens of minutes across 4 bytes
  107.     }
  108.     send(BYTE); // write BYTE
  109. }
  110.  
  111. unsigned char read_minutes(unsigned char units) {
  112.     reset(); // reset pins ready for communication
  113.     unsigned char tmp; // temp variable
  114.     send(0x83); // tell ds1302 what we want to read minutes
  115.     tmp = read(); // read byte previously requested
  116.     if (units == 1) tmp = (tmp & 0x0F); //  grab first 4 bits
  117.     if (units == 10) tmp = ((tmp & 0x70) >> 4); // grab bits 5-7 and shift right
  118.     return tmp;
  119. }
  120.  
  121. void write_hours(unsigned char BYTE) {
  122.     reset(); // reset pins ready for communication
  123.     send(0x84); //tell ds1302 what we want to read/write
  124.     if (BYTE > 0x09) {
  125.         unsigned char tmp; // temp variable
  126.         tmp = BYTE / 0x0A; // copy tens of minutes to tmp
  127.         BYTE %= 0x0A; // remove tens of minutes from BYTE
  128.         BYTE |= (tmp<<4); //move tens of minutes across 4 bytes
  129.     }
  130.     send(BYTE); // write BYTE
  131. }
  132.  
  133. unsigned char read_hours(unsigned char units) {
  134.  
  135.     reset(); // reset pins ready for communication
  136.     unsigned char tmp; // temp variable
  137.     send(0x85); // tell ds1302 what we want to read minutes
  138.     tmp = read(); // read byte previously requested
  139.     if (units == 1) tmp = (tmp & 0x0F); //  grab first 4 bits
  140.     if (units == 10) tmp = ((tmp & 0x70) >> 4); // grab bits 5-7 and shift right
  141.     return tmp;
  142. }
  143.  
  144. unsigned char day(unsigned char RW, unsigned char BYTE) {
  145.     reset(); // reset pins ready for communication
  146.     send(0x8D); // send ds1302 request for data
  147.     if (RW) {
  148.         unsigned char day; // temp variable
  149.         day = read(); // read byte previously requested
  150.         day = (day & 0x07); // grab first 3 bits
  151.         return day;
  152.     } else {
  153.         send(BYTE);
  154.     }
  155. }
  156.  
  157. void send(unsigned char W_Byte) //writes the W_Byte to the DS1302
  158.  
  159. {
  160.     unsigned char i;
  161.     //DDRB = 0xFF;
  162.     DDRD |= ( (1<<IO) | (1<<SCLK) | (1<<CE) ); // set all 3 pins to output
  163.  
  164.     for(i = 0; i < 8; i++)
  165.     {
  166.         //cbi(PORTD,io);
  167.         PORTD &= ~(1<<IO); // set IO to high
  168.         if(W_Byte &0x01) // compare bit 0, if high set IO to high
  169.         {
  170.             //sbi(PORTD,io);
  171.             PORTD |= (1<<IO); // set IO to high
  172.         }
  173.         //sbi(PORTD,clk);
  174.         PORTD |= (1<<SCLK);
  175.         //delay_ms(1);
  176.         //cbi(PORTD,clk);
  177.         PORTD &= ~(1<<SCLK);
  178.         //delay_ms(1);
  179.         W_Byte >>=1;
  180.     }
  181.    
  182. }
  183.  
  184. unsigned char read_byte(unsigned char w_byte)   //read the byte with register w_byte
  185. {
  186.     unsigned char temp;
  187.     reset();
  188.     send(w_byte);
  189.     temp = read();
  190.     //cbi(PORTD,rst);
  191.     //cbi(PORTD,clk);
  192.     PORTD &= ~(1<<CE);
  193.     PORTD &= ~(1<<SCLK);
  194.     return temp;
  195. }
  196.  
  197. void reset(void)        //sets the pins to begin and end the ds1302 communication
  198. {
  199.     DDRD |= (1<<CE);  // set as output
  200.     PORTD &= ~(1<<SCLK); // set SCLK low
  201.     PORTD &= ~(1<<CE); // set CE low
  202.     PORTD |= (1<<CE); // set CE high
  203. }
  204.  
  205. unsigned char read(void)        //reads the ds1302 reply
  206. {
  207.     unsigned char i;
  208.     unsigned char R_Byte, TmpByte;
  209.     R_Byte = 0x00;
  210.    
  211.     // initiate communication
  212.     DDRD |= (1<<IO); //set IO to output
  213.     PORTD &= ~(1<<IO); //set IO low
  214.     DDRD &= ~(1<<IO); // set IO to input
  215.  
  216.     for(i = 0; i < 8; i++) //loop 8 times to read whole byte
  217.     {
  218.         TmpByte = 0; // clear TmpByte
  219.         //if(bit_is_set(PINB,io))
  220.         if (PIND & (1<<IO)) // if IO is high set TmpByte to 1
  221.             TmpByte = 1;
  222.        
  223.         TmpByte <<= 7; // shift TmpByte to 7th
  224.         R_Byte >>= 1; // shift all of R_Byte to the right 1 to make room
  225.         R_Byte |= TmpByte; // |= to copy byte across if its set to 1
  226.  
  227.         PORTD |= (1<<SCLK); //set SCLK to high
  228.         PORTD &= ~(1<<SCLK); // set SCLK to low
  229.     }
  230.     return R_Byte;
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement