SHIFTOUT FUNCTION

pioneer
Fri Aug 19, 2016 7:48 am
I never used the Shiftout function before, untill now. I am working with a stepper driver http://datasheet.octopart.com/A3992SLP- … 790836.pdf This driver required two 19 bits word with 3 wire serial port (latch, clock, data) and MSBFIRST to control stepper motor. I searched the net and found only 8 or 16 bits shiftout(). How do I shiftout the rest of 3 bits to make it a total of 19 bits? Big help if you can show me the code. Thank you. Please advise.

Pito
Fri Aug 19, 2016 8:14 am
You may use a loop and bitbanging the pins, for example (not tested):
..
pinMode(pinstrobe, OUTPUT);
pinMode(pinclk, OUTPUT);
pinMode(pindata, OUTPUT);
// idle states according to the datasheet
digitalWrite(pinstrobe,1);
digitalWrite(pinclk,0);
..
// a function to send 19bits of unsigned int data, MSBFIRST
void senddata19bits( unsigned int data ) {
unsigned int i;
// start the data transfer
digitalWrite(pinstrobe,0);
for (i=0; i<19; i++) {
if (data & (1<<18)) {
digitalWrite(pindata,1); digitalWrite(pinclk,1); digitalWrite(pinclk,0);}
else {
digitalWrite(pindata,0); digitalWrite(pinclk,1); digitalWrite(pinclk,0);}
data = data << 1;
}
// end of the datatransfer
digitalWrite(pinstrobe,1);
}
..

racemaniac
Fri Aug 19, 2016 8:28 am
pioneer wrote:I never used the Shiftout function before, untill now. I am working with a stepper driver http://datasheet.octopart.com/A3992SLP- … 790836.pdf This driver required two 19 bits word with 3 wire serial port (latch, clock, data) and MSBFIRST to control stepper motor. I searched the net and found only 8 or 16 bits shiftout(). How do I shiftout the rest of 3 bits to make it a total of 19 bits? Big help if you can show me the code. Thank you. Please advise.

Pito
Fri Aug 19, 2016 8:57 am
I’ve made it as a function “senddata(data)”, typos corrected.. :)

racemaniac
Fri Aug 19, 2016 9:05 am
Pito wrote:I’ve made it as a function “senddata(data)”, typos corrected.. :)

ahull
Fri Aug 19, 2016 10:14 am
racemaniac wrote:Pito wrote:I’ve made it as a function “senddata(data)”, typos corrected.. :)

Pito
Fri Aug 19, 2016 2:40 pm
Anyhow, I would be careful with too much optimization there, as the driver chip OP is using is slow with its 3wire interface :)
A. Minimum Data Setup Time 15 ns
B. Minimum Data Hold Time 10 ns
C. Minimum Setup Strobe to Clock rising edge 120 ns
D. Minimum Clock High Pulse Width 40 ns
E. Minimum Clock Low Pulse Width 40 ns
F. Minimum Setup Clock rising edge to Strobe 50 ns
G. Minimum Strobe Pulse Width 120 ns
H. Minimum Sleep to Clock Setup Time 50 μs
I. Setup “Idle” Release to Output Enable 1 ms

pioneer
Sat Aug 20, 2016 12:43 am
Thanks very much Pito. I able to test with serial monitor to see zero’s and one’s out come with different data. For your last post on the Serial Port Timing Diagram. Do you think the A3992 chip is too fast for my Microcontroller? I am using the STM32F103VET6. As I saw the A3992’s Timing Diagram need to use ns (nano second) to work with.

RogerClark
Sat Aug 20, 2016 3:00 am
@pioneer

You can always slow data down to peripherals that can’t accept it at a high rate.

So don’t worry about the processor being too fast.

If you really want, there is a way to drop the processor speed to 48Mhz (instead of 72Mhz) and still retain USB, but below that USB will not run.

The lowest speed you can run is the external clock freq, which is normally 8Mhz on these boards. But I don’t recommend you reduce the processor speed, just put small delays into your code.

The easiest way to do that is something like

for(volatile int mydelay=0;mydelay<1000;mydelay++);


