我是靠谱客的博主 光亮茉莉,这篇文章主要介绍定时器TIM2输出pwm波,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#define TIM2_CLK_ENABLE() ( RCC->APB1ENR |= RCC_APB1ENR_TIM2EN ) #define TIM3_CLK_ENABLE() ( RCC->APB1ENR |= RCC_APB1ENR_TIM3EN ) #define TIMx_CHANNEL_GPIOA_PORT() ( RCC->IOPENR |= (RCC_IOPENR_GPIOAEN)) #define TIMx_CHANNEL_GPIOB_PORT() ( RCC->IOPENR |= (RCC_IOPENR_GPIOBEN)) arm_drv_tim_pwm_channel_config (TIM2_BASE, GPIO_A, GPIO_A_15, GPIO_AF5_TIM2, TIM_CHANNEL_1); short drv_io_config_pin( unsigned long pin, unsigned char config ) { unsigned short port, index, bits; GPIO_TypeDef* GPIO_Init_PORT; GPIO_InitTypeDef GPIO_InitStruct; port = (unsigned short)(pin >> 16); // port index for PORT A-H (0-4) if(port > (MAXIMUM_GPIO_PORT-1)) // only has 5 ports: PORT_[A~H] return 1; GPIO_Init_PORT = GPIO_PORT[port]; GPIO_InitStruct.Pin = 0; bits = 0x8000; // start from bit 15. // prepare configure register for( index = 0; index < 16; index++ ) { if( pin & bits ) { // the bit is selected. GPIO_InitStruct.Pin = bits; // config format: 0011,0001.pupd[7:6]=00=no pull-up, pull-down; ospeed[5:4]=11=high speed; otype[2]=0=push-pull; mode[1:0]=01=output. GPIO_InitStruct.Mode = (GPIOMode_TypeDef) (config & 0x03); // GPIO_Mode; GPIO_InitStruct.OType = (GPIOOType_TypeDef)((config>>2)&0x01); // GPIO_OType; GPIO_InitStruct.Speed = (GPIOSpeed_TypeDef)((config>>4)&0x03); // GPIO_OSpeed; GPIO_InitStruct.PuPd = (GPIOPuPd_TypeDef)((config>>6)&0x03); //GPIO_PuPd GPIO_Init(GPIO_Init_PORT, &GPIO_InitStruct); } bits >>= 1; } return 0; } void drv_io_af_config(unsigned long pin, unsigned char gpio_af) { unsigned short port, GPIO_PinSource; unsigned long pos; GPIO_TypeDef* GPIOx; unsigned long temp = 0x00; unsigned long temp_2 = 0x00; unsigned long temp_3 = 0x00; port = (unsigned short)(pin >> 16); // port index for PORT A-H (0-4) if(port > (MAXIMUM_GPIO_PORT-1)) // only has 5 ports: PORT_[A~H] return; GPIOx = GPIO_PORT[port]; for(GPIO_PinSource = 0x00; GPIO_PinSource < 0x10; GPIO_PinSource++) { pos = ((unsigned long)0x01) << GPIO_PinSource; if(pin & pos) //Get the port pin position { temp = (unsigned long)(gpio_af) << ((unsigned long)((unsigned long)GPIO_PinSource & (unsigned long)0x07) * 4); temp_2 = GPIOx->AFR[GPIO_PinSource >> 0x03] & (~((unsigned long)0xF << ((unsigned long)((unsigned long)GPIO_PinSource & (unsigned long)0x07) * 4))); temp_3 = temp_2 | temp; GPIOx->AFR[GPIO_PinSource >> 0x03] = temp_3; } } } void arm_drv_tim_pwm_channel_config (uint32_t u32TimerBase, uint32_t u16GpioPortNo, uint32_t u16GpioPinNo, uint32_t u16GpioAltNo, uint16_t u16TimerChannel) { TIM_TypeDef *TIMx; GPIO_TypeDef *GPIOx; TIMx = (TIM_TypeDef *)u32TimerBase; TIM_Base_InitTypeDef TIM_InitStructure; TIM_OC_InitTypeDef sConfig; // uint32_t u32PinAddr; uint32_t tmpcr1 = 0; switch (u16GpioPortNo) { case GPIO_A: GPIOx = GPIOA; break; case GPIO_B: GPIOx = GPIOB; break; case GPIO_C: GPIOx = GPIOC; break; case GPIO_D: GPIOx = GPIOD; break; } /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* TIMx Peripheral clock enable */ if ( TIMx == TIM2) { TIM2_CLK_ENABLE(); } else if ( TIMx == TIM3) { TIM3_CLK_ENABLE(); } /* Enable all GPIO Channels Clock requested */ if ( GPIOx == GPIOA) { TIMx_CHANNEL_GPIOA_PORT(); } else if ( GPIOx == GPIOB) { TIMx_CHANNEL_GPIOB_PORT(); } /*##-1.1- setup GPIO port accordingly #################################*/ /* Common configuration for all channels */ drv_io_config_pin(u16GpioPinNo, GPIO_AFO_PP_HIGH_UP); // // setup AFR with pin number drv_io_af_config(u16GpioPinNo, u16GpioAltNo); /*##-2.1- initialize the timer to be the PWM with known period ##############*/ // fill up the common initialization settings TIM_InitStructure.Prescaler= (uint32_t)(SystemCoreClock / 2000000) - 1; TIM_InitStructure.CounterMode= TIM_COUNTERMODE_UP; //TIM_InitStructure.Period= PERIOD_VALUE; TIM_InitStructure.Period= *DEVICE_CONFIG_1_BIAS_PWM_PERIOD_COUNT; TIM_InitStructure.ClockDivision= 0; //arm_drv_tim_Base_SetConfig(TIMx, &TIM_InitStructure); // can create a new standalone function for this functionality /* save CR1 content first */ tmpcr1 = TIMx->CR1; /* set the counter Mode */ tmpcr1 &= ~(TIM_CR1_DIR | TIM_CR1_CMS); tmpcr1 |= TIM_InitStructure.CounterMode; /* set the clock division*/ tmpcr1 &= ~TIM_CR1_CKD; tmpcr1 |= (uint32_t)TIM_InitStructure.ClockDivision; TIMx->CR1 = tmpcr1; /* Set the Autoreload value */ TIMx->ARR = (uint32_t)TIM_InitStructure.Period ; /* Set the Prescaler value */ TIMx->PSC = (uint32_t)TIM_InitStructure.Prescaler; /* Generate an update event to reload the Prescaler value immediatly */ TIMx->EGR = TIM_EGR_UG; //arm_drv_tim_Base_SetConfig(TIMx, &TIM_InitStructure); /*##-2- Configure the PWM channels #########################################*/ /* Common configuration for all channels */ sConfig.OCMode = TIM_OCMODE_PWM1; sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; sConfig.OCFastMode = TIM_OCFAST_DISABLE; /* configure the timer with the given channel # */ arm_drv_tim_pwm_channel_setting (TIMx, &sConfig, u16TimerChannel); /* start the timer for the desired channel */ arm_drv_tim_pwm_start (TIMx, (uint32_t)u16TimerChannel); } 1、配置GPIO口 2、初始化定时器 3、设置TIM2_CH1的PWM模式,使能TIM2的CH1输出 4、使能定时器2 5、使用PWM

最后

以上就是光亮茉莉最近收集整理的关于定时器TIM2输出pwm波的全部内容,更多相关定时器TIM2输出pwm波内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部