SHARE
TWEET

Untitled

a guest Dec 9th, 2011 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # From: http://web.icedsl.hu/enicke/mcprtc/mcpcode.html
  2.  
  3. #include <Wire.h>
  4.  
  5. void setup()
  6. {
  7.    Serial.begin(9600);
  8.    Wire.begin();
  9. }
  10.  
  11. void loop(){
  12.  
  13.   WriteRTCByte(0,0);       //STOP RTC
  14.   WriteRTCByte(1,0x18);    //MINUTE=18
  15.   WriteRTCByte(2,0x08);    //HOUR=8
  16.   WriteRTCByte(3,0x09);    //DAY=1(MONDAY) AND VBAT=1
  17.   WriteRTCByte(4,0x28);    //DATE=28
  18.   WriteRTCByte(5,0x02);    //MONTH=2
  19.   WriteRTCByte(6,0x11);    //YEAR=11
  20.   WriteRTCByte(0,0x80);    //START RTC, SECOND=00
  21.   delay(100);
  22.  
  23.   while(1){
  24.     Serial.print("20");    //year beginning with 20xx
  25.     DisplayRTCData(6,8);
  26.     Serial.print(".");
  27.     DisplayRTCData(5,5);
  28.     Serial.print(".");
  29.     DisplayRTCData(4,6);    
  30.     Serial.print(" ");
  31.     DisplayRTCData(2,6);
  32.     Serial.print(":");
  33.     DisplayRTCData(1,7);
  34.     Serial.print(":");
  35.     DisplayRTCData(0,7);
  36.     Serial.println();
  37.    
  38.     delay(1000);
  39.   }  
  40. }
  41.  
  42. unsigned char ReadRTCByte(const unsigned char adr){
  43.   unsigned char data;
  44.  
  45.   Wire.beginTransmission(0x6f);
  46.   Wire.send(adr);
  47.   Wire.endTransmission();
  48.  
  49.   Wire.requestFrom(0x6f,1);
  50.   while (Wire.available()) data=Wire.receive();
  51.  
  52.   return data;
  53. }
  54.  
  55. void WriteRTCByte(const unsigned char adr, const unsigned char data){
  56.   Wire.beginTransmission(0x6f);
  57.   Wire.send(adr);
  58.   Wire.send(data);
  59.   Wire.endTransmission();
  60. }
  61.  
  62. void DisplayRTCData(const unsigned char adr, const unsigned char validbits){
  63.   unsigned char data;
  64.  
  65.   data=ReadRTCByte(adr);
  66.   data=data & 0xff>>(8-validbits);
  67.   if (data<10) Serial.print("0");  //leading zero
  68.   Serial.print(data,HEX);
  69. }
  70.  
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top