本帖最后由 kaloha 于 2017-5-15 18:37 编辑
--------------------------------------------------------------------------------------- 本文转自:www.waveshare.net/study/article-624-1.html --------------------------------------------------------------------------------------- 树莓派本身没有AD/DA功能,如果树莓派外接模拟传感器,则必须外接AD/DA功能扩展板才能用。Pioneer 600扩展带有AD/DA芯片PCF8591,pcf8591带1通道8位DA,4通道8位AD,通过I2C控制。 一、DAC 1、bcm2835程序
- #include
- #include
- #include
- int main(int argc, char **argv)
- {
- char Buf[]={0,0};
- unsigned char value=0;
- if (!bcm2835_init())return 1;
- bcm2835_i2c_begin();
- bcm2835_i2c_setSlaveAddress(0x48);
- bcm2835_i2c_set_baudrate(10000);
- printf("start..........n");
- while(1)
- {
- Buf[0] = 0x40;
- Buf[1] = value++;
- bcm2835_i2c_write(Buf,2);
- printf("AOUT: %dn",value);
- bcm2835_delay(20);
- }
- bcm2835_i2c_end();
- bcm2835_close();
- return 0;
- }
复制代码
编译并执行
- gcc –Wall pcf8591.c –o pcf8591 –lbcm2835
- sudo ./ pcf8591
复制代码
2、Python程序
- #!/usr/bin/python
- # -*- coding:utf-8 -*-
- import smbus
- import time
- address = 0x48
- cmd = 0x40
- value = 0
- bus = smbus.SMBus(1)
- while True:
- bus.write_byte_data(address,cmd,value)
- value += 1
- if value == 256:
- value =0
- print("AOUT:%3d" %value)
- time.sleep(0.01)
复制代码
执行程序
3、wiringPi程序
- #include
- #include
- #include
- #define Address 0x48
- #define BASE 64
- #define A0 BASE+0
- #define A1 BASE+1
- #define A2 BASE+2
- #define A3 BASE+3
- int main(void)
- {
- unsigned char value;
- wiringPiSetup();
- pcf8591Setup(BASE,Address);
- while(1)
- {
- analogWrite(A0,value);
- printf("AOUT: %dn",value++);
- delay(20);
- }
- }
复制代码
编译并执行程序
- gcc –Wall pcf8591.c –o pcf8591 –lbcm2835 -lwiringPi
- sudo ./ pcf8591
复制代码
二、ADC 1、bcm2835程序
- #include
- #include
- #include
- int main(int argc, char **argv)
- {
- char Buf[]={0};
- unsigned char i;
- if (!bcm2835_init())return 1;
- bcm2835_i2c_begin();
- bcm2835_i2c_setSlaveAddress(0x48);
- bcm2835_i2c_set_baudrate(10000);
- printf("start..........n");
- while(1)
- {
- for(i = 0;i < 4;i++)
- {
- Buf[0] = i;
- bcm2835_i2c_write_read_rs(Buf,1,Buf,1);
- bcm2835_i2c_read (Buf,1);
- printf("AIN%d:%5.2f ",i,(double)Buf[0]*3.3/255);
- }
- printf("n");
- bcm2835_delay(100);
- }
-
- bcm2835_i2c_end();
- bcm2835_close();
- return 0;
- }
复制代码
编译并执行
- gcc –Wall pcf8591.c –o pcf8591 –lbcm2835
- sudo ./ pcf8591
复制代码
2、Python程序
- #!/usr/bin/python
- # -*- coding:utf-8 -*-
- import smbus
- import time
- address = 0x48
- A0 = 0x40
- A1 = 0x41
- A2 = 0x42
- A3 = 0x43
- bus = smbus.SMBus(1)
- while True:
- bus.write_byte(address,A0)
- value = bus.read_byte(address)
- print("AOUT:%1.3f " %(value*3.3/255))
- time.sleep(0.1)
复制代码
执行程序
3、wiringPi程序
- #include
- #include
- #include
- #define Address 0x48
- #define BASE 64
- #define A0 BASE+0
- #define A1 BASE+1
- #define A2 BASE+2
- #define A3 BASE+3
- int main(void)
- {
- int value;
- wiringPiSetup();
- pcf8591Setup(BASE,Address);
- while(1)
- {
- value = analogRead(A0);
- printf("Analoge: %dmvn",value*3300/255);
- delay(1000);
- }
- }
复制代码
编译并执行程序
- gcc –Wall pcf8591.c –o pcf8591 -lwiringPi
- sudo ./ pcf8591
复制代码
2
|
|
|
|