📄 ads7870_8c-source.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: ads7870.c Source File</title><link href="dox.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.4.2 --><div class="qindex"><a class="qindex" href="main.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a></div><h1>ads7870.c</h1><a href="ads7870_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file ads7870.c \brief TI ADS7870 12-bit 8ch A/D Converter Driver Library. */</span>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name : 'ads7870.c'</span>00005 <span class="comment">// Title : TI ADS7870 12-bit 8ch A/D Converter Driver Library</span>00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2005</span>00007 <span class="comment">// Created : 2005.07.19</span>00008 <span class="comment">// Revised : 2005.07.21</span>00009 <span class="comment">// Version : 0.1</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 <avr/io.h></span>00023 <span class="preprocessor">#include <avr/signal.h></span>00024 <span class="preprocessor">#include <avr/interrupt.h></span>00025 00026 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>00027 <span class="preprocessor">#include "<a class="code" href="spi_8h.html">spi.h</a>"</span>00028 <span class="preprocessor">#include "<a class="code" href="ads7870_8h.html">ads7870.h</a>"</span>00029 00030 <span class="comment">// global variables</span>00031 00032 <span class="comment">// Functions</span><a name="l00033"></a><a class="code" href="group__ads7870.html#ga0">00033</a> u08 <a class="code" href="group__ads7870.html#ga0">ads7870Init</a>(<span class="keywordtype">void</span>)00034 {00035 <span class="comment">// initialize spi interface</span>00036 spiInit();00037 <span class="comment">// switch to f/4 bitrate</span>00038 cbi(SPCR, SPR0);00039 cbi(SPCR, SPR1);00040 <span class="comment">//sbi(SPSR, SPI2X);</span>00041 00042 <span class="comment">// setup chip select</span>00043 sbi(ADS7870_CS_PORT, ADS7870_CS_PIN);00044 sbi(ADS7870_CS_DDR, ADS7870_CS_PIN);00045 00046 <span class="comment">// check ID register</span>00047 <span class="keywordflow">if</span>(<a class="code" href="group__ads7870.html#ga4">ads7870ReadReg</a>(ADS7870_ID) != ADS7870_ID_VALUE)00048 <span class="keywordflow">return</span> 0;00049 00050 <span class="comment">// setup reference and buffer</span>00051 <a class="code" href="group__ads7870.html#ga5">ads7870WriteReg</a>(ADS7870_REFOSC, ADS7870_REFOSC_OSCE | ADS7870_REFOSC_REFE | ADS7870_REFOSC_BUFE);00052 00053 <span class="comment">// return success</span>00054 <span class="keywordflow">return</span> 1;00055 }00056 <a name="l00057"></a><a class="code" href="group__ads7870.html#ga1">00057</a> s16 <a class="code" href="group__ads7870.html#ga1">ads7870Convert</a>(u08 channel)00058 {00059 <span class="comment">// set single-ended channel bit</span>00060 channel |= ADS7870_CH_SINGLE_ENDED;00061 <span class="comment">// do conversion</span>00062 <span class="keywordflow">return</span> <a class="code" href="group__ads7870.html#ga3">ads7870ConvertRaw</a>(channel);00063 }00064 <a name="l00065"></a><a class="code" href="group__ads7870.html#ga2">00065</a> s16 <a class="code" href="group__ads7870.html#ga2">ads7870ConvertDiff</a>(u08 channel)00066 {00067 <span class="comment">// clear single-ended channel bit</span>00068 channel &= ~ADS7870_CH_SINGLE_ENDED;00069 <span class="comment">// do conversion</span>00070 <span class="keywordflow">return</span> <a class="code" href="group__ads7870.html#ga3">ads7870ConvertRaw</a>(channel);00071 }00072 <a name="l00073"></a><a class="code" href="group__ads7870.html#ga3">00073</a> s16 <a class="code" href="group__ads7870.html#ga3">ads7870ConvertRaw</a>(u08 channel)00074 {00075 s16 result;00076 <span class="comment">// assert chip select</span>00077 cbi(ADS7870_CS_PORT, ADS7870_CS_PIN);00078 <span class="comment">// start conversion</span>00079 spiTransferByte(ADS7870_CONVERT | channel);00080 <span class="comment">// wait for completion</span>00081 <span class="keywordflow">while</span>( <a class="code" href="group__ads7870.html#ga4">ads7870ReadReg</a>(ADS7870_GAINMUX) & ADS7870_GAINMUX_CNVBSY);00082 <span class="comment">// assert chip select</span>00083 cbi(ADS7870_CS_PORT, ADS7870_CS_PIN);00084 <span class="comment">// read result</span>00085 spiTransferByte(ADS7870_REG_READ | ADS7870_REG_16BIT | ADS7870_RESULTHI);00086 result = spiTransferByte(0x00)<<8;00087 result |= spiTransferByte(0x00);00088 <span class="comment">// release chip select</span>00089 sbi(ADS7870_CS_PORT, ADS7870_CS_PIN);00090 <span class="comment">// return result</span>00091 <span class="keywordflow">return</span> result;00092 }00093 <a name="l00094"></a><a class="code" href="group__ads7870.html#ga4">00094</a> u08 <a class="code" href="group__ads7870.html#ga4">ads7870ReadReg</a>(u08 reg)00095 {00096 u08 data;00097 <span class="comment">// assert chip select</span>00098 cbi(ADS7870_CS_PORT, ADS7870_CS_PIN);00099 <span class="comment">// issue reg read command</span>00100 spiTransferByte(ADS7870_REG_READ | reg);00101 <span class="comment">// read data</span>00102 data = spiTransferByte(0x00);00103 <span class="comment">// release chip select</span>00104 sbi(ADS7870_CS_PORT, ADS7870_CS_PIN);00105 <span class="comment">// return data</span>00106 <span class="keywordflow">return</span> data;00107 }00108 <a name="l00109"></a><a class="code" href="group__ads7870.html#ga5">00109</a> <span class="keywordtype">void</span> <a class="code" href="group__ads7870.html#ga5">ads7870WriteReg</a>(u08 reg, u08 value)00110 {00111 <span class="comment">// assert chip select</span>00112 cbi(ADS7870_CS_PORT, ADS7870_CS_PIN);00113 <span class="comment">// issue reg write command</span>00114 spiTransferByte(ADS7870_REG_WRITE | reg);00115 <span class="comment">// write data</span>00116 spiTransferByte(value);00117 <span class="comment">// release chip select</span>00118 sbi(ADS7870_CS_PORT, ADS7870_CS_PIN);00119 }00120 </pre></div><hr size="1"><address style="align: right;"><small>Generated on Mon Aug 22 04:29:27 2005 for Procyon AVRlib by <a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -