Ir ao conteúdo
  • Cadastre-se

Registrador de Deslocamento Arduino


Posts recomendados

Galera boa noite, estou tentando trabalhar com expansão de portas em um Arduíno, só que a maior parte do conteúdo que achei na net trabalha com as entradas ou saídas todas juntas, eu estou precisando trabalhar com cada uma separadamente, estou tentando fazer algumas modificação em uma programação que eu achei no Arduino.cc mas ta me complicando 

a principio é um registrador de deslocamento, que esta funcionando em PISO, (paralela input, serial output)

o meu maior problema é que,  este programa le e guarda qual botão que foi pressionado e imprime, porém ao invés de imprimir quero correlacionar o botão de entrada precionado com uma saída do arduino

exemplo :

Quando botão do registrador 1 for precionado ligar saida 1

espero que alguem consiga me ajudar...

 

 

/define where your pins are
int latchPin = 8;
int dataPin = 9;
int clockPin = 7;

//Define variables to hold the data 
//for shift register.
//starting with a non-zero numbers can help
//troubleshoot
byte switchVar1 = 72;  //01001000

//define an array that corresponds to values for each 
//of the shift register's pins
char note2sing[] = {
  'C', 'd', 'e', 'f', 'g', 'a', 'b', 'c'}; 


void setup() {
  //start serial
  Serial.begin(9600);

  //define pin modes
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT); 
  pinMode(dataPin, INPUT);

}

void loop() {

  //Pulse the latch pin:
  //set it to 1 to collect parallel data
  digitalWrite(latchPin,1);
  //set it to 1 to collect parallel data, wait
  delayMicroseconds(20);
  //set it to 0 to transmit data serially  
  digitalWrite(latchPin,0);

  //while the shift register is in serial mode
  //collect each shift register into a byte
  //the register attached to the chip comes in first 
  switchVar1 = shiftIn(dataPin, clockPin);

  //Print out the results.
  //leading 0's at the top of the byte 
  //(7, 6, 5, etc) will be dropped before 
  //the first pin that has a high input
  //reading  
  Serial.println(switchVar1, BIN);


  //This for-loop steps through the byte
  //bit by bit which holds the shift register data 
  //and if it was high (1) then it prints
  //the corresponding location in the array
  for (int n=0; n<=7; n++)
  {
    //so, when n is 3, it compares the bits
    //in switchVar1 and the binary number 00001000
    //which will only return true if there is a 
    //1 in that bit (ie that pin) from the shift
    //register.
    if (switchVar1 & (1 << n) ){
      //print the value of the array location
      Serial.println(note2sing[n]);
    }
  }

//This following is a away to examine the whole
//byte at once and create combinations
//of settings.

//By passing the switchVar1 variable to
//a "switch" statement and comparing it against
//a set nemerical value (written in binary)
//you can create special cases

  switch (switchVar1) {
  case B00101010:
    Serial.println("D minor");
    break;
  case B00010101:
    Serial.println("C major");
    break;
  case B01010100:
    Serial.println("E minor");
    break;
  case B00101001:
    Serial.println("F major");
    break;    
  case B01010010:
    Serial.println("G major");
    break;        
  case B00100101:
    Serial.println("A minor");
    break;     
  case B01001010:
    Serial.println("B diminished");
    break;     
  default: 
    // if nothing else matches, do the default
    Serial.println("Play It, Joe"); 
  }

//white space
Serial.println("-------------------");
//delay so all these print satements can keep up. 
delay(500);

}

//------------------------------------------------end main loop

////// ----------------------------------------shiftIn function
///// just needs the location of the data pin and the clock pin
///// it returns a byte with each bit in the byte corresponding
///// to a pin on the shift register. leftBit 7 = Pin 7 / Bit 0= Pin 0

byte shiftIn(int myDataPin, int myClockPin) { 
  int i;
  int temp = 0;
  int pinState;
  byte myDataIn = 0;

  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, INPUT);
//we will be holding the clock pin high 8 times (0,..,7) at the
//end of each time through the for loop

//at the begining of each loop when we set the clock low, it will
//be doing the necessary low to high drop to cause the shift
//register's DataPin to change state based on the value
//of the next bit in its serial information flow.
//The register transmits the information about the pins from pin 7 to pin 0
//so that is why our function counts down
  for (i=7; i>=0; i--)
  {
    digitalWrite(myClockPin, 0);
    delayMicroseconds(0.2);
    temp = digitalRead(myDataPin);
    if (temp) {
      pinState = 1;
      //set the bit to 0 no matter what
      myDataIn = myDataIn | (1 << i);
    }
    else {
      //turn it off -- only necessary for debuging
     //print statement since myDataIn starts as 0
      pinState = 0;
    }

    //Debuging print statements
    //Serial.print(pinState);
    //Serial.print("     ");
    //Serial.println (dataIn, BIN);

    digitalWrite(myClockPin, 1);

  }
  //debuging print statements whitespace
  //Serial.println();
  //Serial.println(myDataIn, BIN);
  return myDataIn;
}

 

 

 

aaaaa.png

Link para o comentário
Compartilhar em outros sites

Dá para você preparar o byte toda vez antes de enviar, dai você configura o pino desejado e então envia o byte, tipo:

#define Pino0 0b00000001
#define Pino1 0b00000010
#define Pino2 0b00000100
#define Pino3 0b00001000
#define Pino4 0b00010000
#define Pino5 0b00100000
#define Pino6 0b01000000
#define Pino7 0b10000000

byte SaidasPinos07=0;

// Assim vamos supor que vocês quer setar uma saída:
SaidasPinos07 |= Pino0; // seta o pino 0
//Aqui vai o código para enviar byte para as saídas expandidas com estado atualizado

// E quando você quiser zerar uma saída
SaidasPinos07 &= ~Pino7; // zera o pino 7
//Aqui vai o código para enviar byte para as saídas expandidas com estado atualizado


Se for entradas você pode fazer o seguinte:

#define Pino0 0b00000001
#define Pino1 0b00000010
#define Pino2 0b00000100
#define Pino3 0b00001000
#define Pino4 0b00010000
#define Pino5 0b00100000
#define Pino6 0b01000000
#define Pino7 0b10000000
  
byte EntradasPinos07=0;

// Verificando o estado de uma entrada
//Aqui vai o código para pegar o byte para as entradas expandidas (do Pino0 ao Pino7)
if(EntradasPinos07 & Pino0)
   // Pino0 = 1 -> Pino zero em nível lógico alto 5V (Ard. UNO)
else
   // Pino0 = 0 -> Pino zero em nível lógico baixo 0V

 

Link para o comentário
Compartilhar em outros sites

Visitante
Este tópico está impedido de receber novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...