FwLib_STC8/demo/pca/pca_pwm_10bit_2ch.c
2022-01-09 19:26:52 +08:00

71 lines
2.2 KiB
C

// Copyright 2021 IOsetting <iosetting(at)outlook.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fw_hal.h"
__BIT dir = SET;
uint16_t dc = 0;
void main(void)
{
SYS_SetClock();
// UART1, baud 115200, baud source Timer1, 1T mode, no interrupt
UART1_Config8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200);
// Set GPIO pin mode
GPIO_P3_SetMode(GPIO_Pin_2|GPIO_Pin_3, GPIO_Mode_Output_PP);
// Stop PCA/PWM
PCA_SetCounterState(HAL_State_OFF);
// Stop counter in idle mode
PCA_SetStopCounterInIdle(HAL_State_ON);
// Use SYSCLK as clock source
PCA_SetClockSource(PCA_ClockSource_SysClkDiv12);
// Turn off overflow interrupt
PCA_EnableCounterOverflowInterrupt(HAL_State_OFF);
// Set PCA0 and PCA1 to PWM mode
PCA_PCA0_SetWorkMode(PCA_WorkMode_PWM_NonInterrupt);
PCA_PCA1_SetWorkMode(PCA_WorkMode_PWM_NonInterrupt);
// Set both to 8-bit PWM
PCA_PWM0_SetBitWidth(PCA_PWM_BitWidth_10);
PCA_PWM1_SetBitWidth(PCA_PWM_BitWidth_10);
// Set initial duty cycle
PCA_PCA0_SetCompareValue10bit(dc);
PCA_PCA1_SetCompareValue10bit(~dc & 0x3FF);
// Set output port for STC8G1K08A, PWM0:P32, PWM1:P33
PCA_SetPort(PCA_AlterPort_G1K08A_P55_P32_P33_P54);
// Start PWM
PCA_SetCounterState(HAL_State_ON);
while(1)
{
PCA_PCA0_ChangeCompareValue10bit(dc);
PCA_PCA1_ChangeCompareValue10bit(~dc & 0x3FF);
UART1_TxHex(dc >> 8);
UART1_TxHex(dc & 0xFF);
if (dir)
{
dc++;
if (dc == 0x03FF) dir = !dir;
}
else
{
dc--;
if (dc == 0) dir = !dir;
}
UART1_TxString("\r\n");
SYS_Delay(10);
}
}