SPI 功能使用方法 Application Note SPI 功能使用方法 1 適用產品:SM59R16A2 / SM59R08A2 2 SPI 使用概述: SPI通信使用4個引腳,分別為 z SPI_MOSI: 當master時資料輸出;當slave時資料輸入 z SPI_MISO: 當master時資料輸入;當slave時資料輸出 z SPI_SCK: SPI的時脈信號由master主控產生;資料 (輸出及輸入) 和時脈同步 z SPI_SS: 此引腳功能唯有當作slave mode時可使用(master mode此引腳則當做GPIO使用) = 0: master致能slave = 1: master禁能slave Master Slave 2 Slave 1 MOSI MISO CLK IO IO MOSI MISO CLK MOSI MISO CLK SS SS Fig. 15-1: SPI signals between master and slave devices 3 以下說明與 SPI 相關的特殊控制暫存器及特殊狀態暫存器 Special Function Register (SFR) SPI SPIC1 SPIC2 SPIS SPITXD SPIRXD Description Direct Bit 7 SPI control register 1 SPI control register 2 F1h F2h F5h SPIEN SPI status register SPI transmit data buffer SPI receive data buffer SPIFD - Bit 6 Bit 5 Bit 4 SPI function SPIMSS SPISSP SPICKP TBC[2:0] SPIMLS SPIOV SPITXIF Bit 3 Bit 2 Bit 1 SPICKE SPIBR[2:0] - RBC[2:0] SPITDR SPIRXIF SPIRDR Bit 0 RESET SPIRS 08h 00h 40h F3h SPITXD[7:0] 00h F4h SPIRXD[7:0] 00h Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A079 1 Ver A 2008/05 SPI 功能使用方法 Application Note Mnemonic: SPIC1 7 6 5 SPIEN SPIMSS SPISSP 4 SPICKP Address: F1h 3 2 1 0 SPICKE SPIBR[2:0] Reset 08h SPIEN: SPI 模主致能旗標: “1” – 致能 “0” – 禁能 SPIMSS: 主從模式選擇旗標(Master or Slave mode Select) “1” – MCU 做為 Master mode. “0” – MCU 做為 Slave mode. SPISSP: (SS)引腳致能狀態旗標;當 MCU 為 slave 時,可由旗標設定 Slave Select (SS)引腳致能狀態 (slave mode used only) “1” – 高準位致能 high active. “0” – 低準位致能 low active. SPICKP: 時脈閒置準位旗標(master mode used only) “1” – 時脈閒置時為高準位(SCK high during idle), Ex : “0” – 時脈閒置時為低準位(SCK high during idle), Ex : SPICKE: 時脈取樣旗標 Clock sample edge select. “1” – 正緣取樣 data latch in rising edge “0” – 負緣取樣 data latch in falling edge. * 為確保資料取樣的正確性,無論使用正緣或負緣取樣,時脈及資料同步時皆需有足夠的準備時間 (set-up time)及保持時間(hold time),SM59R16A2/SM59R08A2 時序產生如下圖: sufficient set-up time sufficient hold time SPIBR[2:0]: SPI 鮑率選擇(master mode used only), Fosc 為晶振頻率: SPIBR[2:0] Baud rate 0:0:0 Fosc/4 0:0:1 Fosc/8 0:1:0 Fosc/16 0:1:1 Fosc/32 1:0:0 Fosc/64 1:0:1 Fosc/128 1:1:0 Fosc/256 1:1:1 Fosc/512 Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A079 2 Ver A 2008/05 SPI 功能使用方法 Application Note Mnemonic: SPIC2 7 6 5 SPIFD TBC[2:0] 4 3 - 2 1 RBC[2:0] Address: F2h 0 Reset 00h SPIFD: 全雙工模式旗標(Full-duplex mode enable) “1” : 全雙工模式致能 “0” : 全雙工模式禁能 當全雙工模式旗標”SPIFD”致能時,TBC[2:0]和 RBC[2:0]會被清除並保持為零,即 SPI 全雙工模式 僅允許 8 位元通訊. Input Shift register SPIRXD Output Shift register SPITXD Clock Generator MISO MISO MOSI MOSI SCK SCK SyncMOS Master Output Shift register SPITXD Input Shift register SPIRXD SyncMOS Slave TBC[2:0]: SPI 傳送元位旗標(全雙工模式僅允許 8 位元通訊) TBC[2:0] Bit counter 0:0:0 8 bits output 0:0:1 1 bit output 0:1:0 2 bits output 0:1:1 3 bits output 1:0:0 4 bits output 1:0:1 5 bits output 1:1:0 6 bits output 1:1:1 7 bits output RBC[2:0]: SPI 接收元位旗標(全雙工模式僅允許 8 位元通訊) RBC[2:0] Bit counter 0:0:0 8 bits input 0:0:1 1 bit input 0:1:0 2 bits input 0:1:1 3 bits input 1:0:0 4 bits input 1:0:1 5 bits input 1:1:0 6 bits input 1:1:1 7 bits input Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A079 3 Ver A 2008/05 SPI 功能使用方法 Application Note Mnemonic: SPIS 7 6 5 SPIMLS SPIOV 4 SPITXIF 3 SPITDR 2 SPIRXIF Address: F5h 0 Reset SPIRS 40h 1 SPIRDR SPIMLS: MSB or LSB output /input first “1” : 最高位元優先 (MSB output/input first) “0” : 最低位元優先 (LSB output/input first) SPIOV: 溢位旗標(Overflow flag) “1” : 當 SPIRDR 已設定(SPIRXD 原有資料未被讀取)且下一筆資料正寫入 SPIRXD 時,SPIOV 將被設定為”1”,告知 SPIRXD 資料以有損毀. “0” : 當 SPIRDR 清為零時,SPIOV 則由硬體清除. SPITXIF: Transmit Interrupt Flag. “1” : 當 SPITXD 的資料已載入移位暫存器 “0” : 傳送資料完成後必須由軟體清除。 SPITDR: Transmit Data Ready. “1”: 當程序為傳送模式時,資料儲存至 SPITXD 後,由軟體設定此旗標為”1” 告知 SPI module 可 傳出資料 “0”: 當SPI module由SPITXD完成傳送時或SPITXD被載至移位暫存器時,此旗標則由硬體自動清除 SPIRXIF: Receive Interrupt Flag. “1” : 當 SPIRXD 被載入新一筆資料後 “0” : 接收資料完成後必須由軟體清除。 SPIRDR: Receive Data Ready. “1” : SPI module接收資料時,SPIRDR由硬體自動設定為”1”,以告知MCU完成接收並儲存至 SPIRXD;當新的一筆資料寫入SPIRXD,而SPIRDR未清除時,SPIRXD原有的資料將被覆寫,產 生overflow “0” : 由SPIRXD讀取資料後,必須由軟體清除此旗標 SPIRS: Receive Start. “1” : 告知 SPI 模組可由 SPIRXD 接收資料 Mnemonic: SPITXD 7 6 5 4 3 SPITXD[7:0] 2 1 0 Address: F3h Reset 00h SPITXD[7:0]: 傳送資料緩衝器. Mnemonic: SPIRXD 7 6 5 4 3 SPIRXD[7:0] 2 1 Address: F4h 0 Reset 00h SPIRXD[7:0]: 接收資料緩衝器 Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A079 4 Ver A 2008/05 SPI 功能使用方法 Application Note 4 以下為 SPI 相對應的中斷向量表,和中斷有關的致能旗標 Table 11-1: Interrupt vectors IE0 – External interrupt 0 TF0 – Timer 0 interrupt Interrupt Vector Address 0003h 000Bh Interrupt Number *(use Keil C Tool) 0 1 IE1 – External interrupt 1 0013h 2 TF1 – Timer 1 interrupt RI0/TI0 – Serial channel 0 interrupt TF2/EXF2 – Timer 2 interrupt 001Bh 0023h 002Bh 3 4 5 SPIIF – SPI interrupt 004Bh 9 ADCIF – A/D converter interrupt EEIIF – Expanded External Interrupt IICIF – IIC interrupt RI1/TI1 – Serial channel 1 interrupt 0053h 005Bh 006Bh 0083h 10 11 13 16 Interrupt Request Flags *See Keil C about C51 User’s Guide about Interrupt Function description Mnemonic IEN0 IEN1 IEN2 IP0 IP1 Description Interrupt Enable 0 register Interrupt Enable 1 register Interrupt Enable 2 register Interrupt priority level 0 Interrupt priority level 1 Direct Bit 7 Bit 6 Bit 5 Interrupt Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 RESET A8h EA - ET2 ES0 ET1 EX1 ET0 EX0 00h B8h EXEN2 - IEIIC - IEEEI IEADC IESPI - 00h 9Ah - - - - - - - ES1 00h A9h - - IP0.5 IP0.4 IP0.3 IP0.2 IP0.1 IP0.0 00h B9h - - IP1.5 IP1.4 IP1.3 IP1.2 IP1.1 IP1.0 00h *如果需要使用中斷程序,可參考以下設置: (1) SPI 中斷致能設罝: IEN0 |= 0x80; //Enable interrupt All IEN1 |= 0x02; //Enable interrupt SPI (2) SPI 中斷程序表示: void SPI_interrupt(void) interrupt 13 using 1 { IRCON_SPIIF = 0; //Clear interrupt flag } Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A079 5 Ver A 2008/05 SPI 功能使用方法 Application Note 5 以下為兩顆 MCU 分別當做 SPI master 及 slave 通訊的流程圖 Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A079 6 Ver A 2008/05 SPI 功能使用方法 Application Note 6 SPI master 及 slave 通訊的範例程式 說明: 6.1 Master 將以資料(0xFF~0x00)傳至 Slave 6.2 Slave 接受後,以相同的資料回傳給 Master 6.3 Master 接受 Slave 回傳的資料後,將該資料與原傳出資料比較,相同則表示通訊正確,不相同 則反之 Description Main program Master: //==================================================================== // // SYNCMOS TECHNOLOGY // //==================================================================== #include "..\h\SM59R16A2.h" #include "..\h\SM59R16A2_extradef.h" #include "..\LCD\LCD16x2.h" #include "..\MISC\delay.h" #define Control_Byte 0xA0 #define SPI_SS P1_0 #define SPITDR=1 SPIS |= 0x08 /* void SPI_interrupt(void) interrupt 13 using 1 { IRCON_SPIIF = 0; //Clear interrupt flag }*/ void SPI_Init(void) { SPIC1 = 0xD8; //SPIC1 |= 7; SPIC2 = 0x00; SPIS = 0x40; } //SPI Enable/ master/ NC/ idle hi/ rising latch //BR=(Fosc/512) //SPI full-duplex disable /8-bit communicate //MSB send first void SPI_TX( unsigned char DATA) { SPITXD = DATA; SPIS |= 0x08; while( !(SPIS&SPI_TXIF) ); SPIS &= (~SPI_TXIF); } unsigned char SPI_RX(void) { unsigned char Temp; SPIS |= 0x01; while( !(SPIS&SPI_RXIF) ); Temp = SPIRXD; SPIS |= 0x02; SPIS &= (~SPI_RXIF); return Temp; //Load to Buffer //SPITDR //TX completed //Clear TXIF bit //Receive Start //RX completed //Load from Buffer //Receive Data Ready //Clear RXIF bit Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A079 7 Ver A 2008/05 SPI 功能使用方法 Application Note Description Main program Master: } void main() { unsigned long Err =0, counter =1; unsigned char TxData, RxData,j; LCD_Init(); SPI_Init(); Delay10mSec(1); //must wait for LCD stable while(1) { for(TxData=0xFF; TxData>0; TxData--) { PrintLcdStrLX( 1, 0, "M_TX: PrintLcdStrLX( 2, 0, "M_RX: SPI_SS = 0; SPI_TX( TxData ); //for(j=0; j<0; j++); RxData = SPI_RX(); SPI_SS = 1; "); "); //CS //TX //Dealy time //RX //CS SetCursorAddr(1, 6); PrintLcdHex( TxData ); SetCursorAddr(2, 6); PrintLcdHex( RxData ); counter++; SetCursorAddr(1, 12); if(TxData!=RxData) SetCursorAddr(2, 12); P2=TxData; PrintLcdDec( counter ); Err++; PrintLcdDec( Err ); //result } } } Description Main program Slave: //==================================================================== // // SYNCMOS TECHNOLOGY // //==================================================================== #include "..\h\SM59R16A2.h" #include "..\h\SM59R16A2_extradef.h" #include "..\LCD\LCD16x2.h" #include "..\MISC\delay.h" void SPI_Init(void) { SPIC1 =0x98; //SPI Enable/ slave/ Low active / NC / rising latch/ NC NC NC SPIC2 =0x00; //SPI full-duplex disable /8-bit communicate SPIS =0x40; //MSB send first Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A079 8 Ver A 2008/05 SPI 功能使用方法 Application Note Description Main program Slave: } void SPI_TX( unsigned char DATA) { SPITXD = DATA; SPIS |= 0x08; while( !(SPIS&SPI_TXIF) ); SPIS &= (~SPI_TXIF); } unsigned char SPI_RX(void) { unsigned char Temp; SPIS |= 0x01; while( !(SPIS&SPI_RXIF) ); Temp = SPIRXD; SPIS |= 0x02; SPIS &= (~SPI_RXIF); return Temp; } //Load to Buffer //SPITDR //TX completed //Clear TXIF bit //Receive Start //RX completed //Load from Buffer //SPIRDR //Clear RXIF bit void main() { unsigned long Err =0, counter =1; unsigned char TxData=0, RxData=0,j; LCD_Init(); SPI_Init(); Delay10mSec(1); //must wait for LCD stable while(1) { RxData = SPI_RX(); //RX SPI_TX( RxData ); //TX P2 = RxData; //result } } Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A079 9 Ver A 2008/05