GY-63_MS5611/libraries/FastShiftOut/FastShiftOut.cpp

112 lines
2.6 KiB
C++
Raw Normal View History

2013-08-28 16:30:00 -04:00
//
// FILE: FastShiftOut.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.5
// PURPOSE: shiftout that implements the Print interface
// DATE: 2013-08-22
2013-08-28 16:30:00 -04:00
// URL:
//
// Released to the public domain
//
// HISTORY
// 0.1.5 - changed masking in inner loop
//
2013-08-28 16:30:00 -04:00
#include "FastShiftOut.h"
//
// Constructor
// prepares the digitalWrite()
FastShiftOut::FastShiftOut(const uint8_t datapin, const uint8_t clockpin, const uint8_t bitOrder)
2013-08-28 16:30:00 -04:00
{
_bitorder = bitOrder;
_value = -1;
pinMode(datapin, OUTPUT);
pinMode(clockpin, OUTPUT);
// uint8_t _datatimer = digitalPinToTimer(datapin);
// if (_datatimer != NOT_ON_TIMER) turnOffPWM(_datatimer); TODO
uint8_t _dataport = digitalPinToPort(datapin);
2013-08-28 16:30:00 -04:00
_dataout = portOutputRegister(_dataport);
_databit = digitalPinToBitMask(datapin);
// uint8_t _clocktimer = digitalPinToTimer(clockpin);
2013-08-28 16:30:00 -04:00
// if (_clocktimer != NOT_ON_TIMER) turnOffPWM(_clocktimer);
uint8_t _clockport = digitalPinToPort(clockpin);
2013-08-28 16:30:00 -04:00
_clockout = portOutputRegister(_clockport);
_clockbit = digitalPinToBitMask(clockpin);
2013-08-28 16:30:00 -04:00
}
//
// write() must implement the virtual write of Print class
//
// approx 30 us/byte
size_t FastShiftOut::write(const uint8_t data)
2013-08-28 16:30:00 -04:00
{
_value = data;
for (uint8_t i = 0, m = 1, n = 128; i < 8; i++, m <<= 1, n >>= 1)
2013-08-28 16:30:00 -04:00
{
uint8_t v;
if (_bitorder == LSBFIRST) v = (_value & m);
else v = (_value & n);
2013-08-28 16:30:00 -04:00
uint8_t oldSREG = SREG;
cli();
if (v == 0) *_dataout &= ~_databit;
else *_dataout |= _databit;
2013-08-28 16:30:00 -04:00
*_clockout |= _clockbit;
*_clockout &= ~_clockbit;
SREG = oldSREG;
}
return 1;
2013-08-28 16:30:00 -04:00
}
//
// this version is twice as fast,
// but it is in CLI() mode
// approx 32us / byte
// size_t FastShiftOut::write(uint8_t data)
// {
// _value = data;
// // prep masks
// uint8_t dm1 = *_dataout | _databit;
// uint8_t dm0 = *_dataout & ~_databit;
// uint8_t cm1 = *_clockout | _clockbit;
// uint8_t cm0 = *_clockout & ~_clockbit;
// uint8_t oldSREG = SREG;
// cli();
// if (_bitorder == LSBFIRST)
// {
// for (uint8_t m = 0x01; m != 0x80; m <<= 1)
// {
// if (_value & m) *_dataout = dm1;
// else *_dataout = dm0;
// *_clockout = cm1;
// *_clockout = cm0;
// }
// }
// else
// {
// for (uint8_t m = 0x80; m > 0; m >>= 1)
// {
// if (_value & m) *_dataout = dm1;
// else *_dataout = dm0;
// *_clockout = cm1;
// *_clockout = cm0;
// }
// }
// SREG = oldSREG;
// return 1;
// }
//
// reads back the last value written.
//
2013-08-28 16:30:00 -04:00
int FastShiftOut::read()
{
return _value;
}
// -- END OF FILE --