新聞中心

STM32 TIM重映射

作者: 時(shí)間:2016-11-10 來(lái)源:網(wǎng)絡(luò) 收藏
復(fù)用功能沒(méi)有重映射部分重映射完全重映射
TIM3_CH1PA6PB4PC6
CH2PA7PB5PC7
CH3PB0PB0PC8
CH4PB1PB1PC9
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
/**重映射 tim3 使得PC7 PC8輸出Pwm波
******************************************************************************
* @file Project/Template/main.c
* @author MCD Application Team
* @version V3.0.0
* @date 04/06/2009
* @brief Main program body
******************************************************************************
* @copy
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
*

COPYRIGHT 2009 STMicroelectronics

*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32f10x_tim.h"
#include "misc.h"
voidGPIO_AFIODeInit(void);//這個(gè)函數(shù)不能落下,否則不能成功
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//配置PWM需要使用的結(jié)構(gòu)體聲名
TIM_OCInitTypeDef TIM_OCInitStructure;
u16 CCR3_Val =250;
u16 CCR4_Val =250;
voidGPIO_Config(void);
voidTimer_Config(void);
/**
* @brief Delay program.
* @param None
* @retval : None
*/
voidDelay (u32 nCount)
{
for(; nCount != 0; nCount--);
}
/**
* @brief Main program.
* @param None
* @retval : None
*/
intmain(void)
{
/* Setup STM32 system (clock, PLL and Flash configuration) */
SystemInit();
GPIO_Config();
Timer_Config();
/* Infinite loop */
while(1) {
}
}
/**
* @brief GPIO_Config program.
* @param None
* @retval : None
*/
voidGPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);
GPIO_AFIODeInit();//剛開(kāi)始沒(méi)有這個(gè)語(yǔ)句,所以一直沒(méi)有重映射成功
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3 , ENABLE);//這個(gè)就是重映射功能函數(shù)
/*GPIOA Configuration: TIM3 channel 3 and 4 as alternate function push-pull */
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);//這四句是為了校驗(yàn)究竟PB0,PB1是否還有PWM輸出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
voidTimer_Config(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_DeInit(TIM3);
TIM_TimeBaseStructure.TIM_Period = 999;//在下一個(gè)更新事件裝入活動(dòng)的自動(dòng)重裝載寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler = 0;// 設(shè)置了用來(lái)作為 TIMx 時(shí)鐘頻率除數(shù)的預(yù)分頻值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//TIM_OCMode
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;//設(shè)置了待裝入捕獲比較寄存器的值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;//TIM_OCPolarity :TIM 輸出比較極性高
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);//使能或者失能 TIM3 在 CCR3 上的預(yù)裝載寄存器
/* PWM1 Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE);//開(kāi)啟時(shí)鐘,別落下
}
/**
* @brief EXTI_Config Program.
* @param None
* @retval : None
*/
/**
* @brief Configures the nested vectored interrupt controller.
* @param None
* @retval : None
*/
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval : None
*/
voidassert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %drn", file, line) */
/* Infinite loop */
while(1)
{
}
}
#endif
/**
* @}
*/
/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/

本文引用地址:http://m.butianyuan.cn/article/201611/317499.htm


關(guān)鍵詞: STM32TIM重映

評(píng)論


技術(shù)專區(qū)

關(guān)閉