?? spi.c
字號(hào):
/*--------------------------------------------------*/
/* AVR-ARM開(kāi)發(fā)網(wǎng)論壇 */
/* http://www.avrarm.com */
/* AVR生成代碼測(cè)試程序 */
/*--------------------------------------------------*/
/* 程序由AVR輔助開(kāi)發(fā)工具V2.1.1自動(dòng)生成 */
/* AVR系統(tǒng)的處理器為: ATMega128 */
/* AVR系統(tǒng)的晶振頻率: 8.0000 Mhz */
/*--------------------------------------------------*/
#include <iom128v.h>
#include "spi.h"
//-----------------------------------------------------------
//SPI Master初始化子程序
//-----------------------------------------------------------
void SPI_MasterInit(void)
{
DDR_SPI = (1<<DD_MOSI)|(1<<DD_SS)|(1<<DD_SCK); /* 設(shè)置MOSI 和SCK 為輸出,其他為輸入 */
SPCR = 0x70; /* 使能SPI 主機(jī)模式 */
SPSR = 0x00; /* 倍速 */
}
//-----------------------------------------------------------
//SPI Master發(fā)送子程序
//-----------------------------------------------------------
void SPI_MasterTransmit(unsigned char cData)
{
SPDR = cData; /* 啟動(dòng)數(shù)據(jù)傳輸 */
while(!(SPSR & (1<<SPIF))); /* 等待傳輸結(jié)束 */
}
//-----------------------------------------------------------
//SPI Master接收子程序
//-----------------------------------------------------------
unsigned char SPI_MasterReceive(void)
{
SPDR = 0xFF;
while(!(SPSR & (1<<SPIF))); /* 等待接收結(jié)束 */
return SPDR; /* 返回?cái)?shù)據(jù) */
}