/*
NLSF595 Arduino Shift Register example for Wokwi
Copyright (C) 2021, Uri Shaked
License: MIT.
*/
//===============================================
/* Usando registrador de deslocamento
//===============================================
const int dataPin = 2; // SI
const int clockPin = 3; // SCK
const int latchPin = 4; // RCK
void setup() {
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
}
void sendData(uint8_t pattern) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, pattern); //MSBFIRST ou LSBFIRST
digitalWrite(latchPin, HIGH);
}
void loop() {
// Red
sendData(0b11011011); //Envia o LSB primeiro
delay(500);
// Green
sendData(0b10110111);
delay(500);
// Purple
sendData(0b01001011);
delay(500);
}
//===============================================
// Fim - Usando registrador de deslocamento
//=============================================*/
//===============================================
//* Usando hardware SPI
//===============================================
#include <SPI.h>
//https://docs.arduino.cc/learn/communication/spi
//CS - pino 10 - RCL (register clock)
//MOSI - pino 11 - SI (serial input)
//MISO - pino 12
//CLK - pino 13 - SCL (shift clock)
//Mode Clock Polarity (CPOL) Clock Phase (CPHA) Output Edge Data Capture
//SPI_MODE0 0 0 Falling Rising
//SPI_MODE1 0 1 Rising Falling
//SPI_MODE2 1 0 Rising Falling
//SPI_MODE3 1 1 Falling Rising
//Is data shifted in:
//MSBFIRST Most Significant Bit (MSB) first
//LSBFIRST Least Significant Bit (LSB) first
const int slaveSelectPin = 10;
void setup() {
pinMode(slaveSelectPin, OUTPUT);
//Inicializa a SPI
//Modo básico padrão
SPI.begin();
//Modo comppleto - SPISettings mySetting(speedMaximum, dataOrder, dataMode)
//SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
//SPI.beginTransaction(SPISettings(1000000, LSBFIRST, SPI_MODE0)); //Não auterou o código
//Neste modo, por razões desconhecidas o clock voltava para nível alto alguns microssegundos após encerrar o dado
}
void sendData(uint8_t dado) {
// take the SS pin low to select the chip:
digitalWrite(slaveSelectPin, LOW); //Está no Latch Clock
delayMicroseconds(20);
// send in the address and value via SPI:
SPI.transfer(dado);
delayMicroseconds(20);
// take the SS pin high to de-select the chip:
digitalWrite(slaveSelectPin, HIGH);
}
void loop() {
// Red
//sendData(0b11011011);
sendData(0b11011011); //DBh Envia MSB primeiro
delay(500); //Comentar para análise-lógica no pulse
delayMicroseconds(20);
// Green
//sendData(0b10110111);
sendData(0b11101101); //EDh
delay(500); //Comentar para análise-lógica no pulse
delayMicroseconds(20);
// Purple
//sendData(0b01001011);
sendData(0b11110110); //F6h
delay(500); //Comentar para análise-lógica no pulse
delayMicroseconds(20);
}
//===============================================
// Fim - Usando hardware SPI
//=============================================*/