[STM32F1] 大家帮我看下一个程序啊

[复制链接]
1806|5
 楼主| 贪玩不回家 发表于 2015-12-27 15:48 | 显示全部楼层 |阅读模式
大家帮我看下一个程序啊   st库自带的六部换向程序   
但我用示波器量到的的是上桥三路都是高电平,全部对齐的,另外三路都是低电平啊
 楼主| 贪玩不回家 发表于 2015-12-27 15:50 | 显示全部楼层
 楼主| 贪玩不回家 发表于 2015-12-27 15:51 | 显示全部楼层
  1. /**
  2.   ******************************************************************************
  3.   * [url=home.php?mod=space&uid=288409]@file[/url]    TIM/6Steps/main.c
  4.   * [url=home.php?mod=space&uid=187600]@author[/url]  MCD Application Team
  5.   * [url=home.php?mod=space&uid=895143]@version[/url] V3.5.0
  6.   * [url=home.php?mod=space&uid=212281]@date[/url]    08-April-2011
  7.   * [url=home.php?mod=space&uid=247401]@brief[/url]   Main program body
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12.   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13.   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14.   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15.   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16.   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17.   *
  18.   * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  19.   ******************************************************************************
  20.   */

  21. /* Includes ------------------------------------------------------------------*/
  22. #include "stm32f10x.h"

  23. /** @addtogroup STM32F10x_StdPeriph_Examples
  24.   * @{
  25.   */

  26. /** @addtogroup TIM_6Steps
  27.   * @{
  28.   */

  29. /* Private typedef -----------------------------------------------------------*/
  30. /* Private define ------------------------------------------------------------*/
  31. /* Private macro -------------------------------------------------------------*/
  32. /* Private variables ---------------------------------------------------------*/
  33. TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  34. TIM_OCInitTypeDef  TIM_OCInitStructure;
  35. TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
  36. uint16_t CCR1_Val = 32767;
  37. uint16_t CCR2_Val = 24575;
  38. uint16_t CCR3_Val = 16383;
  39. uint16_t CCR4_Val = 8191;

  40. /* Private function prototypes -----------------------------------------------*/
  41. void RCC_Configuration(void);
  42. void GPIO_Configuration(void);
  43. void SysTick_Configuration(void);
  44. void NVIC_Configuration(void);

  45. /* Private functions ---------------------------------------------------------*/

  46. /**
  47.   * @brief   Main program
  48.   * @param  None
  49.   * @retval None
  50.   */
  51. int main(void)
  52. {
  53.   /*!< At this stage the microcontroller clock setting is already configured,
  54.        this is done through SystemInit() function which is called from startup
  55.        file (startup_stm32f10x_xx.s) before to branch to application main.
  56.        To reconfigure the default setting of SystemInit() function, refer to
  57.        system_stm32f10x.c file
  58.      */     
  59.            
  60.   /* System Clocks Configuration */
  61.   RCC_Configuration();

  62.   /* NVIC Configuration */
  63.   NVIC_Configuration();

  64.   /* GPIO Configuration */
  65.   GPIO_Configuration();

  66.   /* SysTick Configuration */
  67.   SysTick_Configuration();

  68.   /*-----------------------------------------------------------------------------
  69.   The STM32F10x TIM1 peripheral offers the possibility to program in advance the
  70.   configuration for the next TIM1 outputs behaviour (step) and change the configuration
  71.   of all the channels at the same time. This operation is possible when the COM
  72.   (commutation) event is used.
  73.   The COM event can be generated by software by setting the COM bit in the TIM1_EGR
  74.   register or by hardware (on TRC rising edge).
  75.   In this example, a software COM event is generated each 100 ms: using the Systick
  76.   interrupt.
  77.   The TIM1 is configured in Timing Mode, each time a COM event occurs,
  78.   a new TIM1 configuration will be set in advance.
  79.   The following Table  describes the TIM1 Channels states:
  80.               -----------------------------------------------
  81.              | Step1 | Step2 | Step3 | Step4 | Step5 | Step6 |
  82.    ----------------------------------------------------------
  83.   |Channel1  |   1   |   0   |   0   |   0   |   0   |   1   |
  84.    ----------------------------------------------------------
  85.   |Channel1N |   0   |   0   |   1   |   1   |   0   |   0   |
  86.    ----------------------------------------------------------
  87.   |Channel2  |   0   |   0   |   0   |   1   |   1   |   0   |
  88.    ----------------------------------------------------------
  89.   |Channel2N |   1   |   1   |   0   |   0   |   0   |   0   |
  90.    ----------------------------------------------------------
  91.   |Channel3  |   0   |   1   |   1   |   0   |   0   |   0   |
  92.    ----------------------------------------------------------
  93.   |Channel3N |   0   |   0   |   0   |   0   |   1   |   1   |
  94.    ----------------------------------------------------------
  95.   -----------------------------------------------------------------------------*/

  96.   /* Time Base configuration */
  97.   TIM_TimeBaseStructure.TIM_Prescaler = 0;
  98.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  99.   TIM_TimeBaseStructure.TIM_Period = 4095;
  100.   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  101.   TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

  102.   TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

  103.   /* Channel 1, 2,3 and 4 Configuration in PWM mode */
  104.   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
  105.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  106.   TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  107.   TIM_OCInitStructure.TIM_Pulse = 2047;
  108.   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  109.   TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
  110.   TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  111.   TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;

  112.   TIM_OC1Init(TIM1, &TIM_OCInitStructure);

  113.   TIM_OCInitStructure.TIM_Pulse = 1023;
  114.   TIM_OC2Init(TIM1, &TIM_OCInitStructure);

  115.   TIM_OCInitStructure.TIM_Pulse = 511;
  116.   TIM_OC3Init(TIM1, &TIM_OCInitStructure);

  117.   /* Automatic Output enable, Break, dead time and lock configuration*/
  118.   TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
  119.   TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
  120.   TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
  121.   TIM_BDTRInitStructure.TIM_DeadTime = 1;
  122.   TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
  123.   TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
  124.   TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;

  125.   TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);

  126.   TIM_CCPreloadControl(TIM1, ENABLE);

  127.   TIM_ITConfig(TIM1, TIM_IT_COM, ENABLE);

  128.   /* TIM1 counter enable */
  129.   TIM_Cmd(TIM1, ENABLE);

  130.   /* Main Output Enable */
  131.   TIM_CtrlPWMOutputs(TIM1, ENABLE);

  132.   while (1)
  133.   {}
  134. }

  135. /**
  136.   * @brief  Configures the different system clocks.
  137.   * @param  None
  138.   * @retval None
  139.   */
  140. void RCC_Configuration(void)
  141. {  
  142.   /* TIM1, GPIOA, GPIOB, GPIOE and AFIO clocks enable */
  143.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOE|
  144.                          RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO, ENABLE);
  145. }

  146. /**
  147.   * @brief  Configure the TIM1 Pins.
  148.   * @param  None
  149.   * @retval None
  150.   */
  151. void GPIO_Configuration(void)
  152. {
  153.   GPIO_InitTypeDef GPIO_InitStructure;

  154. #ifdef STM32F10X_CL
  155.   /* GPIOE Configuration: Channel 1/1N, 2/2N, 3/3N as alternate function push-pull */
  156.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_11|GPIO_Pin_13|
  157.                                 GPIO_Pin_8|GPIO_Pin_10|GPIO_Pin_12;
  158.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  159.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  160.   GPIO_Init(GPIOE, &GPIO_InitStructure);

  161.   /* GPIOE Configuration: BKIN pin */
  162.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
  163.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  164.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  165.   GPIO_Init(GPIOE, &GPIO_InitStructure);

  166.   /* TIM1 Full remapping pins */
  167.   GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE);

  168. #else
  169.   /* GPIOA Configuration: Channel 1, 2 and 3 as alternate function push-pull */
  170.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
  171.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  172.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  173.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  174.   /* GPIOB Configuration: Channel 1N, 2N and 3N as alternate function push-pull */
  175.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  176.   GPIO_Init(GPIOB, &GPIO_InitStructure);

  177.   /* GPIOB Configuration: BKIN pin */
  178.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  179.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  180.   GPIO_Init(GPIOB, &GPIO_InitStructure);  
  181. #endif
  182. }

  183. /**
  184.   * @brief  Configures the SysTick.
  185.   * @param  None
  186.   * @retval None
  187.   */
  188. void SysTick_Configuration(void)
  189. {
  190.   /* Setup SysTick Timer for 100 msec interrupts  */
  191.   if (SysTick_Config((SystemCoreClock) / 10))
  192.   {
  193.     /* Capture error */
  194.     while (1);
  195.   }

  196.    NVIC_SetPriority(SysTick_IRQn, 0x0);
  197. }

  198. /**
  199.   * @brief  Configures the nested vectored interrupt controller.
  200.   * @param  None
  201.   * @retval None
  202.   */
  203. void NVIC_Configuration(void)
  204. {
  205.   NVIC_InitTypeDef NVIC_InitStructure;

  206.   /* Enable the TIM1 Interrupt */
  207. #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
  208.   NVIC_InitStructure.NVIC_IRQChannel = TIM1_TRG_COM_TIM17_IRQn;
  209. #else
  210.   NVIC_InitStructure.NVIC_IRQChannel = TIM1_TRG_COM_IRQn;
  211. #endif
  212.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  213.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  214.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  215.   NVIC_Init(&NVIC_InitStructure);

  216. }

  217. #ifdef  USE_FULL_ASSERT

  218. /**
  219.   * @brief  Reports the name of the source file and the source line number
  220.   *         where the assert_param error has occurred.
  221.   * @param  file: pointer to the source file name
  222.   * @param  line: assert_param error line source number
  223.   * @retval None
  224.   */
  225. void assert_failed(uint8_t* file, uint32_t line)
  226. {
  227.   /* User can add his own implementation to report the file name and line number,
  228.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  229.   while (1)
  230.   {}
  231. }

  232. #endif
  233. /**
  234.   * @}
  235.   */

  236. /**
  237.   * @}
  238.   */

  239. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
 楼主| 贪玩不回家 发表于 2015-12-27 15:55 | 显示全部楼层
D:\用户目录\我的文档\Tencent Files\912656189\FileRecv\MobileFile
 楼主| 贪玩不回家 发表于 2015-12-27 19:55 | 显示全部楼层
真的没人帮忙吗   
lvyunhua 发表于 2015-12-27 21:27 | 显示全部楼层
楼主是用的Cube库吗!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

48

主题

317

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部