149 lines
3.4 KiB
C
Raw Permalink 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.

/*
* PWM.c
*
* Created on: 2022年03月12日
* Author: User
*/
/* Includes ------------------------------------------------------------------*/
#include <main.h>
/* Private define-------------------------------------------------------------*/
#define CCP_S1 BIT5
#define CCP_S0 BIT4
#define EPC0H BIT1
#define EPC0L BIT0
/* Private variables----------------------------------------------------------*/
/* Private function prototypes------------------------------------------------*/
static void PWM_Init(void); // PWM初始化
static void PWM1_HeatingSetting(uint8_t Percent); //通道1设置加热
static void PWM2_HeatingSetting(uint8_t Percent); //通道2设置加热
/* Public variables-----------------------------------------------------------*/
PWM_t PWM =
{0,
0,
PWM_Init,
PWM1_HeatingSetting,
PWM2_HeatingSetting};
/*
* @name PWM_Init
* @brief PWM初始化
* @param None
* @retval None
*/
static void PWM_Init()
{
PWM0_MAP = 0x22; // PWM0通道映射P22
PWM01_MAP = 0x23; // PWM01通道映射P23
PWM0C = 0x01; // PWM0高有效 PWM01高有效 时钟8分频
//独立模式下PWM0和PWM01共用一个周期寄存器
// PWM0的占空比调节使用 PWM0组的占空比寄存器
// PWM01的占空比调节使用 PWM0组的死区寄存器
//周期计算 = 0x03ff / (Fosc / PWM分频系数)
// = 0x03ff / (16000000 / 8)
// = 1023 /2000000
// = 511.5us 约1.955kHz
PWM0PH = 0x03; //周期高4位设置为0x03
PWM0PL = 0xE7; //周期低8位设置为0xFF
//占空比计算= 0x0155 / (Fosc / PWM分频系数) Fosc见系统时钟配置的部分
// = 0x0155 / (16000000 / 8)
// = 341 / 2000000
// = 170.5us 占空比为 170.5/511.5 = 33.3%
PWM0DH = 0x00; // PWM0高4位占空比
PWM0DL = 0x00; // PWM0低4位占空比
PWM0DTH = 0x00; // PWM01高4位占空比
PWM0DTL = 0x00; // PWM01低8位占空比
PWM0EN = 0x0F; //使能PWM0 工作于独立模式
}
/*
* @name PWM1_HeatingSetting
* @brief 调整加热功率
* @param None
* @retval None
*/
static void PWM1_HeatingSetting(uint8_t Percent)
{
uint16_t Temp_Value = 0;
Temp_Value = Percent * 10;
if (Public.Channel1_StatusFlag != FALSE)
{
if (Channel1_Error_Flag != System_Fault) //判断是否故障
{
if (Percent == 0)
{
PWM.Heating_Flag1 = System_Complete;//加热温度已经到达
}
else
{
PWM.Heating_Flag1 = TRUE; //正在加热
}
}
else
{
PWM.Heating_Flag1 = System_Fault;
Temp_Value = 0;
}
}
else
{
PWM.Heating_Flag1 = FALSE;
}
PWM0DTH = (Temp_Value >> 8) & 0xFF;
PWM0DTL = (Temp_Value & 0xFF);
}
/*
* @name PWM2_HeatingSetting
* @brief 调整加热功率
* @param None
* @retval None
*/
static void PWM2_HeatingSetting(uint8_t Percent)
{
uint16_t Temp_Value = 0;
Temp_Value = Percent * 10;
if (Public.Channel2_StatusFlag != FALSE)
{
if (Channel2_Error_Flag != System_Fault) //判断是否故障
{
if (Percent == 0)
{
PWM.Heating_Flag2 = System_Complete; //加热温度已经到达
}
else
{
PWM.Heating_Flag2 = TRUE; //正在加热
}
}
else
{
PWM.Heating_Flag2 = System_Fault;
Temp_Value = 0;
}
}
else
{
PWM.Heating_Flag2 = FALSE;
}
PWM0DH = (Temp_Value >> 8) & 0xFF;
PWM0DL = (Temp_Value & 0xFF);
}
/********************************************************
End Of File
********************************************************/