Chinaunix
标题:
赏析之 M3 TIM1 PWM 输出互补形式脉宽波 (19)
[打印本页]
作者:
yuweixian4230
时间:
2011-12-21 08:41
标题:
赏析之 M3 TIM1 PWM 输出互补形式脉宽波 (19)
工程代码:
12_PWM1互补输出脉冲.rar
使PWM功能,输出互补形式的脉宽波
PA8 pwm输出, PB13互补pwm输出
int
main
(
)
{
u16 dat
=
1
;
RCC_Configuration
(
)
;
delay_init
(
)
;
/
/
LED_Init
(
)
;
TIM1_GPIO_Configuration
(
)
;
TIM1_Configuration
(
)
;
while
(
1
)
{
delay_us
(
1000
)
;
/
/
延时
if
(
dat
<
0xfe0
)
dat
=
dat
+
20
;
/
/
延时一定时间脉冲宽度
+
20
else
dat
=
0
;
PWMSet
(
0xfff
,
dat
)
;
/
/
设置占空比
}
return 0
;
}
#ifndef __TIM1_H
#define __TIM1_H
#include
"stm32f10x_tim.h"
#include
"stm32f10x_gpio.h"
#include
"stm32f10x_type.h"
void TIM1_GPIO_Configuration
(
void
)
;
void TIM1_Configuration
(
void
)
;
void PWMSet
(
u16 base_wide
,
u16 pulse_wide
)
;
#endif
#
if
0
/
*
硬件连接:指示灯连接 PA8
现 象:灯渐渐变亮, 将LED灯连接PB13 ,灯渐渐变暗
功能描述:使WM功能,输出互补形式的脉宽波
使用tim1 PWM 例子
*
/
int
main
(
)
{
u16 dat
=
1
;
RCC_Configuration
(
)
;
delay_init
(
)
;
/
/
LED_Init
(
)
;
TIM1_GPIO_Configuration
(
)
;
TIM1_Configuration
(
)
;
while
(
1
)
{
delay_us
(
1000
)
;
/
/
延时
if
(
dat
<
0xfe0
)
dat
=
dat
+
20
;
/
/
延时一定时间脉冲宽度
+
20
else
dat
=
0
;
PWMSet
(
0xfff
,
dat
)
;
/
/
设置占空比
}
}
#endif
#include
"tim1.h"
/
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
名 称:GPIO_Configuration
(
)
*
功 能:配置输入输出
*
接口参数:
*
全局变量:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
*
说明:使用库函数,配置IO口
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
/
void TIM1_GPIO_Configuration
(
void
)
{
GPIO_InitTypeDef GPIO_InitStructure
;
/
*
PA8设置为输出,PWM OC1输出
*
/
GPIO_InitStructure
.
GPIO_Pin
=
GPIO_Pin_8
;
GPIO_InitStructure
.
GPIO_Mode
=
GPIO_Mode_AF_PP
;
GPIO_InitStructure
.
GPIO_Speed
=
GPIO_Speed_50MHz
;
GPIO_Init
(
GPIOA
,
&
GPIO_InitStructure
)
;
/
*
PB13设置为输出 PWM OC1互补输出
*
/
GPIO_InitStructure
.
GPIO_Pin
=
GPIO_Pin_13
;
GPIO_InitStructure
.
GPIO_Mode
=
GPIO_Mode_AF_PP
;
GPIO_InitStructure
.
GPIO_Speed
=
GPIO_Speed_50MHz
;
GPIO_Init
(
GPIOB
,
&
GPIO_InitStructure
)
;
}
/
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
名 称:TIM1_Configuration
(
)
*
功 能
:
定时器1配置
*
接口参数:
*
全局变量:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
*
说明:配置为PWM输出模式,输出引脚PA8
,
互补输出到PB13
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
/
void TIM1_Configuration
(
void
)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure
;
TIM_OCInitTypeDef TIM_OCInitStructure
;
TIM_DeInit
(
TIM1
)
;
TIM_TimeBaseStructure
.
TIM_Prescaler
=
0x0
;
/
/
时钟预分频得CK_INT时钟提供给计数器 APB2时钟
/
(
n
+
1
)
n
=
1
~
65536
TIM_TimeBaseStructure
.
TIM_CounterMode
=
TIM_CounterMode_Up
;
/
/
向上计数模式
TIM_TimeBaseStructure
.
TIM_Period
=
0xffff
;
/
/
自动重装载值
TIM_TimeBaseStructure
.
TIM_ClockDivision
=
TIM_CKD_DIV1
;
/
/
时钟倍频因子,输出时钟供给死区发生器等
TIM_TimeBaseStructure
.
TIM_RepetitionCounter
=
0x0
;
/
/
周期计数器值 0
~
255
TIM_TimeBaseInit
(
TIM1
,
&
TIM_TimeBaseStructure
)
;
/
/
初始化
TIM_OCInitStructure
.
TIM_OCMode
=
TIM_OCMode_PWM2
;
/
/
PWM2模式
TIM_OCInitStructure
.
TIM_OutputState
=
TIM_OutputState_Enable
;
/
/
信号输出到对应的输出引脚
TIM_OCInitStructure
.
TIM_OutputNState
=
TIM_OutputNState_Enable
;
/
/
互补信号输出到对应的输出引脚
TIM_OCInitStructure
.
TIM_Pulse
=
0
;
/
/
脉冲宽度
TIM_OCInitStructure
.
TIM_OCPolarity
=
TIM_OCNPolarity_High
;
/
/
互补输出高电平有效
TIM_OCInitStructure
.
TIM_OCNPolarity
=
TIM_OCNPolarity_High
;
/
/
互补输出高电平有效
TIM_OCInitStructure
.
TIM_OCIdleState
=
TIM_OCIdleState_Set
;
/
/
输出空闲状态为1
TIM_OCInitStructure
.
TIM_OCNIdleState
=
TIM_OCIdleState_Reset
;
/
/
互补输出空闲状态为0
TIM_OC1Init
(
TIM1
,
&
TIM_OCInitStructure
)
;
/
/
OC1通道初始化
TIM_Cmd
(
TIM1
,
ENABLE
)
;
/
/
使能定时器1
TIM_CtrlPWMOutputs
(
TIM1
,
ENABLE
)
;
/
/
使能PWM输出
/
/
时钟使能 在 rcc 中声明了
/
/
RCC_APB2PeriphClockCmd
(
RCC_APB2Periph_TIM1
,
ENABLE
)
;
/
/
给TIM1提供时钟
}
/
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
名 称:PWMSet
(
)
*
功 能
:
设置PWM基波宽度,占空比
*
接口参数:base_wide 基波宽度
pulse_wide 脉冲宽度
*
全局变量:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
*
说明:
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
/
void PWMSet
(
u16 base_wide
,
u16 pulse_wide
)
{
TIM_SetAutoreload
(
TIM1
,
base_wide
)
;
TIM_SetCompare1
(
TIM1
,
pulse_wide
)
;
}
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2