⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spi_8c-source.html

📁 avr 设计开发板说明
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>Procyon AVRlib: spi.c Source File</title><link href="dox.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.3.6 --><div class="qindex"><a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data&nbsp;Structures</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Data&nbsp;Fields</a> | <a class="qindex" href="globals.html">Globals</a></div><h1>spi.c</h1><a href="spi_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <span class="comment">/*! \file spi.c \brief SPI interface driver. */</span>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name    : 'spi.c'</span>00005 <span class="comment">// Title        : SPI interface driver</span>00006 <span class="comment">// Author       : Pascal Stang - Copyright (C) 2000-2002</span>00007 <span class="comment">// Created      : 11/22/2000</span>00008 <span class="comment">// Revised      : 06/06/2002</span>00009 <span class="comment">// Version      : 0.6</span>00010 <span class="comment">// Target MCU   : Atmel AVR series</span>00011 <span class="comment">// Editor Tabs  : 4</span>00012 <span class="comment">//</span>00013 <span class="comment">// NOTE: This code is currently below version 1.0, and therefore is considered</span>00014 <span class="comment">// to be lacking in some functionality or documentation, or may not be fully</span>00015 <span class="comment">// tested.  Nonetheless, you can expect most functions to work.</span>00016 <span class="comment">//</span>00017 <span class="comment">// This code is distributed under the GNU Public License</span>00018 <span class="comment">//      which can be found at http://www.gnu.org/licenses/gpl.txt</span>00019 <span class="comment">//</span>00020 <span class="comment">//*****************************************************************************</span>00021 00022 <span class="preprocessor">#include &lt;avr/io.h&gt;</span>00023 <span class="preprocessor">#include &lt;avr/signal.h&gt;</span>00024 <span class="preprocessor">#include &lt;avr/interrupt.h&gt;</span>00025 00026 <span class="preprocessor">#include "<a class="code" href="spi_8h.html">spi.h</a>"</span>00027 00028 <span class="comment">// Define the SPI_USEINT key if you want SPI bus operation to be</span>00029 <span class="comment">// interrupt-driven.  The primary reason for not using SPI in</span>00030 <span class="comment">// interrupt-driven mode is if the SPI send/transfer commands</span>00031 <span class="comment">// will be used from within some other interrupt service routine</span>00032 <span class="comment">// or if interrupts might be globally turned off due to of other</span>00033 <span class="comment">// aspects of your program</span>00034 <span class="comment">//</span>00035 <span class="comment">// Comment-out or uncomment this line as necessary</span>00036 <span class="preprocessor">#define SPI_USEINT</span>00037 <span class="preprocessor"></span>00038 <span class="comment">// global variables</span>00039 <span class="keyword">volatile</span> u08 spiTransferComplete;00040 00041 <span class="comment">// SPI interrupt service handler</span>00042 <span class="preprocessor">#ifdef SPI_USEINT</span>00043 <span class="preprocessor"></span><a class="code" href="a2d_8c.html#a10">SIGNAL</a>(SIG_SPI)00044 {00045     spiTransferComplete = TRUE;00046 }00047 <span class="preprocessor">#endif</span>00048 <span class="preprocessor"></span>00049 <span class="comment">// access routines</span>00050 <span class="keywordtype">void</span> spiInit()00051 {00052 <span class="preprocessor">#ifdef __AVR_ATmega128__</span>00053 <span class="preprocessor"></span>    <span class="comment">// setup SPI I/O pins</span>00054     sbi(PORTB, 1);  <span class="comment">// set SCK hi</span>00055     sbi(DDRB, 1);   <span class="comment">// set SCK as output</span>00056     cbi(DDRB, 3);   <span class="comment">// set MISO as input</span>00057     sbi(DDRB, 2);   <span class="comment">// set MOSI as output</span>00058     sbi(DDRB, 0);   <span class="comment">// SS must be output for Master mode to work</span>00059 <span class="preprocessor">#else</span>00060 <span class="preprocessor"></span>    <span class="comment">// setup SPI I/O pins</span>00061     sbi(PORTB, 7);  <span class="comment">// set SCK hi</span>00062     sbi(DDRB, 7);   <span class="comment">// set SCK as output</span>00063     cbi(DDRB, 6);   <span class="comment">// set MISO as input</span>00064     sbi(DDRB, 5);   <span class="comment">// set MOSI as output</span>00065     sbi(DDRB, 4);   <span class="comment">// SS must be output for Master mode to work</span>00066 <span class="preprocessor">#endif</span>00067 <span class="preprocessor"></span>    00068     <span class="comment">// setup SPI interface :</span>00069     <span class="comment">// master mode</span>00070     sbi(SPCR, MSTR);00071     <span class="comment">// clock = f/4</span>00072 <span class="comment">//  cbi(SPCR, SPR0);</span>00073 <span class="comment">//  cbi(SPCR, SPR1);</span>00074     <span class="comment">// clock = f/16</span>00075     cbi(SPCR, SPR0);00076     sbi(SPCR, SPR1);00077     <span class="comment">// select clock phase positive-going in middle of data</span>00078     cbi(SPCR, CPOL);00079     <span class="comment">// Data order MSB first</span>00080     cbi(SPCR,DORD);00081     <span class="comment">// enable SPI</span>00082     sbi(SPCR, SPE);00083         00084     00085     <span class="comment">// some other possible configs</span>00086     <span class="comment">//outp((1&lt;&lt;MSTR)|(1&lt;&lt;SPE)|(1&lt;&lt;SPR0), SPCR );</span>00087     <span class="comment">//outp((1&lt;&lt;CPHA)|(1&lt;&lt;CPOL)|(1&lt;&lt;MSTR)|(1&lt;&lt;SPE)|(1&lt;&lt;SPR0)|(1&lt;&lt;SPR1), SPCR );</span>00088     <span class="comment">//outp((1&lt;&lt;CPHA)|(1&lt;&lt;MSTR)|(1&lt;&lt;SPE)|(1&lt;&lt;SPR0), SPCR );</span>00089     00090     <span class="comment">// clear status</span>00091     inp(SPSR);00092     spiTransferComplete = TRUE;00093 00094     <span class="comment">// enable SPI interrupt</span>00095 <span class="preprocessor">    #ifdef SPI_USEINT</span>00096 <span class="preprocessor"></span>    sbi(SPCR, SPIE);00097 <span class="preprocessor">    #endif</span>00098 <span class="preprocessor"></span>}00099 <span class="comment">/*</span>00100 <span class="comment">void spiSetBitrate(u08 spr)</span>00101 <span class="comment">{</span>00102 <span class="comment">    outb(SPCR, (inb(SPCR) &amp; ((1&lt;&lt;SPR0)|(1&lt;&lt;SPR1))) | (spr&amp;((1&lt;&lt;SPR0)|(1&lt;&lt;SPR1)))));</span>00103 <span class="comment">}</span>00104 <span class="comment">*/</span>00105 <span class="keywordtype">void</span> spiSendByte(u08 data)00106 {00107     <span class="comment">// send a byte over SPI and ignore reply</span>00108 <span class="preprocessor">    #ifdef SPI_USEINT</span>00109 <span class="preprocessor"></span>        <span class="keywordflow">while</span>(!spiTransferComplete);00110 <span class="preprocessor">    #else</span>00111 <span class="preprocessor"></span>        <span class="keywordflow">while</span>(!(inp(SPSR) &amp; (1&lt;&lt;SPIF)));00112 <span class="preprocessor">    #endif</span>00113 <span class="preprocessor"></span>00114     spiTransferComplete = FALSE;00115     outp(data, SPDR);00116 }00117 00118 u08 spiTransferByte(u08 data)00119 {00120     <span class="comment">// make sure interface is idle</span>00121 <span class="preprocessor">    #ifdef SPI_USEINT</span>00122 <span class="preprocessor"></span>        <span class="keywordflow">while</span>(!spiTransferComplete);00123 <span class="preprocessor">    #else</span>00124 <span class="preprocessor"></span>        <span class="keywordflow">while</span>(!(inp(SPSR) &amp; (1&lt;&lt;SPIF)));00125 <span class="preprocessor">    #endif</span>00126 <span class="preprocessor"></span>00127     <span class="comment">// send the given data</span>00128     spiTransferComplete = FALSE;00129     outp(data, SPDR);00130 00131     <span class="comment">// wait for transfer to complete</span>00132 <span class="preprocessor">    #ifdef SPI_USEINT</span>00133 <span class="preprocessor"></span>        <span class="keywordflow">while</span>(!spiTransferComplete);00134 <span class="preprocessor">    #else</span>00135 <span class="preprocessor"></span>        <span class="keywordflow">while</span>(!(inp(SPSR) &amp; (1&lt;&lt;SPIF)));00136         <span class="comment">// *** reading of the SPSR and SPDR are crucial</span>00137         <span class="comment">// *** to the clearing of the SPIF flag</span>00138         <span class="comment">// *** in non-interrupt mode</span>00139         <span class="comment">//inp(SPDR);</span>00140         <span class="comment">// set flag</span>00141         spiTransferComplete = TRUE;00142 <span class="preprocessor">    #endif</span>00143 <span class="preprocessor"></span>    <span class="comment">// return the received data</span>00144     <span class="keywordflow">return</span> inp(SPDR);00145 }00146 00147 u16 spiTransferWord(u16 data)00148 {00149     u16 rxData = 0;00150 00151     <span class="comment">// send MS byte of given data</span>00152     rxData = (spiTransferByte((data&gt;&gt;8) &amp; 0x00FF))&lt;&lt;8;00153     <span class="comment">// send LS byte of given data</span>00154     rxData |= (spiTransferByte(data &amp; 0x00FF));00155 00156     <span class="comment">// return the received data</span>00157     <span class="keywordflow">return</span> rxData;00158 }</pre></div><hr size="1"><address style="align: right;"><small>Generated on Fri Oct 15 03:50:22 2004 for Procyon AVRlib by<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border=0 > </a>1.3.6 </small></address></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -