当前位置: 技术问答>linux和unix
求简单的44b0按键中断测试程序
来源: 互联网 发布时间:2016-03-29
本文导语: 我现在想做一个s3c44b0的中断测试,有两个按键K1和K2,两个LED:LED1和LED2,打算实现按下K1,LED1亮,再按一下,LED1灭;按下K2,LED2亮,再按一下,LED2灭。能实现这个功能就可以了,别的不做要求,用C语言编写,能在LINUX下编...
我现在想做一个s3c44b0的中断测试,有两个按键K1和K2,两个LED:LED1和LED2,打算实现按下K1,LED1亮,再按一下,LED1灭;按下K2,LED2亮,再按一下,LED2灭。能实现这个功能就可以了,别的不做要求,用C语言编写,能在LINUX下编译通过。其中各按键和灯对应GPIO口:K1---PC2,K2---PC1;LED1---PC0,LED2---PC3.
|
一个简单的GPIO输出高低电平例子源码
一个GPIO的驱动,输出高低电平
驱动源代码:GPIO_DRIVER.C
#include
//#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
MODULE_LICENSE("GPL");
#define IOPORT_MAJOR 220 //定义主设备号
typedef char ioport_device_t;
static ioport_device_t gpio_devices[257];
#define IOWRITE 1;
#define IOCLEAR 2;
static int gpio_open(struct inode *inode,struct file *filp)
{
int minor;
minor = MINOR(inode->i_rdev);
at91_set_gpio_output(AT91_PIN_PB3,0 );
gpio_devices[minor]++;
return 0;
}
static int gpio_release(struct inode *inode,struct file *filp)
{
int minor;
minor = MINOR(inode->i_rdev);
if(gpio_devices[minor])
gpio_devices[minor]--;
return 0;
}
static int gpio_ctl_ioctl(struct inode *inode, struct file *filp,
unsigned int command, unsigned long arg)
{
int err = 0;
int minor = MINOR(inode->i_rdev);
switch(command)
{
case IOWRITE:
err = at91_set_gpio_value(AT91_PIN_PB3,1);//输出1高电平
break;
case IOCLEAR:
err = at91_set_gpio_value(AT91_PIN_PB3,0);//输出0低电平
break;
}
return err;
}
static struct file_operations gpio_ctl_fops={
owner: THIS_MODULE,
ioctl: gpio_ctl_ioctl,
open: gpio_open,
release: gpio_release,
};
static int __init gpio_init(void)
{
register_chrdev(IOPORT_MAJOR ,"gpiotest",&gpio_ctl_fops);
return 0;
}
static void __exit gpio_exit(void)
{
unregister_chrdev(IOPORT_MAJOR,"gpiotest");
return 0;
}
module_init(gpio_init);
module_exit(gpio_exit);
测试程序:
#include
#include
#include
#include
#include
#include
#include
#include
#define DEVICE_GPIOTEST "/dev/gpiotest"
#define IOWRITE 1;
#define IOCLEAR 2;
int main()
{
int fd;
int val=-1;
if( (fd=open(DEVICE_GPIOTEST,O_WRITE | O_NONBLOCK))
一个GPIO的驱动,输出高低电平
驱动源代码:GPIO_DRIVER.C
#include
//#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
MODULE_LICENSE("GPL");
#define IOPORT_MAJOR 220 //定义主设备号
typedef char ioport_device_t;
static ioport_device_t gpio_devices[257];
#define IOWRITE 1;
#define IOCLEAR 2;
static int gpio_open(struct inode *inode,struct file *filp)
{
int minor;
minor = MINOR(inode->i_rdev);
at91_set_gpio_output(AT91_PIN_PB3,0 );
gpio_devices[minor]++;
return 0;
}
static int gpio_release(struct inode *inode,struct file *filp)
{
int minor;
minor = MINOR(inode->i_rdev);
if(gpio_devices[minor])
gpio_devices[minor]--;
return 0;
}
static int gpio_ctl_ioctl(struct inode *inode, struct file *filp,
unsigned int command, unsigned long arg)
{
int err = 0;
int minor = MINOR(inode->i_rdev);
switch(command)
{
case IOWRITE:
err = at91_set_gpio_value(AT91_PIN_PB3,1);//输出1高电平
break;
case IOCLEAR:
err = at91_set_gpio_value(AT91_PIN_PB3,0);//输出0低电平
break;
}
return err;
}
static struct file_operations gpio_ctl_fops={
owner: THIS_MODULE,
ioctl: gpio_ctl_ioctl,
open: gpio_open,
release: gpio_release,
};
static int __init gpio_init(void)
{
register_chrdev(IOPORT_MAJOR ,"gpiotest",&gpio_ctl_fops);
return 0;
}
static void __exit gpio_exit(void)
{
unregister_chrdev(IOPORT_MAJOR,"gpiotest");
return 0;
}
module_init(gpio_init);
module_exit(gpio_exit);
测试程序:
#include
#include
#include
#include
#include
#include
#include
#include
#define DEVICE_GPIOTEST "/dev/gpiotest"
#define IOWRITE 1;
#define IOCLEAR 2;
int main()
{
int fd;
int val=-1;
if( (fd=open(DEVICE_GPIOTEST,O_WRITE | O_NONBLOCK))