SY8835_For_Demo_Ourself/UsrSrc/pwm/pwm.c
Alex xu 30111f86c1 更改内容:1、优化代码,全局变量在定义时不赋初值,减小ROM占用;
2、将显示UI模块化,单独成文件displayui.c和display_ui.h;
3、将部分功能模块的状态位、标志位变量和配置参数置于congfig.h;
4、将工程程序中的TP3315字符更改SY8835。
2025-01-21 18:00:07 +08:00

143 lines
3.9 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
******************************************************************************
*
* @file pwm.c
* @brief pwm module
* @ic sy8835
*
* @version 1.0
* @date 2024/11/01 09:50:40
* @author Alex Xu
*
* Copyright (c) 2013-2099,Tkplusemi Technology Co.,Ltd.
* All Rights Reserved
*
* History:
* Revision Date Author Desc
* 1.0.0 2024/11/01 Alex build this file
* DescriptionPWM功能中4个通道公用一个源也就是分频数PWM_PRE0、计数初值PWM0_REL只有一个通过更改PWMx_CMP设置不同通道PWM输出的占空比。
******************************************************************************
*/
#include "pwm.h"
#ifdef PWM_ENABLE
/******************************************************************************\
Macro definitions
\******************************************************************************/
/******************************************************************************\
Variables definitions
\******************************************************************************/
/******************************************************************************\
Functions definitions
\******************************************************************************/
/*
*******************************************************************************
* void PWM_Disable(Pwm_Channels_e nPWM_Ch)
*
* Description : PWM Disable PWM按通道禁能
*
* Arguments : None
* Returns :
* Notes :
*
*******************************************************************************
*/
void PWM_Disable(Pwm_Channels_e nPWM_Ch)
{
SFRADDR = MFP_CTL1;
SFRDATA &= ~( 0x03 << (nPWM_Ch * 2) );
if(nPWM_Ch == PWM_ALL)
{
SFRADDR = MFP_CTL1;
SFRDATA = 0x00;
}
}
/*
*******************************************************************************
* void PWM_Init_Set(uint8_t nPWM_PRE,uint8_t nPWM_REL)
*
* Description : PWM Set1 设置PWM输出时钟分频、频率参数。
例如PWM输出PWM频率10kHz占空比50%。
则参数设置如下PWM计数器时钟Fre(pwm) = Fre(sys)/PWM_PRE = 12M / n^PWM_PRE = 12M / 8 = 1.5MHz,
PWM_REL = 255 - 1.5M / 10K = 255-150 = 105
nPWM_CMP = 105 + 150 * (1-50%) = 180。
*
* Arguments : uint8_t nPWM_PRE --- PWM counter reload value
* uint8_t nPWM_REL --- PWM compare value
*
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
void PWM_Init_Set(uint8_t nPWM_PRE,uint8_t nPWM_REL)
{
SFRADDR = PWM_PRE; //PWM clock divide setting
SFRDATA = nPWM_PRE & 0x07;
SFRADDR = PWM_REL; //PWM counter reload value
SFRDATA = nPWM_REL;
SFRADDR = PWM_CTL;
SFRDATA = 0x01;
}
/*
*******************************************************************************
* void PWM_Duty_Set(Pwm_Channels_e nPWM_Ch,uint8_t nPWM_CMP)
*
* Description : PWM Set 按通道设置PWM输出占空比参数。
例如PWM输出PWM频率10kHz占空比50%。
则参数设置如下PWM计数器时钟Fre(pwm) = Fre(sys)/PWM_PRE = 11.0592M / n^PWM_PRE = 11.0592M / 8 = 1.38MHz,
PWM_REL = 255 - 1.5M / 10K = 255-150 = 105
nPWM_CMP = 105 + 150 * (1-50%) = 180。
*
* Arguments : nPWM_Ch (PWM0 ~ PWM3);
* uint8_t nPWM_CMP --- PWM compare value
*
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
void PWM_Duty_Set(Pwm_Channels_e nPWM_Ch,uint8_t nPWM_CMP)
{
SFRADDR = MFP_CTL1;
SFRDATA &=~( 0x03 << nPWM_Ch*2 );
SFRDATA |= 0x01 << nPWM_Ch*2;
SFRADDR = P1_OE;
SFRDATA |= 0x01 << nPWM_Ch;
if(nPWM_Ch == PWM_CH0)
{
PWM0_CMP = nPWM_CMP;
}
else if(nPWM_Ch == PWM_CH1)
{
PWM1_CMP = nPWM_CMP;
}
else if(nPWM_Ch == PWM_CH2)
{
PWM2_CMP = nPWM_CMP;
}
else if(nPWM_Ch == PWM_CH3)
{
PWM3_CMP = nPWM_CMP;
}
}
#endif