[問題] Arduino SPI 使用語法的問題

看板ASM (組合語言)作者 (單程車票)時間11年前 (2013/11/21 14:36), 編輯推噓0(0024)
留言24則, 3人參與, 最新討論串1/1
各位大大午安, 自從前天開始接觸Arduino這個東西後, 便開始被他多樣的功能給吸引住, 而網路上豐富且循序漸進的資料讓從沒學過C語言的我也能很順利的上手。 順便推薦一下這兩天我所看覺得非常棒的網站: 1. http://www.youtube.com/watch?v=E3W8Fxc7tHo
這個網站是由Arduino的發展人之一所拍的教學影片,共有10個主題, 從最基本開始讓你慢慢了解這玩意是什麼! 在看到第四個lesson後也有了靈感寫了我的第一支程式, http://0rz.tw/fdhGW (讓Ardino唱卡農,連結到我的FB影片) 2. http://0rz.tw/2akCj 這個網站裡的Arduino Tutorials 目前共有57個chapters,而且持續更新中, 藉由做專題的形式來學習Arduino。 打了落落長的廢話我想問的問題是: a. 我在看了Arduino官網裡關於使用SPI寫EEPROM的文章後一直卡在一個地方, 網路上找了很多資料後還是無法了解.... (http://arduino.cc/en/Tutorial/SPIEEPROM,文章連結) 文章裡其中一段程式碼 : // SPCR = 01010000 //interrupt disabled,spi enabled,msb 1st,master,clk low when idle, //sample on leading edge of clk,system clock/4 rate (fastest) SPCR = (1<<SPE)|(1<<MSTR); clr=SPSR; clr=SPDR; delay(10); 我的理解為我想要在SPI Control Register 裡寫入01010000這個指令, 我不懂的是這串指令要怎麼從(1<<SPE)|(1<<MSTR)產生? 我看程式的一開始沒有宣告SPE與MSTR為何種變數, 為什麼它可以這樣直接使用? 為什麼不能直接寫 : SPCR=01010000就好? b. 在一個使用SPI控制DAC的範例裡 : http://0rz.tw/Y9QGW 因為此DAC為12 bit input且需4 bit來做參數設定, 由於SPI只能做byte的傳輸,因此範例裡使用的是 highbyte()與lowbyte()兩指令, 將一個16 bit的變數分成兩部分寫入DAC, 其中包含參數設定的那部分使用了bitmask來使得該4 bit永遠為固定值。 部分程式碼為 : word outputValue = 0; // a word is a 16-bit number byte data = 0; // and a byte is an 8-bit number void loop() { for (int a=0; a<=4095; a++) { outputValue = a; digitalWrite(10, LOW); data = highByte(outputValue); data = 0b00001111 & data; data = 0b00110000 | data; SPI.transfer(data); data = lowByte(outputValue); SPI.transfer(data); digitalWrite(10, HIGH); delay(del); } 這裡我不懂的地方是在bitmask裡0b00001111 or 0b00110000 為什麼會多出0b? 不能直接寫00001111 or 00110000嗎? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.109.112.200

11/21 14:45, , 1F
+ 是or, SPE/MSTR是你一開始include library時拿來用的
11/21 14:45, 1F

11/21 14:46, , 2F
00001111就變成一千一百一十一了...
11/21 14:46, 2F

11/21 14:47, , 3F
0b00001111是一個二進位數,十進裡的15
11/21 14:47, 3F
這裡我了解了,原來是在二進位時開頭必須加入的字! 感謝指引方向~ 不過在SPE/MSTR這裡我還是不懂.... 你的意思是一開始include的library裡就有預先定義好的嗎? 我剛看了一下程式碼一開始只有include SPI.h, 點進去看也沒看到 SPE, MSTR 或 SPSR, SPDR相關的定義.... ※ 編輯: escorpion 來自: 140.109.112.200 (11/21 15:30)

11/21 17:39, , 4F
SPSR在arduino/hardware/tools/avr/avr/include/avr裡
11/21 17:39, 4F

11/21 17:39, , 5F
這些都是AVR微控制器的暫存器名稱
11/21 17:39, 5F
謝謝你的回答! 在進去那個資料夾點了幾個標頭檔還是沒發現那幾個字的定義後我放棄了.....冏 不過一結果來看我應該可以假設他們分別是6與4這兩個值, 並且被已經被定義在某處了吧? 再來是如果我想要寫01010000進SPSR, 可以直接寫成 : SPSR=B01010000就好嗎? ※ 編輯: escorpion 來自: 140.109.112.200 (11/21 22:37)

11/22 10:18, , 6F
1. SPCR、SPSR、SPDR所指定的暫存器位址寫在 iom8.h
11/22 10:18, 6F

11/22 10:18, , 7F
2. 你可以直接給予數值
11/22 10:18, 7F

11/22 10:20, , 8F

11/22 10:20, , 9F
SPCR
11/22 10:20, 9F

11/22 10:20, , 10F
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
11/22 10:20, 10F

11/22 10:20, , 11F
| SPIE | SPE | DORD | MSTR | CPOL | CPHA | SPR1 |SPR0
11/22 10:20, 11F

11/22 10:21, , 12F
SPIE - Enables the SPI interrupt when 1
11/22 10:21, 12F

11/22 10:21, , 13F
SPE - Enables the SPI when 1
11/22 10:21, 13F

11/22 10:21, , 14F
DORD - Sends data least Significant Bit First when 1,
11/22 10:21, 14F

11/22 10:21, , 15F
most Significant Bit first when 0
11/22 10:21, 15F

11/22 10:22, , 16F
MSTR - Sets the Arduino in master mode when 1, slave
11/22 10:22, 16F

11/22 10:22, , 17F
mode when 0
11/22 10:22, 17F

11/22 10:22, , 18F
CPOL - Sets the data clock to be idle when high if set
11/22 10:22, 18F

11/22 10:22, , 19F
to 1, idle when low if set to 0
11/22 10:22, 19F

11/22 10:22, , 20F
CPHA - Samples data on the falling edge of the data
11/22 10:22, 20F

11/22 10:23, , 21F
clock when 1, rising edge when 0
11/22 10:23, 21F

11/22 10:23, , 22F
SPR1 and SPR0 - Sets the SPI speed, 00 is fastest
11/22 10:23, 22F

11/22 10:23, , 23F
(4MHz) 11 is slowest (250KHz)
11/22 10:23, 23F

11/22 10:25, , 24F
以上宣告在 io2333.h
11/22 10:25, 24F
太感謝了!我剛剛終於在裡面找到了! ※ 編輯: escorpion 來自: 140.109.112.200 (11/22 12:49)
文章代碼(AID): #1IZQdIR7 (ASM)
文章代碼(AID): #1IZQdIR7 (ASM)