12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- /* 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 <user_config.h>
- #include <SmingCore/SmingCore.h>
- #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 reg, uint8_t data)
- {
- MAX7219_LOAD0; // îïóñòèëè çàù¸ëêó
- // spi_SendByte(reg);
- // spi_SendByte(data);
- // software spi
- uint8_t i; // ñ÷åò÷èê áèò
- uint16_t sdata = (reg<<8) || data;
- for (i=16; i!=0; i--) {
- PinRes(PIN_CLK); // âûäàëè ñòðîá
- if (bit_is_set(sdata,15)) { // åñëè ñòàðøèé áèò == 1
- PinSet(PIN_DIN); // MOSI = 1
- } else { // åñëè ñòàðøèé áèò == 0
- PinRes(PIN_DIN); // MOSI = 0
- }
- PinSet(PIN_CLK); // çàù¸ëêíóëè ñòðîá
- sdata <<= 1; // ñäâèã âëåâî íà 1 áèò
- }
- 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); // âêëþ÷èëè ïèòàíèå
- }
|