main.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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: ADXL345
  16. * Board: STC8H8K64U
  17. * 8H8K64U
  18. * GND -> GND G
  19. * VDD(Vcc) -> VCC V
  20. * CS -> GPIO OUT P35 34
  21. * INT1 -> GPIO IN P36 INT2 35
  22. * INT2 -> GPIO IN P37 INT3 36
  23. * SDO -> MISO P33 30
  24. * SDI/SDA -> MOSI P34 31
  25. * SCL -> SCLK P32 29
  26. */
  27. #include "fw_hal.h"
  28. #include "adxl345.h"
  29. #include <stdio.h>
  30. volatile uint8_t count_int2 = 0, count_int3 = 0, count_double_tap = 0;
  31. volatile int16_t x, y, z;
  32. void SPI_Init(void)
  33. {
  34. // ADXL345, SPI CLK max frequency is 5MHz
  35. SPI_SetClockPrescaler(SPI_ClockPreScaler_16);
  36. // Clock is high when idle
  37. SPI_SetClockPolarity(HAL_State_ON);
  38. // Data transfer is driven by lower SS pin
  39. SPI_SetClockPhase(SPI_ClockPhase_TrailingEdge);
  40. // MSB first
  41. SPI_SetDataOrder(SPI_DataOrder_MSB);
  42. // Define the output pins
  43. SPI_SetPort(SPI_AlterPort_P35_P34_P33_P32);
  44. // Ignore SS pin, use MSTR to swith between master/slave mode
  45. SPI_IgnoreSlaveSelect(HAL_State_ON);
  46. // Master mode
  47. SPI_SetMasterMode(HAL_State_ON);
  48. // Start SPI
  49. SPI_SetEnabled(HAL_State_ON);
  50. }
  51. void GPIO_Init(void)
  52. {
  53. // Configure GPIO pins before SPI and device
  54. // MISO(P33), MOSI(P34)
  55. GPIO_P3_SetMode(GPIO_Pin_3|GPIO_Pin_4, GPIO_Mode_InOut_QBD);
  56. // SCLK(P32), CS(P35),
  57. GPIO_P3_SetMode(GPIO_Pin_2|GPIO_Pin_5, GPIO_Mode_Output_PP);
  58. // INT2(P36), INT3(P37)
  59. GPIO_P3_SetMode(GPIO_Pin_6|GPIO_Pin_7, GPIO_Mode_Input_HIP);
  60. }
  61. void INT_Init()
  62. {
  63. EXTI_Int2_SetIntState(HAL_State_ON);
  64. EXTI_Int3_SetIntState(HAL_State_ON);
  65. EXTI_Global_SetIntState(HAL_State_ON);
  66. }
  67. INTERRUPT(Int2_Routine, EXTI_VectInt2)
  68. {
  69. count_int2++;
  70. if (ADXL345_IsInterrupt(ADXL345_INT_DOUBLE_TAP) > 0)
  71. {
  72. count_double_tap++;
  73. }
  74. }
  75. INTERRUPT(Int3_Routine, EXTI_VectInt3)
  76. {
  77. count_int3++;
  78. x = ADXL345_ReadInt(ADXL345_REG_DATAX0);
  79. y = ADXL345_ReadInt(ADXL345_REG_DATAY0);
  80. z = ADXL345_ReadInt(ADXL345_REG_DATAZ0);
  81. }
  82. int main(void)
  83. {
  84. SYS_SetClock();
  85. GPIO_Init();
  86. UART1_Config8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200);
  87. SPI_Init();
  88. INT_Init();
  89. ADXL345_Init(
  90. ADXL345_DATARATE_100_HZ,
  91. ADXL345_SPI_WIRE_4,
  92. ADXL345_INT_ACTIVE_LOW,
  93. ADXL345_DATA_RESOLVE_FULL,
  94. ADXL345_DATA_ALIGNMENT_RIGHT,
  95. ADXL345_G_RANGE_8G
  96. );
  97. // Tap threshold: 62.5mg / LSB, value = 2.5g / 0.0625g = 0x28
  98. ADXL345_WriteByte(ADXL345_REG_THRESH_TAP, 0x28);
  99. // Tap duration: 625us / LSB, value = 0.02s / 0.000625s = 0x20
  100. ADXL345_WriteByte(ADXL345_REG_DUR, 0x20);
  101. // Tap latency: 1.25ms / LSB, value = 0.1s / 0.00125s = 0x50
  102. ADXL345_WriteByte(ADXL345_REG_LATENT, 0x50);
  103. // Tap window: 1.25ms / LSB, value = 0.3s / 0.00125s = 0xF0
  104. ADXL345_WriteByte(ADXL345_REG_WINDOW, 0xF0);
  105. ADXL345_EnableTapDetectOnAxes(
  106. ADXL345_TAP_DETECT_AXIS_X|ADXL345_TAP_DETECT_AXIS_Y|ADXL345_TAP_DETECT_AXIS_Z);
  107. ADXL345_SetInterrupts(
  108. ADXL345_INT_DATA_READY|ADXL345_INT_SINGLE_TAP|ADXL345_INT_DOUBLE_TAP);
  109. ADXL345_RemapInterrupts(ADXL345_INT_DATA_READY);
  110. // read to clear interrupts, or the counter will never receive interrupts
  111. x = ADXL345_ReadInt(ADXL345_REG_DATAX0);
  112. y = ADXL345_ReadInt(ADXL345_REG_DATAY0);
  113. z = ADXL345_ReadInt(ADXL345_REG_DATAZ0);
  114. x = ADXL345_ReadByte(ADXL345_REG_INT_SOURCE);
  115. while(1)
  116. {
  117. printf("X:%6d, Y:%6d, Z:%6d, DAT:%3d, TAP:%3d, 2-TAP:%3d\r\n",
  118. x, y, z, count_int3, count_int2, count_double_tap);
  119. SYS_Delay(50);
  120. }
  121. }