SHIFTIN

Top  Previous  Next

Action

Shifts a bit stream into a variable.

 

 

Syntax

SHIFTIN pin , pclock , var , option [, bits , delay ]

 

 

Remarks

Pin

The port pin which serves as an input.PINB.2 for example

Pclock

The port pin which generates the clock.

Var

The variable that is assigned. The existing value is not preserved. For example when you shiftin 3 bits, the whole byte will be replaced with the 3 bits.

See CONFIG SHIFTIN for other SHIFTIN behaviour.

Option

Option can be :

 

0 – MSB shifted in first when clock goes low

1 – MSB shifted in first when clock goes high

2 – LSB shifted in first when clock goes low

3 – LSB shifted in first when clock goes high

Adding 4 to the parameter indicates that an external clock signal is used for the clock. In this case the clock will not be generated. So using 4 will be the same a 0 (MSB shifted in first when clock goes low) but the clock must be generated by an external signal.

 

4 – MSB shifted in first when clock goes high with ext. clock

5 – MSB shifted in first when clock goes low with ext. clock

6 – LSB shifted in first when clock goes high with ext. clock

7 – LSB shifted in first when clock goes low with ext. clock

 

Bits

Optional number of bits to shift in. Maximum 255. The number of bits is automatic loaded depending on the used variable. For a long for example which is 4 bytes long, 32 will be loaded.

Delay

Optional delay in uS.

 

If you do not specify the number of bits to shift, the number of shifts will depend on the type of the variable.

When you use a byte, 8 shifts will occur and for an integer, 16 shifts will occur. For a Long and Single 32 shifts will occur.

 

The SHIFTIN routine can be used to interface with all kind of chips.

The PIN is normally connected with the output of chip that will send information.

 

The PCLOCK pin can be used to clock the bits as a master, that is the clock pulses will be generated. Or it can sample a pin that generates these pulses.

 

The VARIABLE is a normal BASIC variable. And may be of any type except for BIT. The data read from the chip is stored in this variable.

 

The OPTIONS is a constant that specifies the direction of the bits. The chip that outputs the data may send the LS bit first or the MS bit first. It also controls on which edge of the clock signal the data must be stored.

 

When you add 4 to the constant you tell the compiler that the clock signal is not generated but that there is an external clock signal.

The number of bits may be specified. You may omit this info. In that case the number of bits of the element data type will be used.

The DELAY normally consists of 2 NOP instructions. When the clock is too fast you can specify a delay time(in uS).

 

 

SHIFTIN with option NEW

The new option CONFIG SHIFTIN=NEW , will change the behaviour of the SHIFTIN statement.

When using this option, it will work for all SHIFTIN statements. The SHIFTIN will work more like the normal SHIFT statement. Bits are shifted from left to right or right to left.

 

The new SHIFTIN can preserve the value/bits when shifting in bits.

For example when the value of a word is &B101 and you shift in 3 bits with value &B111, the resulting value will be &B101111. When you not want to preserve the value, you can add a value of 8 to the parameter. When you add a value of 16, the value will also not be preserved, but then the value will be cleared initially. You would only need this when shifting in less 8 bits then the size of the variable.

Another important difference is that the new SHIFTIN can only SHIFTIN a maximum of 8 bytes. For quick operation, register R16-R23 are used. You may specify the number of bits to shiftin. This may be a variable too. When you shiftin a value into a Word, the number of bits is automatic loaded with 16. This is true for all numeric data types.

 

Some of the code is stored in the MCS library. While this reduces code when SHIFTIN is used multiple times, it has the drawback that the code is written for 8 bytes and thus is not optimal for shifting in less bytes.

You can choose to generate a part of the library code instead. Add a value of 32 to the parameter to do so.

Another new option is not to set the initial pin state for the clock and input pin. By default the clock pin is made an input or output, depending on the external clock option. And the clock is set to an initial state when no external clock is used.

When you want to use shiftin after a shiftout, you might not want the level to change. In this case, add 64 to the parameter.

 

Pin

The port pin which serves as an input.PINB.2 for example

Pclock

The port pin which generates the clock. An external signal can also be used for the clock. In that case, the pin is used in input mode.

Var

The variable that is assigned. The existing value is preserved.

