Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 68 | jlesech | 1 | /**************************************************************************//** |
| 2 | * \brief MAX7219 library |
||
| 3 | * \author Copyright (C) 2011 Julien Le Sech - www.idreammicro.com |
||
| 4 | * \version 1.0 |
||
| 5 | * \date 20110908 |
||
| 6 | * |
||
| 7 | * This file is part of the iDreamMicro library. |
||
| 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.c |
||
| 25 | ******************************************************************************/ |
||
| 26 | |||
| 27 | /****************************************************************************** |
||
| 28 | * Header file inclusions. |
||
| 29 | ******************************************************************************/ |
||
| 30 | |||
| 31 | #include "../max7219.h" |
||
| 32 | |||
| 33 | #include <digital_io/digital_io.h> |
||
| 34 | #include <spi/spi.h> |
||
| 35 | |||
| 36 | #include <util/delay.h> |
||
| 37 | |||
| 38 | #include <stdbool.h> |
||
| 39 | #include <stdint.h> |
||
| 40 | |||
| 41 | /****************************************************************************** |
||
| 42 | * Private macros. |
||
| 43 | ******************************************************************************/ |
||
| 44 | |||
| 45 | #define REG_NO_OP 0x00 |
||
| 46 | #define REG_DIGIT_0 0x01 |
||
| 47 | #define REG_DIGIT_1 0x02 |
||
| 48 | #define REG_DIGIT_2 0x03 |
||
| 49 | #define REG_DIGIT_3 0x04 |
||
| 50 | #define REG_DIGIT_4 0x05 |
||
| 51 | #define REG_DIGIT_5 0x06 |
||
| 52 | #define REG_DIGIT_6 0x07 |
||
| 53 | #define REG_DIGIT_7 0x08 |
||
| 54 | #define REG_DECODE_MODE 0x09 |
||
| 55 | #define REG_INTENSITY 0x0A |
||
| 56 | #define REG_SCAN_LIMIT 0x0B |
||
| 57 | #define REG_SHUTDOWN 0x0C |
||
| 58 | #define REG_DISPLAY_TEST 0x0F |
||
| 59 | |||
| 60 | /****************************************************************************** |
||
| 61 | * Private constants. |
||
| 62 | ******************************************************************************/ |
||
| 63 | |||
| 64 | static const digital_io__pin_position_t chip_select_pin = |
||
| 65 | DIGITAL_IO__PORT_B | DIGITAL_IO__PIN_2; |
||
| 66 | |||
| 67 | /****************************************************************************** |
||
| 68 | * Private function prototypes. |
||
| 69 | ******************************************************************************/ |
||
| 70 | |||
| 71 | /**************************************************************************//** |
||
| 72 | * \fn void max7219__write(uint8_t address, uin8_t value) |
||
| 73 | * |
||
| 74 | * \brief Write value into MAX7219 register. |
||
| 75 | * |
||
| 76 | * \param address Register address. |
||
| 77 | * \param value Value to write. |
||
| 78 | ******************************************************************************/ |
||
| 79 | void |
||
| 80 | max7219__write |
||
| 81 | ( |
||
| 82 | uint8_t address, |
||
| 83 | uint8_t value |
||
| 84 | ); |
||
| 85 | |||
| 86 | /****************************************************************************** |
||
| 87 | * Public function definitions. |
||
| 88 | ******************************************************************************/ |
||
| 89 | |||
| 90 | /**************************************************************************//** |
||
| 91 | * \fn void max7219__initialize(void) |
||
| 92 | * |
||
| 93 | * \brief Initialize SPI to drive MAX7219. |
||
| 94 | ******************************************************************************/ |
||
| 95 | void |
||
| 96 | max7219__initialize |
||
| 97 | ( |
||
| 98 | void |
||
| 99 | ){ |
||
| 100 | // Configure chip select pin as output. |
||
| 101 | digital_io__configure_pin(chip_select_pin, DIGITAL_IO__DIRECTION__OUTPUT); |
||
| 102 | digital_io__set_pin_level(chip_select_pin, DIGITAL_IO__LEVEL__HIGH); |
||
| 103 | |||
| 104 | // Wait a little to allow MAX7219 to see a correct logic level on CS pin. |
||
| 105 | _delay_ms(2); |
||
| 106 | |||
| 107 | // Configure SPI. |
||
| 108 | spi_master__initialize(); |
||
| 109 | spi__set_data_order(SPI__DATA_ORDER__MSB_FIRST); |
||
| 110 | spi__set_data_mode(SPI__DATA_MODE_0); |
||
| 111 | spi__set_clock_rate(SPI__CLOCK_RATE__FOSC_OVER_4); |
||
| 112 | } |
||
| 113 | |||
| 114 | /**************************************************************************//** |
||
| 115 | * \fn void max7219__set_decode_mode(max7219__decode_mode_t mode) |
||
| 116 | * |
||
| 117 | * \brief Set MAX7219 decode mode. |
||
| 118 | * |
||
| 119 | * \param mode Decode mode to set. |
||
| 120 | ******************************************************************************/ |
||
| 121 | void |
||
| 122 | max7219__set_decode_mode |
||
| 123 | ( |
||
| 124 | max7219__decode_mode_t mode |
||
| 125 | ){ |
||
| 126 | max7219__write(REG_DECODE_MODE, mode); |
||
| 127 | } |
||
| 128 | |||
| 129 | /**************************************************************************//** |
||
| 130 | * \fn void max7219__set_intensity(max7219__intensity_t intensity) |
||
| 131 | * |
||
| 132 | * \brief Set MAX7219 intensity. |
||
| 133 | * |
||
| 134 | * \param intensity Intensity to set. |
||
| 135 | ******************************************************************************/ |
||
| 136 | void |
||
| 137 | max7219__set_intensity |
||
| 138 | ( |
||
| 139 | max7219__intensity_t intensity |
||
| 140 | ){ |
||
| 141 | max7219__write(REG_INTENSITY, intensity); |
||
| 142 | } |
||
| 143 | |||
| 144 | /**************************************************************************//** |
||
| 145 | * \fn void max7219__set_scan_limit(max7219__scan_limit_t limit) |
||
| 146 | * |
||
| 147 | * \brief Set MAX7219 scan limit. |
||
| 148 | * |
||
| 149 | * \param limit Scan limit to set. |
||
| 150 | ******************************************************************************/ |
||
| 151 | void |
||
| 152 | max7219__set_scan_limit |
||
| 153 | ( |
||
| 154 | max7219__scan_limit_t limit |
||
| 155 | ){ |
||
| 156 | max7219__write(REG_SCAN_LIMIT, limit); |
||
| 157 | } |
||
| 158 | |||
| 159 | /**************************************************************************//** |
||
| 160 | * \fn void max7219__set_shutdown_mode(max7219__shutdown_mode_t mode) |
||
| 161 | * |
||
| 162 | * \brief Set MAX7219 shutdown mode. |
||
| 163 | * |
||
| 164 | * \param mode Shutdown mode to set. |
||
| 165 | ******************************************************************************/ |
||
| 166 | void |
||
| 167 | max7219__set_shutdown_mode |
||
| 168 | ( |
||
| 169 | max7219__shutdown_mode_t mode |
||
| 170 | ){ |
||
| 171 | max7219__write(REG_SHUTDOWN, mode); |
||
| 172 | } |
||
| 173 | |||
| 174 | /**************************************************************************//** |
||
| 175 | * \fn void max7219__set_display_test_mode(max7219__display_test_mode_t mode) |
||
| 176 | * |
||
| 177 | * \brief Set MAX7219 display test mode. |
||
| 178 | * |
||
| 179 | * \param mode Display test mode to set. |
||
| 180 | *****************************************************************************/ |
||
| 181 | void |
||
| 182 | max7219__set_display_test_mode |
||
| 183 | ( |
||
| 184 | max7219__display_test_mode_t mode |
||
| 185 | ){ |
||
| 186 | max7219__write(REG_DISPLAY_TEST, mode); |
||
| 187 | } |
||
| 188 | |||
| 189 | /**************************************************************************//** |
||
| 190 | * \fn void max7219__write_char_on_digit( |
||
| 191 | * max7219__digit_t digit, |
||
| 192 | * max7219__character_t character, |
||
| 193 | * bool decimal_point) |
||
| 194 | * |
||
| 195 | * \brief Write character on digit. |
||
| 196 | * |
||
| 197 | * \param digit Digit to write. |
||
| 198 | * \param character Character to write. |
||
| 199 | * \param decimal_point Display decimal point. |
||
| 200 | ******************************************************************************/ |
||
| 201 | void |
||
| 202 | max7219__write_char_on_digit |
||
| 203 | ( |
||
| 204 | max7219__digit_t digit, |
||
| 205 | max7219__character_t character, |
||
| 206 | bool decimal_point |
||
| 207 | ){ |
||
| 208 | uint8_t value = character; |
||
| 209 | if (decimal_point) |
||
| 210 | { |
||
| 211 | // If decimal point must be switched on. |
||
| 212 | value |= max7219__segment__DP; |
||
| 213 | } |
||
| 214 | max7219__write(digit, value); |
||
| 215 | } |
||
| 216 | |||
| 217 | /**************************************************************************//** |
||
| 218 | * \fn void max7219__write_segments_on_digit( |
||
| 219 | * max7219__digit_t digit, |
||
| 220 | * max7219__segment_t segments) |
||
| 221 | * |
||
| 222 | * \brief Set segment(s) on digit. |
||
| 223 | * |
||
| 224 | * \param digit Digit to write. |
||
| 225 | * \param segment Segment(s) to set. |
||
| 226 | ******************************************************************************/ |
||
| 227 | void |
||
| 228 | max7219__write_segments_on_digit |
||
| 229 | ( |
||
| 230 | max7219__digit_t digit, |
||
| 231 | max7219__segment_t segments |
||
| 232 | ){ |
||
| 233 | max7219__write(digit, segments); |
||
| 234 | } |
||
| 235 | |||
| 236 | /****************************************************************************** |
||
| 237 | * Private function definitions. |
||
| 238 | ******************************************************************************/ |
||
| 239 | |||
| 240 | /**************************************************************************//** |
||
| 241 | * \fn void max7219__write(uint8_t address, uint8_t value) |
||
| 242 | * |
||
| 243 | * \brief Write value into MAX7219 register. |
||
| 244 | * |
||
| 245 | * \param address Register address. |
||
| 246 | * \param value Value to write. |
||
| 247 | ******************************************************************************/ |
||
| 248 | void |
||
| 249 | max7219__write |
||
| 250 | ( |
||
| 251 | uint8_t address, |
||
| 252 | uint8_t value |
||
| 253 | ){ |
||
| 254 | // Reset chip select pin to select the device. |
||
| 255 | digital_io__set_pin_level(chip_select_pin, DIGITAL_IO__LEVEL__LOW); |
||
| 256 | |||
| 257 | // tcss |
||
| 258 | _delay_ms(1); |
||
| 259 | |||
| 260 | // Transmit data to the device, register address and value. |
||
| 261 | spi_master__transmit(address); |
||
| 262 | spi_master__transmit(value); |
||
| 263 | |||
| 264 | // Set chip select pin to valid data onto the device. |
||
| 265 | digital_io__set_pin_level(chip_select_pin, DIGITAL_IO__LEVEL__HIGH); |
||
| 266 | |||
| 267 | // tcsw |
||
| 268 | _delay_ms(1); |
||
| 269 | } |