[问题] Arduino SPI 使用语法的问题

楼主: escorpion (pavone先生)   2013-11-21 14:35:59
各位大大午安,
自从前天开始接触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吗?
作者: kdjf (我抓得到什么呢?)   2012-01-21 14:45:00
+ 是or, SPE/MSTR是你一开始include library时拿来用的00001111就变成一千一百一十一了...0b00001111是一个二进制数,十进里的15
作者: alibuda174 (阿哩不达)   2012-01-21 17:39:00
SPSR在arduino/hardware/tools/avr/avr/include/avr里这些都是AVR微控制器的暂存器名称
作者: kikiqqp (喵食罐头)   2012-01-22 10:18:00
1. SPCR、SPSR、SPDR所指定的暂存器位址写在 iom8.h2. 你可以直接给予数值3. http://arduino.cc/en/Tutorial/SPIEEPROMSPCR| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0| SPIE | SPE | DORD | MSTR | CPOL | CPHA | SPR1 |SPR0SPIE - Enables the SPI interrupt when 1SPE - Enables the SPI when 1DORD - Sends data least Significant Bit First when 1,most Significant Bit first when 0MSTR - Sets the Arduino in master mode when 1, slavemode when 0CPOL - Sets the data clock to be idle when high if setto 1, idle when low if set to 0CPHA - Samples data on the falling edge of the dataclock when 1, rising edge when 0SPR1 and SPR0 - Sets the SPI speed, 00 is fastest(4MHz) 11 is slowest (250KHz)以上宣告在 io2333.h

Links booklink

Contact Us: admin [ a t ] ucptt.com