That will only work correctly when mask == 1. For all other values of mask (2, 4, 8, etc) the results of (mask & 1) is 0.
Yeah I noticed that. Can anyone recommend a very small SD library?
BTW here's my devastated new LCD library.
If you notice I've been trying to use SPI to replace write4Bits. If anyone has any suggestions for how to do this...
#include "MyLiquidCrystal.h"
#include "Arduino.h"
LiquidCrystal::LiquidCrystal(){
SPI.begin();
initSPI();
init();
begin();
}
void LiquidCrystal::initSPI(){
SPI.setClockDivider(SPI_CLOCK_DIV2);
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
}
void LiquidCrystal::init(){
_rs_pin = 1;
_rw_pin = 255;
_enable_pin = 3;
_data_pins[0] = 4;
_data_pins[1] = 5;
_data_pins[2] = 6;
_data_pins[3] = 7;
pinMode(_rs_pin, OUTPUT);
pinMode(_enable_pin, OUTPUT);
}
void LiquidCrystal::begin() {
// SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
// according to datasheet, we need at least 40ms after power rises above 2.7V
// before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50
delayMicroseconds(50000);
// Now we pull both RS and R/W low to begin commands
digitalWrite(_rs_pin, LOW);
digitalWrite(_enable_pin, LOW);
//put the LCD into 4 bit mode
// this is according to the hitachi HD44780 datasheet
// figure 24, pg 46
// we start in 8bit mode, try to set 4 bit mode
/*_bitString=3;
spiSendOut();
pulseEnable();*/
write4bits(0x03);
delayMicroseconds(4500); // wait min 4.1ms
// second try
write4bits(0x03);
delayMicroseconds(4500); // wait min 4.1ms
// third go!
write4bits(0x03);
delayMicroseconds(150);
// finally, set to 4-bit interface
write4bits(0x02);
// finally, set # lines, font size, etc.
command(LCD_FUNCTIONSET | LCD_2LINE);
// turn the display then clear
command(LCD_DISPLAYCONTROL | LCD_DISPLAYON);
command(LCD_CLEARDISPLAY);
}
/********** high level commands, for the user! */
void LiquidCrystal::home()
{
command(LCD_SETDDRAMADDR);
}
/*********** mid level commands, for sending data/cmds */
inline void LiquidCrystal::command(uint8_t value) {
send(value, LOW);
}
inline size_t LiquidCrystal::write(uint8_t value) {
send(value, HIGH);
return 1; // assume sucess
}
/************ low level data pushing commands **********/
// write either command or data, with automatic 4/8-bit selection
void LiquidCrystal::send(uint8_t value, uint8_t mode) {
bitWrite(_bitString, _rs_pin, mode); //set RS to mode
spiSendOut();
//we are not using RW with SPI so we are not even bothering
//or 8BITMODE so we go straight to write4bits
write4bits(value>>4);
write4bits(value);
}
void LiquidCrystal::pulseEnable(void) {
bitWrite(_bitString, _enable_pin, LOW);
spiSendOut();
delayMicroseconds(1);
bitWrite(_bitString, _enable_pin, HIGH);
spiSendOut();
delayMicroseconds(1); // enable pulse must be >450ns
bitWrite(_bitString, _enable_pin, LOW);
spiSendOut();
delayMicroseconds(40); // commands need > 37us to settle
}
void LiquidCrystal::write4bits(byte value) {
for (byte i = 4; i < 8; i++)
{
//we put the four bits in the _bit_string
bitWrite(_bitString, i, ((value >> (i - 4)) & 0x01));
}
//and send it out
spiSendOut();
pulseEnable();
}
void LiquidCrystal::spiSendOut() //SPI #############################
{
initSPI();
digitalWrite(_latchPin, LOW);
SPI.transfer(_bitString);
digitalWrite(_latchPin, HIGH);
}