最近在做一个模拟速度脉冲的功能,利用gpio翻转 产生一个方波。需要4个引脚。比如 A1 A2 A3 A4.如果模拟速度是前进的,需要A1产生的方波比A2产生的方波 快 四分之一的周期。同理A3、A4。
但是现在我A2做了延时四分之一 延时,但是收到的波没有延时效果,还是同时过去的。我的代码如下:(芯片为stm103zet6)
#define GENERAL_TIM TIM6
#define GENERAL_TIM_APBxClock_FUN RCC_APB1PeriphClockCmd
#define GENERAL_TIM_CLK RCC_APB1Periph_TIM6
#define GENERAL_TIM_Period 71
#define GENERAL_TIM_Prescaler 4
#define GENERAL_TIM_IRQTim (GENERAL_TIM_Period+1)*(GENERAL_TIM_Prescaler+1)/72000//ÖD¶Ïê±¼ä
#define GENERAL_TIM_IRQ TIM6_IRQn
#define GENERAL_TIM_IRQHandler TIM6_IRQHandler
static void GENERAL_TIM_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
GENERAL_TIM_APBxClock_FUN(GENERAL_TIM_CLK,ENABLE);
TIM_TimeBaseStructure.TIM_Period=GENERAL_TIM_Period; //71
TIM_TimeBaseStructure.TIM_Prescaler= GENERAL_TIM_Prescaler;// 4 主频为72 中断时间为5us
TIM_TimeBaseInit(GENERAL_TIM, &TIM_TimeBaseStructure);
TIM_ClearFlag(GENERAL_TIM, TIM_FLAG_Update);
TIM_ITConfig(GENERAL_TIM,TIM_IT_Update,ENABLE);
TIM_Cmd(GENERAL_TIM, ENABLE);
//TIM_CtrlPWMOutputs(TIM1,ENABLE);
}
void GENERAL_TIM_IRQHandler(void)//中断产生时间为5us
{
if(TIM_GetITStatus(TIM6,TIM_IT_Update) != RESET)
{
if(speed_str.tim1_ch1_count>=10000000) speed_str.tim1_ch1_count=0;
else speed_str.tim1_ch1_count++;
if(speed_str.tim1_ch2_count>=10000000) speed_str.tim1_ch2_count=0;
else speed_str.tim1_ch2_count++;
}
TIM_ClearITPendingBit(TIM6,TIM_IT_Update);
}
void SPEED_TIM_Init(void)
{
GENERAL_TIM_GPIO_Config();
GENERAL_TIM_NVIC_Config();
GENERAL_TIM_Mode_Config();
}
void SpeedSet(void)
{
float freq1,freq2;
u32 phase_difference_1,phase_difference_0,phase_difference1,phase_difference2,phase_difference3,phase_difference4;//Ïàλ2î
u32 cycle1,cycle2;
speed_str.delay_star=0;
if(0 == speed_str.Diameter)
{
speed_str.Diameter = 840;
}
freq1 =(float)(speed_str.speed1*200/(3.14*speed_str.Diameter/10));
cycle1=200000/freq1;
speed_str.phase_difference1=cycle1/4; //2îËÄ·ÖÖ®ò»¸öÏàλ2î
if(0x55 == speed_str.Direction1)//ÏòÇ°
{
speed_str.delay_star |=0x02;
phase_difference1 = 0;
phase_difference2 = speed_str.phase_difference1;
}else //Ïòoó
{
speed_str.delay_star |=0x01;
phase_difference1 = 0;
phase_difference2 = speed_str.phase_difference1;
}
speed_str.cycle1 = cycle1/2;
speed_str.cycle2 = cycle2/2;
speed_str.speed_en = speed_str.speedEN;
speed_str.tim1_ch2_count = 0;
speed_str.tim1_ch1_count = 0;
speed_str.tim8_ch3_count = 0;
speed_str.tim8_ch4_count = 0;
speed_str.tim1_delay_time = phase_difference1;
speed_str.tim2_delay_time = phase_difference2;
speed_str.tim3_delay_time = phase_difference3;
speed_str.tim4_delay_time = phase_difference4;
}
****************************************************************************************/
void speedput(void)
{
if(speed_str.speedsetEN==1)
{
SpeedSet();
speed_str.speedsetEN=0;
}
if(speed_str.speed_en==0x0f)
{
if(speed_str.tim1_ch1_count>=(speed_str.cycle1+speed_str.tim1_delay_time))
{
if(speed_str.gpio1_on)
{
GPIOE->ODR |= GPIO_Pin_10|GPIO_Pin_12;
speed_str.gpio1_on=0;
}else
{
GPIOE->ODR &= (~GPIO_Pin_10)&(~GPIO_Pin_12);
speed_str.gpio1_on=1;
}
speed_str.tim1_ch1_count=0;
speed_str.tim1_delay_time=0;
}
if(speed_str.tim1_ch2_count>=(speed_str.cycle1+speed_str.tim2_delay_time))
{
if(speed_str.gpio2_on)
{
GPIOE->ODR |= GPIO_Pin_11|GPIO_Pin_13;
speed_str.gpio2_on=0;
}else
{
GPIOE->ODR &= (~GPIO_Pin_11)&(~GPIO_Pin_13);
speed_str.gpio2_on=1;
}
speed_str.tim1_ch2_count=0;
speed_str.tim2_delay_time=0;
}
}else
{
GPIO_WriteBit(GPIOE,GENERAL_TIM_CH1_PIN,(BitAction)0);
GPIO_WriteBit(GPIOE,GENERAL_TIM_CH2_PIN,(BitAction)0);
GPIO_WriteBit(GPIOE,GENERAL_TIM_CH3_PIN,(BitAction)0);
GPIO_WriteBit(GPIOE,GENERAL_TIM_CH4_PIN,(BitAction)0);
}
}
一周热门 更多>