Subversion Repositories idreammicro-arduino

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 jlesech 1
/**************************************************************************//**
2
 * \brief MAX7219 library for Arduino
3
 * \author Copyright (C) 2011  Julien Le Sech - www.idreammicro.com
4
 * \version 1.0
5
 * \date 20110801
6
 *
7
 * This file is part of the MAX7219 library for Arduino.
8
 *
9
 * This library is free software: you can redistribute it and/or modify it under
10
 * the terms of the GNU Lesser General Public License as published by the Free
11
 * Software Foundation, either version 3 of the License, or (at your option) any
12
 * later version.
13
 *
14
 * This library is distributed in the hope that it will be useful, but WITHOUT
15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
17
 * details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with this program. If not, see http://www.gnu.org/licenses/
21
 ******************************************************************************/
22
 
23
/**************************************************************************//**
24
 * \file MAX7219.cpp
25
 ******************************************************************************/
26
 
27
/******************************************************************************
28
 * Header file inclusions.
29
 ******************************************************************************/
30
 
31
#include <WProgram.h>
32
 
33
#include <SPI.h>
34
 
35
#include <MAX7219.h>
36
 
37
/******************************************************************************
38
 * Private macros.
39
 ******************************************************************************/
40
 
41
#define REG_NO_OP           0x00
42
#define REG_DIGIT_0         0x01
43
#define REG_DIGIT_1         0x02
44
#define REG_DIGIT_2         0x03
45
#define REG_DIGIT_3         0x04
46
#define REG_DIGIT_4         0x05
47
#define REG_DIGIT_5         0x06
48
#define REG_DIGIT_6         0x07
49
#define REG_DIGIT_7         0x08
50
#define REG_DECODE_MODE     0x09
51
#define REG_INTENSITY       0x0A
52
#define REG_SCAN_LIMIT      0x0B
53
#define REG_SHUTDOWN        0x0C
54
#define REG_DISPLAY_TEST    0x0F
55
 
56
/******************************************************************************
57
 * Public method definitions.
58
 ******************************************************************************/
59
 
60
/**************************************************************************//**
61
 * \fn MAX7219::MAX7219(byte csPin)
62
 *
63
 * \brief Constructor.
64
 *
65
 * \param       csPin   Chip select pin number.
66
 ******************************************************************************/
67
MAX7219::MAX7219
68
(
69
    byte csPin
70
){
71
        m_csPin = csPin;
72
}
73
 
74
/**************************************************************************//**
75
 * \fn void MAX7219::initialize()
76
 *
77
 * \brief Initialize SPI to drive MAX7219.
78
 ******************************************************************************/
79
void
80
MAX7219::initialize()
81
{
82
    // Configure chip select pin as output.
83
    pinMode(m_csPin, OUTPUT);
84
 
85
    // Wait a little to allow MAX7219 to see a correct logic level on CS pin.
86
    delay(1);
87
 
88
    // Configure SPI.
89
    SPI.begin();
90
    SPI.setBitOrder(MSBFIRST);
91
    SPI.setDataMode(SPI_MODE0);
92
    SPI.setClockDivider(SPI_CLOCK_DIV4);
93
}
94
 
95
/**************************************************************************//**
96
 * \fn void MAX7219::setDecodeMode(DecodeModes mode)
97
 *
98
 * \brief Set MAX7219 decode mode.
99
 *
100
 * \param       mode    Decode mode to set.
101
 ******************************************************************************/
102
void
103
MAX7219::setDecodeMode
104
(
105
    DecodeModes mode
106
){
107
    write(REG_DECODE_MODE, mode);
108
}
109
 
110
/**************************************************************************//**
111
 * \fn void MAX7219::setIntensity(Intensities intensity)
112
 *
113
 * \brief Set MAX7219 intensity.
114
 *
115
 * \param       itensity        Intensity to set.
116
 ******************************************************************************/
117
void
118
MAX7219::setIntensity
119
(
120
    Intensities intensity
121
){
122
    write(REG_INTENSITY, intensity);
123
}
124
 
125
/**************************************************************************//**
126
 * \fn void MAX7219::setScanLimit(ScanLimits limit)
127
 *
128
 * \brief Set MAX7219 scan limit.
129
 *
130
 * \param       limit   Scan limit to set.
131
 ******************************************************************************/
132
void
133
MAX7219::setScanLimit
134
(
135
    ScanLimits limit
136
){
137
    write(REG_SCAN_LIMIT, limit);
138
}
139
 
140
/**************************************************************************//**
141
 * \fn void MAX7219::setShutdownMode(ShutdownModes mode)
142
 *
143
 * \brief Set MAX7219 shutdown mode.
144
 *
145
 * \param       mode    Shutdown mode to set.
146
 ******************************************************************************/
147
void
148
MAX7219::setShutdownMode
149
(
150
    ShutdownModes mode
151
){
152
    write(REG_SHUTDOWN, mode);
153
}
154
 
155
/**************************************************************************//**
156
 * \fn void MAX7219::setDisplayTestMode(DisplayTestModes mode)
157
 *
158
 * \brief Set MAX7219 display test mode.
159
 *
160
 * \param       mode    Display test mode to set.
161
 ******************************************************************************/
162
void
163
MAX7219::setDisplayTestMode
164
(
165
    DisplayTestModes mode
166
){
167
    write(REG_DISPLAY_TEST, mode);
168
}
169
 
170
/**************************************************************************//**
171
 * \fn void MAX7219::writeDigit(
172
 * Digits       digit,
173
 * Characters   character,
174
 * bool         decimalPoint)
175
 *
176
 * \brief Write character on digit.
177
 *
178
 * \param       digit               Digit to write.
179
 * \param       character           Character to write.
180
 * \param   decimalPoint    Display decimal point if true.
181
 ******************************************************************************/
182
void
183
MAX7219::writeDigit
184
(
185
    Digits      digit,
186
    Characters  character,
187
    bool        decimalPoint
188
){
189
    byte value = character;
190
    if (decimalPoint)
191
    {
192
        // If decimal point must be switched on.
193
        value |= Segment_DP;
194
    }
195
    write(digit, value);
196
}
197
 
198
/**************************************************************************//**
199
 * \fn void MAX7219::writeDigit(Digits digit, Segments segments)
200
 *
201
 * \brief Set segment(s) on digit.
202
 *
203
 * \param       digit   Digit to write.
204
 * \param       segment Segment(s) to set.
205
 ******************************************************************************/
206
void
207
MAX7219::writeDigit
208
(
209
    Digits      digit,
210
    Segments    segments
211
){
212
    write(digit, segments);
213
}
214
 
215
/******************************************************************************
216
 * Private method definitions.
217
 ******************************************************************************/
218
 
219
/**************************************************************************//**
220
 * \fn void MAX7219::write(byte address, byte value)
221
 *
222
 * \brief Write value into MAX7219 register.
223
 *
224
 * \param       address Register address.
225
 * \param       value   Value to write.
226
 ******************************************************************************/
227
void
228
MAX7219::write
229
(
230
    byte address,
231
    byte value
232
){
233
    // Reset chip select pin to select the device.
234
    digitalWrite(m_csPin, LOW);
235
    // Transmit data to the device, register address and value.
236
    SPI.transfer(address);
237
    SPI.transfer(value);
238
    // Set chip select pin to valid data onto the device.
239
    digitalWrite(m_csPin, HIGH);
240
}