|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2013-9-4 15:02:36
|
显示全部楼层
.titrwh 发表于 2013-9-4 14:51 ![](static/image/common/back.gif)
电路图啊,都不知道是那种方法驱动的,程序也没有,神也帮不了你。 ...
定时器的配置;
void Tim1_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_DeInit(TIM3);
TIM_InternalClockConfig(TIM3);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 250; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 ///此处是重点,设定重的时间长度
//TIM_Period设置了在下一个更新事件装入活动的自动重装载寄存器周期的值。它的取值必须在0x0000和0xFFFF之间
TIM_TimeBaseStructure.TIM_Prescaler =4000; //设置用来作为TIMx时钟频率除数的预分频值 不分频
//TIM_Prescaler设置了用来作为TIMx时钟频率除数的预分频值。它的取值必须在0x0000和0xFFFF之间
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式,TIM_CounterMode_DownTIM向下计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
/* Output Compare Active Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_Pulse = 50; //设置待装入捕获比较寄存器的脉冲值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性高
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIMx在CCR1上的预装载寄存器
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //PWM功能使能
TIM_OCInitStructure.TIM_Pulse = 50; //写比较值(占空比输入)
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //设置正反相位,是先高电平还是先低电平
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); ///TIM_OC1PreloadConfig 里面的1 是重点
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable); ///TIM_OC1PreloadConfig 里面的1 是重点
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable); ///TIM_OC1PreloadConfig 里面的1 是重点
TIM_ARRPreloadConfig(TIM3, ENABLE); //使能TIMx在ARR上的预装载寄存器
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE); //使能TIMx外设
}
main函数中:
while(1)
{
Delay_Ms(1000); //无论这里是多少,占空比都不会变。
TIM_SetCompare1(TIM3,20);
TIM_SetCompare2(TIM3,30); //通过改变TIM3->CCR2的值来改变占空比
TIM_SetCompare3(TIM3,40);
TIM_SetCompare4(TIM3,50);
}
驱动器用的是ULN2803。
|
|