[ARM应用]按键中断驱动实例
nclude
#include
#include
#include
#include
#define UP 0
#define DOWN 1
#define X 2
#define LED_ON 0
#define LED_OFF 1
int main()
{
int fd0,fd1,i,j;
int ret= 0;
int button_status;
fd0 = open("/dev/button",O_RDWR);//打开button设备fd0
if (fd0 == -1) {//打开fd0设备失败
printf("open device button errr!\n");
return 0;
}
fd1 = open("/dev/led",O_RDWR);//打开led设备fd1
if (fd1 == -1) {//打开fd1设备失败
printf("open device led errr!\n");
return 0;
}
ioctl(fd1,LED_OFF); //先熄灭D1
printf("button test show. press ctrl+c to exit \n");
while(1) {//主循环
read(fd0,&button_status,1);//读取按键的状态,看是否被按下
if(button_status == DOWN){//如果按键按下了
ioctl(fd1,LED_ON); //点亮D1
for(i=0;i<300;i++)
for(j=0;j<5000;j++);//延时一段时间
ioctl(fd1,LED_OFF); //熄灭D1
}
for(i=0;i<300;i++)
for(j=0;j<5000;j++);
}
close(fd0);//关闭fd0设备
close(fd1);//关闭fd1设备
return 0;
}
(6)编译测试程序
arm-linux-gcc -o led_button led_button.c
该命令的意思是使用的交叉编译器arm-linux-gcc对测试程序led_button.c进行编译,编译成功后在test目录下会生成目标机的可执行文件led_button。
(7)运行测试程序
创建设备
在目标机中,通过查看/proc/devices中注册进内核的设备条目及相关的设备号。进入/dev目录,创建设备,设备名为led,属于字符型设备,主设备号是212,次设备号是0;创建设备,设备名为button,属于字符型设备,主设备号是213,次设备号是0。(要与led.c和button.c文件中的定义相符):
cat /proc/devices
cd /dev
mknod led c 212 0
mknod button c 213 0
./usr/button/test/led_button或/usr/led/test/led_button
(10)卸载模块
rmmod /dev/button
rmmod /dev/led
From:嵌入式Linux初级实验
- Linux嵌入式系统开发平台选型探讨(11-09)
- 基于ARM体系的嵌入式系统BSP的程序设计方案(04-11)
- 在Ubuntu上建立Arm Linux 开发环境(04-23)
- 达芬奇数字媒体片上系统的架构和Linux启动过程(06-02)
- SQLite嵌入式数据库系统的研究与实现(02-20)
- 革新2410D开发板试用手记(04-21)