249 lines
5.7 KiB
C
249 lines
5.7 KiB
C
|
#include "heater.h"
|
|||
|
#include <stdio.h>
|
|||
|
#include "public.h"
|
|||
|
#include "adc_anychannel_continuousscan_interrupt.h"
|
|||
|
#include "sim_eeprom.h"
|
|||
|
volatile u32 heat_on_delay = 0;
|
|||
|
volatile u32 keep_temp_time = 0;
|
|||
|
volatile u32 temp_time_clok = 0;
|
|||
|
void TIM2_GPIO_Init(void)
|
|||
|
{
|
|||
|
GPIO_InitTypeDef GPIO_InitStruct;
|
|||
|
RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, ENABLE);
|
|||
|
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_2);
|
|||
|
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
|
|||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
|
|||
|
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
|||
|
GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|||
|
}
|
|||
|
|
|||
|
void TIM2_PWM_Init(u16 arr, u16 psc)
|
|||
|
{
|
|||
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
|
|||
|
TIM_OCInitTypeDef TIM_OCInitStruct;
|
|||
|
NVIC_InitTypeDef NVIC_StructInit;
|
|||
|
|
|||
|
RCC_APB1PeriphClockCmd(RCC_APB1ENR_TIM2, ENABLE);
|
|||
|
TIM_TimeBaseStructInit(&TIM_TimeBaseStruct);
|
|||
|
TIM_TimeBaseStruct.TIM_Period = arr;
|
|||
|
TIM_TimeBaseStruct.TIM_Prescaler = psc;
|
|||
|
TIM_TimeBaseStruct.TIM_ClockDivision = TIM_CKD_DIV1;
|
|||
|
TIM_TimeBaseStruct.TIM_RepetitionCounter = 0;
|
|||
|
TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;
|
|||
|
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStruct);
|
|||
|
|
|||
|
//ʹ<><CAB9>TIM2 NVIC<49>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>ȼ<EFBFBD>ͨ<EFBFBD><CDA8>
|
|||
|
NVIC_StructInit.NVIC_IRQChannel = TIM2_IRQn;
|
|||
|
//<2F><><EFBFBD><EFBFBD>TIM2 NVIC<49>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>ȼ<EFBFBD>
|
|||
|
NVIC_StructInit.NVIC_IRQChannelPriority = 2;
|
|||
|
//ʹ<><CAB9>NVIC<49>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>ȼ<EFBFBD>
|
|||
|
NVIC_StructInit.NVIC_IRQChannelCmd = ENABLE;
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>TIM2<4D>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>ȼ<EFBFBD>
|
|||
|
NVIC_Init(&NVIC_StructInit);
|
|||
|
|
|||
|
TIM_ClearFlag(TIM2, TIM_FLAG_CC3);
|
|||
|
//ʹ<><CAB9>TIM2<4D><32><EFBFBD>ϼ<EFBFBD>ʱ<EFBFBD>ж<EFBFBD>
|
|||
|
TIM_ITConfig(TIM2, TIM_IT_CC3, ENABLE);
|
|||
|
|
|||
|
TIM_OCStructInit(&TIM_OCInitStruct);
|
|||
|
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
|
|||
|
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
|
|||
|
TIM_OCInitStruct.TIM_Pulse = 0;
|
|||
|
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;
|
|||
|
TIM_OC3Init(TIM2, &TIM_OCInitStruct);
|
|||
|
|
|||
|
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
|
|||
|
TIM_ForcedOC3Config(TIM2, TIM_ForcedAction_InActive);
|
|||
|
|
|||
|
TIM_ARRPreloadConfig(TIM2, ENABLE);
|
|||
|
TIM_CtrlPWMOutputs(TIM2, ENABLE);
|
|||
|
TIM_Cmd(TIM2, ENABLE);
|
|||
|
}
|
|||
|
|
|||
|
void Heater_Init(void)
|
|||
|
{
|
|||
|
TIM2_GPIO_Init();
|
|||
|
TIM2_PWM_Init(999,359); //20KHZ <20><>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD> 200hz ռ<>ձȿ<D5B1><C8BF>Ե<EFBFBD><D4B5>ڵ<EFBFBD>999
|
|||
|
}
|
|||
|
|
|||
|
void TIM2_IRQHandler(void)
|
|||
|
{
|
|||
|
if(TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET)
|
|||
|
{
|
|||
|
TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void duty(u8 mode, u16 pwm) // mode : 0 <20>ض<EFBFBD> 1 <20><> pwm :ռ<>ձȣ<D5B1>0-1000<30><30>
|
|||
|
{
|
|||
|
static unsigned char PWM_1 = 0;
|
|||
|
if(pwm>999)
|
|||
|
pwm = 999;
|
|||
|
switch( mode )
|
|||
|
{
|
|||
|
case 0: // ȫ<>ض<EFBFBD>
|
|||
|
TIM_ForcedOC3Config(TIM2, TIM_OCMode_PWM1);
|
|||
|
TIM_SetCompare3(TIM2, 0);
|
|||
|
PWM_1 = pwm;
|
|||
|
|
|||
|
break;
|
|||
|
case 1:
|
|||
|
if( PWM_1 != pwm)
|
|||
|
{
|
|||
|
TIM_ForcedOC3Config(TIM2, TIM_OCMode_PWM1);
|
|||
|
TIM_SetCompare3(TIM2, pwm);
|
|||
|
}
|
|||
|
PWM_1 = pwm;
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/****************************************
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ҳ<EFBFBD><EFBFBD>趨<EFBFBD>¶<EFBFBD> work_temp_up(u32 sleep_time)
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>sleep_time <EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>
|
|||
|
|
|||
|
*****************************************/
|
|||
|
u16 work_temp_up(u32 sleep_time) // ms
|
|||
|
{
|
|||
|
int i = 0;
|
|||
|
int time_s = sleep_time/1000;
|
|||
|
u8 find_tmep_flag = 0;
|
|||
|
|
|||
|
u16 temp = 0;
|
|||
|
//0 350 20 350
|
|||
|
for( i = 0; i < g_work.m_set_other_hot.set_up_num; i ++ )
|
|||
|
{
|
|||
|
if( time_s >= g_work.m_set_other_hot.m_set_up[i]._time)
|
|||
|
{
|
|||
|
temp = g_work.m_set_other_hot.m_set_up[i]._temp;
|
|||
|
|
|||
|
find_tmep_flag = 1;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if( find_tmep_flag == 0 )
|
|||
|
{
|
|||
|
temp = g_work.m_set_other_hot.m_set_up[0]._temp;
|
|||
|
}
|
|||
|
#ifdef _debug_
|
|||
|
//printf(" temp target = %d \n",temp);
|
|||
|
#endif
|
|||
|
return temp;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
void temp_control_new(u16 temp,u8 mod)
|
|||
|
{
|
|||
|
static u32 pwm_buffer = 0;
|
|||
|
static u8 over_heat =0;
|
|||
|
static float last_temp = 0.0;
|
|||
|
if( mod == 0)
|
|||
|
{
|
|||
|
over_heat = 0;
|
|||
|
pwm_buffer = 0;
|
|||
|
last_temp = 0.0;
|
|||
|
duty(0,0);
|
|||
|
temp_time_clok = 0;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
bat_v_delay = 50;
|
|||
|
////////////////////////<2F><><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>ʹ<EFBFBD><CAB9>
|
|||
|
// if(heat_on_delay == 0 && heat_stage == 1)
|
|||
|
// {
|
|||
|
// heat_stage = 2;
|
|||
|
// keep_temp_time = 20000; //<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʮ<EFBFBD><CAAE>
|
|||
|
// }
|
|||
|
// else if(keep_temp_time==0 && heat_stage == 2 )
|
|||
|
// {
|
|||
|
// heat_stage = 3;
|
|||
|
// }
|
|||
|
// printf("CCR3 = %d \n",TIM_GetCapture3(TIM2));
|
|||
|
if(temp_all<temp && over_heat == 0)
|
|||
|
{
|
|||
|
last_temp = temp_all;
|
|||
|
pwm_buffer = (temp-temp_all)/temp*690+300;//<2F><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>PWM<57><4D><EFBFBD><EFBFBD><EFBFBD><EFBFBD>pid<69>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>pwm,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
duty(1,pwm_buffer);
|
|||
|
}
|
|||
|
else if(temp_all<temp && over_heat == 1)
|
|||
|
{
|
|||
|
if(temp_all<= last_temp)
|
|||
|
{
|
|||
|
pwm_buffer = TIM_GetCapture3(TIM2);
|
|||
|
if(pwm_buffer<990)
|
|||
|
{
|
|||
|
duty(1,++pwm_buffer);
|
|||
|
}
|
|||
|
last_temp = temp_all;
|
|||
|
}
|
|||
|
}
|
|||
|
else if(temp_all == temp)
|
|||
|
{
|
|||
|
last_temp = temp_all;
|
|||
|
pwm_buffer = TIM_GetCapture3(TIM2);
|
|||
|
over_heat =1;
|
|||
|
}
|
|||
|
else if(temp_all>temp )
|
|||
|
{
|
|||
|
if(temp_all>=last_temp)
|
|||
|
{
|
|||
|
pwm_buffer = TIM_GetCapture3(TIM2);
|
|||
|
over_heat =1;
|
|||
|
if(pwm_buffer > 50 )
|
|||
|
{
|
|||
|
duty(1,--pwm_buffer);
|
|||
|
}
|
|||
|
last_temp = temp_all;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if(heat_on_delay == 0 && heat_stage == 1)
|
|||
|
{
|
|||
|
heat_stage = 2;
|
|||
|
keep_temp_time =(g_work.m_set_other_hot.set_all_time - g_work.m_set_other_hot.set_order_time)*1000;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void heat_control(void)
|
|||
|
{
|
|||
|
u16 temp_buffer;
|
|||
|
if(heat_stage == 0)//û<><C3BB>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>Ҫ<EFBFBD>õ<EFBFBD><C3B5>IJ<EFBFBD><C4B2><EFBFBD>
|
|||
|
{
|
|||
|
heat_on_delay = g_work.m_set_other_hot.set_order_time*1000;//Ԥ<><D4A4><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
temp_time_clok = 0;
|
|||
|
heat_stage = 1;
|
|||
|
}
|
|||
|
else if(heat_stage == 1)
|
|||
|
{
|
|||
|
temp_buffer = work_temp_up(temp_time_clok);
|
|||
|
temp_control_new(temp_buffer,1);
|
|||
|
}
|
|||
|
else if(heat_stage == 2)
|
|||
|
{
|
|||
|
if(keep_temp_time>10000)//<2F><><EFBFBD><EFBFBD>ʮ<EFBFBD><CAAE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7>
|
|||
|
{
|
|||
|
temp_buffer = work_temp_up(temp_time_clok);
|
|||
|
temp_control_new(temp_buffer,1);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
heat_stage = 3;
|
|||
|
}
|
|||
|
}
|
|||
|
else if(heat_stage ==3)
|
|||
|
{
|
|||
|
if(keep_temp_time>0)//<2F><><EFBFBD><EFBFBD>ʮ<EFBFBD><CAAE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7>
|
|||
|
{
|
|||
|
temp_buffer = work_temp_up(temp_time_clok);
|
|||
|
temp_control_new(temp_buffer,1);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
temp_control_new(0,0);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|