linux步进电机驱动
2019-07-13 00:09发布
生成海报
#include <linux/module.h>/*__init, __exit, module_init*/
#include <linux/platform_device.h>/*platform_driver*/
#include <linux/miscdevice.h>/*struct miscdevice*/
#include <linux/fs.h>/*file_operations*/
#include <asm/arch/hardware.h>/*GPCR2*/
#include <asm/arch/pxa-regs.h>
#include <asm/uaccess.h>/*copy_from_user()*/
#include <linux/delay.h>/*mdelay()*/
spinlock_t lock;/*声明一个自旋锁*/
static char ctrl[3;
static int mgc270_stepmotor_ctrl[ = {
81, //---A
82, //---B
83, //---C
84, //---D
};
static int mgc270_stepmotor_open(struct inode *inode, struct file *file)
{
unsigned int i;
/* Set ALl pins GPO Low */
for( i = 0; i < 4; i++)
pxa_gpio_mode(mgc270_stepmotor_ctrl[i | GPIO_OUT | GPIO_DFLT_LOW);
printk("mgc270-stepmotor opened!/n");
return 0;
}
static ssize_t mgc270_stepmotor_write(struct file *file, const char __user *buffer, size_t len, loff_t *ppos)
{
unsigned char n, i;
unsigned long flags;
printk("mgc270-stepmotor write!/n");
spin_lock_irqsave(&lock, flags) ;
if (copy_from_user(&ctrl, buffer, sizeof(ctrl)) )
return -EFAULT;
printk("ctrl data is %d %d %d /n", ctrl[0-0x30, ctrl[1-0x30, ctrl[2-0x30);
for( n = 0; n < ctrl[2-0x30; n++ )
{
if(!(ctrl[0-0x30)) //anticlockwise
{//步进电机顺时针转
// printk("ANTI CLOCKWISE!!/n");
for( i = 0; i < 4; i++ )
{
GPCR2 |= 0x0F << 17;/*GPCRxin置高*/
GPSR2 |= 3 << ( 17 + i );/*GPSRxin置低*/
if( i == 3 )
GPSR2 |= 1 << 17;
mdelay((ctrl[1-0x30)*100);
}
} else { //clockwise
/*逆时针*/
// printk("CLOCK WISE!!/n");
for( i = 0; i < 4; i++ )
{
GPSR2 |= 0x0F << 17;
GPCR2 |= 3 <<
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