步骤4:将所有东西连接在一起然后打开你的树莓派,不知道为啥网上给树莓派起名字叫覆盆子,太tm难听了。raspberry就改成树莓不行么?
手绘电路图,看懂了就过,看不懂就自己学习一下再过。
接驳示意图,ADC的AIN1 通道采样。中间抽头给树莓派,采集信号信息。
C语言编程
下面的内容就是开始编程了,如果你喜欢用C,那么就这样,写个源码:
#include
#include
#include
#include
#include
#include
void main()
{
wiringPiSetup();
//Physical Pin = 40, name is GPIO.29 and wPi name is 29, BCM 21.
pinMode(29, OUTPUT);
int file;
char *bus = "/dev/i2c-1";
if ((file = open(bus, O_RDWR)) < 0)
{
printf("Failed to open the bus.\n");
exit(1);
}
// Get I2C device, ADS1115 I2C address is 0x48(72)
ioctl(file, I2C_SLAVE, 0x48)
// Select configuration register(0x01)
// AINP = AIN0 and AINN = AIN1, +/- 2.048V
// Continuous conversion mode, 128 SPS(0x84, 0x83)
char config[3] = {0};
config[0] = 0x01;
config[1] = 0xD4;
config[2] = 0x83;
write(file, config, 3);
sleep(1);
// Read 2 bytes of data from register(0x00)
// raw_adc msb, raw_adc lsb
char reg[1] = {0x00};
write(file, reg, 1);
char data[2]={0};
if(read(file, data, 2) != 2)
{
printf("Error : Input/Output Error\n");
}
else
{
// Convert the data
int raw_adc = (data[0] * 256 + data[1]);
if (raw_adc > 32767)
{
raw_adc -= 65535;
}
// Output data to screen
printf("Analog Data is: %d \n", raw_adc);
if ( raw_adc > 3200 )
{
printf("Turn on LED\n");
digitalWrite(29, LOW); // turn on the LED
}
else {
printf("Turn off LED\n");
digitalWrite(29, HIGH); //turn off the LED
}
}
}
编译和测试:
gcc -o adc -lwiringPi adc.c
注意: