[DemoCode下载] 四相步进电机驱动

[复制链接]
818|13
 楼主| wanduzi 发表于 2020-5-12 21:24 | 显示全部楼层 |阅读模式
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * $Revision: 3 $
  5. * $Date: 19/06/20 10:26a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    NUC230_240 Series GPIO Driver Sample Code
  7. *
  8. * [url=home.php?mod=space&uid=536309]@NOTE[/url]   PA0~PA3(PIN71~PIN74) will output signal to driver stepmotor
  9. * Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "NUC230_240.h"

  13. #define PLL_CLOCK           72000000

  14. #define d360 (4096)
  15. #define d180 (4096/2)
  16. #define d90 (4096/4)
  17. #define d45 (4096/8)

  18. unsigned char Table_StepMotor_Output[8] = {0x08, 0x0C, 0x04, 0x06, 0x02, 0x03, 0x01, 0x09};

  19. void SYS_Init(void)
  20. {
  21.     /*---------------------------------------------------------------------------------------------------------*/
  22.     /* Init System Clock                                                                                       */
  23.     /*---------------------------------------------------------------------------------------------------------*/

  24.     /* Enable Internal RC 22.1184MHz and HXT clock */
  25.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk | CLK_PWRCON_XTL12M_EN_Msk);

  26.     /* Waiting for Internal RC and HXT clock ready */
  27.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk | CLK_CLKSTATUS_XTL12M_STB_Msk);

  28.     /* Set core clock as PLL_CLOCK from PLL */
  29.     CLK_SetCoreClock(PLL_CLOCK);

  30.     /* Enable UART module clock */
  31.     CLK_EnableModuleClock(UART0_MODULE);

  32.     /* Select UART module clock source */
  33.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));

  34.     /*---------------------------------------------------------------------------------------------------------*/
  35.     /* Init I/O Multi-function                                                                                 */
  36.     /*---------------------------------------------------------------------------------------------------------*/

  37.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  38.     SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk);
  39.     SYS->GPB_MFP |= (SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD);

  40. }

  41. void UART0_Init()
  42. {
  43.     /*---------------------------------------------------------------------------------------------------------*/
  44.     /* Init UART                                                                                               */
  45.     /*---------------------------------------------------------------------------------------------------------*/
  46.     /* Reset UART0 */
  47.     SYS_ResetModule(UART0_RST);

  48.     /* Configure UART0 and set UART0 Baudrate */
  49.     UART_Open(UART0, 115200);
  50. }
  51. void CW_MOTOR(unsigned int deg)
  52. {
  53.     int i = 0, j = 0;

  54.     for (j = 0; j < deg; j += 8)
  55.     {
  56.         for (i = 0; i < 8; i++)                                  // Clockwise rotation
  57.         {
  58.             PA->DOUT = Table_StepMotor_Output[i];
  59.             CLK_SysTickDelay(5000);
  60.         }
  61.     }
  62. }

  63. void CCW_MOTOR(unsigned int deg)
  64. {
  65.     int i = 0, j = 0;

  66.     for (j = 0; j < deg; j += 8)
  67.     {
  68.         for (i = 7; i >= 0; i--)                                 // Anticlockwise rotation
  69.         {
  70.             PA->DOUT = Table_StepMotor_Output[i];
  71.             CLK_SysTickDelay(5000);
  72.         }
  73.     }
  74. }
  75. /*---------------------------------------------------------------------------------------------------------*/
  76. /* MAIN function                                                                                           */
  77. /*---------------------------------------------------------------------------------------------------------*/
  78. int main(void)
  79. {
  80.     uint32_t Cnt ;
  81.     /* Unlock protected registers */
  82.     SYS_UnlockReg();

  83.     /* Init System, peripheral clock and multi-function I/O */
  84.     SYS_Init();

  85.     /* Lock protected registers */
  86.     SYS_LockReg();

  87.     /* Init UART0 for printf */
  88.     UART0_Init();

  89.     /* PA0~PA3(PIN71~PIN74) is configured as output to driver stepmotor */
  90.     GPIO_SetMode(PA, BIT0, GPIO_PMD_OUTPUT);
  91.     GPIO_SetMode(PA, BIT1, GPIO_PMD_OUTPUT);
  92.     GPIO_SetMode(PA, BIT2, GPIO_PMD_OUTPUT);
  93.     GPIO_SetMode(PA, BIT3, GPIO_PMD_OUTPUT);

  94.     while (1)
  95.     {
  96.         CW_MOTOR(d360);

  97.         for (Cnt = 0; Cnt < 10000000; ++Cnt) __nop();

  98.         CCW_MOTOR(d180);

  99.         for (Cnt = 0; Cnt < 100000000; ++Cnt) __nop();
  100.     };
  101. }

  102. /*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/


 楼主| wanduzi 发表于 2020-5-12 21:25 | 显示全部楼层
EC_NUC230_NUC240_Drive_4phase_Step_Motor_V1.00.zip (1.02 MB, 下载次数: 24)
  1. /**************************************************************************//**
  2. * @file     main.c
  3. * @version  V1.00
  4. * $Revision: 3 $
  5. * $Date: 19/06/20 10:26a $
  6. * @brief    NUC230_240 Series GPIO Driver Sample Code
  7. *
  8. * @note   PA0~PA3(PIN71~PIN74) will output signal to driver stepmotor
  9. * Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "NUC230_240.h"

  13. #define PLL_CLOCK           72000000

  14. #define d360 (4096)
  15. #define d180 (4096/2)
  16. #define d90 (4096/4)
  17. #define d45 (4096/8)

  18. unsigned char Table_StepMotor_Output[8] = {0x08, 0x0C, 0x04, 0x06, 0x02, 0x03, 0x01, 0x09};

  19. void SYS_Init(void)
  20. {
  21.     /*---------------------------------------------------------------------------------------------------------*/
  22.     /* Init System Clock                                                                                       */
  23.     /*---------------------------------------------------------------------------------------------------------*/

  24.     /* Enable Internal RC 22.1184MHz and HXT clock */
  25.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk | CLK_PWRCON_XTL12M_EN_Msk);

  26.     /* Waiting for Internal RC and HXT clock ready */
  27.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk | CLK_CLKSTATUS_XTL12M_STB_Msk);

  28.     /* Set core clock as PLL_CLOCK from PLL */
  29.     CLK_SetCoreClock(PLL_CLOCK);

  30.     /* Enable UART module clock */
  31.     CLK_EnableModuleClock(UART0_MODULE);

  32.     /* Select UART module clock source */
  33.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));

  34.     /*---------------------------------------------------------------------------------------------------------*/
  35.     /* Init I/O Multi-function                                                                                 */
  36.     /*---------------------------------------------------------------------------------------------------------*/

  37.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  38.     SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk);
  39.     SYS->GPB_MFP |= (SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD);

  40. }

  41. void UART0_Init()
  42. {
  43.     /*---------------------------------------------------------------------------------------------------------*/
  44.     /* Init UART                                                                                               */
  45.     /*---------------------------------------------------------------------------------------------------------*/
  46.     /* Reset UART0 */
  47.     SYS_ResetModule(UART0_RST);

  48.     /* Configure UART0 and set UART0 Baudrate */
  49.     UART_Open(UART0, 115200);
  50. }
  51. void CW_MOTOR(unsigned int deg)
  52. {
  53.     int i = 0, j = 0;

  54.     for (j = 0; j < deg; j += 8)
  55.     {
  56.         for (i = 0; i < 8; i++)                                  // Clockwise rotation
  57.         {
  58.             PA->DOUT = Table_StepMotor_Output[i];
  59.             CLK_SysTickDelay(5000);
  60.         }
  61.     }
  62. }

  63. void CCW_MOTOR(unsigned int deg)
  64. {
  65.     int i = 0, j = 0;

  66.     for (j = 0; j < deg; j += 8)
  67.     {
  68.         for (i = 7; i >= 0; i--)                                 // Anticlockwise rotation
  69.         {
  70.             PA->DOUT = Table_StepMotor_Output[i];
  71.             CLK_SysTickDelay(5000);
  72.         }
  73.     }
  74. }
  75. /*---------------------------------------------------------------------------------------------------------*/
  76. /* MAIN function                                                                                           */
  77. /*---------------------------------------------------------------------------------------------------------*/
  78. int main(void)
  79. {
  80.     uint32_t Cnt ;
  81.     /* Unlock protected registers */
  82.     SYS_UnlockReg();

  83.     /* Init System, peripheral clock and multi-function I/O */
  84.     SYS_Init();

  85.     /* Lock protected registers */
  86.     SYS_LockReg();

  87.     /* Init UART0 for printf */
  88.     UART0_Init();

  89.     /* PA0~PA3(PIN71~PIN74) is configured as output to driver stepmotor */
  90.     GPIO_SetMode(PA, BIT0, GPIO_PMD_OUTPUT);
  91.     GPIO_SetMode(PA, BIT1, GPIO_PMD_OUTPUT);
  92.     GPIO_SetMode(PA, BIT2, GPIO_PMD_OUTPUT);
  93.     GPIO_SetMode(PA, BIT3, GPIO_PMD_OUTPUT);

  94.     while (1)
  95.     {
  96.         CW_MOTOR(d360);

  97.         for (Cnt = 0; Cnt < 10000000; ++Cnt) __nop();

  98.         CCW_MOTOR(d180);

  99.         for (Cnt = 0; Cnt < 100000000; ++Cnt) __nop();
  100.     };
  101. }

  102. /*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/


wahahaheihei 发表于 2020-5-20 22:44 | 显示全部楼层
学习学习电机驱动
643757107 发表于 2020-5-20 23:17 | 显示全部楼层
学习电机驱动的好例子
734774645 发表于 2020-5-21 17:07 | 显示全部楼层
可以做简单的驱动器了。
gejigeji521 发表于 2020-5-21 20:33 | 显示全部楼层
学习一下这个
598330983 发表于 2020-5-22 13:24 | 显示全部楼层
没想这里面也有nop
643757107 发表于 2020-5-27 23:20 | 显示全部楼层
最近参考了不少新唐的电机方案。
kxsi 发表于 2020-6-1 17:52 | 显示全部楼层
非常感谢楼主分享
nawu 发表于 2020-6-1 17:52 | 显示全部楼层
代码好多呀
qcliu 发表于 2020-6-1 17:52 | 显示全部楼层
很是详细 呵呵
tfqi 发表于 2020-6-1 17:53 | 显示全部楼层
楼主好辛苦
wiba 发表于 2020-6-1 17:53 | 显示全部楼层
必须支持楼主啊
gejigeji521 发表于 2020-6-2 07:55 来自手机 | 显示全部楼层
支持楼主
您需要登录后才可以回帖 登录 | 注册

本版积分规则

156

主题

1882

帖子

3

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