freescale單片機(jī)SPI通信
void InitSPI(void)
本文引用地址:http://butianyuan.cn/article/201611/322588.htm{
SPI1CR1 = 0b01010010;//SPIE SPE SPTIE MSTR CPOL CPHA SSOE LSBFE
//主模式,SS輸出使能
SPI1CR2 = 0b00010000;//0 XFRW 0 MODFEN BIDIROE 0 SPISWAI SPC0
//8位輸出
SPI1BR = 0x70 | 0x05;//SPPR SPR
//Baudrate=fbus/[(SPPR+1)*2^(SPR+1)]
// =80MHz/512=156.25kHz
DDRP = 0b01110000; //PP0 PP1 PP2 PP3
//MISO1 MOSI1 SCK1 SS1
//輸入 輸出 輸出 輸出
}
//---主SPI發(fā)送數(shù)據(jù)
void MasterData2Slave(uchar data)
{
while(!SPI1SR_SPTEF){;} //SPI1SR_SPTEF 1:SPI數(shù)據(jù)寄存器為空;0:SPI數(shù)據(jù)寄存器為非空
//只要數(shù)據(jù)存儲(chǔ)器有數(shù)據(jù),就等待;否則進(jìn)入下一步
SPI1DRL = data; //存儲(chǔ)器的數(shù)據(jù)發(fā)送之后,寫(xiě)入數(shù)據(jù),這個(gè)很重要
}
-------------------------------------------------------------------------------------------
//---從SPI初始化程序:
void InitSPI(void)
{
SPI1CR1 = 0b11000000;//SPIE SPE SPTIE MSTR CPOL CPHA SSOE LSBFE
//SPI接收中斷,從模式
SPI1CR2 = 0b00010000;//0 XFRW 0 MODFEN BIDIROE 0 SPISWAI SPC0
//8位數(shù)據(jù)
SPI1BR = 0x70 | 0x05;//SPPR SPR
//Baudrate=fbus/[(SPPR+1)*2^(SPR+1)]
// =80MHz/512=156.25kHz
}
//---從SPI接收數(shù)據(jù):
void MasterData2Me(void)
{
uchar data;
while(!SPI1SR_SPIF){;} //先查詢狀態(tài)寄存器的狀態(tài),SPIF表示外部是否傳入數(shù)據(jù)
data = SPI1DRL;
}
評(píng)論