---
title: "串行接口SPI接口应用设计"
author: "mmdev"
date: "2007-12-24T22:00:00Z"
tags: []
slug: "spi"
draft: false
lastmod: "2007-12-24T22:00:00Z"
id: 990269
url: "https://mmdev.iteye.com/blog/990269"
---

作者：马潮老师  
　　使用的同步串行三线SPI接口，可以方便的连接采用SPI通信协议的外围或另一片AVR单片机，实现在短距离内的高速同步通信。ATmega128的SPI采用硬件方式实现面向字节的全双工3线同步通信，支持主机、从机和2种不同极性的SPI时序，通信速率有7种选择，主机方式的最高速率为1/2系统时钟，从机方式最高速率为1/4系统时钟。

　　ATmega128单片机内部的SPI接口也被用于程序存储器和数据E2PROM的编程下载和上传。但特别需要注意的是，此时SPI的MOSI和MISO接口不再对应PB2、PB3引脚，而是转换到PE0、PE1引脚上（PDI、PDO），其详见第二章中关于程序存储器的串行编程和校验部分的内容。

　　ATmega128的SPI为硬件接口和传输完成中断申请，所以使用SPI传输数据的有效方法是采用中断方式+数据缓存器的设计方法。在对SPI初始化时，应注意以下几点：

.正确选择和设置主机或从机，以及工作模式（极性），数据传输率；

.注意传送字节的顺序，是低位优先（LSB First）还是高位优先（MSB Frist）；

.正确设置MOSI和MISO接口的输入输出方向，输入引脚使用上拉电阻，可以节省总线上的吊高电阻。

　　 下面一段是SPI主机方式连续发送（接收）字节的例程：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#defineSIZE100  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)unsignedcharSPI\_rx\_buff\[SIZE];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)unsignedcharSPI\_tx\_buff\[SIZE];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)unsignedcharrx\_wr\_index,rx\_rd\_index,rx\_counter,rx\_buffer\_overflow;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)unsignedchartx\_wr\_index,tx\_rd\_index,tx\_counter;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#pragmainterrupt\_handlerspi\_stc\_isr:18  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)voidspi\_stc\_isr(void)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)......{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SPI\_rx\_buff\[rx\_wr\_index]=SPDR;//从ISP口读出收到的字节  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(++rx\_wr\_index==SIZE)rx\_wr\_index=0;//放入接收缓冲区，并调整队列指针  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(++rx\_counter==SIZE)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)......{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)rx\_counter=0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)rx\_buffer\_overflow=1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(tx\_counter)//如果发送缓冲区中有待发的数据  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)......{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)--tx\_counter;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SPDR=SPI\_tx\_buff\[tx\_rd\_index];//发送一个字节数据，并调整指针  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(++tx\_rd\_index==SIZE)tx\_rd\_index=0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)unsignedchargetSPIchar(void)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)......{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)unsignedchardata;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)while(rx\_counter==0);//无接收数据，等待  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)data=SPI\_rx\_buff\[rx\_rd\_index];//从接收缓冲区取出一个SPI收到的数据  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(++rx\_rd\_index==SIZE)rx\_rd\_index=0;//调整指针  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)CLI();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)--rx\_counter;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SEI();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returndata;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)voidputSPIchar(charc)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)......{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)while(tx\_counter==SIZE);//发送缓冲区满，等待  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)CLI();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(tx\_counter||((SPSR&0x80)==0))//发送缓冲区已中有待发数据  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)......{//或SPI正在发送数据时  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SPI\_tx\_buffer\[tx\_wr\_index]=c;//将数据放入发送缓冲区排队  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(++tx\_wr\_index==SIZE)tx\_wr\_index=0;//调整指针  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)++tx\_counter;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SPDR=c;//发送缓冲区中空且SPI口空闲，直接放入SPDR由SIP口发送  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SEI();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)voidspi\_init(void)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)......{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)unsignedchattemp;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)DDRB|=0x080;//MISO=inputandMOSI,SCK,SS=output  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)PORTB|=0x80;//MISO上拉电阻有效  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SPCR=0xD5;//SPI允许，主机模式，MSB，允许SPI中断，极性方式01，1/16系统时钟速率  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SPSR=0x00;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)temp=SPSR;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)temp=SPDR;//清空SPI，和中断标志，使SPI空闲  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)voidmain(void)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)......{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)unsignedcharI;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)CLI();//关中断  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)spi\_init();//初始化SPI接口  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SEI();//开中断  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)while()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)......{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)putSPIchat(i);//发送一个字节  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)i++;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)getSPIchar();//接收一个字节（第一个字节为空字节）  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)………  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

　　这个典型的SPI例程比较简单，主程序中首先对ATmega128的硬件SPI进行初始化。在初始化过程中，将PORTB的MOSI、SCLK和SS引脚作为输出，同时将MISO作为输入引脚，并打开上拉电阻。接着对SPI的寄存器进行初始化设置，并空读一次SPSR、SPDR寄存器（读SPSR后再对SPDR操作将自动清零SPI中断标志自动清零），使ISP空闲等待发送数据。

　　AVR的SPI由一个16位的循环移位寄存器构成，当数据从主机方移出时，从机的数据同时也被移入，因此SPI的发送和接收在一个中断服务中完成。在SPI中断服务程序中，先从SPDR中读一个接收的字节存入接收数据缓冲器中，再从发送数据缓冲器取出一个字节写入SPDR中，由ISP发送到从机。数据一旦写入SPDR，ISP硬件开始发送数据。下一次ISP中断时，表示发送完成，并同时收到一个数据。类似本章介绍的USART接口的使用，程序中putSPIchar()和getSPIchar()为应用程序的底层接口函数（SPI驱动程序是SPI中断服务程序），同时也使用了两个数据缓冲器，分别构成循环队列。这种程序设计的思路，不但程序的结构性完整，同时也适当的解决了高速MCU和低速串口之间的矛盾，实现程序中任务的并行运行，提高了MCU的运行效率。

　　本例程是通过SPI批量输出、输入数据的示例，用户可以使用一片ATmega128，将其MOSI和MISO两个引脚连接起来，构成一个ISP接口自发自收的系统，对程序进行演示验证。需要注意，实际接收到的字节为上一次中断时发出的数据，即第一个收到的字节是空字节。

　　读懂和了解程序的处理思想，读者可以根据需要对程序进行改动，适合实际系统的使用。如在实际应用中外接的从机是一片SPI接口的温度芯片，协议规程为：主机先要连续发送3个字节的命令，然后从机才返回一个字节的数据。那么用户程序可以先循环调用putSPIchar()函数4次，将3个字节的命令和一个字节的空数据发送到从机，然后等待一段时间，或处理一些其它的操作后，再循环调用getSPIchar()函数4次，从接收数据缓冲器中连续读取4个字节，放弃前3个空字节，第4个字节即为从机的返回数据了。

==================================================

改天写一个关于dsp 的spi程序设计出来。。。。