树莓派本身没有AD/DA功能,如果树莓派外接模拟传感器,则必须外接AD/DA功能扩展板才能用。Pioneer 600扩展带有AD/DA芯片PCF8591,pcf8591带1通道8位DA,4通道8位AD,通过I2C控制。
一、DAC
1、bcm2835程序
01 | #include <bcm2835.h> |
02 | #include <stdio.h> |
03 | #include <unistd.h> |
04 |
05 | int main( int argc, char **argv) |
06 | { |
07 | char Buf[]={0,0}; |
08 | unsigned char value=0; |
09 |
10 | if (!bcm2835_init()) return 1; |
11 | bcm2835_i2c_begin(); |
12 | bcm2835_i2c_setSlaveAddress(0x48); |
13 | bcm2835_i2c_set_baudrate(10000); |
14 | printf ( "start..........\n" ); |
15 |
16 | while (1) |
17 | { |
18 | Buf[0] = 0x40; |
19 | Buf[1] = value++; |
20 | bcm2835_i2c_write(Buf,2); |
21 | printf ( "AOUT: %d\n" ,value); |
22 |
23 | bcm2835_delay(20); |
24 | } |
25 |
26 | bcm2835_i2c_end(); |
27 | bcm2835_close(); |
28 | return 0; |
29 | } |
编译并执行
1 | gcc –Wall pcf8591.c –o pcf8591 –lbcm2835 |
2 | sudo ./ pcf8591 |
2、Python程序
01 | #!/usr/bin/python |
02 | # -*- coding:utf-8 -*- |
03 | import smbus |
04 | import time |
05 |
06 | address = 0x48 |
07 | cmd = 0x40 |
08 | value = 0 |
09 |
10 | bus = smbus.SMBus(1) |
11 | while True: |
12 | bus.write_byte_data(address,cmd,value) |
13 | value += 1 |
14 | if value == 256: |
15 | value =0 |
16 | print( "AOUT:%3d" %value) |
17 | time.sleep(0.01) |
执行程序
1 | sudo python pcf8591 |
3、wiringPi程序
01 | #include <wiringPi.h> |
02 | #include <pcf8591.h> |
03 | #include <stdio.h> |
04 |
05 | #define Address 0x48 |
06 | #define BASE 64 |
07 | #define A0 BASE+0 |
08 | #define A1 BASE+1 |
09 | #define A2 BASE+2 |
10 | #define A3 BASE+3 |
11 |
12 | int main( void ) |
13 | { |
14 | unsigned char value; |
15 | wiringPiSetup(); |
16 | pcf8591Setup(BASE,Address); |
17 |
18 | while (1) |
19 | { |
20 | analogWrite(A0,value); |
21 | printf ( "AOUT: %d\n" ,value++); |
22 | delay(20); |
23 | } |
24 | } |
编译并执行程序
1 | gcc –Wall pcf8591.c –o pcf8591 –lbcm2835 -lwiringPi |
2 | sudo ./ pcf8591 |
二、ADC
1、bcm2835程序
01 | #include <bcm2835.h> |
02 | #include <stdio.h> |
03 | #include <unistd.h> |
04 |
05 | int main( int argc, char **argv) |
06 | { |
07 | char Buf[]={0}; |
08 | unsigned char i; |
09 |
10 | if (!bcm2835_init()) return 1; |
11 | bcm2835_i2c_begin(); |
12 | bcm2835_i2c_setSlaveAddress(0x48); |
13 | bcm2835_i2c_set_baudrate(10000); |
14 | printf ( "start..........\n" ); |
15 |
16 | while (1) |
17 | { |
18 | for (i = 0;i < 4;i++) |
19 | { |
20 | Buf[0] = i; |
21 | bcm2835_i2c_write_read_rs(Buf,1,Buf,1); |
22 | bcm2835_i2c_read (Buf,1); |
23 | printf ( "AIN%d:%5.2f " ,i,( double )Buf[0]*3.3/255); |
24 | } |
25 | printf ( "\n" ); |
26 | bcm2835_delay(100); |
27 | } |
28 | |
29 | bcm2835_i2c_end(); |
30 | bcm2835_close(); |
31 | return 0; |
32 | } |
编译并执行
1 | gcc –Wall pcf8591.c –o pcf8591 –lbcm2835 |
2 | sudo ./ pcf8591 |
2、Python程序
01 | #!/usr/bin/python |
02 | # -*- coding:utf-8 -*- |
03 | import smbus |
04 | import time |
05 |
06 | address = 0x48 |
07 | A0 = 0x40 |
08 | A1 = 0x41 |
09 | A2 = 0x42 |
10 | A3 = 0x43 |
11 | bus = smbus.SMBus(1) |
12 | while True: |
13 | bus.write_byte(address,A0) |
14 | value = bus.read_byte(address) |
15 | print( "AOUT:%1.3f " %(value*3.3/255)) |
16 | time.sleep(0.1) |
执行程序
1 | sudo python pcf8591 |
3、wiringPi程序
01 | #include <wiringPi.h> |
02 | #include <pcf8591.h> |
03 | #include <stdio.h> |
04 |
05 | #define Address 0x48 |
06 | #define BASE 64 |
07 | #define A0 BASE+0 |
08 | #define A1 BASE+1 |
09 | #define A2 BASE+2 |
10 | #define A3 BASE+3 |
11 |
12 | int main( void ) |
13 | { |
14 | int value; |
15 | wiringPiSetup(); |
16 | pcf8591Setup(BASE,Address); |
17 |
18 | while (1) |
19 | { |
20 | value = analogRead(A0); |
21 | printf ( "Analoge: %dmv\n" ,value*3300/255); |
22 | delay(1000); |
23 | } |
24 | } |
编译并执行程序
1 | gcc –Wall pcf8591.c –o pcf8591 -lwiringPi |
2 | sudo ./ pcf8591 |