126 lines
2.1 KiB
C++
126 lines
2.1 KiB
C++
|
/*
|
||
|
* max7219.cpp
|
||
|
*
|
||
|
* Created: 17-10-17 21:26:30
|
||
|
* Author: Adrien
|
||
|
*/
|
||
|
#define F_CPU 16000000UL
|
||
|
|
||
|
#include "max7219.h"
|
||
|
#include "util.h"
|
||
|
#include <assert.h>
|
||
|
#include <avr/io.h>
|
||
|
#include <util/delay.h>
|
||
|
#include <avr/cpufunc.h>
|
||
|
|
||
|
max7219::max7219(uint8_t din, uint8_t load, uint8_t clk, uint8_t nOfDigit)
|
||
|
{
|
||
|
this->din = pinArduinoToPINXX(din);
|
||
|
this->load = pinArduinoToPINXX(load);
|
||
|
this->clk = pinArduinoToPINXX(clk);
|
||
|
this->nOfDigit = nOfDigit;
|
||
|
pinDin = pinToPINX(din);
|
||
|
pinLoad = pinToPINX(load);
|
||
|
pinClk = pinToPINX(clk);
|
||
|
ddrDin = pinDin+1; ddrLoad=pinLoad+1;ddrClk=pinClk+1;
|
||
|
portDin = pinDin+2; portLoad=pinLoad+2;portClk=pinClk+2;
|
||
|
*ddrDin |= _BV(pinArduinoToPINXX(din));
|
||
|
*ddrLoad |= _BV(pinArduinoToPINXX(load));
|
||
|
*ddrClk |= _BV(pinArduinoToPINXX(clk));
|
||
|
setLoadTo(1);
|
||
|
setClkTo(0);
|
||
|
setDataTo(0);
|
||
|
_delay_ms(50);
|
||
|
sendRAWdata(nOfDigit, SCANLIMIT);
|
||
|
sendRAWdata((0xff),DECODEMODE);
|
||
|
on();
|
||
|
}
|
||
|
|
||
|
void max7219::sendRAWdata(volatile uint8_t data, volatile address ad)
|
||
|
{
|
||
|
int i = 0;
|
||
|
setClkTo(0);
|
||
|
setLoadTo(0);
|
||
|
_NOP();
|
||
|
_NOP();
|
||
|
|
||
|
for (i=0; i<8 ; i++)
|
||
|
{
|
||
|
setDataTo(ad&(0b10000000>>i));
|
||
|
_NOP();
|
||
|
_NOP();
|
||
|
setClkTo(1);
|
||
|
_delay_us(1);
|
||
|
setClkTo(0);
|
||
|
_NOP();
|
||
|
_NOP();
|
||
|
}
|
||
|
for (i=0; i<8 ; i++)
|
||
|
{
|
||
|
setClkTo(0);
|
||
|
setDataTo(data&(0b10000000>>i));
|
||
|
_NOP();
|
||
|
_NOP();
|
||
|
setClkTo(1);
|
||
|
_delay_us(1);
|
||
|
setClkTo(0);
|
||
|
_NOP();
|
||
|
_NOP();
|
||
|
}
|
||
|
setLoadTo(1);
|
||
|
_delay_us(1);
|
||
|
}
|
||
|
void max7219::on()
|
||
|
{
|
||
|
sendRAWdata(0x01, SHUTDOWN);
|
||
|
}
|
||
|
void max7219::off()
|
||
|
{
|
||
|
sendRAWdata(0x00, SHUTDOWN);
|
||
|
}
|
||
|
void max7219::sendDigitFromArray(uint8_t *array, uint8_t s)
|
||
|
{
|
||
|
sendRAWdata(0xFF, DECODEMODE);
|
||
|
for (int i=0;i<s;i++)
|
||
|
{
|
||
|
sendRAWdata(array[i],(address)(D1+i));
|
||
|
}
|
||
|
}
|
||
|
void max7219::setIntensity(const uint8_t i)
|
||
|
{
|
||
|
sendRAWdata(i, INTENSITY);
|
||
|
}
|
||
|
void max7219::setClkTo(uint8_t v)
|
||
|
{
|
||
|
if(v)
|
||
|
{
|
||
|
*portClk |= _BV(clk);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
*portClk &= ~(_BV(clk));
|
||
|
}
|
||
|
}
|
||
|
void max7219::setLoadTo(uint8_t v)
|
||
|
{
|
||
|
if(v)
|
||
|
{
|
||
|
*portLoad |= _BV(load);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
*portLoad &= ~(_BV(load));
|
||
|
}
|
||
|
}
|
||
|
void max7219::setDataTo(uint8_t v)
|
||
|
{
|
||
|
if(v)
|
||
|
{
|
||
|
*portDin |= _BV(din);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
*portDin &= ~(_BV(din));
|
||
|
}
|
||
|
}
|