📄 stepmotor.c
字号:
#include "S3C2410.h"
#include "Def2.h"
// 步进电机控制口线定义
#define MOTOA (1<<5) /* GPC5 */
#define MOTOB (1<<6) /* GPC6 */
#define MOTOC (1<<7) /* GPC7 */
#define MOTOD (1<<0) /* GPC0 */
#define GPIOSET(PIN) rGPCDAT = rGPCDAT | PIN /* 设置PIN输出1,PIN为MOTOA--MOTOD */
#define GPIOCLR(PIN) rGPCDAT = rGPCDAT & (~PIN) /* 设置PIN输出0,PIN为MOTOA--MOTOD */
/*********************************************************************************************************
** Function name: DelayNS1
** Descriptions : 软件延时 **延时时间与系统时钟有关**
** Input : dly
** Output: -
** Created Date: 2007-8-2 09:53上午
********************************************************************************************************/
void DelayNS1(uint32 dly)
{
uint32 i;
for(; dly>0; dly--)
for(i=0; i<50000; i++);
}
#define DelayNS DelayNS1
/*********************************************************************************************************
** Function name: MOTO_Mode2
** Descriptions : 步进电机双四拍运行, 时序为AB--BC--CD--DA--AB, 转20*4步
** 步距角为18度,即,转动80*18度=4*360度,转动4圈
** Input : dly, 每一步时延,即步进速度.
** Output: -
** Created Date: 2007-8-2 10:02上午
********************************************************************************************************/
void stepmotor_clockwise(unsigned char dly)
{
// uint32 i;
// for(i=0; i<20; i++)
{
// AD
GPIOSET(MOTOA);
GPIOSET(MOTOD);
DelayNS1(dly);
GPIOCLR(MOTOA);
// GPIOCLR(MOTOB);
// DB
GPIOSET(MOTOD);
GPIOSET(MOTOB);
DelayNS1(dly);
GPIOCLR(MOTOD);
// GPIOCLR(MOTOC);
// BC
GPIOSET(MOTOB);
GPIOSET(MOTOC);
DelayNS1(dly);
GPIOCLR(MOTOB);
// GPIOCLR(MOTOD);
// CA
GPIOSET(MOTOC);
GPIOSET(MOTOA);
DelayNS1(dly);
GPIOCLR(MOTOC);
// GPIOCLR(MOTOA);
}
GPIOCLR(MOTOA);
}
void stepmotor_anti_clockwise(unsigned char dly)
{
// int i;
// for(i=0; i<40; i++)
{
// DA
GPIOSET(MOTOD);
GPIOSET(MOTOA);
DelayNS(dly);
GPIOCLR(MOTOA);
// GPIOCLR(MOTOA);
// CD
GPIOSET(MOTOC);
GPIOSET(MOTOD);
DelayNS(dly);
GPIOCLR(MOTOD);
// GPIOCLR(MOTOD);
// BC
GPIOSET(MOTOB);
GPIOSET(MOTOC);
DelayNS(dly);
GPIOCLR(MOTOC);
// GPIOCLR(MOTOC);
// AB
GPIOSET(MOTOA);
GPIOSET(MOTOB);
DelayNS(dly);
GPIOCLR(MOTOB);
// GPIOCLR(MOTOB);
}
GPIOCLR(MOTOA);
}
/*********************************************************************************************************
** Function name: main
** Descriptions : 使用GPIO控制步进电机,双四拍方式。
** Input : -
** Output: -
** Created Date: 2007-8-2 10:05上午
********************************************************************************************************/
void InitStepMotorTest(void)
{
// 步进电机控制口设置
rGPCCON = (rGPCCON & (~0x0000FC03)) | (0x00005401); // GPC0、GPC5--7口设置为输出
rGPCUP = rGPCUP | 0x00E1; // 禁止GPC0、GPC5--7口的上拉电阻
rGPCDAT = rGPCDAT & (~0x00E1); // 设置GPC0、GPC5--7口输出低电平
}
#define __raw_writel(v,port) (port) = (v)
#define __raw_readl(port) (port)
#define S3C2410_GPGCON rGPGCON
#define S3C2410_GPGUP rGPGUP
#define S3C2410_GPGDAT rGPGDAT
#define S3C2410_GPHCON rGPHCON
#define S3C2410_GPHUP rGPHUP
#define S3C2410_GPHDAT rGPHDAT
void dcmotor_stop(void)
{
__raw_writel(__raw_readl(S3C2410_GPGCON) & (~(0x03<<22)), S3C2410_GPGCON); //input
__raw_writel(__raw_readl(S3C2410_GPGUP) | (1<<11), S3C2410_GPGUP); // 禁止TOUT0口的上拉电阻
__raw_writel(__raw_readl(S3C2410_GPGDAT) & ( ~(1<<11)), S3C2410_GPGDAT); // 输出0电平
__raw_writel((__raw_readl(S3C2410_GPHCON) & (~(0x03<<18))) | (0x01<<18), S3C2410_GPHCON); // GPH9
__raw_writel(__raw_readl(S3C2410_GPHUP) | (1<<9), S3C2410_GPHUP);
__raw_writel(__raw_readl(S3C2410_GPHDAT) & (~(1<<9)), S3C2410_GPHDAT); // 输出0电
}
void dcmotor_clockwise(void)
{
__raw_writel((__raw_readl(S3C2410_GPGCON) & (~(0x03 << 22))) | (0x01 << 22), S3C2410_GPGCON); // S3C2410_GPBCON[1:0] = 10b,设置TOUT0功能
__raw_writel(__raw_readl(S3C2410_GPGUP) | (1<<11), S3C2410_GPGUP); // 禁止TOUT0口的上拉电阻
__raw_writel(__raw_readl(S3C2410_GPGDAT) & (~(1<<11)), S3C2410_GPGDAT); // 输出0电平
__raw_writel((__raw_readl(S3C2410_GPHCON) & (~(0x03 << 18))) | (0x01 << 18), S3C2410_GPHCON); // GPH9
__raw_writel(__raw_readl(S3C2410_GPGUP) | (1<<9), S3C2410_GPGUP);
__raw_writel(__raw_readl(S3C2410_GPHDAT) | (1 << 9), S3C2410_GPHDAT); // 输出1电平
}
void dcmotor_anti_clockwise(void)
{
__raw_writel((__raw_readl(S3C2410_GPGCON) & (~(0x03 << 22)))| (0x01 << 22), S3C2410_GPGCON); // S3C2410_GPBCON[1:0] = 10b,设置TOUT0功能
__raw_writel(__raw_readl(S3C2410_GPGUP) | (1<<11), S3C2410_GPGUP); // 禁止TOUT0口的上拉电阻
__raw_writel(__raw_readl(S3C2410_GPGDAT) | (1<<11), S3C2410_GPGDAT); // 输出1电平
__raw_writel((__raw_readl(S3C2410_GPHCON) & (~(0x03 << 18))) | (0x01 << 18), S3C2410_GPHCON); // GPH9
__raw_writel(__raw_readl(S3C2410_GPGUP) | (1<<9), S3C2410_GPGUP);
__raw_writel(__raw_readl(S3C2410_GPHDAT) & (~(1 << 9)), S3C2410_GPHDAT); // 输出0电平
}
/* end of file */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -