adc_interrupt_10bit.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. * STC8H3K64S2
  16. * AGrnd -> GND
  17. * AVcc -> VCC
  18. * AVref -> VCC
  19. * Vcc -> VCC
  20. * Gnd -> GND
  21. * ADC1 -> Test voltage
  22. *
  23. * STC8H1K08
  24. * AVref -> VCC
  25. * Vcc -> VCC
  26. * Gnd -> GND
  27. * ADC1 -> Test voltage
  28. */
  29. #include "fw_hal.h"
  30. uint16_t res;
  31. INTERRUPT(ADC_Routine, EXTI_VectADC)
  32. {
  33. ADC_ClearInterrupt();
  34. res = ADC_RESL;
  35. res |= (ADC_RES & 0x0F) << 8;
  36. // Restart ADC for continuous sampling
  37. ADC_Start();
  38. }
  39. void main(void)
  40. {
  41. SYS_SetClock();
  42. // For debug print
  43. UART1_Config8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200);
  44. // Set ADC1(GPIO P1.1) HIP
  45. GPIO_P1_SetMode(GPIO_Pin_1, GPIO_Mode_Input_HIP);
  46. // Channel: ADC1
  47. ADC_SetChannel(0x01);
  48. // ADC Clock = SYSCLK / 2 / (1+15) = SYSCLK / 32
  49. ADC_SetClockPrescaler(0x0F);
  50. // Right alignment, high 2-bit in ADC_RES, low 8-bit in ADC_RESL
  51. ADC_SetResultAlignmentRight();
  52. // Enable interrupts
  53. EXTI_Global_SetIntState(HAL_State_ON);
  54. EXTI_ADC_SetIntState(HAL_State_ON);
  55. // Turn on ADC power
  56. ADC_SetPowerState(HAL_State_ON);
  57. // Start ADC
  58. ADC_Start();
  59. while(1)
  60. {
  61. UART1_TxString("Result: ");
  62. UART1_TxHex(res >> 8);
  63. UART1_TxHex(res & 0xFF);
  64. UART1_TxString("\r\n");
  65. SYS_Delay(100);
  66. }
  67. }