刚开始看书自学,想尝试使用TIM1输出7路方波,按照书上的例程做的,但是没有书上的stm32f10x_heads_t1.h这个头文件,不知道应该怎么改动,能不能给我提供一个改动的思路,在此先行谢过。
之前找了一下类似的问题但是没有找到,看到有人说看外设库例程学习,求解应该在哪里找。
代码如下:
#include "stm32f10x_heads_t1.h" //注意
#include "HelloRobot.h"
TIM1_TimeBaseInitTypeDef TIM1_TimeBaseStructure;
TIM1_OCInitTypeDef TIM1_OCInitStructure;
TIM1_BDTRInitTypeDefTIM1_BDTRInitStructure;
u16 capture=0;
u16 CCR1_Val=1800;//设置TIM1 通道1 输出占空比:50%
u16 CCR2_Val=900;//设置TIM1 通道1 输出占空比:25%
u16 CCR3_Val=450;//设置TIM1 通道1 输出占空比:12.5%
Tim1_Init()
{ TIM1_DeInit();//将外设TIM1 寄存器重设为默认值
/*Time Base configuration, 设置时间基准 */
TIM1_TimeBaseStructure.TIM1_Prescaler=0x0;//TIM1 时钟频率的预分频值
TIM1_TimeBaseStructure.TIM1_CounterMode=TIM1_CounterMode_Up;
TIM1_TimeBaseStructure.TIM1_Period=3600-1; //自动重装在寄存器周期值
TIM1_TimeBaseStructure.TIM1_ClockDivision=0x0;//时钟分割值
TIM1_TimeBaseStructure.TIM1_RepetitionCounter=0x0;
TIM1_TimeBaseInit(&TIM1_TimeBaseStructure);//初始化TIM1 的时间基数数据
//Channel 1,2,3 and 4 Configuration in PWMmode
/*选择定时器输出比较为PWM 模式 2。在向上计数时,当TIM1_CNT<TIM1_CCR1时,通道1为无效电平,否则为有效电平;
在向下计数时,当TIM1_CNT>TIM1_ccr1时,通道1为有效电平,否则为无效电平,如果选择PWM模式1,则相反。*/
TIM1_OCInitStructure.TIM1_OCMode=TIM1_OCMode_PWM2;
//选择输出比较状态,以及互补输出比较状态
TIM1_OCInitStructure.TIM1_OutputState=TIM1_OutputState_Enable;
TIM1_OCInitStructure.TIM1_OutputNState=TIM1_OutputNState_Enable;
//设置通道1捕获比较寄存器的脉冲值——占空比为%
TIM1_OCInitStructure.TIM1_Pulse=CCR1_Val;
//输出极性和互补输出极性的有效电平为低
TIM1_OCInitStructure.TIM1_OCPolarity=TIM1_OCPolarity_Low;
TIM1_OCInitStructure.TIM1_OCNPolarity=TIM1_OCNPolarity_Low;
//选择空闲状态下的非工作状态(MOE=0时,设置TIM1输出比较空闲状态)
TIM1_OCInitStructure.TIM1_OCIdleState=TIM1_OCIdleState_Set;
TIM1_OCInitStructure.TIM1_OCNIdleState=TIM1_OCIdleState_Reset;
TIM1_OC1Init(&TIM1_OCInitStructure);
//设置通道2捕获比较寄存器的脉冲值——占空比为25%
TIM1_OCInitStructure.TIM1_Pulse=CCR2_Val;
TIM1_OC2Init(&TIM1_OCInitStructure);
//设置通道3捕获比较寄存器的脉冲值——占空比为12.5%
TIM1_OCInitStructure.TIM1_Pulse=CCR3_Val;
TIM1_OC3Init(&TIM1_OCInitStructure);
/*Automatic Ouput enable,Break,dead time and lock configuration*/ //设置在运行模式下非工作状态
TIM1_BDTRInitStructure.TIM1_OSSRState=TIM1_OSSRState_Enable;
TIM1_BDTRInitStructure.TIM1_OSSIState=TIM1_OSSIState_Enable;
//写保护:锁定级别为1,不能写入TIM1_BDTR寄存器的DTG/BKE/BKP/AOE位、TIM1_CR2寄存器的OISx、OISxN位。系统复位后,只能写入一次LOCK位,其内容冻结至复位。
TIM1_BDTRInitStructure.TIM1_LOCKLevel=TIM1_LOCKLevel_1;
TIM1_BDTRInitStructure.TIM1_DeadTime=0x75;
TIM1_BDTRInitStructure.TIM1_Break=TIM1_Break_Enable;
//配置刹车(中止)输入信号特性:高电平有效,允许自动输出
TIM1_BDTRInitStructure.TIM1_BreakPolarity=TIM1_BreakPolarity_High;
TIM1_BDTRInitStructure.TIM1_AutomaticOutput=TIM1_AutomaticOutput_Enable;
TIM1_BDTRConfig(&TIM1_BDTRInitStructure);
}
int main(void)
{
BSP_Init();
Tim1_Init(); //定时器初始化函数
TIM1_Cmd(ENABLE); //TIM1 counter enable,启动TIM1
TIM1_CtrlPWMOutputs(ENABLE); //TIM1 Main Output Enable,输出PWM
while(1);
}
在hellorobot.h中配置管脚的输入输出模式代码如下:
void GPIO_Configuration()
{
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11;
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_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
|