Arduino week6

Download Report

Transcript Arduino week6

內容:SPI、 74HC595+shiftOut()
實作:數位擴充
簡報:廖崇義



https://www.youtube.com/watch?v=VzX_hWK
N5ak
https://www.youtube.com/watch?v=PZVizaSa
aK8&hd=1
https://www.youtube.com/watch?v=4zjXr6Q
npcw

功能
序列周邊界面(Serial Peripheral Interface)簡稱SPI,是
一種同步序列資料傳輸的協定,用來作為微處理器與周邊裝
置在短距離內快速的的通訊, 亦可作為兩個微處理器間的
通訊。
Master In Slave Out (MISO) (SDI)從屬端線路 傳送資料給主控端(Master)
Master Out Slave In (MOSI) (SDO)主控端線路 傳送資料給附屬端(Slave)
Serial Clock (SCK)同步資料的時脈線路,通常由主控端產生
SS (Slave Select pin)附屬端腳位選擇 ,由主控端指定分配,啟動或關閉
附屬端裝置,以避免因雜訊干擾造成傳輸錯誤。



控制暫存器(SPCR)為不同的微處理器的功能性作控制設定的編碼。通常在控制暫存器
的一位元(bit)就會影響一種設定,像是速度或是極性。
資料暫存器(SPDR)僅僅作為暫時存放位元組,舉例而言,SPI資料暫存器(SPDR)會暫
時儲存即將透過MOSI線傳出的位原組和已經由MISO傳進來的位元組。
狀態暫存器(SPSR)用來改變基於不同微處理器條件的狀態。例如,當SPI有值傳進來或
傳出時,SPI狀態暫存器(SPSR)的第7個位元會設為1。
Ex.SPI控制暫存器(SPCR)有8個位元,每個位元都控制SPI的某種特殊設定:
7
6
5
4
SPIE
SPE
DORD MSTR
3
2
1
0
CPOL
CPHA
SPR1
SPR2






SPI.begin():啟用SPI
SPI.end() :關閉SPI
SPI.setBitOrder(order) :設定先傳輸高位還是低位元
order:LSBFIRST(最低位在前)或MSBFIRST(最高位在前)
SPI.setClockDivider() :設定分頻值預設設置是
SPI.setClockDivider(SPI_CLOCK_DIV4)
設定值有2,4,8,16,32,64或128
SPI.setDataMode(mode) :
mode : SPI_MODE0(上升沿採樣,下降沿置位,SCK閒置時為0),
SPI_MODE1(上升沿置位,下降沿採樣,SCK閒置時為0),
SPI_MODE2(下降沿採樣,上升沿置位,SCK閒置時為1),
SPI_MODE3(下降沿置位,上升沿採樣,SCK閒置時為1)。
SPI.transfer(val) : val:向SPI匯流排發送的資料值
val= SPI.transfer() , val為從SPI匯流排上接收的資料值




串列資料傳輸時,是先傳輸高位(MSB)還是先傳
輸低位(LSB),這是由 SPI.setBitOrder()函數來
控制的。
資料時鐘SCK在空閒時,是高電位還是低電位?
採樣時,是在時鐘脈衝的上升沿還是下降沿?這是
由 SPI.setDataMode() 函數來控制的
SPI是在怎樣速度下運行的,即SCK提供多大的時鐘
脈衝使SPI運行?這是由 SPI.setClockDivider() 函
數來控制的。
腳位編號
名稱
說明
1-7, 15
Q0 ~ Q7
輸出腳位
8
GND
接地
9
Q7’
序列輸出 (Serial Out)
10
MR
Master Reset, 清除所有資料,
低電位有效 (Active low)
11
SH_CP
SHift register clock pin
(Clock Pin)
12
ST_CP
STorage register clock pin
(Latch Pin)
13
OE
Output Enable, 允許輸出,低
電位有效 (Active low)
14
DS
序列資料輸入 (Serial data
input)
16
Vcc
供應電壓

shiftOut(dataPin, clockPin, bitOrder, value)
dataPin:序列資料輸出至74HC5952的DS
clockPin:時脈輸出至74HC5952的SH_CP
bitOrder :由高位先輸出或是低位先輸出
value:欲輸出資料值
程式碼
1.
//Pin connected to ST_CP of 74HC595
2.
int latchPin = 8;
3.
//Pin connected to SH_CP of 74HC595
4.
int clockPin = 12;
5.
////Pin connected to DS of 74HC595
6.
int dataPin = 11;
7.
void setup() {
8.
//set pins to output because they are addressed in the main loop
9.
pinMode(latchPin, OUTPUT);
10.
pinMode(clockPin, OUTPUT);
11.
pinMode(dataPin, OUTPUT);
12.
}
13.
void loop() {
14.
//count up routine
15.
for (int j = 0; j < 256; j++) {
16.
//ground latchPin and hold low for as long as you are transmitting
17.
digitalWrite(latchPin, LOW);
18.
shiftOut(dataPin, clockPin, LSBFIRST, j);
19.
//return the latch pin high to signal chip that it no longer needs to listen for information
20.
digitalWrite(latchPin, HIGH);
21.
delay(1000);
}
22.
23.
}
74HC595
pin
顯示
號碼
6
a
7
5
b
6
4
c
4
3
d
2
2
e
1
1
f
9
15
g
10
0
1
1
1
1
1
1
0
1
0
1
1
0
0
0
0
2
1
1
0
1
1
0
1
3
1
1
1
1
0
0
1
4
0
1
1
0
0
1
1
5
1
0
1
1
0
1
1
6
1
0
1
1
1
1
1
7
1
1
1
0
0
0
0
8
1
1
1
1
1
1
1
9
1
1
1
0
0
1
1
1.
2.
3.
4.
5.
6.
7.
8.
//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;
byte seven_disp[10] = { B01111110,B00110000,B01101101,B01111001,B00110011,
B01011011,B01011111,B01110000,B01111111,B01110011 };
14.
void setup() {
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
15.
void loop() {
9.
10.
11.
12.
13.
16.
for(int k=0;k<10;k++)
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST,seven_disp[k]);
digitalWrite(latchPin, HIGH);
delay(500);
17.
18.
19.
20.
21.
22.
23.
}
24.
25.
26.
}



設計一跑馬燈由右至左一顆燈來回跑
電路同上題
語法參考
http://arduino.cc/en/Reference/Bitshift