參考鏈接: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; }