• slider image 442
  • slider image 483
  • slider image 484
  • slider image 485
  • slider image 486
  • slider image 487
:::


Browsing this Thread:   1 Anonymous Users






請問,關於SPI的Slave mode
#1
高級會員
高級會員


查看用戶資訊
請教一下,
最近小弟在學PIC的SPI傳輸,
目前想完成的工作為:
將一顆18f4620的SPI設為Master
(已成功,有用示波器看到訊號為正確),
另一顆18f4620設定為Slave,
但是一直無法收到訊號
(我是設定收到SPI值後由USART丟出),
請各位MCHP的高手給點建議!!

我的程式碼如下:

#include "p18f4620.h"
#include "USART.h"
#include "delays.h"
(巨集設定與函式宣告省略...)

// Master與Slave程式由下面定義來決定,
// Master mode已經可以跑,
// Slave仍為失敗!!

//#define MASTER
#define SLAVE

void main()
{
unsigned char i=0;

TRISAbits.TRISA0=0; // Light
TRISAbits.TRISA1=0; // Light
SSPCON1bits.SSPEN=0; // SPI initial
SCK= 0; // SCK,RC3,
SDI= 1; // SDI,RC4
SDO= 0; // SDO,RC5
usart_init();

// ----------- MASTER ------------------
#ifdef MASTER
CS=0; // CS, RA5
SSPSTAT = 0b00000000; // CKE=0;
SSPCON1 = 0b00110010; // CKP=1

LATCbits.LATC3 = 0; // SCK
LATCbits.LATC5 = 0; // SDO
LATAbits.LATA5 = 1; // CS= high

// Signal Light for debug
LED1=0;
LED2=1;

while(1)
{
//-----------Master
i=usart_Rx();
Delay10TCYx(20);
LATAbits.LATA5=0;
SPIPut(i);
LATAbits.LATA5=1;
LED1=!LED1;
LED2=!LED2;
Delay10TCYx(20);
usart_putch(i);
}
#endif

// ----------- SLAVE ------------------
#ifdef SLAVE
CS=1; // CS, RA5 input
SDI= 1; // SDI,RC4
TRISAbits.TRISA5=1;
SSPSTAT = 0b00000000; // CKE=0;
SSPCON1 = 0b00110100; // CKP=1,SS(RA5) enable

LATCbits.LATC3 = 0; // SCK
//LATCbits.LATC4 = 0; // SDI
LATCbits.LATC5 = 0; // SDO
//LATAbits.LATA5 = 0; // CS

// Signal Light for debug
LED1=1;
LED2=0;

while(1)
{
//-----------Slave
if(LATAbits.LATA5==0)
{
i=SPIGet(); // SPI Get
LED1=!LED1;
LED2=!LED2;
Delay10TCYx(20);
usart_putch(i);
Delay10KTCYx(20);
}
//-----------
} // end of while
#endif


} // end of main

// ---------------------------

void SPIPut(BYTE v)
{
SSPIF_BIT = 0;
do
{
WCOL_BIT = 0;
SSPBUF_REG = v;
}
while(WCOL_BIT);
while(SSPIF_BIT == 0);
}


BYTE SPIGet(void)
{
SPIPut(0x00); // Trigger
return SSPBUF_REG;
}
// ---------------------------

發表於: 2008/7/23 23:32
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部







You can view topic.
不可以 發起新主題
You cannot reply to posts.
You cannot edit your posts.
You cannot delete your posts.
You cannot add new polls.
You cannot vote in polls.
You cannot attach files to posts.
You cannot post without approval.
You cannot use topic type.
You cannot use HTML syntax.
You cannot use signature.
You cannot create PDF files.
You cannot get print page.

[進階搜尋]


:::

Microchip連結

https://www.facebook.com/microchiptechnologytaiwan/
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=13
https://mu.microchip.com/page/tmu
http://elearning.microchip.com.tw/modules/tad_link/index.php?cate_sn=1
https://page.microchip.com/APAC-PrefCenters-TW.html
http://www.microchip.com/
http://www.microchip.com/treelink
http://www.microchipdirect.com/
http://www.microchip.com.cn/newcommunity/index.php?m=Video&a=index&id=103
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=2
http://www.microchip.com.tw/Data_CD/eLearning/index.html
http://www.microchip.com.tw/RTC/RTC_DVD/
https://www.microchip.com/development-tools/
https://www.youtube.com/user/MicrochipTechnology
[ more... ]

教育訓練中心

!開發工具購買
辦法說明 [業界客戶] [教育單位]
----------------------------------
!校園樣品申請
辦法說明 [教師資格] [學生資格]
----------------------------------