adc_poll_10bit.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. void main(void)
  31. {
  32. uint16_t res;
  33. SYS_SetClock();
  34. // For debug print
  35. UART1_Config8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200);
  36. // Set ADC1(GPIO P1.1) HIP
  37. GPIO_P1_SetMode(GPIO_Pin_1, GPIO_Mode_Input_HIP);
  38. // Channel: ADC1
  39. ADC_SetChannel(0x01);
  40. // ADC Clock = SYSCLK / 2 / (1+1) = SYSCLK / 4
  41. ADC_SetClockPrescaler(0x01);
  42. // Right alignment, high 2-bit/4-bit in ADC_RES, low 8-bit in ADC_RESL
  43. ADC_SetResultAlignmentRight();
  44. // Turn on ADC power
  45. ADC_SetPowerState(HAL_State_ON);
  46. while(1)
  47. {
  48. ADC_Start();
  49. NOP();
  50. NOP();
  51. while (!ADC_SamplingFinished());
  52. ADC_ClearInterrupt();
  53. /*
  54. res = ADC_RESL;
  55. res |= (ADC_RES & 0x0F) << 8;
  56. */
  57. UART1_TxHex(ADC_RES);
  58. UART1_TxHex(ADC_RESL);
  59. UART1_TxString("\r\n");
  60. SYS_Delay(100);
  61. }
  62. }