美文网首页
【树莓派+arduino实验记录10 】PCF8591数字模拟信

【树莓派+arduino实验记录10 】PCF8591数字模拟信

作者: Geekero | 来源:发表于2020-08-11 12:45 被阅读0次

Arduino

/********************************************************
 * name:Analog-Digital Converter-PCF8591
 * function:the indicator light D2 on the PCF8591 gradually lights up and goes out alternatedly.
 * The same happens to the LED attached to pin 13 of the Arduino Uno board.
 ********************************************************/
 #include "Wire.h"
 #define PCF8591 (0X90 >> 1) // I2C bus address

 void setup()
 {
  Wire.begin();
  Serial.begin(9600);
  Serial.println(sin(PI/2));
 }

 void loop()
 {
  for (int i=0; i<256; i++)
  {
    Wire.beginTransmission(PCF8591); //wake up PCF8591
    Wire.write(0x40); //control byte -turn on DAC (binary 01000000),analog OUTPUT
    Wire.write(i); //value to send to DAC
    Wire.endTransmission(); //end transmission
    delay(10*sin(i/256.0*90/180*PI));
    Serial.println(100*sin(i/256.0*90/180*PI));
  }

  for (int i=255; i>=0; --i)
  {
    Wire.beginTransmission(PCF8591); //wake up PCF8591
    Wire.write(0x40); //control byte - turn on DAC (binary 1000000)
    Wire.write(i); //value to send to DAC
    Wire.endTransmission(); //end transmission
    delay(10*sin(i/256.0*90/180*PI));
    Serial.println(100*sin(i/256.0*90/180*PI));
  }
}

树莓派

C

#include <stdio.h>
#include <wiringPi.h>
#include <pcf8591.h>

#define PCF  120

int main(void)
{
    int value;
    wiringPiSetup();
    // Setup pcf8591 on base pin 120, and address 0x48
    pcf8591Setup(PCF, 0X48);
    while(1) //loop forever
    {
        value = analogRead(PCF+0);
        printf("%d\n", value);
        analogWrite(PCF+0, value);
        delay(10);
    }
    return 0;
}

Python

#!/us/bin/env python
import PCF8591 as ADC

def setup():
    ADC.setup(0x48)

def loop():
    while True:
        print(ADC.read(0))
        ADC.write(ADC.read(0))

def destroy():
    ADC.write(0)

if __name__ == "__main__":
    try:
        setup()
        loop()
    except KeyboardInterrupt:
        destroy()
第三方库PCF8591
#!/usr/bin/env python
#------------------------------------------------------
#
#       This is a program for PCF8591 Module.
#
#       Warnng! The Analog input MUST NOT be over 3.3V!
#    
#       In this script, we use a poteniometer for analog
#   input, and a LED on AO for analog output.
#
#       you can import this script to another by:
#   import PCF8591 as ADC
#   
#   ADC.Setup(Address)  # Check it by sudo i2cdetect -y -1
#   ADC.read(channal)   # Channal range from 0 to 3
#   ADC.write(Value)    # Value range from 0 to 255     
#
#------------------------------------------------------
import smbus
import time

# for RPI version 1, use "bus = smbus.SMBus(0)"
bus = smbus.SMBus(1)

#check your PCF8591 address by type in 'sudo i2cdetect -y -1' in terminal.
def setup(Addr):
    global address
    address = Addr

def read(chn): #channel
    if chn == 0:
        bus.write_byte(address,0x40)
    if chn == 1:
        bus.write_byte(address,0x41)
    if chn == 2:
        bus.write_byte(address,0x42)
    if chn == 3:
        bus.write_byte(address,0x43)
    bus.read_byte(address) # dummy read to start conversion
    return bus.read_byte(address)

def write(val):
    temp = val # move string value to temp
    temp = int(temp) # change string to integer
    # print temp to see on terminal else comment out
    bus.write_byte_data(address, 0x40, temp)

if __name__ == "__main__":
    setup(0x48)
    while True:
        print 'AIN0 = ', read(0)
        print 'AIN1 = ', read(1)
        tmp = read(0)
        tmp = tmp*(255-125)/255+125 # LED won't light up below 125, so convert '0-255' to '125-255'
        write(tmp)
#       time.sleep(0.3)

第三方库存放位置:

/usr/local/lib/python2.7/dist-packages

网友评论

      本文标题:【树莓派+arduino实验记录10 】PCF8591数字模拟信

      本文链接:https://www.haomeiwen.com/subject/ddnedktx.html