/* max7219.cpp * MAX7219 Interaction Code * --------------------------- * For more information see * http://www.adnbr.co.uk/articles/max7219-and-7-segment-displays * * Created on: 29 січ. 2016 * Author: shilov */ #include #include #include "configuration.h" #include "max7219.h" // защёлка #define MAX7219_LOAD1 PinSet(PIN_LOAD) #define MAX7219_LOAD0 PinRes(PIN_LOAD) #define MAX7219_ON 0x01 #define MAX7219_OFF 0x00 #define MAX7219_BRIGHT 0x08 // перевод числа 0-7 в номер индикатора const uint8_t max7219_dig[8] = { 0x05,0x01,0x07,0x03,0x04,0x08,0x06,0x02 }; // программный SPI. Выводит байт начиная со старшего бита // выходные пины -- SCK, MOSI static void spi_SendByte (uint8_t DataByte) { uint8_t i; // счетчик бит for (i=8; i!=0; i--) { PinRes(PIN_CLK); // выдали строб if (bit_is_set(DataByte,7)) { // если бит 7 == 1 PinSet(PIN_DIN); // MOSI = 1 } else { // если бит 7 == 0 PinRes(PIN_DIN); // MOSI = 0 } PinSet(PIN_CLK); // защёлкнули строб DataByte <<= 1; // сдвиг влево на 1 бит } } void MAX7219_writeData(uint8_t data_register, uint8_t data) { MAX7219_LOAD0; // Send the register where the data will be stored spi_SendByte(data_register); // Send the data to be stored spi_SendByte(data); MAX7219_LOAD1; } void MAX7219_Init(void) { // настройка пинов SPI pinMode(PIN_LOAD, OUTPUT); pinMode(PIN_DIN, OUTPUT); pinMode(PIN_CLK, OUTPUT); PinSet(PIN_LOAD); PinSet(PIN_DIN); PinSet(PIN_CLK); // Настройка MAX71219 MAX7219_writeData(MAX7219_MODE_DECODE, 0x00); // все без BCD декодирования MAX7219_writeData(MAX7219_MODE_SCAN_LIMIT, MAX7219_DIGITS - 1); // Scan limit runs from 0. MAX7219_writeData(MAX7219_MODE_INTENSITY, MAX7219_BRIGHT); // яркость из 16 MAX7219_writeData(MAX7219_MODE_POWER,MAX7219_ON); // включили питание }