spi.c
来自「avr+TFT+SD卡实现数码相框功能」· C语言 代码 · 共 60 行
C
60 行
//*****************************************************************************
//
// 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)
{
/* 设置MOSI和SCK及SS为输出,其他为输入 */
PORTB |= (1<<MOSI)|(1<<SCK);
/* 使能SPI,主机模式,设置时钟速率为Fosc/2 */
SPCR = (1<<SPE)|(1<<MSTR);
SPSR |= (1<<SPI2X); //SPI倍速
}
void spiSendByte(unsigned char spi_data)
{
/* 启动数据传输 */
SPDR = spi_data;
/* 等待传输结束 */
while(!(SPSR & (1<<SPIF)))
;
}
unsigned char spiTransferByte(unsigned char out_data)
{
/* 启动数据传输 */
SPDR = out_data;
/* 等待传输结束 */
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;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?