Monday, May 05, 2014

A little more shifty....

Recap

In the previous post, I quickly reviewed the techniques for interfacing shift registers to Arduino based hardware and highlighted some simple code for reading and writing to the external shift registers.

Whilst to some, this may have appeared to be a whistle-stop tour, there is however a wealth of similar information available online and I felt that it would have been of little overall value just repeating the excellent examples and documentation provided by others.

Instead I wanted to get something up and running quickly which would extend the Arduino and act as the basis of a variety of projects, requiring additional input output devices such as LEDs, switches, and actuators such as stepper motors and hobby servos.

I am using the SIMPL language to control the shift registers.  If you have not come across SIMPL yet - please see my related blog post, and those going back to May of 2013.

Output to Shift Registers

With my recent experiments with shift registers, I wanted to send a 16 bit word out to a pair of output shift registers to selectively illuminate an array of LEDs.

In SIMPL, I chose the lower case ascii character "r" to represent this output to register function using the following Arduino code:

      case 'r':            // Send an 16 bit byte to the shift register using shiftOut
   
      PORTB &= ~_BV(0);
      shiftOut(dataPin, clockPin, MSBFIRST, x,8);      // shift out the high bits:
      shiftOut(dataPin, clockPin, MSBFIRST, x);        // shift out the low bits:
      PORTB |= _BV(0);
      break;

To use this function, you need to supply the 16 bit integer x immediately preceding the r character,

so 0r will turn off all the LEDs and 65535r will turn them all on.

Light Chasers

These are very simple to set up just by turning on the LEDs in a sequence. SIMPL is ideal for this.

We have a SIMPL command "r" which does a 16 bit write to the pair of shift registers.

128r will turn on LED 7, 64r will turn on LED 6 and so on.  We can use the millisecond command m, to keep the LEDs lit for, say 100mS.

128r100m64r100m and so on.

So let's define a LED sequence that counts down from the left - ie moves to the Right. We do this using the colon to start a definition - called R

:R 128r100m64r100m32r100m16r100m8r100m4r100m2r100m1r100m

And similarly a LED sequence that moves to the Left

:L 1r100m2r100m4r100m8r100m16r100m32r100m64r100m128r100

Now you can use L and R to make chase sequences

10{LR}  - 10 iterations of the Left-right chaser sequence

Stepper Motors

From creating a moving light chaser display, it is a very simple step to controlling the motion of a stepper motor.  Rather than turning the LEDs on in a sequence, it is simply a matter of energising the windings of the stepper motor in the correct sequence.

The hardware we will use is available cheaply on ebay, and consists of a small unipolar 5V stepper motor with 64:1 reduction gearbox, and a small circuit board that contains a power driver IC (ULN2003) and usually 4 LEDs.

The stepper motor coils are energised in the following sequence for forwards motion. This sequence of 8 steps is known as a half step sequence :

1
1 and 2
2
2 and 3
3
3 and 4
4
4 and 1

So let's code these into a number sequence that can be sent to the shift register and driver.

1
3
2
6
4
12
8
9

Now let's define a Pause statement, so that we don't have to keep changing the 100m delay when we want to change speed.  100mS may be OK for testing, so as you can see what is going on, but for the real miniature stepper motors, this delay should only be about 1mS.

:P  1m

Now let's code these numbers into definitions F, for Forwards and B for Backwards

:F 1rP3rP2rP6rP4rP12rP8rP9rP

:B 9rP8rP12rP4rP6rP2rP3rP1rP

So we can now use a sequence of F or B to provide forwards or backwards motion

512{F} will produce one complete turn of the motor shaft, because there are 512 steps to a full rotation of the gearbox shaft, and each iteration of "F" produces 4 steps.

At the end of each sequence t might be wise to send a 0r - in order to turn off all of the windings whilst the stepper is idle.

There are other sequences of coil energisation, which are used, sometimes when more torque is needed. A useful link here.

step-fig-3-1.gif (5087 bytes)


step-fig-4-1.gif (10593 bytes)

Input from Shift Registers

To complement the output command q, I wrote a short function to read the input shift registers. Command "t" is used to test the input registers and put the resultant value into x.

In order to display the value x, you need the print command p.  So the following construct was used

tp   test the input register and print the value to the serial monitor

tq   test the input register and display the value on the output LEDs

The code behind the "t" function is as follows:

     case 't':     // test the inputs from 74HC165 shift registers
   
      digitalWrite(loadPin, LOW);
      digitalWrite(loadPin, HIGH);
      x = shiftIn(sinPin, sclkPin, MSBFIRST);  // Read incoming word from 74HC165
      digitalWrite(sclkPin,HIGH);
   
      break;

In the next part, I'll be looking at the high power TPIC6B595 and using it to control a pair of stepper motors using SIMPL.



No comments: