kaoapithepp

Untitled

Feb 18th, 2021
593
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. i2c_start();
  2.  
  3. // i2c_write(100|0);
  4. // i2c_write(5); //set pointer
  5. // i2c_write(50);
  6.  
  7. /* bitwise นับไป 8bit หลัง 8bit ไปคือ byte บน
  8. เลข 1000 จะมี binary เป็น 0011 1110 1000 -> 0011 จะเป็น byte บน = 3 ใน decimal
  9. และ 1110 1000 = 232 ใน decimal*/
  10.  
  11. i2c_write(100|0);
  12. i2c_write(5);
  13. // i2c_write(/*byte bon*/);
  14. // i2c_write(/*byte lang*/);
  15. i2c_write(3);
  16. i2c_write(232);
  17.  
  18. i2c_stop();
  19.  
  20. /*Lab3.2อ่านค่าจาก slave*/
  21. i2c_write(addr | 0); //master จะเขียนไปยัง slave
  22. i2c_write(5);
  23. i2c_start();
  24. i2c_write(100 | 1); //อ่านค่าจาก slave
  25.  
  26. returnValue = i2c_read(0);
  27.  
  28. i2c_stop();
  29.  
  30. int8 value[7], hour ,min , sec ,date ,month, year ;
  31.    char day[4];
  32.    while(1) {
  33.        
  34.       i2c_start();
  35.       i2c_write(0xD0 | 0);// Clock module address = 0xD4
  36.      
  37.       i2c_write(0);          // indicate we want to read from register address 6 (year)
  38.       i2c_start();           // restart i2c
  39.       i2c_write(0xD0 | 1);   // send address with bit 0 = 1 indicating that we want to read from slave
  40.       value[2] = i2c_read();   // read one byte. Note parameter 0
  41.       value[1] = i2c_read();
  42.       value[0] = i2c_read();
  43.       value[3] = i2c_read();
  44.       value[4] = i2c_read();
  45.       value[5] = i2c_read();
  46.       value[6] = i2c_read(0);
  47.       i2c_stop();            // end i2c operation
  48.       hour = (value[0] >> 4) * 10  + (value[0] & 15);
  49.       min = (value[1] >> 4) * 10 + (value[1] & 15 );
  50.       sec = (value[2] >> 4) * 10 + (value[2] & 15 );
  51.       switch(value[3]){
  52.          case 1:
  53.          day = "Mon";
  54.          break;
  55.          case 2:
  56.          day = "Tue";
  57.          break;
  58.          case 3:
  59.          day = "Wed";
  60.          break;
  61.          case 4:
  62.          day = "Thu";
  63.          break;
  64.          case 5:
  65.          day = "Fri";
  66.          break;
  67.          case 6:
  68.          day = "Sat";
  69.          break;
  70.          case 0:
  71.          day = "Sun";
  72.          break;
  73.       }
  74.       date = (value[4] >> 4) * 10 + (value[4] & 15 );
  75.       month = (value[5] >> 4) * 10 + (value[5] & 15 );
  76.       year = (value[6] >> 4)*10 + (value[6] & 0xf);  // transform the year format
  77.  
  78.       printf("%02d:%02d:%02d %s %02u/%02u/%02u\r\n",hour,min,sec,day,date,month, year);
  79.       delay_ms(1000);
  80.  
  81.  
RAW Paste Data