/*
* \file gpio_test.c
* LIS3LV02DQ
*
* \version 1.0.0
* \date 2014年03月10日
* \author jiangdou <jiangdouu88@126.com>
*
* Copyright (c) 2014 jiangdou. All Rights Reserved.
*
*
dou@ubuntu:~/a10/linshi_chenxu$ /home/dou/a10/a20/A20-420-V12/lichee/out/linux/common/buildroot/external-toolchain/bin/arm-linux-gnueabi-gcc -o gpio gpio_test.c -static
*/
#include <stdio.h>
#include <linux/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
static const char *dev_data = "/sys/class/gpio_sw/PH19/data"; //raed write data
static const char *dev_mul_sel = "/sys/class/gpio_sw/PH19/mul_sel"; //0 is in,and 1 is out
static const char *dev_pull = "/sys/class/gpio_sw/PH19/pull"; //GPIO的内置电阻状态 0代表高阻,1代表上拉,2代表下拉
static const char *dev_drv_level = "/sys/class/gpio_sw/PH19/drv_level"; //*GPIO的驱动能力 有0到3四个等级
int gpio_init(void)
{
//int fd1,fd2;
int ret = 0;
FILE *fd1 = fopen(dev_mul_sel, "w");
if (fd1 < 0)
printf("can't open device\n\r");
fprintf(fd1,"0"); //设置为输入,6为中断模式
fclose(fd1);
FILE *fd2 = fopen(dev_pull, "w");
if (fd2 < 0)
printf("can't open device\n\r");
fprintf(fd2,"1"); //端口上拉
fclose(fd2);
return 0;
}
int main(void)
{
printf("mcp2515 in\n\r");
int ret = 0;
int value;
unsigned char temp = 0;
int a;
gpio_init();
#if 0
printf("gpio init OK\n\r");
int fd3 = open(dev_data, O_RDWR);
if (fd3 < 0)
printf("can't open device\n\r");
char valueStr[32];
memset(valueStr, 0,sizeof(valueStr));
read(fd3, (void*)valueStr, sizeof(valueStr)-1);
char *end;
value =strtol(valueStr, &end, 0);
if(end == valueStr){
printf("Fail inconvert string %s to number.", valueStr);
close(fd3);
return -1;
}
printf("temp is %d\n\r",value);
#endif
char valueStr[32];
while(1)
{
int fd3 = open(dev_data, O_RDWR);
if (fd3 < 0)
printf("can't open device\n\r");
//char valueStr[32];
memset(valueStr, 0,sizeof(valueStr));
read(fd3, (void*)valueStr, sizeof(valueStr)-1);
char *end;
value =strtol(valueStr, &end, 0);
if(end == valueStr){
printf("Fail inconvert string %s to number.", valueStr);
close(fd3);
return -1;
}
printf("temp 2 is %d\n\r",value);
a = atoi(value);
printf("a 2 is %d\n\r",a);
if(a = 0)
{
printf("temp is 3 %d\n\r",value);
usleep(1000);
if(a = 0)
{
printf("temp is 4 %d\n\r",value);
}
}
usleep(80000);
}
return 0;
}
gpio_test
最新推荐文章于 2023-11-03 14:53:53 发布