at24c08_stc8h3k.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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
  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. __CODE int8_t dat[20] = {0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB};
  26. void I2C_Init(void)
  27. {
  28. // Master mode
  29. I2C_SetWorkMode(I2C_WorkMode_Master);
  30. /**
  31. * I2C clock = FOSC / 2 / (__prescaler__ * 2 + 4)
  32. */
  33. I2C_SetClockPrescaler(0x3F);
  34. // Switch alternative port
  35. I2C_SetPort(I2C_AlterPort_P32_P33);
  36. // Start I2C
  37. I2C_SetEnabled(HAL_State_ON);
  38. }
  39. void GPIO_Init(void)
  40. {
  41. // SDA
  42. GPIO_P3_SetMode(GPIO_Pin_3, GPIO_Mode_InOut_QBD);
  43. // SCL
  44. GPIO_P3_SetMode(GPIO_Pin_2, GPIO_Mode_Output_PP);
  45. }
  46. int main(void)
  47. {
  48. uint8_t offset, i, buf[20];
  49. SYS_SetClock();
  50. // UART1 configuration: baud 115200 with Timer2, 1T mode, no interrupt
  51. UART1_Config8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200);
  52. GPIO_Init();
  53. I2C_Init();
  54. I2C_Write(AT24C_ADDR, 0x00, dat, 12);
  55. while(1)
  56. {
  57. for (offset = 0; offset < 4; offset++)
  58. {
  59. I2C_Read(AT24C_ADDR, offset, buf, 6);
  60. for (i = 0; i < 6; i++)
  61. {
  62. UART1_TxHex(buf[i]);
  63. UART1_TxChar(':');
  64. }
  65. UART1_TxString(" ");
  66. SYS_Delay(10);
  67. }
  68. UART1_TxString("\r\n");
  69. SYS_Delay(1000);
  70. }
  71. }