linux内核模拟pwm信号控制舵机

需要用到的知识点:

A)linux内核延时函数,这里参考文章https://www.cnblogs.com/Ph-one/p/4678361.html

#include <linux/delay.h>
1、void ndelay(unsigned long nsecs);         纳秒级
2、void udelay(unsigned long usecs);         微秒级
3、void mdelay(unsigned long msecs);         毫秒级

udelay一般适用于一个比较小的delay,如果你填的数大于2000,系统会认为你这个是一个错误的delay函数,因此如果需要2ms以上的delay需要使用mdelay函数。

这里强调如果模拟pwm信号,不要去用init_timer定时器和hrtimer_init,即使是hrtimer_init可以到达纳秒级别。hrtimer_init模拟的pwm信号,用示波器查看方波图达不到很精准。

B)PWM信号

个人见解是一个方波图,类似下图,只不过pwm方波图的高低电平是可以控制的。

C)控制舵机的pwm方波图,这里参考文章https://blog.csdn.net/qq_38960810/article/details/83017629

其实pwm信号的周期不是很重要,关键是pwm方波图中高电平的时间(自己可以做实验,用pwm信号发生器去控制舵机,100HZ和200HZ都能控制转动的,只要控制它的高电平符合下面角度对应的毫秒即可)。

0.5ms--------------0度;

1.0ms------------45度;

1.5ms------------90度;

2.0ms-----------135度;

2.5ms-----------180度;
也就是说只要pwm方波图中的高电平有0.5ms,舵机转角度就为0,2.5ms的话就是180度。和周期没有直接关系。

 

 

调试中个人总结:

1:
控制舵机的pwm信号必须是一直持续的,比如你提供一个1ms的pwm信号给舵机,舵机能够转动到90度,
但是接着你不继续提供1ms的pwm信号,这时舵机虽然不会有啥变化,但是你用手去转动舵机是可以转动的,
反之持续的提供1ms的pwm信号,舵机是会一直保持90度,你就是用手去掰也不会转动的。千万不要只发一个周期的pwm方波信号。
2:
使用舵机过程中最好配备pwm信号发生器(淘宝有卖),手持示波器。这样方便调式找出问题。

下面是代码,代码是在JZ2440开发板基础上,只需要修改对应的GPIO口就行,这里要注意的是舵机转动到相应的角度其实也是需要一定的时间,不可能瞬间到达的,所以我们必须要延时一会,tdata变量就是用来延时的

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/ioctl.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/hrtimer.h>
static struct timer_list buttons_timer;
static struct hrtimer timer;
ktime_t kt;


#define DEVICE_NAME     "pwdtimer"  /* 加载模式后,执行”cat /proc/devices”命令看到的设备名称 */
#define PWD_MAJOR       231     /* 主设备号 */

static struct class *pwd_class;
static struct class_device	*pwd_class_devs;


/* bit0<=>D10, 0:亮, 1:灭 
 *  bit1<=>D11, 0:亮, 1:灭 
 *  bit2<=>D12, 0:亮, 1:灭 
 */ 
static char leds_status = 0x0;  
static DECLARE_MUTEX(leds_lock); // 定义赋值

//static int minor;
static unsigned long gpio_va;

#define GPIO_OFT(x) ((x) - 0x56000000)
#define GPFCON  (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000050)))
#define GPFDAT  (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000054)))

#define GPACON  (*(volatile unsigned long *)(gpio_va))
#define GPADAT  (*(volatile unsigned long *)(gpio_va))

/* 应用程序对设备文件/dev/leds执行open(...)时,
 * 就会调用s3c24xx_pwd_open函数
 */
static int s3c24xx_pwd_open(struct inode *inode, struct file *file)
{
	// 配置2引脚为输出
	//s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);
	GPFCON &= ~(0x3<<4);
	GPFCON |= (1<<4);
	GPFCON &= ~(0x3<<8);
	GPFCON |= (1<<8);
	// 都输出0
	//s3c2410_gpio_setpin(S3C2410_GPF4, 0);
	GPFDAT &= ~(1<<2);
	GPFDAT &= ~(1<<4);
	GPFDAT |= (1<<4);
	GPFCON &= ~(0x3<<2);
	GPFCON |= (1<<2);
	
	GPFDAT &= ~(1<<1);	
    return 0;
}

