This will become a hardware abstraction layer for my mbed projects. It will make it easier to port sketches from Arduino to mbed
wiring_shift.c@2:7e1ff33bc76a, 2011-06-04 (annotated)
- Committer:
- rbohne
- Date:
- Sat Jun 04 20:01:33 2011 +0000
- Revision:
- 2:7e1ff33bc76a
added some basic wiring functions
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rbohne | 2:7e1ff33bc76a | 1 | /* |
rbohne | 2:7e1ff33bc76a | 2 | wiring_shift.c - shiftOut() function |
rbohne | 2:7e1ff33bc76a | 3 | Part of Arduino - http://www.arduino.cc/ |
rbohne | 2:7e1ff33bc76a | 4 | |
rbohne | 2:7e1ff33bc76a | 5 | Copyright (c) 2005-2006 David A. Mellis |
rbohne | 2:7e1ff33bc76a | 6 | |
rbohne | 2:7e1ff33bc76a | 7 | This library is free software; you can redistribute it and/or |
rbohne | 2:7e1ff33bc76a | 8 | modify it under the terms of the GNU Lesser General Public |
rbohne | 2:7e1ff33bc76a | 9 | License as published by the Free Software Foundation; either |
rbohne | 2:7e1ff33bc76a | 10 | version 2.1 of the License, or (at your option) any later version. |
rbohne | 2:7e1ff33bc76a | 11 | |
rbohne | 2:7e1ff33bc76a | 12 | This library is distributed in the hope that it will be useful, |
rbohne | 2:7e1ff33bc76a | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
rbohne | 2:7e1ff33bc76a | 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
rbohne | 2:7e1ff33bc76a | 15 | Lesser General Public License for more details. |
rbohne | 2:7e1ff33bc76a | 16 | |
rbohne | 2:7e1ff33bc76a | 17 | You should have received a copy of the GNU Lesser General |
rbohne | 2:7e1ff33bc76a | 18 | Public License along with this library; if not, write to the |
rbohne | 2:7e1ff33bc76a | 19 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
rbohne | 2:7e1ff33bc76a | 20 | Boston, MA 02111-1307 USA |
rbohne | 2:7e1ff33bc76a | 21 | |
rbohne | 2:7e1ff33bc76a | 22 | $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $ |
rbohne | 2:7e1ff33bc76a | 23 | */ |
rbohne | 2:7e1ff33bc76a | 24 | |
rbohne | 2:7e1ff33bc76a | 25 | #include "wiring_private.h" |
rbohne | 2:7e1ff33bc76a | 26 | |
rbohne | 2:7e1ff33bc76a | 27 | uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) { |
rbohne | 2:7e1ff33bc76a | 28 | uint8_t value = 0; |
rbohne | 2:7e1ff33bc76a | 29 | uint8_t i; |
rbohne | 2:7e1ff33bc76a | 30 | |
rbohne | 2:7e1ff33bc76a | 31 | for (i = 0; i < 8; ++i) { |
rbohne | 2:7e1ff33bc76a | 32 | digitalWrite(clockPin, HIGH); |
rbohne | 2:7e1ff33bc76a | 33 | if (bitOrder == LSBFIRST) |
rbohne | 2:7e1ff33bc76a | 34 | value |= digitalRead(dataPin) << i; |
rbohne | 2:7e1ff33bc76a | 35 | else |
rbohne | 2:7e1ff33bc76a | 36 | value |= digitalRead(dataPin) << (7 - i); |
rbohne | 2:7e1ff33bc76a | 37 | digitalWrite(clockPin, LOW); |
rbohne | 2:7e1ff33bc76a | 38 | } |
rbohne | 2:7e1ff33bc76a | 39 | return value; |
rbohne | 2:7e1ff33bc76a | 40 | } |
rbohne | 2:7e1ff33bc76a | 41 | |
rbohne | 2:7e1ff33bc76a | 42 | void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val) |
rbohne | 2:7e1ff33bc76a | 43 | { |
rbohne | 2:7e1ff33bc76a | 44 | uint8_t i; |
rbohne | 2:7e1ff33bc76a | 45 | |
rbohne | 2:7e1ff33bc76a | 46 | for (i = 0; i < 8; i++) { |
rbohne | 2:7e1ff33bc76a | 47 | if (bitOrder == LSBFIRST) |
rbohne | 2:7e1ff33bc76a | 48 | digitalWrite(dataPin, !!(val & (1 << i))); |
rbohne | 2:7e1ff33bc76a | 49 | else |
rbohne | 2:7e1ff33bc76a | 50 | digitalWrite(dataPin, !!(val & (1 << (7 - i)))); |
rbohne | 2:7e1ff33bc76a | 51 | |
rbohne | 2:7e1ff33bc76a | 52 | digitalWrite(clockPin, HIGH); |
rbohne | 2:7e1ff33bc76a | 53 | digitalWrite(clockPin, LOW); |
rbohne | 2:7e1ff33bc76a | 54 | } |
rbohne | 2:7e1ff33bc76a | 55 | } |