Shiftout 16bits data help..

Hi experts,

I am try to shift out 16bits data when PGb is low and Vpg is high. Now have no ideal how to code the data and shift out.
Any suggesting? And also try to switch back PGb to High and Vpg to Low forever after PGb stay on Low for 2s.
Thanks

// Name : Temp Sensor Code

  int PGb = 8;  //latchPinB
  int Clk = 12;
  int Din = 11;
  int Dout = 10;
  int Vpg = 9;
  int Data = AA55;  // 16 bits data  10101010,01010101 ??

void setup(){
  pinMode(PGb, OUTPUT);
  pinMode(Clk, OUTPUT);
  pinMode(Din, OUTPUT);
  pinMode(Vpg, OUTPUT);
  pinMode(Dout, INPUT);  
}

void loop(){
  digitalWrite(PGb, HIGH);
  digitalWrite(Vpg, LOW);

  for(int i = 0; i < 16; i++ ){
    shiftOut(Din, Clk, LSBFIRST, Data);
  }
  digitalWrite(PGb, LOW);
  digitalWrite(Vpg, HIGH);
  delay(2000);
    digitalWrite(PGb, HIGH);
  digitalWrite(Vpg, LOW);
}

Did you have a look at Nick Gammon's pages?

  for(int i = 0; i < 16; i++ ){
    shiftOut(Din, Clk, LSBFIRST, Data);

That shifts out 16 bytes not 16 bits. Each call to shift out does 8 bits.

Grumpy_Mike:

  for(int i = 0; i < 16; i++ ){

shiftOut(Din, Clk, LSBFIRST, Data);



That shifts out 16 bytes not 16 bits. Each call to shift out does 8 bits.

How to shift 16 bits? Like this? and int Data = AA55; Is it right ?

  for(int i = 0; i < 2; i++ ){
    shiftOut(Din, Clk, LSBFIRST, Data);

doaway: Is it right ?

No. shiftOut accepts a byte of data, not an int. You need to use highByte() and lowByte() to extract the bytes from an int.

Arrch:

doaway:
Is it right ?

No. shiftOut accepts a byte of data, not an int. You need to use highByte() and lowByte() to extract the bytes from an int.

I use a single 16bit shift register not 2 of 74HC595. Any suggestion?

  int PGb = 8;  //latchPinB
  int Clk = 12;
  int Din = 11;
  int Dout = 10;
  int Vpg = 9;
  byte Data0 = B10101010;  // 16 bits data 
  byte Data1 = B01010101;

void setup(){
  pinMode(PGb, OUTPUT);
  pinMode(Clk, OUTPUT);
  pinMode(Din, OUTPUT);
  pinMode(Vpg, OUTPUT);
  pinMode(Dout, INPUT);  
}

void loop(){
  digitalWrite(PGb, HIGH);
  digitalWrite(Vpg, LOW);

  for(int i = 0; i < 8; i++ ){
    shiftOut(Din, Clk, LSBFIRST, Data0);
    shiftOut(Din, Clk, LSBFIRST, Data1);
  }
  digitalWrite(PGb, LOW);
  digitalWrite(Vpg, HIGH);
  delay(2000);
  digitalWrite(PGb, HIGH);
  digitalWrite(Vpg, LOW);
}

doaway: I use a single 16bit shift register not 2 of 74HC595. Any suggestion?

With what? Does that code work? If not, how so? What are you expecting to do, and how does that differ from what it's doing?

I use a single 16bit shift register not 2 of 74HC595. Any suggestion?

Makes no differance the code is still the same.

Grumpy_Mike:

I use a single 16bit shift register not 2 of 74HC595. Any suggestion?

Makes no differance the code is still the same.

Thanks, I think using SPI is more fast, but the default 4MHz is too fast for me.
I try to divide the clock and data(?) by 20, so it runs at 100KHz.

However, there is an error <no matching function for call to SPIClass::begin(int)>.

// CLK - digital pin 13
// Din - digital pin 11 <MOSI pin>
// MISO - digital pin 12
// Vpg - digital pin 8
// PGb - digital pin 10

#include <SPI.h>
const int PGb = 10;  // Set Slave select pin 10
const int Vpg = 8; // Set Programming pin 12
const int Dout = 9;

void setup(){
  pinMode (PGb, OUTPUT);
  pinMode (Dout, INPUT);
  SPI.begin(13);   // error here
  SPI.begin(11);
  SPI.setClockDivider(11,20);
  SPI.setClockDivider(13,20);
  
}

void loop(){
  digitalWrite(PGb, HIGH);
  digitalWrite(Vpg, LOW);
  SPI.transfer(0xAA);   // transfer second 8 bits
  SPI.transfer(0x55);  // transfer first 8 bits
  digitalWrite(PGb, LOW);
  digitalWrite(Vpg, HIGH);
  delay(2000);
  //digitalWrite(PGb, HIGH);
  //digitalWrite(Vpg, LOW);
}

doaway:
However, there is an error <no matching function for call to SPIClass::begin(int)>.

That’s a pretty clear error message; the begin function doesn’t accept an int argument (Assuming you are not using a Due), so why are you trying to pass it one.

Arrch:

doaway:
However, there is an error <no matching function for call to SPIClass::begin(int)>.

That’s a pretty clear error message; the begin function doesn’t accept an int argument (Assuming you are not using a Due), so why are you trying to pass it one.

Yes, I am using uno. Here is an example on Arduino for Due, but no uno…

void setup(){
  // initialize the bus for the device on pin 4
  SPI.begin(4);
  // Set clock divider on pin 4 to 21
  SPI.setClockDivider(4, 21); 
  // initialize the bus for the device on pin 10
  SPI.begin(10);
  // Set clock divider on pin 10 to 84
  SPI.setClockDivider(10, 84);
}

Let me rephrase, what do you think the number that you are passing to begin() is supposed to be for?

Arrch: Let me rephrase, what do you think the number that you are passing to begin() is supposed to be for?

The pin number, like clk @ pin 13

doaway:

Arrch: Let me rephrase, what do you think the number that you are passing to begin() is supposed to be for?

The pin number, like clk @ pin 13

You can't specify the clock pin when using the hardware SPI. Uno's clock pin is already 13.

Arrch:

doaway:

Arrch: Let me rephrase, what do you think the number that you are passing to begin() is supposed to be for?

The pin number, like clk @ pin 13

You can't specify the clock pin when using the hardware SPI. Uno's clock pin is already 13.

Thanks. I connect the pin12(shiftout CLK_pin) to oscilloscope and expect waveform shown on screen. But there is nothing on that pin. Do not know why.

Can you post your current code? The previous one didn't compile, right?

[quote author=Nick Gammon link=topic=178396.msg1329583#msg1329583 date=1374786797] Can you post your current code? The previous one didn't compile, right? [/quote] Hi Nick, I want to shift 16 bits data to a 16 bit series in-parallel out shift register. Here is my code, I connect pin12 to oscillator, pin11 to multimeter, I did not see a waveform on the oscillator and 0 voltage on multimeter.

Maybe my code does not work.

  int PGb = 8;  //latchPinB
  int Clk = 12;
  int Din = 11;
  int Dout = 10;
  int Vpg = 9;
 // byte Data0 = B00000000;  // 16 bits data 
//  byte Data1 = B11111111;
  int Data = 500;

void setup(){
  pinMode(PGb, OUTPUT);
  pinMode(Clk, OUTPUT);
  pinMode(Din, OUTPUT);
  pinMode(Vpg, OUTPUT);
  pinMode(Dout, INPUT); 
  shift(); 
}

void shift(){
  digitalWrite(PGb, HIGH);
  digitalWrite(Vpg, LOW);
  shiftOut(Din, Clk, LSBFIRST, Data);
  shiftOut(Din, Clk, LSBFIRST, (Data>>8));
  digitalWrite(PGb, LOW);
  digitalWrite(Vpg, HIGH);
  delay(1000);
  digitalWrite(PGb, HIGH);
  digitalWrite(Vpg, LOW); 
}

void loop(){

}

I ran your code and got results. I moved the call to shift() to be inside loop rather than setup, so it would output repeatedly.

Have no idea how long will the CLK will lasting. I want the CLK run forever and only shift 16 bits data once.

[quote author=Nick Gammon link=topic=178396.msg1329622#msg1329622 date=1374788956] I ran your code and got results. I moved the call to shift() to be inside loop rather than setup, so it would output repeatedly.

[/quote]

Thanks, what software do you use to show the output ?