×
单片机 > 单片机程序设计 > 详情

外设一个一个学_PWM

发布时间:2020-05-30 发布时间:
|
PWM脉冲宽度调制(PWM)!
一般用途:电机、机器人。系统时钟(当然一般是RTC)、蜂鸣器、手机屏幕明暗调节:
1、书写流程。
根据芯片手册、
关于使用PWM驱动蜂鸣器的步骤。
①:设置PWM的模式为PWMTOUT1 GPDCON 0XE0300080  
  在4~7位 写入 0x2
②:PWM_ON  PWM_OFF  /*作为控制命令参数*/
③:设置预分频函数:SET_PRE SET_CNT TCNTM=2TCMPB 占空比
④:关于寄存器的修改
TCFG0 0XEA000000 
TCFG1 4~7
TCON 0XEA000008
8~11 0x2 0010 
0x9 1001
驱动如下:

#ifndef __S5PC100_LED_HHHH
#define __S5PC100_LED_HHHH

//need arg = 0/1/2/3
#define PWM_ON _IO('K', 0)
#define PWM_OFF _IO('K', 1)
#define SET_PRE _IO('K', 2)
#define SET_CNT _IO('K', 3)

#endif


#include
#include
#include
#include
#include

#include
#include

#include "s5pc100_pwm.h"

MODULE_LICENSE("GPL");

#define S5PC100_GPDCON 0xE0300080
#define S5PC100_TIMER_BASE 0xEA000000

#define S5PC100_TCFG0 0x00
#define S5PC100_TCFG1 0x04
#define S5PC100_TCON 0x08
#define S5PC100_TCNTB1 0x18
#define S5PC100_TCMPB1 0x1C

static int pwm_major = 250;
static int pwm_minor = 0;
static int number_of_device = 1;

struct s5pc100_pwm
{
struct cdev cdev;
unsigned int *gpdcon;
void __iomem *timer_base;
};

struct s5pc100_pwm *pwm;

static int s5pc100_pwm_open(struct inode *inode, struct file *file)
{
writel((readl(pwm->gpdcon) & ~(0xf << 4)) | (0x2 << 4), pwm->gpdcon);
writel(readl(pwm->timer_base + S5PC100_TCFG0) | 0xff, pwm->timer_base + S5PC100_TCFG0);
writel((readl(pwm->timer_base + S5PC100_TCFG1) & ~(0xf << 4)) | (0x2 << 4), pwm->timer_base + S5PC100_TCFG1);
writel(0x200, pwm->timer_base + S5PC100_TCNTB1);
writel(0x100, pwm->timer_base + S5PC100_TCMPB1);
writel((readl(pwm->timer_base + S5PC100_TCON) & ~(0xf << 8)) | (0x2 << 8), pwm->timer_base + S5PC100_TCON);
//writel((readl(pwm->timer_base + S5PC100_TCON) & ~(0xf << 8)) | (0x9 << 8), pwm->timer_base + S5PC100_TCON);

return 0;
}

static int s5pc100_pwm_release(struct inode *inode, struct file *file)
{
return 0;
}

static long s5pc100_pwm_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
switch(cmd)
{
case PWM_ON:
writel((readl(pwm->timer_base + S5PC100_TCON) & ~(0xf << 8)) | (0x9 << 8), pwm->timer_base + S5PC100_TCON);
break;
case PWM_OFF:
writel(readl(pwm->timer_base + S5PC100_TCON) & ~(0xf << 8), pwm->timer_base + S5PC100_TCON);
break;
case SET_PRE:
writel(readl(pwm->timer_base + S5PC100_TCON) & ~(0xf << 8), pwm->timer_base + S5PC100_TCON);
writel((readl(pwm->timer_base + S5PC100_TCFG0) & ~0xff) | arg, pwm->timer_base + S5PC100_TCFG0);
writel((readl(pwm->timer_base + S5PC100_TCON) & ~(0xf << 8)) | (0x9 << 8), pwm->timer_base + S5PC100_TCON);
break;
case SET_CNT:
writel(arg, pwm->timer_base + S5PC100_TCNTB1);
writel(arg >> 1, pwm->timer_base + S5PC100_TCMPB1);
break;
}

return 0;
}

static struct file_operations s5pc100_pwm_fops = {
.owner = THIS_MODULE,
.open = s5pc100_pwm_open,
.release = s5pc100_pwm_release,
.unlocked_ioctl = s5pc100_pwm_unlocked_ioctl,
};

static int s5pc100_pwm_init(void)
{
int ret;

dev_t devno = MKDEV(pwm_major, pwm_minor);

ret = register_chrdev_region(devno, number_of_device, "s5pc100_pwm");
if (ret < 0) {
printk("register_chrdev_region\n");
return ret;
}

pwm = kmalloc(sizeof(*pwm), GFP_KERNEL);
if (pwm == NULL) {
ret = -ENOMEM;
goto err1;
}

cdev_init(&pwm->cdev, &s5pc100_pwm_fops);
pwm->cdev.owner = THIS_MODULE;
ret = cdev_add(&pwm->cdev, devno, 1);
if (ret < 0) {
printk("cdev_add\n");
goto err2;
}

pwm->gpdcon = ioremap(S5PC100_GPDCON, 4);
if (pwm->gpdcon == NULL) {
ret = -EINVAL;
goto err3;
}

pwm->timer_base = ioremap(S5PC100_TIMER_BASE, 0x30);
if (pwm->timer_base == NULL) {
ret = -EINVAL;
goto err4;
}

return 0;
err4:
iounmap(pwm->gpdcon);
err3:
cdev_del(&pwm->cdev);
err2:
kfree(pwm);
err1:
unregister_chrdev_region(devno, number_of_device);
return ret;
}

static void s5pc100_pwm_exit(void)
{
dev_t devno = MKDEV(pwm_major, pwm_minor);
iounmap(pwm->gpdcon);
iounmap(pwm->timer_base);
cdev_del(&pwm->cdev);
kfree(pwm);
unregister_chrdev_region(devno, number_of_device);
}

module_init(s5pc100_pwm_init);
module_exit(s5pc100_pwm_exit);

 
/**/



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
单片机中高阻态的实质及意义