pwm_2ch_complementary.c 2.7 KB

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