static ssize_t s3c24xx_leds_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
    char val;
    copy_from_user(&val, buf, 1);
  
	s3c2410_gpio_setpin(S3C2410_GPF4, (val & 0x1));
	s3c2410_gpio_setpin(S3C2410_GPF5, (val & 0x1));
	s3c2410_gpio_setpin(S3C2410_GPF6, (val & 0x1));

    return 1;
}

static long  s3c24xx_pwd_ioctl(struct file * file, unsigned int cmd, unsigned long data)
{
	int m_sec = 0,m_msec = 0,m_secex = 0,m_msecex = 0;

	m_sec = data/1000;
	m_msec = data%1000;
	
	int tdata = 0;
	m_secex = (10000 - data)/1000;
	m_msecex = (10000 - data)%1000;	
	printk("m_sec:%d,m_msec:%d,m_secex:%d,m_msecex:%d\n",m_sec,m_msec,m_secex,m_msecex);
	switch(data) {
		case 500 : 
		
			while(1) {
				GPFDAT &= ~(1<<2);
				GPFDAT |= (1<<2);
				
				GPFDAT &= ~(1<<4);
				GPFDAT |= (1<<4);
				GPFDAT |= (1<<1);
				//mdelay(m_sec);
				udelay(500);	

				GPFDAT &= ~(1<<2);
				GPFDAT &= ~(1<<4);

				GPFDAT &= ~(1<<1);
				mdelay(9);
				udelay(500);
				tdata++;
				if (tdata > 1000)
					break;
			}
			break;
		case 1000 : 
			while(1) {
				GPFDAT &= ~(1<<2);
				GPFDAT |= (1<<2);
				
				GPFDAT &= ~(1<<4);
				GPFDAT |= (1<<4);
				GPFDAT |= (1<<1);
				mdelay(1);
				//udelay(500);	

				GPFDAT &= ~(1<<2);
				GPFDAT &= ~(1<<4);

				GPFDAT &= ~(1<<1);
				mdelay(9);
				//udelay(500);					
				tdata++;
				if (tdata > 1000)
					break;				
			}
				
			break;
		case 1500 : 
			while(1) {
					GPFDAT &= ~(1<<2);
				GPFDAT |= (1<<2);
				
				GPFDAT &= ~(1<<4);
				GPFDAT |= (1<<4);
				GPFDAT |= (1<<1);
				mdelay(1);
				udelay(500);	

				GPFDAT &= ~(1<<2);
				GPFDAT &= ~(1<<4);

				GPFDAT &= ~(1<<1);
				mdelay(8);
				udelay(500);	
				tdata++;
				if (tdata > 1000)
					break;								
			}
				
			break;	
		case 2000 : 
		
			while(1) {
			GPFDAT &= ~(1<<2);
			GPFDAT |= (1<<2);
			
			GPFDAT &= ~(1<<4);
			GPFDAT |= (1<<4);
			GPFDAT |= (1<<1);
			mdelay(2);
			//udelay(500);	

			GPFDAT &= ~(1<<2);
			GPFDAT &= ~(1<<4);

			GPFDAT &= ~(1<<1);
			mdelay(8);
			//udelay(500);	
				tdata++;
				if (tdata > 1000)
					break;				
			}
				
			break;		
		case 2500 : 
		while(1) {
				GPFDAT &= ~(1<<2);
			GPFDAT |= (1<<2);
			
			GPFDAT &= ~(1<<4);
			GPFDAT |= (1<<4);
			GPFDAT |= (1<<1);
			mdelay(2);
			udelay(500);	

			GPFDAT &= ~(1<<2);
			GPFDAT &= ~(1<<4);

			GPFDAT &= ~(1<<1);
			mdelay(7);
			udelay(500);	
				tdata++;
				if (tdata > 1000)
					break;							
		}
		
			break;			
		default:
			break;	
	}
}
/* 这个结构是字符设备驱动程序的核心
 * 当应用程序操作设备文件时所调用的open、read、write等函数,
 * 最终会调用这个结构中指定的对应函数
 */
static struct file_operations s3c24xx_pwd_fops = {
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   s3c24xx_pwd_open,        
	.write	=	s3c24xx_leds_write,	  
	.unlocked_ioctl = s3c24xx_pwd_ioctl,
};

/*
 * 执行insmod命令时就会调用这个函数 
 */
