pca_pwm_10bit_2ch.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #include "fw_hal.h"
  15. __BIT dir = SET;
  16. uint16_t dc = 0;
  17. void main(void)
  18. {
  19. SYS_SetClock();
  20. // UART1, baud 115200, baud source Timer1, 1T mode, no interrupt
  21. UART1_Config8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200);
  22. // Set GPIO pin mode
  23. GPIO_P3_SetMode(GPIO_Pin_2|GPIO_Pin_3, GPIO_Mode_Output_PP);
  24. // Stop PCA/PWM
  25. PCA_SetCounterState(HAL_State_OFF);
  26. // Stop counter in idle mode
  27. PCA_SetStopCounterInIdle(HAL_State_ON);
  28. // Use SYSCLK as clock source
  29. PCA_SetClockSource(PCA_ClockSource_SysClkDiv12);
  30. // Turn off overflow interrupt
  31. PCA_EnableCounterOverflowInterrupt(HAL_State_OFF);
  32. // Set PCA0 and PCA1 to PWM mode
  33. PCA_PCA0_SetWorkMode(PCA_WorkMode_PWM_NonInterrupt);
  34. PCA_PCA1_SetWorkMode(PCA_WorkMode_PWM_NonInterrupt);
  35. // Set both to 8-bit PWM
  36. PCA_PWM0_SetBitWidth(PCA_PWM_BitWidth_10);
  37. PCA_PWM1_SetBitWidth(PCA_PWM_BitWidth_10);
  38. // Set initial duty cycle
  39. PCA_PCA0_SetCompareValue10bit(dc);
  40. PCA_PCA1_SetCompareValue10bit(~dc & 0x3FF);
  41. // Set output port for STC8G1K08A, PWM0:P32, PWM1:P33
  42. PCA_SetPort(PCA_AlterPort_G1K08A_P55_P32_P33_P54);
  43. // Start PWM
  44. PCA_SetCounterState(HAL_State_ON);
  45. while(1)
  46. {
  47. PCA_PCA0_ChangeCompareValue10bit(dc);
  48. PCA_PCA1_ChangeCompareValue10bit(~dc & 0x3FF);
  49. UART1_TxHex(dc >> 8);
  50. UART1_TxHex(dc & 0xFF);
  51. if (dir)
  52. {
  53. dc++;
  54. if (dc == 0x03FF) dir = !dir;
  55. }
  56. else
  57. {
  58. dc--;
  59. if (dc == 0) dir = !dir;
  60. }
  61. UART1_TxString("\r\n");
  62. SYS_Delay(10);
  63. }
  64. }