給微控制器電路增加一個(gè)傳感器户辫,測(cè)量感興趣的值
這周開始渡讼,芯片 Attiny45 將會(huì)頻繁出場(chǎng):
電路設(shè)計(jì)
嘗試用 phototransistor (光電晶體管)作為輸入模塊溜哮,去控制 LED 掸屡。
在 Eagle 先畫 schematic:
然后生成 board擎勘±涫兀可以先用 autorouter 自動(dòng)布線刀崖,然后再根據(jù)需要手動(dòng)調(diào)整。
在完成設(shè)計(jì)之前拍摇,需要用 ERC 命令檢查 schematic 的錯(cuò)誤亮钦,用 DRC 檢查 board 的錯(cuò)誤。根據(jù)機(jī)器刀頭的尺寸充活,設(shè)置線路間隔為 16mil 蜂莉。
全部仔細(xì)檢查后,再分別把 top 圖層和 dimension 圖層輸出為兩個(gè) png 文件混卵,要注意勾選單色輸出:
Download the eagle sch & brd & png files.
電路板制作
依然使用 Roland SMR-20 刻電路板映穗,用 1/64 刀頭刻線路,1/32 切割外框幕随。在 fabmodules.org 里面蚁滋,設(shè)置輸入的 png 圖像 dpi 為 1500 ,然后計(jì)算雕刻路徑:
將元件們焊好:
程序
這周開始用 Arduino 寫程序赘淮。在上傳之前辕录,需要選好板子的類型以及芯片。 Attiny45 使用 8 MHz 內(nèi)頻梢卸。
代碼如下走诞,也可以到這里下載。
// set pin numbers:
const int buttonPin = 3; // the number of the pushbutton pin
const int ledPin = 4; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
在這個(gè)簡(jiǎn)單的電路程序中低剔,光電晶體管起到開關(guān)的作用速梗,根據(jù)光亮控制電路中 LED 的明滅:
試驗(yàn)1:用 Python 和串口通信
制作一塊帶有光電晶體管的板子:
下載 hello.light.45.c 和 makefile肮塞。在 terminal cd 進(jìn)入文件夾,輸入命令 make -f hello.light.45.make
得到回應(yīng):
avr-gcc -mmcu=attiny45 -Wall -Os -DF_CPU=8000000 -I./ -o hello.button.45.out hello.button.45.c
avr-objcopy -O ihex hello.button.45.out hello.button.45.c.hex;\
avr-size --mcu=attiny45 --format=avr hello.button.45.out
AVR Memory Usage
----------------
Device: attiny45
Program: 364 bytes (8.9% Full)
(.text + .data + .bootloader)
Data: 0 bytes (0.0% Full)
(.data + .bss + .noinit)
然后輸入命令: sudo make -f hello.light.45.make program-usbtiny
得到回應(yīng):
avr-objcopy -O ihex hello.light.45.out hello.light.45.c.hex;\
avr-size --mcu=attiny45 --format=avr hello.light.45.out
AVR Memory Usage
----------------
Device: attiny45
Program: 502 bytes (12.3% Full)
(.text + .data + .bootloader)
Data: 1 bytes (0.4% Full)
(.data + .bss + .noinit)
avrdude -p t45 -P usb -c usbtiny -U flash:w:hello.light.45.c.hex
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.00s
avrdude: Device signature = 0x1e9206
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "hello.light.45.c.hex"
avrdude: input file hello.light.45.c.hex auto detected as Intel Hex
avrdude: writing flash (502 bytes):
Writing | ################################################## | 100% 0.74s
avrdude: 502 bytes of flash written
avrdude: verifying flash memory against hello.light.45.c.hex:
avrdude: load data flash data from input file hello.light.45.c.hex:
avrdude: input file hello.light.45.c.hex auto detected as Intel Hex
avrdude: input file hello.light.45.c.hex contains 502 bytes
avrdude: reading on-chip flash data:
Reading | ################################################## | 100% 0.94s
avrdude: verifying ...
avrdude: 502 bytes of flash verified
Python 程序可以在這里下載.
用 USB to TTL 連接好板子并運(yùn)行程序:
python hello.light.45.py /dev/ttyUSB0
這時(shí)出現(xiàn)錯(cuò)誤提示:
Traceback (most recent call last):
File "hello.light.45.py", line 62, in <module>
ser = serial.Serial(port,9600)
File "/Library/Python/2.7/site-packages/serial/serialutil.py", line 180, in __init__
self.open()
File "/Library/Python/2.7/site-packages/serial/serialposix.py", line 294, in open
raise SerialException(msg.errno, "could not open port %s: %s" % (self._port, msg))
serial.serialutil.SerialException: [Errno 2] could not open port /dev/ttyUSB0: [Errno 2] No such file or directory: '/dev/ttyUSB0'
Google 一下這個(gè)問題 Failed to open port /dev/ttyUSB0 - ROS Answers: Open Source Q&A Forum姻锁。
問題可能是因?yàn)槲疫€沒有裝 FTDI (用于 USB 和串口通信)驅(qū)動(dòng)枕赵。
使用命令 ls /dev/tty*
可以列出當(dāng)前可用的串口。確實(shí)沒有 /dev/tty.usbserial-A400gwhT 串口位隶。所以我嘗試安裝 FTDI 驅(qū)動(dòng) - D2XX Direct Drivers 以及 Virtual COM Port Drivers拷窜。但是依然不成功。
接著涧黄,Google 到這一篇 How to Install FTDI Drivers - learn.sparkfun.com 照著再裝了一遍驅(qū)動(dòng)篮昧。重啟電腦后,插入 FTDI 2 USB 設(shè)備笋妥, /dev/tty.usbserial-A400gwhT 總算出現(xiàn)在串口列表中懊昨。
接著,嘗試用新的串口運(yùn)行程序:
python hello.light.45.py /dev/tty.usbserial-A400gwhT 9600
繼續(xù)看到錯(cuò)誤提示:
command line: hello.light.45.py serial_port
到 python 程序中仔細(xì)看了看春宣,將 len(sys.argv) 從 2 改為 3:
if (len(sys.argv) != 3):
print "command line: hello.light.45.py serial_port"
sys.exit()
port = sys.argv[1]
問題解決:
實(shí)驗(yàn)2:開關(guān)
做了一個(gè)帶開關(guān)的板子:
下載 hello.button.45.c 和 makefile砂吞。 在 terminal cd 進(jìn)入文件夾向挖,運(yùn)行命令: make -f hello.button.45.make
和 sudo make -f hello.button.45.make program-usbtiny
名段。都正常红氯。
然后用 TTL 2 USB 連接板子:
運(yùn)行命令
python term.py /dev/tty.usbserial-A400gwhT 9600
每按一次按鈕,屏幕打出一個(gè)“du”:
實(shí)驗(yàn)3:溫度傳感器
做了一個(gè)帶有溫度傳感器的板子:
下載 hello.temp.45.c 和 makefile嚷辅。輸入命令 make -f hello.button.45.make
和 sudo make -f hello.button.45.make program-usbtiny
簿姨。
然后運(yùn)行程序 python hello.temp.45.py /dev/tty.usbserial-A400gwhT 9600
。傳感器開始實(shí)時(shí)監(jiān)測(cè)溫度變化簸搞。