PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
shiftIn.c
1 /*
2  * @file shiftIn.c
3  *
4  * @author Andy Lindsay
5  *
6  * @version 0.85
7  *
8  * @copyright Copyright (C) Parallax, Inc. 2012. See end of file for
9  * terms of use (MIT License).
10  *
11  * @brief shiftIn function source, see simpletools.h for documentation.
12  *
13  * @detail Please submit bug reports, suggestions, and improvements to
14  * this code to editor@parallax.com.
15  */
16 
17 #include "simpletools.h" // simpletools function prototypes
18 
19 // shiftIn function definition
20 int shift_in(int pinDat, int pinClk, int mode, int bits)
21 {
22  int vi, vf, inc;
23  int value = 0;
24  int preflag = 0;
25  if((mode == MSBPRE)||(mode == LSBPRE)) preflag = 1;
26  switch(mode)
27  {
28  case MSBPRE:
29  vi = bits - 1;
30  vf = -1;
31  inc = -1;
32  //value |= (input(pinDat) << bits);
33  break;
34  case LSBPRE:
35  vi = 0;
36  vf = bits;
37  inc = 1;
38  value |= input(pinDat);
39  break;
40  case MSBPOST:
41  vi = bits -1;
42  vf = -1;
43  inc = -1;
44  break;
45  default: // case LSBPOST:
46  vi = 0;
47  vf = bits;
48  inc = 1;
49  break;
50  }
51  low(pinClk);
52  int i;
53  for(i = vi; i != vf; i += inc)
54  {
55  if(preflag) value |= (input(pinDat) << i);
56  toggle(pinClk);
57  toggle(pinClk);
58  //if(!i)
59  //{
60  // if(preflag) break;
61  //}
62  if(!preflag) value |= (input(pinDat) << i);
63  }
64  return value;
65 }
66 
MSBPRE
#define MSBPRE
For use with shift_in. Stands for most significant bit first, pre-clock.
Definition: simpletools.h:381
low
void low(int pin)
Set an I/O pin to output-low.
Definition: low.c:19
simpletools.h
This library provides convenient functions for a variety of microcontroller I/O, timing,...
LSBPRE
#define LSBPRE
For use with shift_in. Stands for least significant bit first, pre-clock.
Definition: simpletools.h:388
shift_in
int shift_in(int pinDat, int pinClk, int mode, int bits)
Receive data from a synchronous serial device.
Definition: shiftIn.c:20
input
int input(int pin)
Set an I/O pin to input and return 1 if pin detects a high signal, or 0 if it detects low.
Definition: input.c:19
toggle
unsigned int toggle(int pin)
Toggle the output state of the I/O pin.
Definition: toggle.c:19
MSBPOST
#define MSBPOST
For use with shift_in. Stands for most significant bit first, post-clock.
Definition: simpletools.h:395