GD32VW553-IOT V2驱动舵机

[复制链接]
873|0
本文转载自iCEasy商城口碑评测作者:user202505087040
欢迎大家戳链接GD32VW553-IOT V2驱动舵机与原贴作者互动交流哦~

视频链接
https://www.bilibili.com/video/BV1MhJyzzEFT/?vd_source=e6a3da217be9a2a32f73bd3723b43cec
GD32VW553-IOT V2 介绍
1. GD32VW553-IOT V2在前一版GD32VW553-IOT 的基础上进行了较大幅度的改进,包括自带下载串口、新增引脚丝印、使用拨码开关替代跳线帽等。
1. 设计图
二、GD32VW553-IOT V2 驱动舵机
代码部分
  1. #include "gd32vw55x.h"
  2. #include "systick.h"
  3. #include <stdio.h>
  4. #include "main.h"
  5. #include "gd32vw553h_eval.h"

  6. #include "bsp_sg90.h"

  7. /*!
  8. \brief toggle the led every 500ms
  9. \param[in] none
  10. \param[out] none
  11. \retval none
  12. */
  13. void led_spark(void)
  14. {
  15. static __IO uint32_t timingdelaylocal = 0U;

  16. if(timingdelaylocal) {

  17. if(timingdelaylocal < 500U) {
  18. gd_eval_led_on(LED1);
  19. } else {
  20. gd_eval_led_off(LED1);
  21. }

  22. timingdelaylocal--;
  23. } else {
  24. timingdelaylocal = 1000U;
  25. }
  26. }

  27. /*!
  28. \brief main function
  29. \param[in] none
  30. \param[out] none
  31. \retval none
  32. */
  33. int main(void)
  34. {
  35. #ifdef __FIRMWARE_VERSION_DEFINE
  36. uint32_t fw_ver = 0;
  37. #endif /* __FIRMWARE_VERSION_DEFINE */

  38. /* configure systick */
  39. systick_config();
  40. eclic_priority_group_set(ECLIC_PRIGROUP_LEVEL3_PRIO1);
  41. /* initilize the LEDs, USART and key */
  42. gd_eval_led_init(LED1);
  43. gd_eval_com_init(EVAL_COM0);
  44. gd_eval_key_init(KEY_TAMPER_WAKEUP, KEY_MODE_GPIO);

  45. #ifdef __FIRMWARE_VERSION_DEFINE
  46. fw_ver = gd32vw55x_firmware_version_get();
  47. printf("\r\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\r\n\n");
  48. printf("\r\n=== Welcome to use the LC-GD32VW553-HMQ6 development board ====\r\n\n");
  49. printf("\r\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\r\n");
  50. /* print firmware version */
  51. printf("\r\nGD32VW55X series firmware version: V%d.%d.%d", (uint8_t)(fw_ver >> 24), (uint8_t)(fw_ver >> 16), (uint8_t)(fw_ver >> 8));
  52. #endif /* __FIRMWARE_VERSION_DEFINE */

  53. /* print out the clock frequency of system, AHB, APB1 and APB2 */
  54. printf("\r\nCK_SYS is %d\r\n", rcu_clock_freq_get(CK_SYS));
  55. printf("\r\nCK_AHB is %d\r\n", rcu_clock_freq_get(CK_AHB));
  56. printf("\r\nCK_APB1 is %d\r\n", rcu_clock_freq_get(CK_APB1));
  57. printf("\r\nCK_APB2 is %d\r\n", rcu_clock_freq_get(CK_APB2));

  58. uint8_t i = 0;

  59. SG90_Init();
  60. printf("\r\nSG90 Init Success\r\n");

  61. /* 先让舵机转到最大角度位置,等待1000ms */
  62. Set_SG90_Servo_Angle(180);
  63. delay_1ms(1000);
  64. /* 再让舵机转到最小角度位置,等待1000ms */
  65. Set_SG90_Servo_Angle(0);
  66. delay_1ms(1000);
  67. printf("\r\nSG90 Set Angle Success\r\n");

  68. while(1)
  69. {
  70. Set_SG90_Servo_Angle(i);
  71. i=i+30;
  72. if( i >= 180 )
  73. {
  74. i = 0;
  75. }
  76. delay_1ms(500);
  77. }
  78. }

  79. #include "bsp_sg90.h"

  80. uint8_t Servo_Angle = 0; //舵机角度

  81. /******************************************************************
  82. 配置占空比 范围 0 ~ (per-1)
  83. t = 0.5ms-------------------舵机会转动 0 °
  84. t = 1.0ms-------------------舵机会转动 45°
  85. t = 1.5ms-------------------舵机会转动 90°
  86. t = 2.0ms-------------------舵机会转动 135°
  87. t = 2.5ms-------------------舵机会转动180°
  88. ******************************************************************/
  89. /******************************************************************
  90. * 函 数 名 称:Set_SG90_Servo_Angle
  91. * 函 数 说 明:设置角度
  92. * 函 数 形 参:angle=要设置的角度,范围0-180
  93. * 函 数 返 回:无
  94. ******************************************************************/
  95. void Set_SG90_Servo_Angle(uint32_t angle)
  96. {
  97. if(angle > 180)
  98. {
  99. angle = 180; // 限制角度在0到180度之间
  100. }

  101. Servo_Angle = angle;

  102. // 计算PWM占空比
  103. // 0.5ms对应的计数 ~= 500
  104. // 2.5ms对应的计数 ~= 2500
  105. float min_count = 500.0f;
  106. float max_count = 2500.0f;
  107. float range = max_count - min_count;
  108. float ServoAngle = min_count + (((float)angle / 180.0f) * range);

  109. timer_channel_output_pulse_value_config(BSP_PWM_TIMER, BSP_PWM_TIMER_CH, (uint32_t)ServoAngle);
  110. }

  111. /******************************************************************
  112. * 函 数 名 称:读取当前角度
  113. * 函 数 说 明:Get_SG90_Servo_Angle
  114. * 函 数 形 参:无
  115. * 函 数 返 回:当前角度
  116. * 备 注:使用前必须确保之前使用过
  117. Set_SG90_Servo_Angle
  118. 函数设置过角度
  119. ******************************************************************/
  120. uint8_t Get_SG90_Servo_Angle(void)
  121. {
  122. return Servo_Angle;
  123. }

  124. /******************************************************************
  125. * 函 数 名 称:SG90_Init
  126. * 函 数 说 明:SG90舵机初始化
  127. * 函 数 形 参:无
  128. * 函 数 返 回:无
  129. ******************************************************************/
  130. void SG90_Init(void)
  131. {
  132. /* 使能相关时钟 */
  133. Module_RCU_Enable();

  134. gpio_mode_set(BSP_PWM_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, BSP_PWM_PIN);
  135. gpio_output_options_set(BSP_PWM_PORT,GPIO_OTYPE_PP, GPIO_OSPEED_25MHZ,BSP_PWM_PIN);
  136. /* 配置IO为定时器的通道 */
  137. gpio_af_set(BSP_PWM_PORT, BSP_PWM_AF, BSP_PWM_PIN);

  138. timer_parameter_struct timer_initpara; // 定义定时器结构体

  139. /* 开启时钟 */
  140. rcu_periph_clock_enable(BSP_PWM_TIMER_RCU); // 开启定时器时钟
  141. /* CK_TIMERx = 2 x CK_APB1 = 2x80M = 160MHZ */
  142. rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL2); // 配置定时器时钟
  143. timer_deinit(BSP_PWM_TIMER); // 复位定时器

  144. /* 配置定时器参数 */
  145. timer_initpara.prescaler = 160-1; // 时钟预分频值 PSC_CLK= 160MHZ / 160 = 1MHZ
  146. timer_initpara.alignedmode = TIMER_COUNTER_EDGE; // 边缘对齐
  147. timer_initpara.counterdirection = TIMER_COUNTER_UP; // 向上计数
  148. timer_initpara.period = 20000 -1; // 周期 T = 20000 T-time = 20ms f = 50HZ
  149. /* 在输入捕获的时候使用 数字滤波器使用的采样频率之间的分频比例 */
  150. timer_initpara.clockdivision = TIMER_CKDIV_DIV1; // 分频因子
  151. /* 只有高级定时器才有。配置为x,就重复x+1次进入中断 */
  152. timer_initpara.repetitioncounter = 0; // 重复计数器 0-255
  153. timer_init(BSP_PWM_TIMER,&timer_initpara); // 初始化定时器

  154. /* 使能定时器 */
  155. timer_enable(BSP_PWM_TIMER);

  156. /* 定义定时器输出参数 */
  157. timer_oc_parameter_struct timer_ocintpara;
  158. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH; // 输出极性为高
  159. timer_ocintpara.outputstate = TIMER_CCX_ENABLE; // 使能输出

  160. /* 配置定时器输出功能 */
  161. timer_channel_output_config(BSP_PWM_TIMER, BSP_PWM_TIMER_CH, &timer_ocintpara);

  162. /* 配置定时器通道输出脉冲值 */
  163. timer_channel_output_pulse_value_config(BSP_PWM_TIMER,BSP_PWM_TIMER_CH, 0);

  164. /* 配置定时器通道输出比较模式 */
  165. timer_channel_output_mode_config(BSP_PWM_TIMER,BSP_PWM_TIMER_CH,TIMER_OC_MODE_PWM0);

  166. /* 配置定时器通道输出影子寄存器 */
  167. timer_channel_output_shadow_config(BSP_PWM_TIMER,BSP_PWM_TIMER_CH,TIMER_OC_SHADOW_DISABLE);

  168. /* 使能自动重载影子寄存器 */
  169. timer_auto_reload_shadow_enable(BSP_PWM_TIMER);
  170. }





V2萤火工场·GD32VW553-IOT开源硬件规格书.pdf

777.85 KB, 下载次数: 0

V2原理图.pdf

115.86 KB, 下载次数: 0

您需要登录后才可以回帖 登录 | 注册

本版积分规则

6

主题

9

帖子

0

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