Pito
Sat Aug 20, 2016 6:44 am
No worry, finally it seems the code is slooow enough to drive the chip :).. So everything is well within the chip specification, moreover you may try to optimize the code in order to speed it up.
Below the analyzer’s output when doing (BluePill @72MHz)
senddata19bits(0x45765);

Pito
Sat Aug 20, 2016 8:47 am
This is with fastest setting/clearing the pins (6x faster shiftout than the previous code):

shift19bit_fast.JPG
shift19bit_fast.JPG (45.42 KiB) Viewed 441 times

pioneer
Wed Nov 29, 2017 4:46 pm
Hi Pito,

Does it make any different if I use:

void loop() {
// put your main code here, to run repeatedly:
// a function to send 19bits of unsigned int data, MSBFIRST
senddata19bits(0b1000101011101100101);
delay(1000);
}


victor_pv
Wed Nov 29, 2017 7:20 pm
According to allegro’s own page, that driver uses SPI:
http://www.allegromicro.com/en/Products … ivers.aspx

Since it uses and uncommon lenght, 19bits, I wonder if you can pad with 0s at the start or the end, and send 24bits, which could be just 3 bytes with the SPI port.

I couldn’t find a reference to doing that, but if I was you I would try and see if it works.
I bet padding 0s works fine, either before the MSB or after the LSB. Probably only in 1 case, not both, but that’s enough. If so, you can format your data and send it with the spi port.


ag123
Thu May 17, 2018 4:38 pm
bounce this old thread, i’m intending to mess with shift registers e,g, 74hc595, 74hc165
https://assets.nexperia.com/documents/d … HCT595.pdf
most likely i’m just going to bit bang them. SPI is nice, but for a start i’d try something ‘raw’.
does it really matter if that bit banged clock driving the shift register use those thin spikes like in pito’s timing diagram?
making the clocks look like those evenly spaced clock / timing diagrams on the data sheets seemed pretty tough to do with bit banging
& it may keep the rates pretty low as 1 jump round the loop seem to stretch quite a bit compared vs that thin clock pulse
:)
(oh got an idea, i could make the clock pulse a thin notch and leave it high for a big fat pause as it jumps round the loop, but i’m not sure if that’s going to help though)

just found out 74hc595 shift registers are pretty cheap on aliexpress, going for some 10 pieces for ~ 50c (shipping included in some cases)
https://www.aliexpress.com/w/wholesale-74hc595.html
think it makes rather decent ‘io expanders’ e.g. like the adafruit motor shield
https://learn.adafruit.com/adafruit-motor-shield


RogerClark
Thu May 17, 2018 9:47 pm
If you simply want more IO. Using. STM32F103V or STM32F103Z board would give you lots more IO.

Albeit at more cost than a BP plus shift registers


madias
Thu May 17, 2018 10:19 pm
74hc595 was the first chip I played with starting learning Arduino. Meanwhile I don’t wanna soldering through hole chips anymore…and this is a mess (wiring) with the shift registers, unless you do it SMD with a printed circuit board. Roger is right: There is no real benefit, because the “bigger” STM32F1 boards costs nearly nothing and waste less space.

ag123
Fri May 18, 2018 1:54 am
thanks, i do have the ‘super fast’ stm32f407ve black board (which cost almost as low as stm32f103ve boards these days) & a bunch of blue pills / maple mini,
Had to say that the BP/MM form factor is super attractive as part of the reason and that after stumbling into $0.50 for 10x74hc595 shift registers, i got sufficiently impulsive and grabbed some shift registers. planning to use them in a keyboard with a BP/MM
i think the shift registers has a useful role to play with BP/MM, saves lots of pins for things that don’t need mhz speeds for (e.g. key pads, leds, 7 seg leds, 1602 lcds, etc )

the other thing is that shift registers allows one to split the ‘limbs’ (peripherals) from the ‘brain’ (mcu) so that one can swap the mcu as desired and still plug into a same peripheral module
:D


RogerClark
Fri May 18, 2018 2:03 am
OK

I just thought I’d mention that in case you didnt have one of the V series boards ;-)


Leave a Reply

Your email address will not be published. Required fields are marked *