main.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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: RX8025T I2C RTC/TCXO
  16. * Board: STC8H3K32
  17. *
  18. * P32 -> SCL
  19. * P33 -> SDA
  20. * GND -> GND
  21. * 3.3V -> VCC
  22. */
  23. #include "fw_hal.h"
  24. #include "rx8025t.h"
  25. #define BUFF_SIZE 16
  26. __XDATA uint8_t time[BUFF_SIZE];
  27. void I2C_Init(void)
  28. {
  29. // Master mode
  30. I2C_SetWorkMode(I2C_WorkMode_Master);
  31. /**
  32. * I2C clock = FOSC / 2 / (__prescaler__ * 2 + 4)
  33. */
  34. I2C_SetClockPrescaler(0x3F);
  35. // Switch alternative port
  36. I2C_SetPort(I2C_AlterPort_P32_P33);
  37. // Start I2C
  38. I2C_SetEnabled(HAL_State_ON);
  39. }
  40. void GPIO_Init(void)
  41. {
  42. // SDA
  43. GPIO_P3_SetMode(GPIO_Pin_3, GPIO_Mode_InOut_QBD);
  44. // SCL
  45. GPIO_P3_SetMode(GPIO_Pin_2, GPIO_Mode_Output_PP);
  46. }
  47. int main(void)
  48. {
  49. uint8_t i;
  50. SYS_SetClock();
  51. // UART1 configuration: baud 115200 with Timer2, 1T mode, no interrupt
  52. UART1_Config8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200);
  53. GPIO_Init();
  54. I2C_Init();
  55. RX8025T_Init();
  56. time[0] = 0x10; // second
  57. time[1] = 0x10; // minute
  58. time[2] = 0x10; // hour
  59. time[3] = 0x40; // week day
  60. time[4] = 0x16; // day
  61. time[5] = 0x07; // month
  62. time[6] = 0x22; // year
  63. RX8025T_SetTime(time);
  64. while(1)
  65. {
  66. RX8025T_GetTime(time);
  67. for (i = 0; i < BUFF_SIZE; i++)
  68. {
  69. UART1_TxHex(time[i]);
  70. UART1_TxChar('-');
  71. }
  72. UART1_TxString("\r\n");
  73. SYS_Delay(1000);
  74. }
  75. }