Skip to content

shiftIn

gicking edited this page Feb 6, 2018 · 2 revisions

back to Command Reference / Advanced-IO

Description

Shifts in a byte of data one bit at a time. Starts from either the most (i.e. the leftmost) or least (rightmost) significant bit. For each bit, the clock pin is pulled high, the next bit is read from the data line, and then the clock pin is taken low.

If you’re interfacing with a device that’s clocked by rising edges, you’ll need to make sure that the clock pin is low before the first call to shiftIn(), e.g. with a call to pinLow().

Note: this is a software implementation. The STM8 also provides a SPI hardware, which is faster but only works on specific pins.

Notes:

  • after reset all I/O pins are configured as INPUT
  • data pin needs to be configured as INPUT*, clock pin as OUTPUT* using pinMode().
  • generally it is a good idea to synchronize master and slave frames, e.g. via a dedicated pin like in SPI, or via a frame timeout

Inclusion

  • defined in shift.h
  • not loaded by main_general.h
  • no #define required

Syntax

shiftIn(pPortData,pinData,pPortClock,pinClock,clockWidth,bitOrder)

Parameters

  • input:

    • pPortData: pointer to port for data input, e.g. &PORT_A
    • pinData: pin number for data input
    • pPortData: pointer to port for clock output, e.g. &PORT_B
    • pinClock: pin number for clock output
    • clockWidth: width of clock pulse in microseconds
    • bitOrder: which order to shift in the bits (MSBFIRST / LSBFIRST)
  • output:

    • none

Returns

  • read byte

Example Code

The below program shifts in data from a slave via pins PE5 and PD2 in master mode.

#include "main_general.h"
#include "shift.h"

void setup() {
  pinMode(&PORT_E, 5, INPUT);     // data:  muBoard io_1
  pinMode(&PORT_D, 2, OUTPUT);    // clock: muBoard io_2
  pinLow(&PORT_D, 2);
}

void loop() {
  uint8_t val;
  val = shiftIn(&PORT_E, 5, &PORT_D, 2, 10, MSBFIRST);
  delay(200);
}

Relevant Tutorial

  • tbd

Notes and Warnings

To avoid damage

  • do not expose I/Os to voltages outside [-0.3V; Vdd+0.3V], else limit injection currents to the specificied limits
  • for OUTPUT pins assert that sink and source currents are below the specificied limits
  • do not directly connect two OUTPUT pins. If e.g. half-duplex is required, use a pull-up and OUTPUT_OPENDRAIN, instead

See also

Clone this wiki locally