打印
[应用相关]

STM32F103PWM+重映射

[复制链接]
1645|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
tier|  楼主 | 2012-7-18 23:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
01.我的开发板是学习板,很多管脚没有引出。弄到定时器产生PWM这一块,想用下管脚重映射功能,毕竟库函数没有像操作寄存器那样容易理解。所以一开始着实糊涂了一会,最后还是给调出来了。结合很多别人的例程,但是发现都不够详细,所以就共享下,如果有同样问题的同学,希望能给你帮助。下面就共享下main.c。声明:以下是本人开发板的管道对应引脚

02.

03.

04.

05.复用功能

06.没有重映射

07.部分重映射

08.完全重映射

09.

10.TIM3_CH1

11.PA6

12.PB4

13.PC6

14.

15.CH2

16.PA7

17.PB5

18.PC7

19.

20.CH3

21.PB0

22.PB0

23.PC8

24.

25.CH4

26.PB1

27.PB1

28.PC9

29./**重映射 tim3 使得PC7 PC8输出Pwm波   

30.  ******************************************************************************

31.  * @file    Project/Template/main.c

32.  * @author  MCD Application Team

33.  * @version V3.0.0

34.  * @date    04/06/2009

35.  * @brief   Main program body

36.  ******************************************************************************

37.  * @copy

38.  *

39.  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS

40.  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE

41.  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY

42.  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING

43.  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE

44.  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.

45.  *

46.  * <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>

47.  */

48./* Includes ------------------------------------------------------------------*/

49.#include "stm32f10x.h"

50.#include "stm32f10x_tim.h"

51.#include "misc.h"

52.void GPIO_AFIODeInit(void);//这个函数不能落下,否则不能成功

53.TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;//配置PWM需要使用的结构体声名

54.TIM_OCInitTypeDef          TIM_OCInitStructure;

55.u16 CCR3_Val =250;

56.u16 CCR4_Val =250;

57.               

58.void GPIO_Config(void);

59.void Timer_Config(void);

60./**

61.  * @brief  Delay program.

62.  * @param  None

63.  * @retval : None

64.  */

65.void  Delay (u32 nCount)

66.{

67.  for(; nCount != 0; nCount--);

68.}

69./**

70.  * @brief  Main program.

71.  * @param  None

72.  * @retval : None

73.  */

74.int main(void)

75.{

76./* Setup STM32 system (clock, PLL and Flash configuration) */

77.SystemInit();

78.GPIO_Config();

79.Timer_Config();

80./* Infinite loop */

81.while (1) {

82.               }

83.}

84./**

85.  * @brief  GPIO_Config program.

86.  * @param  None

87.  * @retval : None

88.  */

89.void GPIO_Config(void)

90.{

91.  GPIO_InitTypeDef GPIO_InitStructure;

92.  

93.  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |            RCC_APB2Periph_GPIOC |RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);

94.

95.   GPIO_AFIODeInit();  //刚开始没有这个语句,所以一直没有重映射成功

96.   GPIO_PinRemapConfig(GPIO_FullRemap_TIM3 , ENABLE); //这个就是重映射功能函数

97./*GPIOA Configuration: TIM3 channel 3 and 4 as alternate function push-pull */

98.  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0 | GPIO_Pin_1;

99.  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //这里也要注意

100.  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  

101.  GPIO_Init(GPIOB, &GPIO_InitStructure); //这四句是为了校验究竟PB0,PB1是否还有PWM输出  

102.  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_8 | GPIO_Pin_9;

103.  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

104.  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  

105.  GPIO_Init(GPIOC, &GPIO_InitStructure);

106.  

107.  

108.}

109.void Timer_Config(void)

110.{

111.RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

112.

113.  TIM_DeInit(TIM3);

114.  TIM_TimeBaseStructure.TIM_Period = 999;  //在下一个更新事件装入活动的自动重装载寄存器周期的值

115.  TIM_TimeBaseStructure.TIM_Prescaler = 0; //  设置了用来作为 TIMx 时钟频率除数的预分频值

116.  TIM_TimeBaseStructure.TIM_ClockDivision = 0;   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  

117.  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

118.  /* PWM1 Mode configuration: Channel3 */

119.  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;  //TIM_OCMode

120.  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

121.  TIM_OCInitStructure.TIM_Pulse = CCR3_Val;      //设置了待装入捕获比较寄存器的值

122.  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //TIM_OCPolarity :TIM 输出比较极性高

123.  TIM_OC3Init(TIM3, &TIM_OCInitStructure);

124.  TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);  //使能或者失能 TIM3 在 CCR3 上的预装载寄存器

125.  /* PWM1 Mode configuration: Channel4 */

126.  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  

127.  TIM_OCInitStructure.TIM_Pulse = CCR4_Val;

128.  TIM_OC4Init(TIM3, &TIM_OCInitStructure);

129.  TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);

130.  /* TIM3 enable counter */

131.  TIM_Cmd(TIM3, ENABLE);          //开启时钟,别落下

132.}

133./**

134.  * @brief  EXTI_Config Program.

135.  * @param  None

136.  * @retval : None

137.  */

138.

139./**

140.  * @brief  Configures the nested vectored interrupt controller.

141.  * @param  None

142.  * @retval : None

143.  */

144.

145.#ifdef  USE_FULL_ASSERT

146./**

147.  * @brief  Reports the name of the source file and the source line number

148.  *   where the assert_param error has occurred.

149.  * @param file: pointer to the source file name

150.  * @param line: assert_param error line source number

151.  * @retval : None

152.  */

153.void assert_failed(uint8_t* file, uint32_t line)

154.{

155.  /* User can add his own implementation to report the file name and line number,

156.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

157.  /* Infinite loop */

158.  while (1)

159.  {

160.  }

161.}

162.#endif

163./**

164.  * @}

165.  */

166.
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

31

帖子

0

粉丝