Robofun 機器人論壇

標題: SPI 傳輸寫法問題 [打印本頁]

作者: AdamQ    時間: 2013-11-21 23:51
標題: SPI 傳輸寫法問題
各位大大好,我想請問一個關於要將指令寫入SPCR的小問題。
起因為在Ardinuo官網看到這一篇"Interfacing a serial EEprom using SPI",
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);


它的目的是將01010000寫入SPCR裡,
使用的寫法為
(1<<SPE)|(1<<MSTR)
我想請問:
1. 在程式的開頭SPCR、SPE、MSTR、SPSR、SPDR、clr....etc 皆沒有被定義,
  若照結果來推測,SPE與MSTR應該是6與4,
  我的說法對嗎? 若是的話這些數值是預先被定義在哪呢?


2. 如果我想在SPCR裡寫入上述的指令,
我可以直接將程式碼改為 : SPCR=B01010000嗎?


先感謝各位大大的回答!
作者: vegewell    時間: 2013-11-23 02:47
回復 1# AdamQ

>>我想請問:
>>1. 在程式的開頭SPCR、SPE、MSTR、SPSR、SPDR、clr....etc 皆沒有被定義,
  >>若照結果來推測,SPE與MSTR應該是6與4,
  >>我的說法對嗎? 若是的話這些數值是預先被定義在哪呢?
An SPI library is now included in the Arduino software.

The Arudiuno will not have SPI enabled unless I load a sketch / library which sets the SPRC register and enables SPI, correct?  If I load a sketch which uses SPI will those changes remain when the Arduino reboots. Lets say I change sketch to "Blink", will the SPCR still be set to enabled?

If I enable SPI by making the appropriate changes to the registers. What would I need to clear all of these SPI settings?


>>2. 如果我想在SPCR裡寫入上述的指令,
>>我可以直接將程式碼改為 : SPCR=B01010000嗎?
    你可以試試看,行就行,不行就不行,反正程式碼怎麼規格寫,都是隨創作者高興作的,




歡迎光臨 Robofun 機器人論壇 (https://robofun.net/forum/) Powered by Discuz! X3.2