Pages: [1]   Go Down
Author Topic: ShiftOut producing unpredictable results  (Read 842 times)
0 Members and 1 Guest are viewing this topic.
Denmark
Offline Offline
God Member
*****
Karma: 15
Posts: 730
Please don't send questions as PM!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.
I am working on controlling a thermal printhead. Put simply, it's just a long shift register, you clock in data, flip a latch signal, enable heat for a second and you print a line. Rinse and repeat.

I tried making a test program with the ShiftOut function, but i get unpredictable results  smiley-eek

My code looks like this:

Code:
//**************************************************************//
//  Name    : Test code for Thermal printhead                   //
//****************************************************************

int ENPin = 3;
int clockPin = 4;
int dataPin = 5;
int Strobe1 = 2;                 // digital pin 2 connected to Strobe1

void setup() {
  
  pinMode(ENPin, OUTPUT);                                // sets the digital pin as output
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(Strobe1, OUTPUT);      
  
  digitalWrite(ENPin, HIGH);                            // Make sure all outputs are high
  digitalWrite(clockPin, HIGH);
  digitalWrite(dataPin, HIGH);
  digitalWrite(Strobe1, HIGH);
 
  
    shiftOut(dataPin, clockPin, LSBFIRST, B01010101);   // Clock out the 8 bits to the shift register
    
    digitalWrite(ENPin, LOW);                           // Toggle Latch on shift register
    delayMicroseconds(2);
    digitalWrite(ENPin, HIGH);
}

void loop() {

    digitalWrite(Strobe1, LOW);  // Strobe the heating elements
    delay(10);
    digitalWrite(Strobe1, HIGH);
    
    delay(100);
    
  }

And the outputs look like this when viewed with my logic analyser:


I have read the doc for ShiftOut and i cannot see why ENPin and Strobe1 is toggled at the start of the ShiftOut part of the program.

Thanks in advance

// Per.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8525
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Does it do the same thing if you send a second byte?

Maybe there's something peculiar about the first call.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13739
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Does the printhead needs a start bit? datasheet?

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Denmark
Offline Offline
God Member
*****
Karma: 15
Posts: 730
Please don't send questions as PM!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Does the printhead needs a start bit? datasheet?

Nope, it doesn't need any start bit. It's 100% "dumb"

Some excerts from datasheet:





You simply clock in the 448 bits and you're done.

// Per.

// Per.
Logged

Denmark
Offline Offline
God Member
*****
Karma: 15
Posts: 730
Please don't send questions as PM!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Does it do the same thing if you send a second byte?

Maybe there's something peculiar about the first call.
Tried to send the exact same data twice:
Code:
    shiftOut(dataPin, clockPin, LSBFIRST, B01010101);   // Clock out the 8 bits to the shift register
   
    shiftOut(dataPin, clockPin, LSBFIRST, B01010101);   // Clock out the 8 bits to the shift register

And this is the result:



I'm puzzled why is does this!

// Per.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8525
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So it is only on the first transmission. Something funny in the shiftOut() code maybe?

Code:
for (i = 0; i < 8; i++)  {
if (bitOrder == LSBFIRST)
digitalWrite(dataPin, !!(val & (1 << i)));
else
digitalWrite(dataPin, !!(val & (1 << (7 - i))));

digitalWrite(clockPin, HIGH);
digitalWrite(clockPin, LOW);
}

Nope, that's all it does.

As a work around, how about doing the first shiftOut() to an unused pin (or the same pins before you make them outputs), then start talking to your device.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Pages: [1]   Go Up
Jump to: