[C++] 纯文本查看 复制代码
#include <FrequencyTimer2.h>
#define NOP() do { __asm__ __volatile__ ("nop"); } while (0)
#define _countof(a) (sizeof(a) / sizeof(a[0]))
class HC595LED
{
public:
HC595LED(int ser, int rck, int srck)
{
_SER = ser;
_RCK = rck;
_SRCK = srck;
}
void begin()
{
pinMode(_SER, OUTPUT);
pinMode(_RCK, OUTPUT);
pinMode(_SRCK, OUTPUT);
digitalWrite(_SER, LOW);
digitalWrite(_RCK, LOW);
digitalWrite(_SRCK, LOW);
}
void displayRow(uint8_t nRow, uint8_t data)
{
nRow = 7 - nRow;
for(int i = 0; i < 8; i++)
_rowEnable(i == nRow);
for(int i = 7; i >= 0; i--) {
_colEnable(bitRead(data, i));
}
flushScreen();
}
void clear()
{
for(int i = 0; i < 8; i++)
_rowEnable(false);
for(int i = 0; i < 8; i++)
_colEnable(false);
}
void writeItem(bool bHigh)
{
digitalWrite(_SER, bHigh ? HIGH : LOW);
_flushItem();
}
void flushScreen()
{
digitalWrite(_RCK, HIGH);
NOP();
NOP();
digitalWrite(_RCK, LOW);
}
private:
void _rowEnable(bool bEnable)
{
writeItem(!bEnable);
}
void _colEnable(bool bEnable)
{
writeItem(bEnable);
}
void _flushItem()
{
digitalWrite(_SRCK, HIGH);
NOP();
NOP();
digitalWrite(_SRCK, LOW);
}
private:
int _SER;
int _RCK;
int _SRCK;
};
///////////////////////////////////////////////////////////////////////
//Pin connected to DS of 74HC595
const int SER_PIN = 8;
//Pin connected to ST_CP of 74HC595
const int RCK_PIN = 9;
//Pin connected to SH_CP of 74HC595
const int SRCK_PIN = 10;
HC595LED led(SER_PIN, RCK_PIN, SRCK_PIN);
PROGMEM const char SPACE[] =
{
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
};
PROGMEM const char H[] =
{
0b01000010,
0b01000010,
0b01000010,
0b01111110,
0b01000010,
0b01000010,
0b01000010,
0b01000010,
};
PROGMEM const char E[] =
{
0b01111110,
0b01000000,
0b01000000,
0b01111110,
0b01000000,
0b01000000,
0b01000000,
0b01111110,
};
PROGMEM const char L[] =
{
0b01000000,
0b01000000,
0b01000000,
0b01000000,
0b01000000,
0b01000000,
0b01000000,
0b01111110,
};
PROGMEM const char O[] =
{
0b00011000,
0b00100100,
0b01000010,
0b01000010,
0b01000010,
0b01000010,
0b00100100,
0b00011000,
};
const char * const arrShowData[] = {H, E, L, L, O, SPACE};
char arrShowBuffer[] =
{
0, 0, 0, 0, 0, 0, 0, 0,
};
void display()
{
static int s_nIndex = 0;
led.displayRow(s_nIndex, arrShowBuffer[s_nIndex]);
s_nIndex++;
s_nIndex %= 8;
}
void setup()
{
led.begin();
Serial.begin(9600);
delay(1000);
led.clear();
FrequencyTimer2::disable();
FrequencyTimer2::setPeriod(2000);
FrequencyTimer2::setOnOverflow(display);
}
void loop()
{
int s_nIndex = 0;
while(true) {
noInterrupts();
memcpy_PF(arrShowBuffer, (uint_farptr_t)arrShowData[s_nIndex], sizeof(arrShowBuffer));
interrupts();
s_nIndex++;
s_nIndex %= _countof(arrShowData);
delay(2000);
}
}