Subversion Repositories idreammicro-avr

Rev

Blame | Last modification | View Log | Download | RSS feed

/**************************************************************************//**
 * \brief MAX7219 library
 * \author Copyright (C) 2011  Julien Le Sech - www.idreammicro.com
 * \version 1.0
 * \date 20110908
 *
 * This file is part of the iDreamMicro library.
 *
 * This library is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 ******************************************************************************/


/**************************************************************************//**
 * \file max7219.c
 ******************************************************************************/


/******************************************************************************
 * Header file inclusions.
 ******************************************************************************/


#include "../max7219.h"

#include <digital_io/digital_io.h>
#include <spi/spi.h>

#include <util/delay.h>

#include <stdbool.h>
#include <stdint.h>

/******************************************************************************
 * Private macros.
 ******************************************************************************/


#define REG_NO_OP           0x00
#define REG_DIGIT_0         0x01
#define REG_DIGIT_1         0x02
#define REG_DIGIT_2         0x03
#define REG_DIGIT_3         0x04
#define REG_DIGIT_4         0x05
#define REG_DIGIT_5         0x06
#define REG_DIGIT_6         0x07
#define REG_DIGIT_7         0x08
#define REG_DECODE_MODE     0x09
#define REG_INTENSITY       0x0A
#define REG_SCAN_LIMIT      0x0B
#define REG_SHUTDOWN        0x0C
#define REG_DISPLAY_TEST    0x0F

/******************************************************************************
 * Private constants.
 ******************************************************************************/


static const digital_io__pin_position_t chip_select_pin =
    DIGITAL_IO__PORT_B | DIGITAL_IO__PIN_2;

/******************************************************************************
 * Private function prototypes.
 ******************************************************************************/


/**************************************************************************//**
 * \fn void max7219__write(uint8_t address, uin8_t value)
 *
 * \brief Write value into MAX7219 register.
 *
 * \param       address Register address.
 * \param       value   Value to write.
 ******************************************************************************/

void
max7219__write
(
    uint8_t address,
    uint8_t value
);

/******************************************************************************
 * Public function definitions.
 ******************************************************************************/


/**************************************************************************//**
 * \fn void max7219__initialize(void)
 *
 * \brief Initialize SPI to drive MAX7219.
 ******************************************************************************/

void
max7219__initialize
(
    void
){
    // Configure chip select pin as output.
    digital_io__configure_pin(chip_select_pin, DIGITAL_IO__DIRECTION__OUTPUT);
    digital_io__set_pin_level(chip_select_pin, DIGITAL_IO__LEVEL__HIGH);
   
    // Wait a little to allow MAX7219 to see a correct logic level on CS pin.
    _delay_ms(2);
   
    // Configure SPI.
    spi_master__initialize();
    spi__set_data_order(SPI__DATA_ORDER__MSB_FIRST);
    spi__set_data_mode(SPI__DATA_MODE_0);
    spi__set_clock_rate(SPI__CLOCK_RATE__FOSC_OVER_4);
}

/**************************************************************************//**
 * \fn void max7219__set_decode_mode(max7219__decode_mode_t mode)
 *
 * \brief Set MAX7219 decode mode.
 *
 * \param       mode    Decode mode to set.
 ******************************************************************************/

void
max7219__set_decode_mode
(
    max7219__decode_mode_t  mode
){
    max7219__write(REG_DECODE_MODE, mode);
}

/**************************************************************************//**
 * \fn void max7219__set_intensity(max7219__intensity_t intensity)
 *
 * \brief Set MAX7219 intensity.
 *
 * \param       intensity       Intensity to set.
 ******************************************************************************/

void
max7219__set_intensity
(
    max7219__intensity_t    intensity
){
    max7219__write(REG_INTENSITY, intensity);
}

/**************************************************************************//**
 * \fn void max7219__set_scan_limit(max7219__scan_limit_t limit)
 *
 * \brief Set MAX7219 scan limit.
 *
 * \param       limit   Scan limit to set.
 ******************************************************************************/

void
max7219__set_scan_limit
(
    max7219__scan_limit_t   limit
){
    max7219__write(REG_SCAN_LIMIT, limit);
}

/**************************************************************************//**
 * \fn void max7219__set_shutdown_mode(max7219__shutdown_mode_t mode)
 *
 * \brief Set MAX7219 shutdown mode.
 *
 * \param       mode    Shutdown mode to set.
 ******************************************************************************/

void
max7219__set_shutdown_mode
(
    max7219__shutdown_mode_t    mode
){
    max7219__write(REG_SHUTDOWN, mode);
}

/**************************************************************************//**
 * \fn void max7219__set_display_test_mode(max7219__display_test_mode_t mode)
 *
 * \brief Set MAX7219 display test mode.
 *
 * \param       mode    Display test mode to set.
 *****************************************************************************/

void
max7219__set_display_test_mode
(
    max7219__display_test_mode_t    mode
){
    max7219__write(REG_DISPLAY_TEST, mode);
}

/**************************************************************************//**
 * \fn void max7219__write_char_on_digit(
 * max7219__digit_t     digit,
 * max7219__character_t character,
 * bool                 decimal_point)
 *
 * \brief Write character on digit.
 *
 * \param       digit               Digit to write.
 * \param       character           Character to write.
 * \param   decimal_point   Display decimal point.
 ******************************************************************************/

void
max7219__write_char_on_digit
(
    max7219__digit_t        digit,
    max7219__character_t    character,
    bool                    decimal_point
){
    uint8_t value = character;
    if (decimal_point)
    {
        // If decimal point must be switched on.
        value |= max7219__segment__DP;
    }
    max7219__write(digit, value);
}

/**************************************************************************//**
 * \fn void max7219__write_segments_on_digit(
 * max7219__digit_t     digit,
 * max7219__segment_t   segments)
 *
 * \brief Set segment(s) on digit.
 *
 * \param       digit   Digit to write.
 * \param       segment Segment(s) to set.
 ******************************************************************************/

void
max7219__write_segments_on_digit
(
    max7219__digit_t    digit,
    max7219__segment_t  segments
){
    max7219__write(digit, segments);
}

/******************************************************************************
 * Private function definitions.
 ******************************************************************************/


/**************************************************************************//**
 * \fn void max7219__write(uint8_t address, uint8_t value)
 *
 * \brief Write value into MAX7219 register.
 *
 * \param       address Register address.
 * \param       value   Value to write.
 ******************************************************************************/

void
max7219__write
(
    uint8_t address,
    uint8_t value
){
    // Reset chip select pin to select the device.
    digital_io__set_pin_level(chip_select_pin, DIGITAL_IO__LEVEL__LOW);

    // tcss
    _delay_ms(1);

    // Transmit data to the device, register address and value.
    spi_master__transmit(address);
    spi_master__transmit(value);

    // Set chip select pin to valid data onto the device.
    digital_io__set_pin_level(chip_select_pin, DIGITAL_IO__LEVEL__HIGH);

    // tcsw
    _delay_ms(1);
}