一、前言
初学linux,从hellow word开始,但是我这现在连初学都算不上,还没到学习hellow word驱动那一步,目前就是利用文件io驱动最最最最简单基础的led或者最最最简单的不要求时序和速度的ic
注:本文未使用到驱动,只用到文件io输入输出,和交叉编译
二、环境
Ubuntu16
vaware 12
全志a40i(linux3.10)
window10
a40i交叉编译器
三、正文
1.输出
方式一:指定引脚PG8或其他输出
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
while(1)
{
system("echo 1 > /sys/class/gpio_sw/PG8/data");
usleep(10000);
system("echo 0 > /sys/class/gpio_sw/PG8/data");
usleep(10000);
}
return 0;
}
优点:无需初始化io输入输出状态,直接可以使用
方式二:将引脚转换为对应端口号驱动GPIO的计算公式为(32 * (bank) + (gpio))
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
char *GPIO_PATH = "/sys/class/gpio/gpio271/value";
int main(int argc, char *argv[])
{
int fd;
char *path;
path = GPIO_PATH;
system("echo 271 > /sys/class/gpio/export");//led6
system("echo out > /sys/class/gpio/gpio271/direction");
fd = open(path, O_WRONLY);
if(fd<0)
{
printf("Failed to Open gpio %s\n", path);
exit(1);
}
while(1)
{
write(fd, "1", 1);
usleep(200000);
write(fd, "0", 1);
usleep(200000);
}
close(fd);
return 0;
}
2.输入
方式一:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <poll.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
char *GPIO_PATH = "/sys/class/gpio/gpio41/value";//PB9
long long times=0;
void sysUsecTime();
int main(int argc, char *argv[])
{
system("echo 41 > /sys/class/gpio/export");//PB9
system("echo in > /sys/class/gpio/gpio41/direction");//PB9
//system("echo rising > /sys/class/gpio/gpio41/edge");//PB9
//usleep(100000);//延时100ms
int gpio_value = 1;
char value_str[3];
int fd;
sysUsecTime();
while(gpio_value)
{
fd = open(GPIO_PATH, O_RDONLY);
if (fd < 0) {
perror("Failed to open gpio value for reading!");
return -1;
}
if (read(fd, value_str, 3) < 0) {
perror("Failed to read value!");
return -1;
}
gpio_value=atoi(value_str);
close(fd);
times++;
//usleep(1000);
if(times>1000000)//超时1秒退出
break;
}
printf("times:%d,value:%d\r\n",times,gpio_value);
sysUsecTime();
return 0;
}
方式二:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <poll.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
char *GPIO_PATH = "/sys/class/gpio/gpio41/value";//PB9
long long times=0;
void sysUsecTime();
int main(int argc, char *argv[])
{
system("echo 41 > /sys/class/gpio/export");//PB9
system("echo in > /sys/class/gpio/gpio41/direction");//PB9
//system("echo rising > /sys/class/gpio/gpio41/edge");//PB9
//usleep(100000);//延时100ms
int gpio_value = 1;
FILE *fp;
sysUsecTime();
while(gpio_value)
{
fp = fopen(GPIO_PATH,"rb+");
fscanf(fp, "%d", &gpio_value);
fclose(fp);
times++;
//usleep(1000);
if(times>1000000)
break;
}
printf("times:%d,value:%d\r\n",times,gpio_value);
sysUsecTime();
return 0;
}
快捷输入输出设置方式:
例子1:以AD7606驱动代码为例(目前此方式未成功驱动成功,怀疑此方法不适合驱动AD7606,需要使用驱动的方式)(只是IO输入输出做为参考)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <poll.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#define SUNXI_PA_BASE 0
#define SUNXI_PB_BASE 32
#define SUNXI_PC_BASE 64
#define SUNXI_PD_BASE 96
#define SUNXI_PE_BASE 128
#define SUNXI_PF_BASE 160
#define SUNXI_PG_BASE 192
#define SUNXI_PH_BASE 224
#define SUNXI_PI_BASE 256
#define SUNXI_PJ_BASE 288
#define SUNXI_PK_BASE 320
#define SUNXI_PL_BASE 352
#define SUNXI_PM_BASE 384
#define SUNXI_PN_BASE 416
#define SUNXI_PO_BASE 448
#define AXP_PIN_BASE 1024
/* sunxi gpio name space */
#define GPIOA(n) (SUNXI_PA_BASE + (n))
#define GPIOB(n) (SUNXI_PB_BASE + (n))
#define GPIOC(n) (SUNXI_PC_BASE + (n))
#define GPIOD(n) (SUNXI_PD_BASE + (n))
#define GPIOE(n) (SUNXI_PE_BASE + (n))
#define GPIOF(n) (SUNXI_PF_BASE + (n))
#define GPIOG(n) (SUNXI_PG_BASE + (n))
#define GPIOH(n) (SUNXI_PH_BASE + (n))
#define GPIOI(n) (SUNXI_PI_BASE + (n))
#define GPIOJ(n) (SUNXI_PJ_BASE + (n))
#define GPIOK(n) (SUNXI_PK_BASE + (n))
#define GPIOL(n) (SUNXI_PL_BASE + (n))
#define GPIOM(n) (SUNXI_PM_BASE + (n))
#define GPION(n) (SUNXI_PN_BASE + (n))
#define GPIOO(n) (SUNXI_PO_BASE + (n))
#define GPIO_AXP(n) (AXP_PIN_BASE + (n))
#define data15 get_gpio_value(GPIOB(3))
#define data14 get_gpio_value(GPIOB(5))
#define data13 get_gpio_value(GPIOI(15))
#define data12 get_gpio_value(GPIOG(5))
#define data11 get_gpio_value(GPIOG(4))
#define data10 get_gpio_value(GPIOG(3))
#define data09 get_gpio_value(GPIOG(2))
#define data08 get_gpio_value(GPIOG(0))
#define data07 get_gpio_value(GPIOG(1))
#define data06 get_gpio_value(GPIOI(3))
#define data05 get_gpio_value(GPIOI(2))
#define data04 get_gpio_value(GPIOI(1))
#define data03 get_gpio_value(GPIOI(0))
#define data02 get_gpio_value(GPIOB(12))
#define data01 get_gpio_value(GPIOB(10))
#define data00 get_gpio_value(GPIOB(8))
#define AD7606_BUSY get_gpio_value(GPIOI(11))
#define AD7606_FASTDATA get_gpio_value(GPIOH(0))//no used
#define AD7606_OS0_H() set_gpio_value(GPIOF(5),"1")
#define AD7606_OS0_L() set_gpio_value(GPIOF(5),"0")
#define AD7606_OS1_H() set_gpio_value(GPIOF(4),"1")
#define AD7606_OS1_L() set_gpio_value(GPIOF(4),"0")
#define AD7606_OS2_H() set_gpio_value(GPIOF(3),"1")
#define AD7606_OS2_L() set_gpio_value(GPIOF(3),"0")
#define AD7606_CS_RD_H() set_gpio_value(GPIOF(0),"1")
#define AD7606_CS_RD_L() set_gpio_value(GPIOF(0),"0")
#define AD7606_Reset_H() set_gpio_value(GPIOF(1),"1")
#define AD7606_Reset_L() set_gpio_value(GPIOF(1),"0")
#define AD7606_CONVT_H() set_gpio_value(GPIOF(2),"1")
#define AD7606_CONVT_L() set_gpio_value(GPIOF(2),"0")
#define LED_on() set_gpio_value(GPIOB(11),"0")
#define LED_off() set_gpio_value(GPIOB(11),"1")
需要以下代码做为支撑
long long times=0;
void sysUsecTime();
//创建gpio文件
void init_gpio_export(int n)
{
FILE * fp =fopen("/sys/class/gpio/export","w");
if (fp == NULL)
perror("export open filed");
else
fprintf(fp,"%d",n);
fclose(fp);
}
//设置gpio的模式 输入/输出 in/out
void set_gpio_direction(int n,const char *direction)
{
char path[100] = {0};
sprintf(path,"/sys/class/gpio/gpio%d/direction",n);
FILE * fp =fopen(path,"w");
if (fp == NULL)
perror("direction open filed");
else
fprintf(fp,"%s",direction);
fclose(fp);
}
//获取gpio(n)的数值 0/1
int get_gpio_value(int n)
{
char path[64];
char value_str[3];
int fd;
snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", n);
fd = open(path, O_RDONLY);
if (fd < 0) {
perror("Failed to open gpio value for reading!");
return -1;
}
if (read(fd, value_str, 3) < 0) {
perror("Failed to read value!");
return -1;
}
close(fd);
return (atoi(value_str));
}
//输出gpio(n)的数值 0/1 (方式一)
void set_gpio_value(int n,const char *value)
{
char path[50];
//char value_str[3];
int fd;
snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", n);
fd = open(path, O_WRONLY);
//snprintf(value_str, sizeof(value_str), "%d", value);
write(fd, value, 1);
close(fd);
}
//输出gpio(n)的数值 0/1 (方式二)
int set_gpio_value1(int gpio, int value)
{
FILE *p = NULL;
char gpio_value_path[50];
sprintf(gpio_value_path, "/sys/class/gpio/gpio%d/value", gpio);
p = fopen(gpio_value_path, "w");
fprintf(p, "%d", value);
fclose(p);
return 0;
}
//IO初始化函数
void IOinit(void)
{
/* ------------ AD7606_DB[0..15] ----------- */
init_gpio_export(GPIOB(8));//AD7606_DB0
set_gpio_direction(GPIOB(8),"in");
init_gpio_export(GPIOB(10));//AD7606_DB1
set_gpio_direction(GPIOB(10),"in");
init_gpio_export(GPIOB(12));//AD7606_DB2
set_gpio_direction(GPIOB(12),"in");
init_gpio_export(GPIOI(0));//AD7606_DB3
set_gpio_direction(GPIOI(0),"in");
init_gpio_export(GPIOI(1));//AD7606_DB4
set_gpio_direction(GPIOI(1),"in");
init_gpio_export(GPIOI(2));//AD7606_DB5
set_gpio_direction(GPIOI(2),"in");
init_gpio_export(GPIOI(3));//AD7606_DB6
set_gpio_direction(GPIOI(3),"in");
init_gpio_export(GPIOG(1));//AD7606_DB7
set_gpio_direction(GPIOG(1),"in");
init_gpio_export(GPIOG(0));//AD7606_DB8
set_gpio_direction(GPIOG(0),"in");
init_gpio_export(GPIOG(2));//AD7606_DB9
set_gpio_direction(GPIOG(2),"in");
init_gpio_export(GPIOG(3));//AD7606_DB10
set_gpio_direction(GPIOG(3),"in");
init_gpio_export(GPIOG(4));//AD7606_DB11
set_gpio_direction(GPIOG(4),"in");
init_gpio_export(GPIOG(5));//AD7606_DB12
set_gpio_direction(GPIOG(5),"in");
init_gpio_export(GPIOI(15));//AD7606_DB13
set_gpio_direction(GPIOI(15),"in");
init_gpio_export(GPIOB(5));//AD7606_DB14
set_gpio_direction(GPIOB(5),"in");
init_gpio_export(GPIOB(3));//AD7606_DB15
set_gpio_direction(GPIOB(3),"in");
/* ------------ AD7606_AD_OS[0..2] ----------- */
init_gpio_export(GPIOF(5));//AD7606_OS0
set_gpio_direction(GPIOF(5),"out");
init_gpio_export(GPIOF(4));//AD7606_OS1
set_gpio_direction(GPIOF(4),"out");
init_gpio_export(GPIOF(3));//AD7606_OS2
set_gpio_direction(GPIOF(3),"out");
/* ------------ RESET ----------- */
init_gpio_export(GPIOF(1));//AD7606_RESET
set_gpio_direction(GPIOF(1),"out");
/* ------------ CONVST ----------- */
init_gpio_export(GPIOF(2));//AD7606_CONVST
set_gpio_direction(GPIOF(2),"out");
/* ------------ BUSY ----------- */
init_gpio_export(GPIOI(11));//AD7606_BUSY
set_gpio_direction(GPIOI(11),"in");
/* ------------ FSTDATA ----------- */
init_gpio_export(GPIOH(0));//AD7606_FASTDATA
set_gpio_direction(GPIOH(0),"in");
/* ------------ CS/RD ----------- */
init_gpio_export(GPIOF(0));//AD7606_CS/RD
set_gpio_direction(GPIOF(0),"out");
init_gpio_export(GPIOB(11));
set_gpio_direction(GPIOB(11),"out");
}
//IO释放函数
void IOreleaset(void)
{
}
void IO_In_test(void)
{
printf("\r\n");
printf("channel0=%d\r\n",data00);
printf("channel1=%d\r\n",data01);
printf("channel2=%d\r\n",data02);
printf("channel3=%d\r\n",data03);
printf("channel4=%d\r\n",data04);
printf("channel5=%d\r\n",data05);
printf("channel6=%d\r\n",data06);
printf("channel7=%d\r\n",data07);
printf("channel8=%d\r\n",data08);
printf("channel9=%d\r\n",data09);
printf("channel10=%d\r\n",data10);
printf("channel11=%d\r\n",data11);
printf("channel12=%d\r\n",data12);
printf("channel13=%d\r\n",data13);
printf("channel14=%d\r\n",data14);
printf("channel15=%d\r\n",data15);
printf("busy=%d\r\n",AD7606_BUSY);
printf("fastdata=%d\r\n",AD7606_FASTDATA);
}
void IO_out_test(void)
{
AD7606_OS0_L();
AD7606_OS1_L();
AD7606_OS2_L();
AD7606_CS_RD_L();
AD7606_Reset_L();
AD7606_CONVT_L();
LED_off();
}
//AD7606初始化函数
void AD7606init(void)
{
AD7606_OS0_L();
AD7606_OS1_L();
AD7606_OS2_L();
AD7606_CONVT_H();
AD7606_CS_RD_H();
AD7606_Reset_L();
// AD7606_Reset_H();
//AD7606_Reset_L();
}
int16_t ADC_buf[8];//读取ad缓存
int16_t ADC_1_2[8];//待存储的采集原始数据
float ADC_buf_float[8];//调试查看的转换值,0x38为零漂,这里暂时设为这个,上位机在单独配置每个通道的零漂 和 计算实际数值
//读取数值函数
void AD7606_Read(void)
{
//AD7606_Reset_H();
//AD7606_Reset_L();
AD7606_CS_RD_H();
AD7606_CONVT_L();
AD7606_CONVT_H();
while(AD7606_BUSY);
AD7606_CS_RD_L();
for(int i=0;i<8;i++)
{
ADC_buf[i] = 0;
AD7606_CS_RD_L();
ADC_buf[i] = data15;
ADC_buf[i] = ADC_buf[i]<<1 | data14;
ADC_buf[i] = ADC_buf[i]<<1 | data13;
ADC_buf[i] = ADC_buf[i]<<1 | data12;
ADC_buf[i] = ADC_buf[i]<<1 | data11;
ADC_buf[i] = ADC_buf[i]<<1 | data10;
ADC_buf[i] = ADC_buf[i]<<1 | data09;
ADC_buf[i] = ADC_buf[i]<<1 | data08;
ADC_buf[i] = ADC_buf[i]<<1 | data07;
ADC_buf[i] = ADC_buf[i]<<1 | data06;
ADC_buf[i] = ADC_buf[i]<<1 | data05;
ADC_buf[i] = ADC_buf[i]<<1 | data04;
ADC_buf[i] = ADC_buf[i]<<1 | data03;
ADC_buf[i] = ADC_buf[i]<<1 | data02;
ADC_buf[i] = ADC_buf[i]<<1 | data01;
ADC_buf[i] = ADC_buf[i]<<1 | data00;
AD7606_CS_RD_H();
}
AD7606_CS_RD_L();
printf("\r\n");
for(int i=0;i<8;i++)
{
//ADC_buf_float只是看,没使用到
ADC_buf_float[i] = (float)(ADC_buf[i]) * 5 / 32768*4;
ADC_1_2[i] = ADC_buf[i];
printf("ad7606 get channel-%d value = %f, hex = %d\r\n",i+1,ADC_buf_float[i],ADC_1_2[i]);
}
}
int main()
{
sysUsecTime();
IOinit();
AD7606init();
times=0;
while(1)
{
usleep(1000000);
//IO_out_test();
AD7606_Read();
//IO_In_test();
//if(data00)LED_H();
//else LED_L();
}
return 0;
}
void sysUsecTime()
{
struct timeval tv;
struct timezone tz;
struct tm *p;
gettimeofday(&tv, &tz);
//printf("tv_sec:%ld\n",tv.tv_sec);
//printf("tv_usec:%ld\n",tv.tv_usec);
//printf("tz_minuteswest:%d\n",tz.tz_minuteswest);
//printf("tz_dsttime:%d\n",tz.tz_dsttime);
p = localtime(&tv.tv_sec);
printf("time_now:%d-%d-%d %d:%d:%d.%ld\n", 1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, tv.tv_usec);
}
例子2:以HX711驱动代码为例(同样未成功,使用逻辑分析仪发现sda输入只有一个脉冲,怀疑是此类文件io方式驱动未设置io为浮空上拉模式,故失败,需要采用驱动的形式在测试)(只是IO输入输出做为参考)
//参考链接:https://blog.csdn.net/u011983700/article/details/106923194/
/GPIO的计算公式为(32 * (bank) + (gpio))/
//direction:设置输出还是输入模式
//设置为输入:echo “in” > direction
//设置为输出:echo “out” > direction
//value:输出时,控制高低电平;输入时,获取高低电平
//高电平:echo 1 > value
//低电平:echo 0 > value
//edge:控制中断触发模式,引脚被配置为中断后可以使用poll() 函数监听引脚
//非中断引脚: echo “none” > edge
//上升沿触发:echo “rising” > edge
//下降沿触发:echo “falling” > edge
//边沿触发:echo “both” > edge
//IO配置
//输入IO
//;IIC_SDA1 PG0
//;IIC_SDA2 PG3
//;IIC_SDA3 PG5
//;IIC_SDA4 PI2
//;IIC_SDA5 PI0
//输出IO
//;IIC_SCK1 PG2
//;IIC_SCK2 PG4
//;IIC_SCK3 PI15
//;IIC_SCK4 PI3
//;IIC_SCK5 PI1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <poll.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#define SUNXI_PA_BASE 0
#define SUNXI_PB_BASE 32
#define SUNXI_PC_BASE 64
#define SUNXI_PD_BASE 96
#define SUNXI_PE_BASE 128
#define SUNXI_PF_BASE 160
#define SUNXI_PG_BASE 192
#define SUNXI_PH_BASE 224
#define SUNXI_PI_BASE 256
#define SUNXI_PJ_BASE 288
#define SUNXI_PK_BASE 320
#define SUNXI_PL_BASE 352
#define SUNXI_PM_BASE 384
#define SUNXI_PN_BASE 416
#define SUNXI_PO_BASE 448
#define AXP_PIN_BASE 1024
/* sunxi gpio name space */
#define GPIOA(n) (SUNXI_PA_BASE + (n))
#define GPIOB(n) (SUNXI_PB_BASE + (n))
#define GPIOC(n) (SUNXI_PC_BASE + (n))
#define GPIOD(n) (SUNXI_PD_BASE + (n))
#define GPIOE(n) (SUNXI_PE_BASE + (n))
#define GPIOF(n) (SUNXI_PF_BASE + (n))
#define GPIOG(n) (SUNXI_PG_BASE + (n))
#define GPIOH(n) (SUNXI_PH_BASE + (n))
#define GPIOI(n) (SUNXI_PI_BASE + (n))
#define GPIOJ(n) (SUNXI_PJ_BASE + (n))
#define GPIOK(n) (SUNXI_PK_BASE + (n))
#define GPIOL(n) (SUNXI_PL_BASE + (n))
#define GPIOM(n) (SUNXI_PM_BASE + (n))
#define GPION(n) (SUNXI_PN_BASE + (n))
#define GPIOO(n) (SUNXI_PO_BASE + (n))
#define GPIO_AXP(n) (AXP_PIN_BASE + (n))
#define HX711_SDA1_IN() get_gpio_value(GPIOG(0))
#define HX711_SDA2_IN() get_gpio_value(GPIOG(3))
#define HX711_SDA3_IN() get_gpio_value(GPIOG(5))
#define HX711_SDA4_IN() get_gpio_value(GPIOI(2))
#define HX711_SDA5_IN() get_gpio_value(GPIOI(0))
#define HX711_SDA1_H() set_gpio_value(GPIOG(0),"1")
#define HX711_SDA1_L() set_gpio_value(GPIOG(0),"0")
#define HX711_SCL1_H() set_gpio_value(GPIOG(2),"1")
#define HX711_SCL1_L() set_gpio_value(GPIOG(2),"0")
#define HX711_SDA2_H() set_gpio_value(GPIOG(3),"1")
#define HX711_SDA2_L() set_gpio_value(GPIOG(3),"0")
#define HX711_SCL2_H() set_gpio_value(GPIOG(4),"1")
#define HX711_SCL2_L() set_gpio_value(GPIOG(4),"0")
#define HX711_SDA3_H() set_gpio_value(GPIOG(5),"1")
#define HX711_SDA3_L() set_gpio_value(GPIOG(5),"0")
#define HX711_SCL3_H() set_gpio_value(GPIOI(15),"1")
#define HX711_SCL3_L() set_gpio_value(GPIOI(15),"0")
#define HX711_SDA4_H() set_gpio_value(GPIOI(2),"1")
#define HX711_SDA4_L() set_gpio_value(GPIOI(2),"0")
#define HX711_SCL4_H() set_gpio_value(GPIOI(3),"1")
#define HX711_SCL4_L() set_gpio_value(GPIOI(3),"0")
#define HX711_SDA5_H() set_gpio_value(GPIOI(0),"1")
#define HX711_SDA5_L() set_gpio_value(GPIOI(0),"0")
#define HX711_SCL5_H() set_gpio_value(GPIOI(1),"1")
#define HX711_SCL5_L() set_gpio_value(GPIOI(1),"0")
long long times=0;
void sysUsecTime();
//创建gpio文件
void init_gpio_export(int n)
{
FILE * fp =fopen("/sys/class/gpio/export","w");
if (fp == NULL)
perror("export open filed");
else
fprintf(fp,"%d",n);
fclose(fp);
}
//设置gpio的模式 输入/输出 in/out
void set_gpio_direction(int n,const char *direction)
{
char path[100] = {0};
sprintf(path,"/sys/class/gpio/gpio%d/direction",n);
FILE * fp =fopen(path,"w");
if (fp == NULL)
perror("direction open filed");
else
fprintf(fp,"%s",direction);
fclose(fp);
}
//获取gpio(n)的数值 0/1
int get_gpio_value(int n)
{
char path[64];
char value_str[3];
int fd;
snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", n);
fd = open(path, O_RDONLY);
if (fd < 0) {
perror("Failed to open gpio value for reading!");
return -1;
}
if (read(fd, value_str, 3) < 0) {
perror("Failed to read value!");
return -1;
}
close(fd);
return (atoi(value_str));
}
//输出gpio(n)的数值 0/1 (方式一)
void set_gpio_value(int n,const char *value)
{
char path[50];
//char value_str[3];
int fd;
snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", n);
fd = open(path, O_WRONLY);
//snprintf(value_str, sizeof(value_str), "%d", value);
write(fd, value, 1);
close(fd);
}
//输出gpio(n)的数值 0/1 (方式二)
int set_gpio_value1(int gpio, int value)
{
FILE *p = NULL;
char gpio_value_path[50];
sprintf(gpio_value_path, "/sys/class/gpio/gpio%d/value", gpio);
p = fopen(gpio_value_path, "w");
fprintf(p, "%d", value);
fclose(p);
return 0;
}
//IO初始化函数
void IOinit(void)
{
init_gpio_export(GPIOG(0));//HX711_SDA1
set_gpio_direction(GPIOG(0),"in");
init_gpio_export(GPIOG(3));//HX711_SDA2
set_gpio_direction(GPIOG(3),"in");
init_gpio_export(GPIOG(5));//HX711_SDA3
set_gpio_direction(GPIOG(5),"in");
init_gpio_export(GPIOI(2));//HX711_SDA4
set_gpio_direction(GPIOI(2),"in");
init_gpio_export(GPIOI(0));//HX711_SDA5
set_gpio_direction(GPIOI(0),"in");
init_gpio_export(GPIOG(2));//HX711_SCL1
set_gpio_direction(GPIOG(2),"out");
init_gpio_export(GPIOG(4));//HX711_SCL2
set_gpio_direction(GPIOG(4),"out");
init_gpio_export(GPIOI(15));//HX711_SCL3
set_gpio_direction(GPIOI(15),"out");
init_gpio_export(GPIOI(3));//HX711_SCL4
set_gpio_direction(GPIOI(3),"out");
init_gpio_export(GPIOI(1));//HX711_SCL5
set_gpio_direction(GPIOI(1),"out");
}
//IO释放函数
void IOreleaset(void)
{
}
void IO_In_test(void)
{
printf("\r\n");
printf("HX711_SDA1_IN=%d\r\n",HX711_SDA1_IN());
printf("HX711_SDA2_IN=%d\r\n",HX711_SDA2_IN());
printf("HX711_SDA3_IN=%d\r\n",HX711_SDA3_IN());
printf("HX711_SDA4_IN=%d\r\n",HX711_SDA4_IN());
printf("HX711_SDA5_IN=%d\r\n",HX711_SDA5_IN());
}
void IO_out_test(void)
{
HX711_SDA1_H();
HX711_SCL1_H();
HX711_SDA2_H();
HX711_SCL2_H();
HX711_SDA3_H();
HX711_SCL3_H();
HX711_SDA4_H();
HX711_SCL4_H();
HX711_SDA5_H();
HX711_SCL5_H();
}
unsigned long HX711_Read(void) //读取711读到的数据
{
unsigned long val;
unsigned char i;
HX711_SDA1_H();
usleep(5);
HX711_SCL1_L();
val = 0;
while(HX711_SDA1_IN());
usleep(1);
for(i=0;i<24;i++){
HX711_SCL1_H();
val=val<<1;
usleep(1);
HX711_SCL1_L();
if(HX711_SDA1_IN())
val++;
usleep(1);
}
HX711_SCL1_H();
val = val^0x800000;
usleep(5);
HX711_SCL1_L();
usleep(1);
return val;
}
double hx711_buffer=0;//定义hx711_buffer来接受hx711read读取的信息
double weight_maopi=0;//毛皮重量
double weight_shiwu=0;//实物重量
double Get_Maopi(void)
{
hx711_buffer=HX711_Read();
weight_maopi=hx711_buffer;
return weight_maopi;
}
double Get_Weight(void)
{
hx711_buffer=HX711_Read();
weight_shiwu=hx711_buffer-weight_maopi;
weight_shiwu=weight_shiwu/100;
weight_shiwu=(unsigned int)((float)weight_shiwu/4.30+0.05);//每一个传感器需要矫正4.30这个除数。当发现测试出来的重量偏大时,增加该数值。如果测试出来的重量偏小时,减小改数值.该数值一般在4.0-5.0之间。因传感器线性斜率不同而定。
// if(hx711_buffer>weight_maopi)
// weight_shiwu=hx711_buffer-weight_maopi;
return weight_shiwu;
}
int main()
{
double maopi,shiwu;
sysUsecTime();
IOinit();
maopi=Get_Maopi();
printf("maopi = %.4fg\r\n",maopi);
times=0;
while(1)
{
usleep(1000000);
shiwu=Get_Weight();
printf("shiwu = %.4fg\r\n",shiwu);//输出实物重量并保留两位小数
}
return 0;
}
void sysUsecTime()
{
struct timeval tv;
struct timezone tz;
struct tm *p;
gettimeofday(&tv, &tz);
//printf("tv_sec:%ld\n",tv.tv_sec);
//printf("tv_usec:%ld\n",tv.tv_usec);
//printf("tz_minuteswest:%d\n",tz.tz_minuteswest);
//printf("tz_dsttime:%d\n",tz.tz_dsttime);
p = localtime(&tv.tv_sec);
printf("time_now:%d-%d-%d %d:%d:%d.%ld\n", 1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, tv.tv_usec);
}