inital version
This commit is contained in:
43
shiftreg.h
Normal file
43
shiftreg.h
Normal file
@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
#include <util/delay.h>
|
||||
template <int BITS> class ShiftReg
|
||||
{
|
||||
private:
|
||||
static constexpr int BYTES = (BITS % 8 == 0) ? (BITS/8) : (BITS/8+1);
|
||||
|
||||
volatile unsigned char *_port;
|
||||
const unsigned char _pinSer;
|
||||
const unsigned char _pinSerClk;
|
||||
const unsigned char _pinRClk;
|
||||
|
||||
public:
|
||||
|
||||
ShiftReg(volatile unsigned char* const port, const unsigned char pinSer, const unsigned char pinSerClk, const unsigned char pinRClk):
|
||||
_port(port), _pinSer(pinSer), _pinSerClk(pinSerClk), _pinRClk(pinRClk)
|
||||
{
|
||||
clear();
|
||||
}
|
||||
void write(const unsigned char * const in)
|
||||
{
|
||||
*_port &= ~((1<<_pinSer) | (1<<_pinSerClk));
|
||||
*_port |= 1<<_pinRClk;
|
||||
for(unsigned char i = 0; i < BITS; ++i)
|
||||
{
|
||||
*_port &= ~(1 << _pinSerClk);
|
||||
in[i/8] & (1<<(i%8)) ? (*_port |= (1 << _pinSer)) : (*_port &= ~(1 << _pinSer));
|
||||
*_port |= (1 << _pinSerClk);
|
||||
}
|
||||
*_port &= ~(1<<_pinRClk);
|
||||
}
|
||||
void clear()
|
||||
{
|
||||
*_port &= ~((1<<_pinSer) | (1<<_pinSerClk));
|
||||
*_port |= 1<<_pinRClk;
|
||||
for(unsigned char i = 0; i < BITS; ++i)
|
||||
{
|
||||
*_port &= ~(1 << _pinSerClk);
|
||||
*_port |= (1 << _pinSerClk);
|
||||
}
|
||||
*_port &= ~(1<<_pinRClk);
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user