static int __init s3c24xx_pwd_init(void)
{
	//设置GPF2引脚,根据定时器输出0/1
    int ret;
	int minor = 0;

    gpio_va = ioremap(0x56000000, 0x100000);
	if (!gpio_va) {
		return -EIO;
	}

    /* 注册字符设备
     * 参数为主设备号、设备名字、file_operations结构;
     * 这样,主设备号就和具体的file_operations结构联系起来了,
     * 操作主设备为PWD_MAJOR的设备文件时,就会调用s3c24xx_pwd_fops中的相关成员函数
     * PWD_MAJOR可以设为0,表示由内核自动分配主设备号
     */
    ret = register_chrdev(PWD_MAJOR, DEVICE_NAME, &s3c24xx_pwd_fops);
    if (ret < 0) {
      printk(DEVICE_NAME " can't register major number\n");
      return ret;
    }

	pwd_class = class_create(THIS_MODULE, "pwdtimer");
	if (IS_ERR(pwd_class))
		return PTR_ERR(pwd_class);
   
	pwd_class_devs = class_device_create(pwd_class, NULL, MKDEV(PWD_MAJOR, 0), NULL, "pws"); /* /dev/leds */
	
    printk(DEVICE_NAME " initialized\n");
#if 0
	long count = 1;
	long ti = 10;
	long data = 0;
	while(1) {
		GPFDAT &= ~(1<<2);
		GPFDAT |= (1<<2);
		
		GPFDAT &= ~(1<<4);
		GPFDAT |= (1<<4);
		GPFDAT |= (1<<1);
		udelay(500);
		
		GPFDAT &= ~(1<<2);
		GPFDAT &= ~(1<<4);

		GPFDAT &= ~(1<<1);
		mdelay(9);
		udelay(500);
		count++;
		if (count > 1000)
			break;
	
	}	
	
	printk("100\n");
	count = 1;
	while(1) {
		GPFDAT &= ~(1<<2);
		GPFDAT |= (1<<2);
		
		GPFDAT &= ~(1<<4);
		GPFDAT |= (1<<4);
		GPFDAT |= (1<<1);
		mdelay(2);
		
		GPFDAT &= ~(1<<2);
		GPFDAT &= ~(1<<4);

		GPFDAT &= ~(1<<1);
		mdelay(8);
		//udelay(500);
		count++;
		if (count > 1000)
			break;
	}		
	#endif
    return 0;
}

/*
 * 执行rmmod命令时就会调用这个函数 
 */
static void __exit s3c24xx_pwd_exit(void)
{
	int minor;
    /* 卸载驱动程序 */
    unregister_chrdev(PWD_MAJOR, DEVICE_NAME);


	class_device_unregister(pwd_class_devs);
	
	class_destroy(pwd_class);
    iounmap(gpio_va);
	hrtimer_cancel(&timer);  
}

module_init(s3c24xx_pwd_init);
module_exit(s3c24xx_pwd_exit);


MODULE_LICENSE("GPL");

测试代码

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

/*
  *  ledtest <dev> <on|off>
  */

void print_usage(char *file)
{
    printf("Usage:\n");
    printf("%s <dev> <on|off>\n",file);
    printf("eg. \n");
    printf("%s /dev/leds on\n", file);
    printf("%s /dev/leds off\n", file);
    printf("%s /dev/led1 on\n", file);
    printf("%s /dev/led1 off\n", file);
}

int main(int argc, char **argv)
{
    int fd;
    char filename[] = "/dev/pws";
    char val;

    fd = open(filename, O_RDWR);
    if (fd < 0)
    {
        printf("error, can't open %s\n", filename);
        return 0;
    }
	int i,j;
	volatile int count = 2;
	volatile int count1 = 9000;
	
	int ret = 0;

	
	while(1){
		
		ret = ioctl(fd, 0, 500);
		if (ret) {
			perror("ioctl read:");
			exit(-4);
		}		
		sleep(2);
#if 0
		ret = ioctl(fd, 0, 1000);
		if (ret) {
			perror("ioctl read:");
			exit(-4);
		}				
		sleep(3);
		ret = ioctl(fd, 0, 1500);
		if (ret) {
			perror("ioctl read:");
			exit(-4);
		}				
		sleep(3);
		ret = ioctl(fd, 0, 2000);
		if (ret) {
			perror("ioctl read:");
			exit(-4);
		}				
		sleep(3);
#endif		
		ret = ioctl(fd, 0, 2500);
		if (ret) {
			perror("ioctl read:");
			exit(-4);
		}				
		sleep(2);
		
	}

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值