Line No. | Rev | Author | Line |
---|---|---|---|
1 | 6 | kaklik | /*! \file ads7870.c \brief TI ADS7870 12-bit 8ch A/D Converter Driver Library. */ |
2 | //***************************************************************************** |
||
3 | // |
||
4 | // File Name : 'ads7870.c' |
||
5 | // Title : TI ADS7870 12-bit 8ch A/D Converter Driver Library |
||
6 | // Author : Pascal Stang - Copyright (C) 2005 |
||
7 | // Created : 2005.07.19 |
||
8 | // Revised : 2005.07.21 |
||
9 | // Version : 0.1 |
||
10 | // Target MCU : Atmel AVR Series |
||
11 | // Editor Tabs : 4 |
||
12 | // |
||
13 | // NOTE: This code is currently below version 1.0, and therefore is considered |
||
14 | // to be lacking in some functionality or documentation, or may not be fully |
||
15 | // tested. Nonetheless, you can expect most functions to work. |
||
16 | // |
||
17 | // This code is distributed under the GNU Public License |
||
18 | // which can be found at http://www.gnu.org/licenses/gpl.txt |
||
19 | // |
||
20 | //***************************************************************************** |
||
21 | |||
22 | #include <avr/io.h> |
||
23 | #include <avr/interrupt.h> |
||
24 | |||
25 | #include "global.h" |
||
26 | #include "spi.h" |
||
27 | #include "ads7870.h" |
||
28 | |||
29 | // global variables |
||
30 | |||
31 | // Functions |
||
32 | u08 ads7870Init(void) |
||
33 | { |
||
34 | // initialize spi interface |
||
35 | spiInit(); |
||
36 | // switch to f/4 bitrate |
||
37 | cbi(SPCR, SPR0); |
||
38 | cbi(SPCR, SPR1); |
||
39 | //sbi(SPSR, SPI2X); |
||
40 | |||
41 | // setup chip select |
||
42 | sbi(ADS7870_CS_PORT, ADS7870_CS_PIN); |
||
43 | sbi(ADS7870_CS_DDR, ADS7870_CS_PIN); |
||
44 | |||
45 | // check ID register |
||
46 | if(ads7870ReadReg(ADS7870_ID) != ADS7870_ID_VALUE) |
||
47 | return 0; |
||
48 | |||
49 | // setup reference and buffer |
||
50 | ads7870WriteReg(ADS7870_REFOSC, ADS7870_REFOSC_OSCE | ADS7870_REFOSC_REFE | ADS7870_REFOSC_BUFE); |
||
51 | |||
52 | // return success |
||
53 | return 1; |
||
54 | } |
||
55 | |||
56 | s16 ads7870Convert(u08 channel) |
||
57 | { |
||
58 | // set single-ended channel bit |
||
59 | channel |= ADS7870_CH_SINGLE_ENDED; |
||
60 | // do conversion |
||
61 | return ads7870ConvertRaw(channel); |
||
62 | } |
||
63 | |||
64 | s16 ads7870ConvertDiff(u08 channel) |
||
65 | { |
||
66 | // clear single-ended channel bit |
||
67 | channel &= ~ADS7870_CH_SINGLE_ENDED; |
||
68 | // do conversion |
||
69 | return ads7870ConvertRaw(channel); |
||
70 | } |
||
71 | |||
72 | s16 ads7870ConvertRaw(u08 channel) |
||
73 | { |
||
74 | s16 result; |
||
75 | // assert chip select |
||
76 | cbi(ADS7870_CS_PORT, ADS7870_CS_PIN); |
||
77 | // start conversion |
||
78 | spiTransferByte(ADS7870_CONVERT | channel); |
||
79 | // wait for completion |
||
80 | while( ads7870ReadReg(ADS7870_GAINMUX) & ADS7870_GAINMUX_CNVBSY); |
||
81 | // assert chip select |
||
82 | cbi(ADS7870_CS_PORT, ADS7870_CS_PIN); |
||
83 | // read result |
||
84 | spiTransferByte(ADS7870_REG_READ | ADS7870_REG_16BIT | ADS7870_RESULTHI); |
||
85 | result = spiTransferByte(0x00)<<8; |
||
86 | result |= spiTransferByte(0x00); |
||
87 | // release chip select |
||
88 | sbi(ADS7870_CS_PORT, ADS7870_CS_PIN); |
||
89 | // return result |
||
90 | return result; |
||
91 | } |
||
92 | |||
93 | u08 ads7870ReadReg(u08 reg) |
||
94 | { |
||
95 | u08 data; |
||
96 | // assert chip select |
||
97 | cbi(ADS7870_CS_PORT, ADS7870_CS_PIN); |
||
98 | // issue reg read command |
||
99 | spiTransferByte(ADS7870_REG_READ | reg); |
||
100 | // read data |
||
101 | data = spiTransferByte(0x00); |
||
102 | // release chip select |
||
103 | sbi(ADS7870_CS_PORT, ADS7870_CS_PIN); |
||
104 | // return data |
||
105 | return data; |
||
106 | } |
||
107 | |||
108 | void ads7870WriteReg(u08 reg, u08 value) |
||
109 | { |
||
110 | // assert chip select |
||
111 | cbi(ADS7870_CS_PORT, ADS7870_CS_PIN); |
||
112 | // issue reg write command |
||
113 | spiTransferByte(ADS7870_REG_WRITE | reg); |
||
114 | // write data |
||
115 | spiTransferByte(value); |
||
116 | // release chip select |
||
117 | sbi(ADS7870_CS_PORT, ADS7870_CS_PIN); |
||
118 | } |
||
119 |
Powered by WebSVN v2.8.3