我代码跟书本上得几乎一模一样,不知道为什么不能仿真,用光盘中的例子可以仿真出波形。
代码如下:
/***********main 函数**********/
#include "stm32f10x.h"
#include "pwm_output.h"
/*
* 函数名:main
* 描述 :主函数
* 输入 :无
* 输出 :无
*/
int main(void)
{
TIM3_PWM_Init();
while(1)
{
}
}
#include"pwm_output.h"
static void TIM3_GPIO_Config(void);
static void TIM3_MODE_Config(void);
void TIM3_PWM_Init(void)
{
TIM3_GPIO_Config();
TIM3_MODE_Config();
}
static void TIM3_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //配置定时器时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);//配置GPIO时钟
/*tim为输出比较时,应该设为复用推挽模式*/
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_Init(GPIOB,&GPIO_InitStructure);
}
static void TIM3_MODE_Config(void)
{
TIM_TimeBaseInitTypeDef *pTIM_TimeBaseStructure;
TIM_OCInitTypeDef *pTIM_OCInitStructure;
/*pwm信号电平跳变值*/
u16 CCR1_Val=500;
u16 CCR2_Val=375;
u16 CCR3_Val=250;
u16 CCR4_Val=125;
/* Time base configuration */
pTIM_TimeBaseStructure->TIM_Period=999;
//当定时器从0计数到999,即为1000次,为一个定时周期
pTIM_TimeBaseStructure->TIM_Prescaler=0;
//设置预分频:不预分频,即为72MHZ.
pTIM_TimeBaseStructure->TIM_ClockDivision=TIM_CKD_DIV1;
//设置时钟分频系数:不分频
pTIM_TimeBaseStructure->TIM_CounterMode=TIM_CounterMode_Up;
//向上计数模式
TIM_TimeBaseInit(TIM3,pTIM_TimeBaseStructure);
/* pwm1 mode config:channel1 */
pTIM_OCInitStructure->TIM_OCMode=TIM_OCMode_PWM1;
pTIM_OCInitStructure->TIM_OutputState=TIM_OutputState_Enable;
pTIM_OCInitStructure->TIM_Pulse=CCR1_Val;
pTIM_OCInitStructure->TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC1Init(TIM3,pTIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);
/* pwm2 mode config:channel1 */
pTIM_OCInitStructure->TIM_OutputState=TIM_OutputState_Enable;
pTIM_OCInitStructure->TIM_Pulse=CCR2_Val;
TIM_OC2Init(TIM3,pTIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);
/* pwm3 mode config:channel1 */
pTIM_OCInitStructure->TIM_OutputState=TIM_OutputState_Enable;
pTIM_OCInitStructure->TIM_Pulse=CCR3_Val;
TIM_OC3Init(TIM3,pTIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable);
/* pwm4 mode config:channel1 */
pTIM_OCInitStructure->TIM_OutputState=TIM_OutputState_Enable;
pTIM_OCInitStructure->TIM_Pulse=CCR4_Val;
TIM_OC4Init(TIM3,pTIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3,ENABLE);
TIM_Cmd(TIM3,ENABLE);
}
各位菊苣,什么原因导致不能仿真呢,是不是target options设置有问题 |