pca_pwm_8bit_2ch.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. uint8_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_SysClk);
  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_8);
  37. PCA_PWM1_SetBitWidth(PCA_PWM_BitWidth_8);
  38. // Set initial duty cycle
  39. PCA_PCA0_SetCompareValue(dc);
  40. PCA_PCA1_SetCompareValue(~dc);
  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_ChangeCompareValue(dc);
  48. PCA_PCA1_ChangeCompareValue(~dc);
  49. UART1_TxHex(dc);
  50. UART1_TxChar(' ');
  51. UART1_TxHex(~dc);
  52. if (dir)
  53. {
  54. dc++;
  55. if (dc == 0xFF) dir = !dir;
  56. }
  57. else
  58. {
  59. dc--;
  60. if (dc == 0) dir = !dir;
  61. }
  62. UART1_TxString("\r\n");
  63. SYS_Delay(10);
  64. }
  65. }