?? spi.c
字號(hào):
//*****************************************************************************
//
// File Name : 'spi.c'
// Title : SPI interface driver
// Author : Andy Zhu
// Created : 07/20/2008
// Revised : 07/20/2008
// Version : 0.1
// Target MCU : Atmel AVR ATmega128
//
//*****************************************************************************
#include <avr/io.h>
//#include <avr/signal.h>
//#include <avr/interrupt.h>
#include "spi.h"
// access routines
void SPI_init(void)
{
/* 設(shè)置MOSI和SCK及SS為輸出,其他為輸入 */
PORTB |= (1<<MOSI)|(1<<SCK);
/* 使能SPI,主機(jī)模式,設(shè)置時(shí)鐘速率為Fosc/2 */
SPCR = (1<<SPE)|(1<<MSTR);
SPSR |= (1<<SPI2X); //SPI倍速
}
void spiSendByte(unsigned char spi_data)
{
/* 啟動(dòng)數(shù)據(jù)傳輸 */
SPDR = spi_data;
/* 等待傳輸結(jié)束 */
while(!(SPSR & (1<<SPIF)))
;
}
unsigned char spiTransferByte(unsigned char out_data)
{
/* 啟動(dòng)數(shù)據(jù)傳輸 */
SPDR = out_data;
/* 等待傳輸結(jié)束 */
while(!(SPSR & (1<<SPIF)));
return SPDR;
}
unsigned int spiTransferWord(unsigned int data)
{
unsigned int rxData = 0;
// send MS byte of given data
rxData = (spiTransferByte((data>>8) & 0x00FF))<<8;
// send LS byte of given data
rxData |= (spiTransferByte(data & 0x00FF));
// return the received data
return rxData;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -