2015年5月15日 星期五

Enable spi on raspberry pi



Reference:

做了上一篇的one wire 以後,突然發現我lsmod,竟然看不到spi
執行python 時也發現下列的錯誤
但是spi/etc/modprobe.d/raspi-blacklist.conf 中的blacklist我明明有拿掉阿


後來發現可能是kernal版本的問題,所以spidisable掉了

所以輸入下列指令
sudo nano /boot/config.txt

在最下面輸入
dtparam=spi=on


同理也可以用在i2c上面



Raspberry pi communicate with DS1820

Reference:



vdddata pin之間接一個4.7 -10k的電阻

輸入下列指令,注意系統不能是唯讀
$sudo nano /boot/config.txt

在最後面加入這行,如果gpiopin可以自己選擇
dtoverlay=w1-gpio,gpiopin=4,pullup=on

重新開機

接下來我按照reference中輸入
sudo modprobe w1-gpio sudo modprobe w1-therm
都得到下面的錯誤訊息,但是這不會影響結果

接下來可以看 one wire是否有找到temperature sensor,通常會是28開頭之類
然後可以Cat 他的值來







import os
import glob
import time

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'

def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()
    return lines

def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        return temp_c, temp_f
    
while True:
    print(read_temp())
    time.sleep(1)

os.system 是用來執行系統指令

glob 可以用來取出資料夾下特定的檔案類型,譬如說取出所有的png





2015年5月5日 星期二

Freemat 筆記

Freemat 筆記

Reference :



result.txt

data = load('result.txt');
讀入像result.txt 這樣的檔案

row = length(data);
row表示這樣的資訊一共有幾列
注意如果產生file的形式是用/r/n, row代表列數,如果是用/r,則是代表一共有幾個數字

for( i = 1:row);
temperatur(i) = data(i,8);
voltage(i) = data(i,6);
end
從第一列讀到最後一列,data(1,8)表示第一列的第八個數字,也就是溫度的值,
data(1,6)表示第一列的第八個數字,也就是電壓的值,把他們用循環讀出來塞進temperatur(i),voltage(i)

因為freemap不像matlab那樣可以畫兩個y,所以用subplot來做
subplot(2,1,1)
plot(temperatur);
subplot(2,1,2)
plot(voltage);


subplot把兩個讀出來的值把他畫在一個figure
一般形式為 subplot (m, n, p) ,將視窗分為 m ×n 個區域,下一個 plot 指令繪圖於第 p

如果畫兩個圖用subplot(2,1,1),subplot(2,1,2)
如果畫四個圖用subplot(2,2,1),subplot(2,2,2),subplot(2,2,3),subplot(2,2,4)

讀出來長這樣子



ps.Matlab註解用%