at24c08_nonlib.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright 2021 IOsetting <iosetting(at)outlook.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /***
  15. * Demo: AT24C08 write and read with raw registers
  16. * Board: STC8H3K32
  17. *
  18. * P32 -> SCL
  19. * P33 -> SDA
  20. * GND -> GND, A2
  21. * 3.3V -> VCC
  22. */
  23. #include "fw_hal.h"
  24. #define AT24C_ADDR 0xA0
  25. void Wait()
  26. {
  27. while (!(I2CMSST & 0x40));
  28. I2CMSST &= ~0x40;
  29. }
  30. void Start()
  31. {
  32. I2CMSCR = 0x01;
  33. Wait();
  34. }
  35. void SendData(char dat)
  36. {
  37. I2CTXD = dat;
  38. I2CMSCR = 0x02;
  39. Wait();
  40. }
  41. void RecvACK()
  42. {
  43. I2CMSCR = 0x03;
  44. Wait();
  45. }
  46. char RecvData()
  47. {
  48. I2CMSCR = 0x04;
  49. Wait();
  50. return I2CRXD;
  51. }
  52. void SendACK()
  53. {
  54. I2CMSST = 0x00;
  55. I2CMSCR = 0x05;
  56. Wait();
  57. }
  58. void SendNAK()
  59. {
  60. I2CMSST = 0x01;
  61. I2CMSCR = 0x05;
  62. Wait();
  63. }
  64. void Stop()
  65. {
  66. I2CMSCR = 0x06;
  67. Wait();
  68. }
  69. void I2C_Init(void)
  70. {
  71. // Master mode
  72. I2C_SetWorkMode(I2C_WorkMode_Master);
  73. /**
  74. * I2C clock = FOSC / 2 / (__prescaler__ * 2 + 4)
  75. */
  76. I2C_SetClockPrescaler(0x20);
  77. // Switch alternative port
  78. I2C_SetPort(I2C_AlterPort_P32_P33);
  79. // Start I2C
  80. I2C_SetEnabled(HAL_State_ON);
  81. }
  82. void GPIO_Init(void)
  83. {
  84. // SDA
  85. GPIO_P3_SetMode(GPIO_Pin_3, GPIO_Mode_InOut_QBD);
  86. // SCL
  87. GPIO_P3_SetMode(GPIO_Pin_2, GPIO_Mode_Output_PP);
  88. }
  89. int main(void)
  90. {
  91. uint8_t i, buf[20];
  92. SYS_SetClock();
  93. // UART1 configuration: baud 115200 with Timer2, 1T mode, no interrupt
  94. UART1_Config8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200);
  95. GPIO_Init();
  96. I2C_Init();
  97. P_SW2 |= 0x80;
  98. Start();
  99. SendData(AT24C_ADDR);
  100. RecvACK();
  101. SendData(0x00);
  102. RecvACK();
  103. SendData(0xAF);
  104. RecvACK();
  105. SendData(0xBF);
  106. RecvACK();
  107. SendData(0xCF);
  108. RecvACK();
  109. SendData(0xDF);
  110. RecvACK();
  111. SendData(0xEF);
  112. RecvACK();
  113. SendData(0xFF);
  114. RecvACK();
  115. Stop();
  116. SYS_Delay(50);
  117. while (1)
  118. {
  119. Start();
  120. SendData(AT24C_ADDR);
  121. RecvACK();
  122. SendData(0x00);
  123. RecvACK();
  124. Start();
  125. SendData(AT24C_ADDR|0x01);
  126. RecvACK();
  127. buf[0] = RecvData();
  128. SendACK();
  129. buf[1] = RecvData();
  130. SendNAK();
  131. Stop();
  132. UART1_TxHex(buf[0]);
  133. UART1_TxChar(':');
  134. UART1_TxHex(buf[1]);
  135. UART1_TxChar(' ');
  136. Start();
  137. SendData(AT24C_ADDR);
  138. RecvACK();
  139. SendData(0x01);
  140. RecvACK();
  141. Start();
  142. SendData(AT24C_ADDR|0x01);
  143. RecvACK();
  144. buf[0] = RecvData();
  145. SendACK();
  146. buf[1] = RecvData();
  147. SendNAK();
  148. Stop();
  149. UART1_TxHex(buf[0]);
  150. UART1_TxChar(':');
  151. UART1_TxHex(buf[1]);
  152. UART1_TxChar(' ');
  153. Start();
  154. SendData(AT24C_ADDR);
  155. RecvACK();
  156. SendData(0x02);
  157. RecvACK();
  158. Start();
  159. SendData(AT24C_ADDR|0x01);
  160. RecvACK();
  161. buf[0] = RecvData();
  162. SendACK();
  163. buf[1] = RecvData();
  164. SendNAK();
  165. Stop();
  166. UART1_TxHex(buf[0]);
  167. UART1_TxChar(':');
  168. UART1_TxHex(buf[1]);
  169. UART1_TxChar(' ');
  170. SYS_Delay(200);
  171. }
  172. }