希 望能帮到有需要的人,不用蒙头乱撞:
/**
\brief configure the SHRTIMER peripheral
\param[in] none
\param[out] none
\retval none
周期中断就是重复中断SHRTIMER_MT_ST_INT_REP,不是更新中断用于周期寄存器重新设置
*/
void shrtimer_config(void)
{
/* -----------------------------------------------------------------------
fSHRTIMER_CK:180MHz, from CK_SYS
waveform period: 22500/(180M/1)=125us
ST0_CH1_O: set request at Slave_TIMER0 period event, reset request at Slave_TIMER0 compare 0 event
----------------------------------------------------------------------- */
shrtimer_baseinit_parameter_struct baseinit_para;
shrtimer_timerinit_parameter_struct timerinit_para;
shrtimer_timercfg_parameter_struct timercfg_para;
shrtimer_comparecfg_parameter_struct comparecfg_para;
shrtimer_channel_outputcfg_parameter_struct outcfg_para;
shrtimer_deadtimecfg_parameter_struct deadtimecfg_para;
/* periodic DLL calibration */
shrtimer_dll_calibration_start(SHRTIMER0, SHRTIMER_CALIBRATION_ONCE);//配置和启动DLL校准 最好值设置一次校准,否则会出现丢波,上下管贯通
while(RESET == shrtimer_common_flag_get(SHRTIMER0, SHRTIMER_FLAG_DLLCAL));//获取SHRTIMER通用标志 SHRTIMER_FLAG_DLLCAL=DLL 校准完成中断标志
//shrtimer_dll_calibration_start(SHRTIMER0, SHRTIMER_CALIBRATION_ONCE);//只校准一次,否则会出现周期丢波的错误
/* Slave_TIMER0 time base clock config */
shrtimer_baseinit_struct_para_init(&baseinit_para);//初始化结构体变量
baseinit_para.period = 22500;//周期值 500uS 2KHz
baseinit_para.prescaler = SHRTIMER_PRESCALER_DIV1;//预分频值
baseinit_para.repetitioncounter = 0;//重复计数值
baseinit_para.counter_mode = SHRTIMER_COUNTER_MODE_CONTINOUS;//计数器运行模式
/**************定时器配置*************/
shrtimer_timers_base_init(SHRTIMER0, SHRTIMER_MASTER_TIMER, &baseinit_para);//
shrtimer_timers_base_init(SHRTIMER0, SHRTIMER_SLAVE_TIMER0, &baseinit_para);//初始化Master_TIMER/Slave_TIMER时基
shrtimer_timers_base_init(SHRTIMER0, SHRTIMER_SLAVE_TIMER1, &baseinit_para);//
shrtimer_timers_base_init(SHRTIMER0, SHRTIMER_SLAVE_TIMER2, &baseinit_para);//
shrtimer_timerinit_struct_para_init(&timerinit_para);//初始化shrtimer_timerinit_parameter_struct结构体变量
/*************MASTER_TIMER*************/
timerinit_para.cnt_bunch = SHRTIMER_TIMERBUNCHNMODE_MAINTAINCLOCK;//突发模式下计数器时钟停止或者正常运行
timerinit_para.dac_trigger = SHRTIMER_DAC_TRIGGER_NONE; //DAC触发源
timerinit_para.half_mode = SHRTIMER_HALFMODE_DISABLED;//使能或禁能半波模式
////
timerinit_para.repetition_update = SHRTIMER_UPDATEONREPETITION_ENABLED;//SHRTIMER_UPDATEONREPETITION_DISABLED;//重复事件产生更新事件
timerinit_para.reset_sync = SHRTIMER_SYNCRESET_DISABLED;//同步输入复位计数器
timerinit_para.shadow = SHRTIMER_SHADOW_DISABLED;//使能或者禁能影子寄存器
timerinit_para.start_sync = SHRTIMER_SYNISTART_DISABLED;//同步输入启动计数器
timerinit_para.update_selection = SHRTIMER_STXUPDATETRIGGER_MASTER;//SHRTIMER_STXUPDATETRIGGER_NONE;//SHRTIMER_MT_ST_UPDATE_SELECTION_INDEPENDENT;//更新事件源选择
shrtimer_timers_waveform_init(SHRTIMER0, SHRTIMER_MASTER_TIMER, &timerinit_para);
/*************SLAVE_TIMER*************/
timerinit_para.cnt_bunch = SHRTIMER_TIMERBUNCHNMODE_MAINTAINCLOCK;
timerinit_para.dac_trigger = SHRTIMER_DAC_TRIGGER_NONE;
timerinit_para.half_mode = SHRTIMER_HALFMODE_DISABLED; //互补模式使能
timerinit_para.repetition_update = SHRTIMER_UPDATEONREPETITION_DISABLED;
timerinit_para.reset_sync = SHRTIMER_SYNCRESET_DISABLED;
timerinit_para.shadow = SHRTIMER_SHADOW_DISABLED;
timerinit_para.start_sync = SHRTIMER_SYNISTART_DISABLED;
timerinit_para.update_selection = SHRTIMER_STXUPDATETRIGGER_NONE;//SHRTIMER_MT_ST_UPDATE_SELECTION_INDEPENDENT;//更新事件源选择
shrtimer_timers_waveform_init(SHRTIMER0, SHRTIMER_SLAVE_TIMER0, &timerinit_para);
shrtimer_timers_waveform_init(SHRTIMER0, SHRTIMER_SLAVE_TIMER1, &timerinit_para);
shrtimer_timers_waveform_init(SHRTIMER0, SHRTIMER_SLAVE_TIMER2, &timerinit_para);
/*************配置Slave_TIMER的波形*************/
//====SHRTIMER_MASTER_TIMER==================================
shrtimer_timercfg_struct_para_init(&timercfg_para);
timercfg_para.balanced_mode = SHRTIMER_STXBALANCEDMODE_DISABLED;//禁能均衡模式
/////////
timercfg_para.cnt_reset = SHRTIMER_STXCNT_RESET_UPDATE;//SHRTIMER_STXCNT_RESET_NONE; //计数器复位源
timercfg_para.deadtime_enable = SHRTIMER_STXDEADTIME_DISABLED;//使能或禁能死区功能
timercfg_para.delayed_idle = SHRTIMER_STXDELAYED_IDLE_DISABLED;//延时空闲模式
timercfg_para.fault_enable = SHRTIMER_STXFAULTENABLE_FAULT0;//SHRTIMER_STXFAULTENABLE_NONE//SHRTIMER_STXFAULTENABLE_FAULT0;//使能或禁能Slave_TIMER故障输入通道
timercfg_para.fault_protect = SHRTIMER_STXFAULT_PROTECT_READWRITE;//SHRTIMER_STXFAULT_PROTECT_READONLY;//使能或禁能故障保护功能
//////
timercfg_para.reset_update = SHRTIMER_STXUPDATEONRESET_ENABLED;//SHRTIMER_STXUPDATEONRESET_DISABLED;//计数器复位产生更新事件
timercfg_para.update_source = SHRTIMER_STXUPDATETRIGGER_NONE;//更新事件由其他定时器更新事件产生
shrtimer_slavetimer_waveform_config(SHRTIMER0, SHRTIMER_MASTER_TIMER, &timercfg_para);
//==========SHRTIMER_SLAVE_TIMER0=============
timercfg_para.balanced_mode = SHRTIMER_STXBALANCEDMODE_DISABLED;//使能或禁能均衡模式
timercfg_para.deadtime_enable = SHRTIMER_STXDEADTIME_ENABLED; //死区使能
timercfg_para.delayed_idle = SHRTIMER_STXDELAYED_IDLE_DISABLED;
//timercfg_para.fault_enable = SHRTIMER_STXFAULTENABLE_FAULT0;//SHRTIMER_STXFAULTENABLE_NONE;
timercfg_para.fault_protect = SHRTIMER_STXFAULT_PROTECT_READWRITE;//SHRTIMER_STXFAULT_PROTECT_READONLY;//SHRTIMER_STXFAULT_PROTECT_READONLY;//
timercfg_para.reset_update = SHRTIMER_STXUPDATEONRESET_DISABLED;
timercfg_para.update_source = SHRTIMER_STXUPDATETRIGGER_NONE;
timercfg_para.fault_enable = SHRTIMER_STXFAULTENABLE_FAULT0;//使能或禁能Slave_TIMER故障输入通道;
timercfg_para.cnt_reset = SHRTIMER_STXCNT_RESET_MASTER_CMP0; //这里设置从定时器起始计数点
shrtimer_slavetimer_waveform_config(SHRTIMER0, SHRTIMER_SLAVE_TIMER0, &timercfg_para);
timercfg_para.fault_enable = SHRTIMER_STXFAULTENABLE_FAULT1;//使能或禁能Slave_TIMER故障输入通道;
timercfg_para.cnt_reset = SHRTIMER_STXCNT_RESET_MASTER_CMP1; //这里设置从定时器起始计数点 SHRTIMER_STXCNT_RESET_NONE
shrtimer_slavetimer_waveform_config(SHRTIMER0, SHRTIMER_SLAVE_TIMER1, &timercfg_para);
timercfg_para.fault_enable = SHRTIMER_STXFAULTENABLE_FAULT2;//使能或禁能Slave_TIMER故障输入通道;
timercfg_para.cnt_reset = SHRTIMER_STXCNT_RESET_MASTER_CMP2; //这里设置从定时器起始计数点
shrtimer_slavetimer_waveform_config(SHRTIMER0, SHRTIMER_SLAVE_TIMER2, &timercfg_para);
//===配置主定时器的比较寄存器值======================
shrtimer_mastertimer_compare_value_config(SHRTIMER0, SHRTIMER_COMPARE0, 80); // CMP0 配置主定时器的比较寄存器值
shrtimer_mastertimer_compare_value_config(SHRTIMER0, SHRTIMER_COMPARE1, 80); // CMP1 配置主定时器的比较寄存器值
shrtimer_mastertimer_compare_value_config(SHRTIMER0, SHRTIMER_COMPARE2, 80); // CMP2 配置主定时器的比较寄存器值
//死区配置//
shrtimer_deadtimercfg_struct_para_init(&deadtimecfg_para);
deadtimecfg_para.fallingsign_protect = SHRTIMER_DEADTIME_FALLINGSIGN_PROTECT_DISABLE;
deadtimecfg_para.falling_protect = SHRTIMER_DEADTIME_FALLING_PROTECT_DISABLE;
deadtimecfg_para.falling_sign = SHRTIMER_DEADTIME_FALLINGSIGN_POSITIVE;
deadtimecfg_para.falling_value = 0x0080;//0x0020
deadtimecfg_para.prescaler = SHRTIMER_DEADTIME_PRESCALER_MUL8;
deadtimecfg_para.risingsign_protect = SHRTIMER_DEADTIME_RISINGSIGN_PROTECT_DISABLE;
deadtimecfg_para.rising_protect = SHRTIMER_DEADTIME_RISING_PROTECT_DISABLE;
deadtimecfg_para.rising_sign = SHRTIMER_DEADTIME_RISINGSIGN_POSITIVE;
deadtimecfg_para.rising_value = 0x0080;
shrtimer_slavetimer_deadtime_config(SHRTIMER0, SHRTIMER_SLAVE_TIMER0, &deadtimecfg_para);
shrtimer_slavetimer_deadtime_config(SHRTIMER0, SHRTIMER_SLAVE_TIMER1, &deadtimecfg_para);
shrtimer_slavetimer_deadtime_config(SHRTIMER0, SHRTIMER_SLAVE_TIMER2, &deadtimecfg_para);
/*************设置SLAVE_TIMER0/1/2的PWM比较值,没有实质作用,可以不定义*************/
shrtimer_comparecfg_struct_para_init(&comparecfg_para);
comparecfg_para.compare_value = 100;
shrtimer_slavetimer_waveform_compare_config(SHRTIMER0, SHRTIMER_SLAVE_TIMER0, SHRTIMER_COMPARE0, &comparecfg_para);
shrtimer_slavetimer_waveform_compare_config(SHRTIMER0, SHRTIMER_SLAVE_TIMER1, SHRTIMER_COMPARE0, &comparecfg_para);
shrtimer_slavetimer_waveform_compare_config(SHRTIMER0, SHRTIMER_SLAVE_TIMER2, SHRTIMER_COMPARE0, &comparecfg_para);
/**************通道配置*************/
shrtimer_channel_outputcfg_struct_para_init(&outcfg_para);
outcfg_para.carrier_mode = SHRTIMER_CHANNEL_CARRIER_DISABLED;//使能或禁能载波模式
outcfg_para.deadtime_bunch = SHRTIMER_CHANNEL_BUNCH_ENTRY_REGULAR;//在突发模式进入IDLE状态前是否插入死区时间
outcfg_para.fault_state = SHRTIMER_CHANNEL_FAULTSTATE_HIGHZ;//SHRTIMER_CHANNEL_FAULTSTATE_HIGHZ;//故障时通道输出状态;//故障时通道输出状态
outcfg_para.idle_bunch = SHRTIMER_CHANNEL_BUNCH_IDLE_DISABLE;//通道是否受突发模式控制
outcfg_para.idle_state = SHRTIMER_CHANNEL_IDLESTATE_INACTIVE;//通道输出的空闲状态
outcfg_para.polarity = SHRTIMER_CHANNEL_POLARITY_HIGH;//通道输出极性
outcfg_para.reset_request = SHRTIMER_CHANNEL_RESET_PER;//产生通道输出复位请求的事件
outcfg_para.set_request = SHRTIMER_CHANNEL_SET_CMP0; //SHRTIMER_SLAVE_TIMER0.CMP0产生通道输出置位请求的事件 SHRTIMER_CHANNEL_SET_UPDATE
shrtimer_slavetimer_waveform_channel_config(SHRTIMER0, SHRTIMER_SLAVE_TIMER0, SHRTIMER_ST1_CH0, &outcfg_para);
shrtimer_slavetimer_waveform_channel_config(SHRTIMER0, SHRTIMER_SLAVE_TIMER0, SHRTIMER_ST1_CH1, &outcfg_para);
shrtimer_channel_outputcfg_struct_para_init(&outcfg_para);
outcfg_para.carrier_mode = SHRTIMER_CHANNEL_CARRIER_DISABLED;
outcfg_para.deadtime_bunch = SHRTIMER_CHANNEL_BUNCH_ENTRY_REGULAR;
outcfg_para.fault_state = SHRTIMER_CHANNEL_FAULTSTATE_HIGHZ;//SHRTIMER_CHANNEL_FAULTSTATE_INACTIVE
//INACTIVE=0 ACTIVE=1 HIGHZ
//outcfg_para.fault_state = SHRTIMER_CHANNEL_FAULTSTATE_HIGHZ;//故障时通道输出状态;//故障时通道输出状态
outcfg_para.idle_bunch = SHRTIMER_CHANNEL_BUNCH_IDLE_DISABLE;
outcfg_para.idle_state = SHRTIMER_CHANNEL_IDLESTATE_INACTIVE;
outcfg_para.polarity = SHRTIMER_CHANNEL_POLARITY_HIGH;
outcfg_para.reset_request = SHRTIMER_CHANNEL_RESET_PER;//产生通道输出复位请求的事件
outcfg_para.set_request = SHRTIMER_CHANNEL_SET_CMP0; //SHRTIMER_SLAVE_TIMER1.CMP0产生通道输出置位请求的事件
shrtimer_slavetimer_waveform_channel_config(SHRTIMER0, SHRTIMER_SLAVE_TIMER1, SHRTIMER_ST1_CH0, &outcfg_para);
shrtimer_slavetimer_waveform_channel_config(SHRTIMER0, SHRTIMER_SLAVE_TIMER1, SHRTIMER_ST1_CH1, &outcfg_para);
shrtimer_channel_outputcfg_struct_para_init(&outcfg_para);
outcfg_para.carrier_mode = SHRTIMER_CHANNEL_CARRIER_DISABLED;
outcfg_para.deadtime_bunch = SHRTIMER_CHANNEL_BUNCH_ENTRY_REGULAR;
outcfg_para.fault_state = SHRTIMER_CHANNEL_FAULTSTATE_HIGHZ;//故障时通道输出状态;SHRTIMER_CHANNEL_FAULTSTATE_HIGHZ
outcfg_para.idle_bunch = SHRTIMER_CHANNEL_BUNCH_IDLE_DISABLE;
outcfg_para.idle_state = SHRTIMER_CHANNEL_IDLESTATE_INACTIVE;
outcfg_para.polarity = SHRTIMER_CHANNEL_POLARITY_HIGH;
outcfg_para.reset_request = SHRTIMER_CHANNEL_RESET_PER;//产生通道输出复位请求的事件
outcfg_para.set_request = SHRTIMER_CHANNEL_SET_CMP0;//SHRTIMER_SLAVE_TIMER2.CMP0产生通道输出置位请求的事件
shrtimer_slavetimer_waveform_channel_config(SHRTIMER0, SHRTIMER_SLAVE_TIMER2, SHRTIMER_ST2_CH0, &outcfg_para);
shrtimer_slavetimer_waveform_channel_config(SHRTIMER0, SHRTIMER_SLAVE_TIMER2, SHRTIMER_ST2_CH1, &outcfg_para);
//====================================================================
/* configure the fault input故障输入配置 */
shrtimer_faultcfg_parameter_struct faultcfg_para;
shrtimer_faultcfg_struct_para_init (&faultcfg_para);
faultcfg_para.control = SHRTIMER_FAULT_CHANNEL_ENABLE;//使能或禁能故障输入
faultcfg_para.filter = 0x0;//故障输入数字滤波,0x0~0xF
faultcfg_para.polarity = SHRTIMER_FAULT_POLARITY_HIGH;//故障输入极性 SHRTIMER_FAULT_POLARITY_LOW
faultcfg_para.protect = SHRTIMER_FAULT_PROTECT_ENABLE;//保护故障输入配置
faultcfg_para.source = SHRTIMER_FAULT_SOURCE_PIN;//故障输入源 hrtim
shrtimer_fault_config(SHRTIMER0, SHRTIMER_FAULT_0, &faultcfg_para);
shrtimer_fault_config(SHRTIMER0, SHRTIMER_FAULT_1, &faultcfg_para);
shrtimer_fault_config(SHRTIMER0, SHRTIMER_FAULT_2, &faultcfg_para);
shrtimer_fault_prescaler_config(SHRTIMER0, SHRTIMER_FAULT_PRESCALER_DIV1);
shrtimer_fault_input_enable(SHRTIMER0, SHRTIMER_FAULT_0);//故障输入使能
shrtimer_fault_input_enable(SHRTIMER0, SHRTIMER_FAULT_1);//故障输入使能
shrtimer_fault_input_enable(SHRTIMER0, SHRTIMER_FAULT_2);//故障输入使能
//shrtimer_common_interrupt_enable(SHRTIMER0,SHRTIMER_INT_FLAG_FLT0);// SHRTIMER_INT_FLT0 SHRTIMER_INT_FLAG_FLT0
//shrtimer_common_interrupt_enable(SHRTIMER0,SHRTIMER_INT_FLAG_FLT1);
//shrtimer_common_interrupt_enable(SHRTIMER0,SHRTIMER_INT_FLAG_FLT2);
//nvic_irq_enable(SHRTIMER_IRQ0_IRQn, 0, 4);//开启中断向量(调用处理函数名)
/* clear the Master_TIMER and Slave_TIMER interrupt flag */
shrtimer_timers_interrupt_flag_clear(SHRTIMER0, SHRTIMER_MASTER_TIMER, SHRTIMER_MT_ST_INT_REP);
/* enable the Master_TIMER and Slave_TIMER interrupt SHRTIMER_SLAVE_TIMER0*/
shrtimer_timers_interrupt_enable(SHRTIMER0,SHRTIMER_MASTER_TIMER , SHRTIMER_MT_ST_INT_REP);//SHRTIMER_ST_INT_CAP0
//shrtimer_timers_interrupt_enable(SHRTIMER0,SHRTIMER_SLAVE_TIMER0 , SHRTIMER_MT_ST_INT_CMP0);//SHRTIMER_ST_INT_CAP0
//shrtimer_timers_interrupt_enable(SHRTIMER0,SHRTIMER_SLAVE_TIMER1 , SHRTIMER_MT_ST_INT_CMP0)//SHRTIMER_MT_ST_INT_CMP0
//shrtimer_timers_interrupt_enable(SHRTIMER0,SHRTIMER_SLAVE_TIMER2 , SHRTIMER_MT_ST_INT_CMP0)//SHRTIMER_MT_ST_INT_CMP0
/* enable NVIC request */
nvic_irq_enable(SHRTIMER_IRQ0_IRQn, 0U, 0U);
//nvic_irq_enable(SHRTIMER_IRQ1_IRQn, 0U, 2U);
//nvic_irq_enable(SHRTIMER_IRQ2_IRQn, 0U, 1U);
//nvic_irq_enable(SHRTIMER_IRQ3_IRQn, 0U, 3U);
//=========通道使能=========================================================
shrtimer_output_channel_enable(SHRTIMER0, SHRTIMER_ST0_CH0);//通道ST0_CH0使能
shrtimer_output_channel_enable(SHRTIMER0, SHRTIMER_ST0_CH1);//通道ST0_CH1使能
shrtimer_output_channel_enable(SHRTIMER0, SHRTIMER_ST1_CH0);//通道ST1_CH0使能
shrtimer_output_channel_enable(SHRTIMER0, SHRTIMER_ST1_CH1);//通道ST1_CH1使能
shrtimer_output_channel_enable(SHRTIMER0, SHRTIMER_ST2_CH0);//通道ST2_CH0使能
shrtimer_output_channel_enable(SHRTIMER0, SHRTIMER_ST2_CH1);//通道ST2_CH1使能
//=======定时器计数使能====================================================
shrtimer_timers_counter_enable(SHRTIMER0, SHRTIMER_MT_COUNTER);//定时器计数使能
shrtimer_timers_counter_enable(SHRTIMER0, SHRTIMER_ST0_COUNTER);
shrtimer_timers_counter_enable(SHRTIMER0, SHRTIMER_ST1_COUNTER);
shrtimer_timers_counter_enable(SHRTIMER0, SHRTIMER_ST2_COUNTER);
} |