树莓派 AD/DA测试
参考链接:https://blog.csdn.net/xukai871105/article/details/18450809
https://blog.csdn.net/qq_41923622/article/details/84943916
http://www.waveshare.net/study/article-624-1.html
Python:sudo apt-get install python-smbus
#!/usr/bin//env python # -*- coding:utf-8 -*- import smbus import time address = 0x48 ## address ---> 器件的地址(硬件地址 由器件决定) A0 = 0x40 ## A0 ----> 器件某个端口的地址(数据存储的寄存器) A1 = 0x41 A2 = 0x42 A3 = 0x43 bus = smbus.SMBus(1) ## 开启总线 while True: ##循环查询 bus.write_byte(address,A2) ## 告诉树莓派 你想获取那个器件的那个端口的数据 value = 143-bus.read_byte(address) ## 获得数据 print("当前温度:%1.0f ℃ " %(value)) ##打印数据 time.sleep(1) ##延迟1秒
C代码:
#include <bcm2835.h> #include <stdio.h> #include <unistd.h> // DAC 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: %d\n",value); bcm2835_delay(20); } bcm2835_i2c_end(); bcm2835_close(); return 0; } //ADC 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; }