SHIFTOUT FUNCTION

Post here first, or if you can't find a relevant section!
pioneer
Posts: 16
Joined: Fri Apr 22, 2016 8:11 am

SHIFTOUT FUNCTION

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

User avatar
Pito
Posts: 1701
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: SHIFTOUT FUNCTION

Post by Pito » Fri Aug 19, 2016 8:14 am

You may use a loop and bitbanging the pins, for example (not tested):

Code: Select all

..
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);
}
..
Last edited by Pito on Fri Aug 19, 2016 9:15 am, edited 12 times in total.
Pukao Hats Cleaning Services Ltd.

racemaniac
Posts: 658
Joined: Sat Nov 07, 2015 9:09 am

Re: SHIFTOUT FUNCTION

Post by 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.
As usual: what hae you tried? what Pito answered above is pretty close to what you'll ahve to do: implement spi yourself (which is very simple, it's a very simple protocol).
However his code still has some faults, but if you read up on the protocol a bit, and on how to implement it yourself, it should be a good start :).

User avatar
Pito
Posts: 1701
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: SHIFTOUT FUNCTION

Post by Pito » Fri Aug 19, 2016 8:57 am

I've made it as a function "senddata(data)", typos corrected.. :)
Pukao Hats Cleaning Services Ltd.

racemaniac
Posts: 658
Joined: Sat Nov 07, 2015 9:09 am

Re: SHIFTOUT FUNCTION

Post by racemaniac » Fri Aug 19, 2016 9:05 am

Pito wrote:I've made it as a function "senddata(data)", typos corrected.. :)
that indeed looks better :)
btw, just as with all other operators, you can write "data <<= 1;"

User avatar
ahull
Posts: 1672
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: SHIFTOUT FUNCTION

Post by ahull » Fri Aug 19, 2016 10:14 am

racemaniac wrote:
Pito wrote:I've made it as a function "senddata(data)", typos corrected.. :)
that indeed looks better :)
btw, just as with all other operators, you can write "data <<= 1;"
Thanks... I've learned something new today.... It would be interesting to see how the compiler optimises those two variants. I suspect data <<=1; winds up being a couple of bytes smaller than data = data << 1; , however compilers have some pretty smart tricks, so perhaps they both end up the same. These things matter when speed and code size are important.
- Andy Hull -

User avatar
Pito
Posts: 1701
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: SHIFTOUT FUNCTION

Post by 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 :)

Code: Select all

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
Pukao Hats Cleaning Services Ltd.

pioneer
Posts: 16
Joined: Fri Apr 22, 2016 8:11 am

Re: SHIFTOUT FUNCTION

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

User avatar
RogerClark
Posts: 7546
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: SHIFTOUT FUNCTION

Post by 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

Code: Select all

for(volatile int mydelay=0;mydelay<1000;mydelay++);
Or do a while loop.

Just make sure you declare the variable as volatile, otherwise the compiler will decided to remove the code entirely as it thinks it does nothing.

(Obviously 1000 is an arbitary delay length, you'd need to work out how long you wanted to delay, probably using a scope or a logic analyzer to get the timings)

User avatar
Pito
Posts: 1701
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: SHIFTOUT FUNCTION

Post by 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)

Code: Select all

senddata19bits(0x45765);
The clock's High is 595ns and Low is 1420ns.
shift19bit.JPG
shift19bit.JPG (43.24 KiB) Viewed 723 times
Pukao Hats Cleaning Services Ltd.

Post Reply