Go Down

Topic: ShiftOut producing unpredictable results (Read 998 times) previous topic - next topic

Zapro

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

Graynomad

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

Maybe there's something peculiar about the first call.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

robtillaart


Does the printhead needs a start bit? datasheet?

Rob Tillaart

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

Zapro


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.

Zapro


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

Graynomad

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

Code: [Select]
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
Rob Gray aka the GRAYnomad www.robgray.com

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy