Arusekk

ARDUINO WATCH

Jun 28th, 2016
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.87 KB | None | 0 0
  1. /*
  2. This Code has extra features
  3. including a XY positioning function on Display
  4. and a Line Draw function on Nokia 3310 LCD
  5. It is modded from the original
  6. */
  7. #include "Wire.h"
  8. #define DS3231_I2C_ADDRESS 0x68
  9.  
  10. // Convert normal decimal numbers to binary coded decimal
  11. byte decToBcd(byte val)
  12. {
  13.   return( (val/10*16) + (val%10) );
  14. }
  15.  
  16. // Convert binary coded decimal to normal decimal numbers
  17. byte bcdToDec(byte val)
  18. {
  19.   return( (val/16*10) + (val%16) );
  20. }
  21.  
  22. // Mods by Jim Park
  23. // jim(^dOt^)buzz(^aT^)gmail(^dOt^)com
  24. // hope it works for you
  25. #define PIN_SCE   7  // LCD CS  .... Pin 3
  26. #define PIN_RESET 6  // LCD RST .... Pin 1
  27. #define PIN_DC    5  // LCD Dat/Com. Pin 5
  28. #define PIN_SDIN  4  // LCD SPIDat . Pin 6
  29. #define PIN_SCLK  3  // LCD SPIClk . Pin 4
  30.                      // LCD Gnd .... Pin 2
  31.                      // LCD Vcc .... Pin 8
  32.                      // LCD Vlcd ... Pin 7
  33.  
  34. #define LCD_C     LOW
  35. #define LCD_D     HIGH
  36.  
  37. #define LCD_X     84
  38. #define LCD_Y     48
  39. #define LCD_CMD   0
  40.  
  41. int a = 0;
  42.  
  43. static const byte ASCII[][5] =
  44. {
  45.  {0x00, 0x00, 0x00, 0x00, 0x00} // 20  
  46. ,{0x00, 0x00, 0x5f, 0x00, 0x00} // 21 !
  47. ,{0x00, 0x07, 0x00, 0x07, 0x00} // 22 "
  48. ,{0x14, 0x7f, 0x14, 0x7f, 0x14} // 23 #
  49. ,{0x24, 0x2a, 0x7f, 0x2a, 0x12} // 24 $
  50. ,{0x23, 0x13, 0x08, 0x64, 0x62} // 25 %
  51. ,{0x36, 0x49, 0x55, 0x22, 0x50} // 26 &
  52. ,{0x00, 0x05, 0x03, 0x00, 0x00} // 27 '
  53. ,{0x00, 0x1c, 0x22, 0x41, 0x00} // 28 (
  54. ,{0x00, 0x41, 0x22, 0x1c, 0x00} // 29 )
  55. ,{0x14, 0x08, 0x3e, 0x08, 0x14} // 2a *
  56. ,{0x08, 0x08, 0x3e, 0x08, 0x08} // 2b +
  57. ,{0x00, 0x50, 0x30, 0x00, 0x00} // 2c ,
  58. ,{0x08, 0x08, 0x08, 0x08, 0x08} // 2d -
  59. ,{0x00, 0x60, 0x60, 0x00, 0x00} // 2e .
  60. ,{0x20, 0x10, 0x08, 0x04, 0x02} // 2f /
  61. ,{0x3e, 0x51, 0x49, 0x45, 0x3e} // 30 0
  62. ,{0x00, 0x42, 0x7f, 0x40, 0x00} // 31 1
  63. ,{0x42, 0x61, 0x51, 0x49, 0x46} // 32 2
  64. ,{0x21, 0x41, 0x45, 0x4b, 0x31} // 33 3
  65. ,{0x18, 0x14, 0x12, 0x7f, 0x10} // 34 4
  66. ,{0x27, 0x45, 0x45, 0x45, 0x39} // 35 5
  67. ,{0x3c, 0x4a, 0x49, 0x49, 0x30} // 36 6
  68. ,{0x01, 0x71, 0x09, 0x05, 0x03} // 37 7
  69. ,{0x36, 0x49, 0x49, 0x49, 0x36} // 38 8
  70. ,{0x06, 0x49, 0x49, 0x29, 0x1e} // 39 9
  71. ,{0x00, 0x36, 0x36, 0x00, 0x00} // 3a :
  72. ,{0x00, 0x56, 0x36, 0x00, 0x00} // 3b ;
  73. ,{0x08, 0x14, 0x22, 0x41, 0x00} // 3c <
  74. ,{0x14, 0x14, 0x14, 0x14, 0x14} // 3d =
  75. ,{0x00, 0x41, 0x22, 0x14, 0x08} // 3e >
  76. ,{0x02, 0x01, 0x51, 0x09, 0x06} // 3f ?
  77. ,{0x32, 0x49, 0x79, 0x41, 0x3e} // 40 @
  78. ,{0x7e, 0x11, 0x11, 0x11, 0x7e} // 41 A
  79. ,{0x7f, 0x49, 0x49, 0x49, 0x36} // 42 B
  80. ,{0x3e, 0x41, 0x41, 0x41, 0x22} // 43 C
  81. ,{0x7f, 0x41, 0x41, 0x22, 0x1c} // 44 D
  82. ,{0x7f, 0x49, 0x49, 0x49, 0x41} // 45 E
  83. ,{0x7f, 0x09, 0x09, 0x09, 0x01} // 46 F
  84. ,{0x3e, 0x41, 0x49, 0x49, 0x7a} // 47 G
  85. ,{0x7f, 0x08, 0x08, 0x08, 0x7f} // 48 H
  86. ,{0x00, 0x41, 0x7f, 0x41, 0x00} // 49 I
  87. ,{0x20, 0x40, 0x41, 0x3f, 0x01} // 4a J
  88. ,{0x7f, 0x08, 0x14, 0x22, 0x41} // 4b K
  89. ,{0x7f, 0x40, 0x40, 0x40, 0x40} // 4c L
  90. ,{0x7f, 0x02, 0x0c, 0x02, 0x7f} // 4d M
  91. ,{0x7f, 0x04, 0x08, 0x10, 0x7f} // 4e N
  92. ,{0x3e, 0x41, 0x41, 0x41, 0x3e} // 4f O
  93. ,{0x7f, 0x09, 0x09, 0x09, 0x06} // 50 P
  94. ,{0x3e, 0x41, 0x51, 0x21, 0x5e} // 51 Q
  95. ,{0x7f, 0x09, 0x19, 0x29, 0x46} // 52 R
  96. ,{0x46, 0x49, 0x49, 0x49, 0x31} // 53 S
  97. ,{0x01, 0x01, 0x7f, 0x01, 0x01} // 54 T
  98. ,{0x3f, 0x40, 0x40, 0x40, 0x3f} // 55 U
  99. ,{0x1f, 0x20, 0x40, 0x20, 0x1f} // 56 V
  100. ,{0x3f, 0x40, 0x38, 0x40, 0x3f} // 57 W
  101. ,{0x63, 0x14, 0x08, 0x14, 0x63} // 58 X
  102. ,{0x07, 0x08, 0x70, 0x08, 0x07} // 59 Y
  103. ,{0x61, 0x51, 0x49, 0x45, 0x43} // 5a Z
  104. ,{0x00, 0x7f, 0x41, 0x41, 0x00} // 5b [
  105. ,{0x02, 0x04, 0x08, 0x10, 0x20} // 5c bekslesz
  106. ,{0x00, 0x41, 0x41, 0x7f, 0x00} // 5d ]
  107. ,{0x04, 0x02, 0x01, 0x02, 0x04} // 5e ^
  108. ,{0x40, 0x40, 0x40, 0x40, 0x40} // 5f _
  109. ,{0x00, 0x01, 0x02, 0x04, 0x00} // 60 `
  110. ,{0x20, 0x54, 0x54, 0x54, 0x78} // 61 a
  111. ,{0x7f, 0x48, 0x44, 0x44, 0x38} // 62 b
  112. ,{0x38, 0x44, 0x44, 0x44, 0x20} // 63 c
  113. ,{0x38, 0x44, 0x44, 0x48, 0x7f} // 64 d
  114. ,{0x38, 0x54, 0x54, 0x54, 0x18} // 65 e
  115. ,{0x08, 0x7e, 0x09, 0x01, 0x02} // 66 f
  116. ,{0x0c, 0x52, 0x52, 0x52, 0x3e} // 67 g
  117. ,{0x7f, 0x08, 0x04, 0x04, 0x78} // 68 h
  118. ,{0x00, 0x44, 0x7d, 0x40, 0x00} // 69 i
  119. ,{0x20, 0x40, 0x44, 0x3d, 0x00} // 6a j
  120. ,{0x7f, 0x10, 0x28, 0x44, 0x00} // 6b k
  121. ,{0x00, 0x41, 0x7f, 0x40, 0x00} // 6c l
  122. ,{0x7c, 0x04, 0x18, 0x04, 0x78} // 6d m
  123. ,{0x7c, 0x08, 0x04, 0x04, 0x78} // 6e n
  124. ,{0x38, 0x44, 0x44, 0x44, 0x38} // 6f o
  125. ,{0x7c, 0x14, 0x14, 0x14, 0x08} // 70 p
  126. ,{0x08, 0x14, 0x14, 0x18, 0x7c} // 71 q
  127. ,{0x7c, 0x08, 0x04, 0x04, 0x08} // 72 r
  128. ,{0x48, 0x54, 0x54, 0x54, 0x20} // 73 s
  129. ,{0x04, 0x3f, 0x44, 0x40, 0x20} // 74 t
  130. ,{0x3c, 0x40, 0x40, 0x20, 0x7c} // 75 u
  131. ,{0x1c, 0x20, 0x40, 0x20, 0x1c} // 76 v
  132. ,{0x3c, 0x40, 0x30, 0x40, 0x3c} // 77 w
  133. ,{0x44, 0x28, 0x10, 0x28, 0x44} // 78 x
  134. ,{0x0c, 0x50, 0x50, 0x50, 0x3c} // 79 y
  135. ,{0x44, 0x64, 0x54, 0x4c, 0x44} // 7a z
  136. ,{0x00, 0x08, 0x36, 0x41, 0x00} // 7b {
  137. ,{0x00, 0x00, 0x7f, 0x00, 0x00} // 7c |
  138. ,{0x00, 0x41, 0x36, 0x08, 0x00} // 7d }
  139. ,{0x08, 0x04, 0x08, 0x04, 0x00} // 7e ~
  140. ,{0x00, 0x06, 0x09, 0x09, 0x06} // 7f →
  141. ,{0x10, 0x2a, 0x2a, 0x6a, 0x7c} // 80 ą
  142. ,{0x38, 0x44, 0x46, 0x45, 0x20} // 81 ć
  143. ,{0x1c, 0x2a, 0x2a, 0x6a, 0x4c} // 82 ę
  144. ,{0x00, 0x49, 0x7f, 0x44, 0x00} // 83 ł
  145. ,{0x7c, 0x08, 0x06, 0x05, 0x78} // 84 ń
  146. ,{0x38, 0x44, 0x46, 0x45, 0x38} // 85 ó
  147. ,{0x48, 0x54, 0x56, 0x55, 0x20} // 86 ś
  148. ,{0x44, 0x64, 0x56, 0x4d, 0x44} // 87 ź
  149. ,{0x44, 0x64, 0x55, 0x4c, 0x44} // 88 ż
  150. ,{0x10, 0x08, 0x08, 0x10, 0x08} // 89 ←
  151. };
  152.  
  153.  
  154.  
  155.  
  156. void LcdCharacter(char character)
  157. {
  158.   character -= ' ';
  159.   LcdWrite(LCD_D, 0x00);
  160.   for (int index = 0; index < 5; index++)
  161.     LcdWrite(LCD_D, ASCII[character][index]);
  162.   LcdWrite(LCD_D, 0x00);
  163. }
  164. void LcdNum(byte num)
  165. {
  166.   LcdCharacter('0' + (num>>4));
  167.   LcdCharacter('0' + (num&0xf));
  168. }
  169.  
  170. void LcdClear(void)
  171. {
  172.   for (int index = 0; index < LCD_X * LCD_Y / 8; index++)
  173.     LcdWrite(LCD_D, 0x00);
  174. }
  175.  
  176. void LcdInitialise(void)
  177. {
  178.   pinMode(PIN_SCE,   OUTPUT);
  179.   pinMode(PIN_RESET, OUTPUT);
  180.   pinMode(PIN_DC,    OUTPUT);
  181.   pinMode(PIN_SDIN,  OUTPUT);
  182.   pinMode(PIN_SCLK,  OUTPUT);
  183.  
  184.   digitalWrite(PIN_RESET, LOW);
  185.  // delay(1);
  186.   digitalWrite(PIN_RESET, HIGH);
  187.  
  188.   LcdWrite( LCD_CMD, 0x21 );  // LCD Extended Commands.
  189.   LcdWrite( LCD_CMD, 0xBf );  // Set LCD Vop (Contrast). //B1
  190.   LcdWrite( LCD_CMD, 0x04 );  // Set Temp coefficent. //0x04
  191.   LcdWrite( LCD_CMD, 0x14 );  // LCD bias mode 1:48. //0x13
  192.   LcdWrite( LCD_CMD, 0x0C );  // LCD in normal mode. 0x0d for inverse
  193.   LcdWrite(LCD_C, 0x20);
  194.   LcdWrite(LCD_C, 0x0C);
  195. }
  196.  
  197. void LcdString(const char *characters)
  198. {
  199.   while (*characters)
  200.     LcdCharacter(*characters++);
  201. }
  202.  
  203. void LcdWrite(const byte dc, const byte data)
  204. {
  205.   digitalWrite(PIN_DC, dc);
  206.   digitalWrite(PIN_SCE, LOW);
  207.   shiftOut(PIN_SDIN, PIN_SCLK, MSBFIRST, data);
  208.   digitalWrite(PIN_SCE, HIGH);
  209. }
  210.  
  211. // gotoXY routine to position cursor
  212. // x - range: 0 to 84
  213. // y - range: 0 to 5
  214.  
  215. void gotoXY(const int x, const int y)
  216. {
  217.   LcdWrite( 0, 0x80 | x);  // Column.
  218.   LcdWrite( 0, 0x40 | y);  // Row.  
  219.  
  220. }
  221.  
  222.  
  223.  
  224. void drawLine(void)
  225. {
  226.   unsigned char  j;  
  227.    for(j=0; j<84; j++) // top
  228.   {
  229.           gotoXY (j,0);
  230.     LcdWrite (1,0x01);
  231.   }  
  232.   for(j=0; j<84; j++) //Bottom
  233.   {
  234.           gotoXY (j,5);
  235.     LcdWrite (1,0x80);
  236.   }  
  237.  
  238.   for(j=0; j<6; j++) // Right
  239.   {
  240.           gotoXY (83,j);
  241.     LcdWrite (1,0xff);
  242.   }  
  243.   for(j=0; j<6; j++) // Left
  244.   {
  245.           gotoXY (0,j);
  246.     LcdWrite (1,0xff);
  247.   }
  248.  
  249. }
  250.  
  251.  
  252. void setup(void)
  253. {
  254.  
  255.   LcdInitialise();
  256.   LcdClear();
  257.  
  258.   Wire.begin();
  259.   Serial.begin(9600);
  260.   // set the initial time here:
  261.   // DS3231 seconds, minutes, hours, day, date, month, year
  262.   // setDS3231time(0,58,20,2,27,6,16);
  263. }
  264.  
  265. void loop(void)
  266. {
  267.   // Display some simple character animation
  268.   //
  269.   int a,b;
  270.   char Str[15];
  271.   // Draw a Box
  272.   for(b=1000; b>0; b--){
  273.     // drawLine();
  274.     gotoXY(0,0);
  275.     LcdString("zb@wch:~$ _");
  276.     gotoXY(10,4);
  277.     // Put text in Box
  278.     displayTime(); // display the real-time clock data on the screen
  279.     gotoXY(0,5);
  280.     LcdString("\200\201\202\203\204\205\206\207\210");
  281.  
  282.     delay(500);
  283.   }
  284. }
  285.  
  286. void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
  287. dayOfMonth, byte month, byte year)
  288. {
  289.   // sets time and date data to DS3231
  290.   Wire.beginTransmission(DS3231_I2C_ADDRESS);
  291.   Wire.write(0); // set next input to start at the seconds register
  292.   Wire.write(decToBcd(second)); // set seconds
  293.   Wire.write(decToBcd(minute)); // set minutes
  294.   Wire.write(decToBcd(hour)); // set hours
  295.   Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  296.   Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  297.   Wire.write(decToBcd(month)); // set month
  298.   Wire.write(decToBcd(year)); // set year (0 to 99)
  299.   Wire.endTransmission();
  300. }
  301. void readDS3231time(byte *second,
  302. byte *minute,
  303. byte *hour,
  304. byte *dayOfWeek,
  305. byte *dayOfMonth,
  306. byte *month,
  307. byte *year)
  308. {
  309.   Wire.beginTransmission(DS3231_I2C_ADDRESS);
  310.   Wire.write(0); // set DS3231 register pointer to 00h
  311.   Wire.endTransmission();
  312.   Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  313.   // request seven bytes of data from DS3231 starting from register 00h
  314.   *second = Wire.read() & 0x7f;
  315.   *minute = Wire.read();
  316.   *hour = Wire.read() & 0x3f;
  317.   *dayOfWeek = Wire.read();
  318.   *dayOfMonth = Wire.read();
  319.   *month = Wire.read();
  320.   *year = Wire.read();
  321. }
  322.  
  323. const char* tydzien[] = {
  324.   "niedziela",
  325.   "poniedzia\203ek",
  326.   "wtorek",
  327.   "\206roda",
  328.   "czwartek",
  329.   "pi\200tek",
  330.   "sobota"
  331. };
  332.  
  333. void displayTime()
  334. {
  335.   byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  336.   // retrieve data from DS3231
  337.   readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
  338.   &year);
  339.  
  340.   // send it to the screen
  341.   // convert the byte variable to a decimal number when displayed
  342.   LcdNum(hour);
  343.   LcdCharacter(':');
  344.   LcdNum(minute);
  345.   LcdCharacter(':');
  346.   LcdNum(second);
  347.   gotoXY(1,2);
  348.   LcdNum(dayOfMonth);
  349.   LcdCharacter('/');
  350.   LcdNum(month);
  351.   LcdString("/20");
  352.   LcdNum(year);
  353.   gotoXY(0,3);
  354.   LcdString(tydzien[dayOfWeek-1]);
  355. }
Add Comment
Please, Sign In to add comment