Go Down

Topic: Shiftout 16bits data help.. (Read 4530 times) previous topic - next topic

doaway

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
Code: [Select]
// 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);
}

MAS3

Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

Grumpy_Mike

Code: [Select]
  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.

doaway


Code: [Select]
  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 ?

Code: [Select]
  for(int i = 0; i < 2; i++ ){
    shiftOut(Din, Clk, LSBFIRST, Data);

Arrch


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.

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?

Code: [Select]
  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);
}



Arrch

#6
Jul 22, 2013, 07:25 pm Last Edit: Jul 22, 2013, 09:15 pm by Arrch Reason: 1

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?

Grumpy_Mike

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

Makes no differance the code is still the same.

doaway


Quote
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)>.

Code: [Select]

// 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);
}

Arrch


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.

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...

Code: [Select]
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);
}

Arrch

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

doaway


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

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.

doaway




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.

Go Up