main.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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: DAC voice output simulation
  16. * Board: STC8H3K32 (types with flash size >= 32KB)
  17. *
  18. * P1.0 -> 200R -> Speaker +
  19. * GND -> Speaker -
  20. */
  21. #include "fw_hal.h"
  22. #include "voice.h"
  23. uint8_t dc = 0;
  24. uint16_t voice_pos = 0, voice_size = 27451;
  25. void PWM_Init()
  26. {
  27. // Set GPIO pins output mode P10 -> PWMA.1P
  28. GPIO_P1_SetMode(GPIO_Pin_0, GPIO_Mode_Output_PP);
  29. // Turn off PWMA.1 before change its mode
  30. PWMA_PWM1_SetPortState(HAL_State_OFF);
  31. PWMA_PWM1N_SetPortState(HAL_State_OFF);
  32. // Set PWMA.1 port direction output
  33. PWMA_PWM1_SetPortDirection(PWMB_PortDirOut);
  34. // Set PWMA.1 output low voltage when counter is less than target value
  35. PWMA_PWM1_ConfigOutputMode(PWM_OutputMode_PWM_HighIfLess);
  36. // Enable comparison value preload to make duty cycle changing smooth
  37. PWMA_PWM1_SetComparePreload(HAL_State_ON);
  38. // Turn on PWMA.1
  39. PWMA_PWM1_SetPortState(HAL_State_ON);
  40. // Set PWM frequency to 16kHz, Fpwm = SYSCLK / (PWMx_PSCR + 1) / (PWMx_ARR + 1)
  41. // You will hear obvious noise if PWM frequency is less than 12kHz
  42. PWMA_SetPrescaler(8);
  43. // PWM width = Period + 1 (side alignment), or AutoReloadPeriod * 2 (center alignment)
  44. PWMA_SetPeriod(0xFF);
  45. // Counter direction, down:from [PWMA_ARRH,PWMA_ARRL] to 0
  46. PWMA_SetCounterDirection(PWM_CounterDirection_Down);
  47. // Enable preload on reload-period
  48. PWMA_SetAutoReloadPreload(HAL_State_ON);
  49. // Enable output on PWMA.1P
  50. PWMA_SetPinOutputState(PWM_Pin_1, HAL_State_ON);
  51. // Set PWMA.1 alternative ports to P1.0 and P1.1
  52. PWMA_PWM1_SetPort(PWMA_PWM1_AlterPort_P10_P11);
  53. // Enable overall output
  54. PWMA_SetOverallState(HAL_State_ON);
  55. // Start counter
  56. PWMA_SetCounterState(HAL_State_ON);
  57. }
  58. void Timer0_Init()
  59. {
  60. TIM_Timer0_Config(HAL_State_ON, TIM_TimerMode_16BitAuto, 8000);
  61. EXTI_Timer0_SetIntState(HAL_State_ON);
  62. EXTI_Timer0_SetIntPriority(EXTI_IntPriority_High);
  63. EXTI_Global_SetIntState(HAL_State_ON);
  64. TIM_Timer0_SetRunState(HAL_State_ON);
  65. }
  66. INTERRUPT(Timer0_Routine, EXTI_VectTimer0)
  67. {
  68. uint8_t dc;
  69. dc = voice_bulk[voice_pos++];
  70. PWMA_PWM1_SetCaptureCompareValue(dc);
  71. if (voice_pos == voice_size) voice_pos = 0;
  72. }
  73. void main(void)
  74. {
  75. SYS_SetClock();
  76. PWM_Init();
  77. Timer0_Init();
  78. while(1);
  79. }