pwm_2ch_timer2_interrupt.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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: 2-channel with timer2 interrupt
  16. *
  17. * P1.0 -> 1k Ω resistor -> LED1+
  18. * P1.1 -> 1k Ω resistor -> LED2+
  19. * GND -> LED1-
  20. * GND -> LED2-
  21. */
  22. #include "fw_hal.h"
  23. __BIT dir = SET;
  24. uint8_t dc = 0;
  25. INTERRUPT(Timer2_Routine, EXTI_VectTimer2)
  26. {
  27. PWMA_PWM1_SetCaptureCompareValue(dc);
  28. UART1_TxHex(dc);
  29. UART1_TxString("\r\n");
  30. if (dir)
  31. {
  32. dc++;
  33. if (dc == 0xFF) dir = !dir;
  34. }
  35. else
  36. {
  37. dc--;
  38. if (dc == 0) dir = !dir;
  39. }
  40. }
  41. void main(void)
  42. {
  43. SYS_SetClock();
  44. // UART1, baud 115200, baud source Timer1, 1T mode
  45. UART1_Config8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200);
  46. // Set GPIO pins output mode
  47. GPIO_P1_SetMode(GPIO_Pin_0|GPIO_Pin_1, GPIO_Mode_Output_PP);
  48. // Turn off PWMA.1 before change its mode
  49. PWMA_PWM1_SetPortState(HAL_State_OFF);
  50. PWMA_PWM1N_SetPortState(HAL_State_OFF);
  51. // Set PWMA.1 port direction output
  52. PWMA_PWM1_SetPortDirection(PWMB_PortDirOut);
  53. // Set PWMA.1 output low voltage when counter is less than target value
  54. PWMA_PWM1_ConfigOutputMode(PWM_OutputMode_PWM_LowIfLess);
  55. // Enable comparison value preload to make duty cycle changing smooth
  56. PWMA_PWM1_SetComparePreload(HAL_State_ON);
  57. // Turn on PWMA.1
  58. PWMA_PWM1_SetPortState(HAL_State_ON);
  59. // Turn on PWMA.1 complementary output
  60. PWMA_PWM1N_SetPortState(HAL_State_ON);
  61. // Set highest PWM clock
  62. PWMA_SetPrescaler(0);
  63. // PWM width = Period + 1 (side alignment), or AutoReloadPeriod * 2 (center alignment)
  64. PWMA_SetPeriod(0xFF);
  65. // Counter direction, down:from [PWMA_ARRH,PWMA_ARRL] to 0
  66. PWMA_SetCounterDirection(PWM_CounterDirection_Down);
  67. // Enable preload on reload-period
  68. PWMA_SetAutoReloadPreload(HAL_State_ON);
  69. // Enable output on PWMA.1P, PWMA.1N
  70. PWMA_SetPinOutputState(PWM_Pin_1|PWM_Pin_1N, HAL_State_ON);
  71. // Set PWMA.1 alternative ports to P1.0 and P1.1
  72. PWMA_PWM1_SetPort(PWMA_PWM1_AlterPort_P10_P11);
  73. // Enable overall output
  74. PWMA_SetOverallState(HAL_State_ON);
  75. // Start counter
  76. PWMA_SetCounterState(HAL_State_ON);
  77. // 1T mode, prescaler:255+1, frequency: 100
  78. TIM_Timer2_Config(HAL_State_ON, 0xFF, 100);
  79. // Timer2 interrupt: ON
  80. EXTI_Timer2_SetIntState(HAL_State_ON);
  81. EXTI_Global_SetIntState(HAL_State_ON);
  82. TIM_Timer2_SetRunState(HAL_State_ON);
  83. while(1);
  84. }