回帖:linux 驱动写法
#define GPIO_TO_PIN(bank, gpio) (32 * (bank) + (gpio))
int main(int argc, char * argv)
{
int i, n, fd;
char num,val;
int ret;
fd = open("/dev/kio", O_RDWR);//打开设备
if (fd < 0)
{
printf("can't open /dev/kio!\n");
exit(1);
}
sleep(1);
ioctl(fd, 1, GPIO_TO_PIN(1,22));//设置gpio1-22为输出(user:led3)
ioctl(fd, 0, GPIO_TO_PIN(2, 1));//设置gpio2-1 为输入(p8-18)
while (1)
{
num = 1;
ret = write(fd,"1",GPIO_TO_PIN(1,22)); //置高
if(ret < 0)
{
perror("write");
return -1;
}
sleep(1);
ret = write(fd,"0",GPIO_TO_PIN(1,22));//置低
if(ret < 0)
{
perror("write");
return -1;
}
ret = read(fd, &val, GPIO_TO_PIN(2,1)); //读取电平
if(ret == 1) //高电平
printf("val==1\n");
if(ret == 0) // 低电平
printf("val == 0 \n");
}