Advertisement
Guest User

Untitled

a guest
Feb 10th, 2012
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. #include "stm32f10x.h"
  2. #include "servo.h"
  3. #include "config.h"
  4.  
  5. #define STM32_MASTER_CLOCK 72000000
  6.  
  7. /**
  8. * Sets a throttle output frequency
  9. * @param freq throttle frequency
  10. */
  11. void throttle_freq (unsigned short freq)
  12. {
  13. if (freq < 50)
  14. freq = 50;
  15. if (freq > 200)
  16. freq = 200;
  17.  
  18. TIM_TimeBaseInitTypeDef TIM_struct;
  19.  
  20. TIM_struct.TIM_ClockDivision = TIM_CKD_DIV1;
  21. TIM_struct.TIM_Prescaler = (STM32_MASTER_CLOCK / 1000000) - 1;
  22. TIM_struct.TIM_Period = ((1000000 / freq) - 1);
  23. TIM_struct.TIM_CounterMode = TIM_CounterMode_Up;
  24.  
  25. TIM_TimeBaseInit (TIM2, &TIM_struct);
  26. TIM_TimeBaseInit (TIM1, &TIM_struct);
  27. }
  28.  
  29. void throttle_set (unsigned short v)
  30. {
  31. TIM_SetCompare4 (TIM2, (uint16_t) v);
  32. TIM_SetCompare1 (TIM1, (uint16_t) v);
  33. }
  34.  
  35. /**
  36. * @brief Timer Configuration
  37. * @param None
  38. * @retval None
  39. */
  40. static void timer_conf (void)
  41. {
  42. TIM_OCInitTypeDef TIM_oc;
  43.  
  44. throttle_freq (50);
  45.  
  46. TIM_oc.TIM_OCMode = TIM_OCMode_PWM1;
  47. TIM_oc.TIM_OutputState = TIM_OutputState_Enable;
  48. TIM_oc.TIM_OutputNState = TIM_OutputNState_Disable;
  49. TIM_oc.TIM_Pulse = 1100;
  50. TIM_oc.TIM_OCPolarity = TIM_OCPolarity_High;
  51. TIM_oc.TIM_OCNPolarity = TIM_OCPolarity_High;
  52. TIM_oc.TIM_OCIdleState = TIM_OCIdleState_Reset;
  53. TIM_oc.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
  54.  
  55. /* throttle : TIM2 */
  56. TIM_OC4Init (TIM2, &TIM_oc);
  57. TIM_OC4PreloadConfig (TIM2, TIM_OCPreload_Enable);
  58.  
  59. TIM_ARRPreloadConfig (TIM2, ENABLE);
  60. //TIM_CtrlPWMOutputs (TIM2, ENABLE);
  61. TIM_Cmd (TIM2, ENABLE);
  62.  
  63.  
  64. TIM_OC1Init (TIM1, &TIM_oc);
  65. TIM_OC1PreloadConfig (TIM1, TIM_OCPreload_Enable);
  66.  
  67. TIM_ARRPreloadConfig (TIM1, ENABLE);
  68. TIM_CtrlPWMOutputs (TIM1, ENABLE);
  69. TIM_Cmd (TIM1, ENABLE);
  70. }
  71.  
  72. /**
  73. * @brief Configure the used I/O ports pin
  74. * @param None
  75. * @retval None
  76. */
  77. static void gpio_conf (void)
  78. {
  79. /* Enable the low speed peripheral clock */
  80. RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM2, ENABLE);
  81.  
  82. /* Enable the high speed peripheral clock */
  83. RCC_APB2PeriphClockCmd (RCC_APB2Periph_TIM1, ENABLE);
  84.  
  85. /* throttle */
  86. GPIO_InitTypeDef GPIO_throttle;
  87.  
  88. GPIO_throttle.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_8;
  89. GPIO_throttle.GPIO_Speed = GPIO_Speed_2MHz;
  90. GPIO_throttle.GPIO_Mode = GPIO_Mode_AF_PP;
  91.  
  92. GPIO_Init (GPIOA, &GPIO_throttle);
  93. }
  94.  
  95. int throttle_init ()
  96. {
  97. gpio_conf ();
  98. timer_conf ();
  99.  
  100. unsigned short i;
  101. while (1) {
  102. for (i=1300; i < 1800; i ++) {
  103. throttle_set (i);
  104. sleep (3);
  105. }
  106.  
  107. for (i=1800; i > 1300; i --) {
  108. throttle_set (i);
  109. sleep (3);
  110. }
  111. }
  112.  
  113. return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement