1 | /*
|
---|
2 | Copyright (c) 2011 Arduino. All right reserved.
|
---|
3 |
|
---|
4 | This library is free software; you can redistribute it and/or
|
---|
5 | modify it under the terms of the GNU Lesser General Public
|
---|
6 | License as published by the Free Software Foundation; either
|
---|
7 | version 2.1 of the License, or (at your option) any later version.
|
---|
8 |
|
---|
9 | This library is distributed in the hope that it will be useful,
|
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
12 | See the GNU Lesser General Public License for more details.
|
---|
13 |
|
---|
14 | You should have received a copy of the GNU Lesser General Public
|
---|
15 | License along with this library; if not, write to the Free Software
|
---|
16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include <stdint.h>
|
---|
20 | #include "wiring_shift.h"
|
---|
21 | #include "wiring_digital.h"
|
---|
22 | #include "wiring_private.h"
|
---|
23 |
|
---|
24 | #ifdef __cplusplus
|
---|
25 | extern "C"{
|
---|
26 | #endif
|
---|
27 |
|
---|
28 | uint32_t shiftIn( uint32_t ulDataPin, uint32_t ulClockPin, uint32_t ulBitOrder )
|
---|
29 | {
|
---|
30 | uint8_t value = 0 ;
|
---|
31 | uint8_t i ;
|
---|
32 |
|
---|
33 | for ( i=0 ; i < 8 ; ++i )
|
---|
34 | {
|
---|
35 | digitalWrite( ulClockPin, HIGH ) ;
|
---|
36 |
|
---|
37 | if ( ulBitOrder == LSBFIRST )
|
---|
38 | {
|
---|
39 | value |= digitalRead( ulDataPin ) << i ;
|
---|
40 | }
|
---|
41 | else
|
---|
42 | {
|
---|
43 | value |= digitalRead( ulDataPin ) << (7 - i) ;
|
---|
44 | }
|
---|
45 |
|
---|
46 | digitalWrite( ulClockPin, LOW ) ;
|
---|
47 | }
|
---|
48 |
|
---|
49 | return value ;
|
---|
50 | }
|
---|
51 |
|
---|
52 | void shiftOut( uint32_t ulDataPin, uint32_t ulClockPin, uint32_t ulBitOrder, uint32_t ulVal )
|
---|
53 | {
|
---|
54 | uint8_t i ;
|
---|
55 |
|
---|
56 | for ( i=0 ; i < 8 ; i++ )
|
---|
57 | {
|
---|
58 | if ( ulBitOrder == LSBFIRST )
|
---|
59 | {
|
---|
60 | digitalWrite( ulDataPin, !!(ulVal & (1 << i)) ) ;
|
---|
61 | }
|
---|
62 | else
|
---|
63 | {
|
---|
64 | digitalWrite( ulDataPin, !!(ulVal & (1 << (7 - i))) ) ;
|
---|
65 | }
|
---|
66 |
|
---|
67 | digitalWrite( ulClockPin, HIGH ) ;
|
---|
68 | digitalWrite( ulClockPin, LOW ) ;
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | #ifdef __cplusplus
|
---|
73 | } // extern "C"
|
---|
74 | #endif
|
---|