With some additional constants which you can add to the option parameter, you can influence the behaviour :

- 8 -  Do NOT preserve the value. This saves code.

-16 -  Do not preserve value, but clear the value before shifting in the bits

 

Option

A constant which can be one of the following values :

 

0 – MS bit shifted in first when clock goes low

1 – MS bit shifted in first when clock goes high

2 – LS bit shifted in first when clock goes low

3 – LS bit shifted in first when clock goes high

 

Adding 4 to the parameter indicates that an external clock signal is used for the clock. In this case the clock will not be generated. So using 4 will be the same a 0 (MSB shifted in first when clock goes low) but the clock must be generated by an external signal.

 

4 – MSB shifted in first when clock goes high with ext. clock

5 – MSB shifted in first when clock goes low with ext. clock

6 – LSB shifted in first when clock goes high with ext. clock

7 – LSB shifted in first when clock goes low with ext. clock

 

Add a value of 8 to the option, so the existing variable will not be preserved.

Add a value of 16 to the option to clear the variable first.

Add a value of 32 to the option to generate code instead of using the lib code.

Add a value of 64 to the option when you do not want the clock and input pin data direction and state want to be set. For example, when using SHIFTIN after a SHIFTOUT statement.

 

Example : Shiftin Pind.3 , Portd.4 , W , 2 + 32 + 16 , 3  

Bits

Optional number of bits to shift in. Maximum 64. The number of bits is automatic loaded depending on the used variable. For a long for example which is 4 bytes long, 32 will be loaded. You can use a constant or variable.

Delay

Optional delay in uS. When not specified, 2 nops are used. The delay is intended to slow down the clock frequency.

 

 

The initial state for the clock depends on the option. For option 1 and 3, it will be low. For option 0 and 2 it will be high.

Thus for example option 2 will set the clock pin high. Then the clock is brought low and the data is sampled/stored. After this the clock is made high again. This means when ready, the clock pin will be in the same state as the initial state.

 

 

 

See also

SHIFTOUT , SHIFT

 

 

Example

'-----------------------------------------------------------------------------------------

'name                     : shift.bas

'copyright                : (c) 1995-2005, MCS Electronics

'purpose                  : example for SHIFTIN and SHIFTOUT statement

'micro                    : Mega48

'suited for demo          : yes

'commercial addon needed  : no

'-----------------------------------------------------------------------------------------

 

$regfile = "m48def.dat"                                   ' specify the used micro

$crystal = 4000000                                         ' used crystal frequency

$baud = 19200                                               ' use baud rate

$hwstack = 32                                               ' default use 32 for the hardware stack

$swstack = 10                                               ' default use 10 for the SW stack

$framesize = 40                                             ' default use 40 for the frame space

 

Dim L As Long

 

clock Alias Portb.0

Output Alias Portb.1

sinp Alias Pinb.2                                           'watch the PIN instead of PORT

 

'shiftout pinout,pinclock, var,parameter [,bits , delay]

' value for parameter :

'  0 - MSB first ,clock low

'  1 - MSB first,clock high

'  2 - LSB first,clock low

'  3 - LSB first,clock high

'The bits is a new option to indicate the number of bits to shift out

'For a byte you should specify 1-8 , for an integer 1-16 and for a long 1-32

'The delay is an optional delay is uS and when used, the bits parameter must

'be specified too!

 

'Now shift out 9 most significant bits of the LONG variable L

Shiftout Output , Clock , L , 0 , 9

 

 

 

'shiftin pinin,pinclock,var,parameter [,bits ,delay]

'  0 - MSB first ,clock low  (4)

'  1 - MSB first,clock high  (5)

'  2 - LSB first,clock low   (6)

'  3 - LSB first,clock high  (7)

 

'To use an external clock, add 4 to the parameter

'The shiftin also has a new optional parameter to specify the number of bits

 

'The bits is a new option to indicate the number of bits to shift out

'For a byte you should specify 1-8 , for an integer 1-16 and for a long 1-32

'The delay is an optional delay is uS and when used, the bits parameter must

'be specified too!

 

 

'Shift in 9 bits into a long

Shiftin Sinp , Clock , L , 0 , 9

'use shift to shift the bits to the right place in the long

Shift L , Right , 23

End