Lab 1 Program C

Dependents:   Lab1C

Fork of mbed by -deleted-

Committer:
simon.ford@mbed.co.uk
Date:
Mon May 17 14:18:54 2010 +0000
Revision:
18:b3c9f16cbb96
Parent:
11:1c1ebd0324fa
Child:
27:7110ebee3484
* Digital I/O speed improved
* Added Port
* Added OpenDrain mode
* Fixes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rolf.meyer@arm.com 11:1c1ebd0324fa 1 /* mbed Microcontroller Library - DigitalInOut
rolf.meyer@arm.com 11:1c1ebd0324fa 2 * Copyright (c) 2006-2009 ARM Limited. All rights reserved.
rolf.meyer@arm.com 11:1c1ebd0324fa 3 * sford
rolf.meyer@arm.com 11:1c1ebd0324fa 4 */
rolf.meyer@arm.com 11:1c1ebd0324fa 5
rolf.meyer@arm.com 11:1c1ebd0324fa 6 #ifndef MBED_DIGITALINOUT_H
rolf.meyer@arm.com 11:1c1ebd0324fa 7 #define MBED_DIGITALINOUT_H
rolf.meyer@arm.com 11:1c1ebd0324fa 8
rolf.meyer@arm.com 11:1c1ebd0324fa 9 #include "platform.h"
rolf.meyer@arm.com 11:1c1ebd0324fa 10 #include "PinNames.h"
rolf.meyer@arm.com 11:1c1ebd0324fa 11 #include "PeripheralNames.h"
rolf.meyer@arm.com 11:1c1ebd0324fa 12 #include "Base.h"
rolf.meyer@arm.com 11:1c1ebd0324fa 13
rolf.meyer@arm.com 11:1c1ebd0324fa 14 namespace mbed {
rolf.meyer@arm.com 11:1c1ebd0324fa 15
rolf.meyer@arm.com 11:1c1ebd0324fa 16 /* Class: DigitalInOut
rolf.meyer@arm.com 11:1c1ebd0324fa 17 * A digital input/output, used for setting or reading a bi-directional pin
rolf.meyer@arm.com 11:1c1ebd0324fa 18 */
rolf.meyer@arm.com 11:1c1ebd0324fa 19 class DigitalInOut : public Base {
rolf.meyer@arm.com 11:1c1ebd0324fa 20
rolf.meyer@arm.com 11:1c1ebd0324fa 21 public:
rolf.meyer@arm.com 11:1c1ebd0324fa 22
rolf.meyer@arm.com 11:1c1ebd0324fa 23 /* Constructor: DigitalInOut
rolf.meyer@arm.com 11:1c1ebd0324fa 24 * Create a DigitalInOut connected to the specified pin
rolf.meyer@arm.com 11:1c1ebd0324fa 25 *
rolf.meyer@arm.com 11:1c1ebd0324fa 26 * Variables:
rolf.meyer@arm.com 11:1c1ebd0324fa 27 * pin - DigitalInOut pin to connect to
rolf.meyer@arm.com 11:1c1ebd0324fa 28 */
rolf.meyer@arm.com 11:1c1ebd0324fa 29 DigitalInOut(PinName pin, const char* name = NULL);
rolf.meyer@arm.com 11:1c1ebd0324fa 30
rolf.meyer@arm.com 11:1c1ebd0324fa 31 /* Function: write
rolf.meyer@arm.com 11:1c1ebd0324fa 32 * Set the output, specified as 0 or 1 (int)
rolf.meyer@arm.com 11:1c1ebd0324fa 33 *
rolf.meyer@arm.com 11:1c1ebd0324fa 34 * Variables:
rolf.meyer@arm.com 11:1c1ebd0324fa 35 * value - An integer specifying the pin output value,
rolf.meyer@arm.com 11:1c1ebd0324fa 36 * 0 for logical 0 and 1 (or any other non-zero value) for logical 1
rolf.meyer@arm.com 11:1c1ebd0324fa 37 */
simon.ford@mbed.co.uk 18:b3c9f16cbb96 38 void write(int value) {
simon.ford@mbed.co.uk 18:b3c9f16cbb96 39 if(value) {
simon.ford@mbed.co.uk 18:b3c9f16cbb96 40 _gpio->FIOSET = _mask;
simon.ford@mbed.co.uk 18:b3c9f16cbb96 41 } else {
simon.ford@mbed.co.uk 18:b3c9f16cbb96 42 _gpio->FIOCLR = _mask;
simon.ford@mbed.co.uk 18:b3c9f16cbb96 43 }
simon.ford@mbed.co.uk 18:b3c9f16cbb96 44 }
rolf.meyer@arm.com 11:1c1ebd0324fa 45
rolf.meyer@arm.com 11:1c1ebd0324fa 46 /* Function: read
rolf.meyer@arm.com 11:1c1ebd0324fa 47 * Return the output setting, represented as 0 or 1 (int)
rolf.meyer@arm.com 11:1c1ebd0324fa 48 *
rolf.meyer@arm.com 11:1c1ebd0324fa 49 * Variables:
rolf.meyer@arm.com 11:1c1ebd0324fa 50 * returns - An integer representing the output setting of the pin if it is an output,
rolf.meyer@arm.com 11:1c1ebd0324fa 51 * or read the input if set as an input
rolf.meyer@arm.com 11:1c1ebd0324fa 52 */
simon.ford@mbed.co.uk 18:b3c9f16cbb96 53 int read() {
simon.ford@mbed.co.uk 18:b3c9f16cbb96 54 return ((_gpio->FIOPIN & _mask) ? 1 : 0);
simon.ford@mbed.co.uk 18:b3c9f16cbb96 55 }
simon.ford@mbed.co.uk 18:b3c9f16cbb96 56
rolf.meyer@arm.com 11:1c1ebd0324fa 57
rolf.meyer@arm.com 11:1c1ebd0324fa 58 /* Function: output
rolf.meyer@arm.com 11:1c1ebd0324fa 59 * Set as an output
rolf.meyer@arm.com 11:1c1ebd0324fa 60 */
rolf.meyer@arm.com 11:1c1ebd0324fa 61 void output();
rolf.meyer@arm.com 11:1c1ebd0324fa 62
rolf.meyer@arm.com 11:1c1ebd0324fa 63 /* Function: input
rolf.meyer@arm.com 11:1c1ebd0324fa 64 * Set as an input
rolf.meyer@arm.com 11:1c1ebd0324fa 65 */
rolf.meyer@arm.com 11:1c1ebd0324fa 66 void input();
rolf.meyer@arm.com 11:1c1ebd0324fa 67
rolf.meyer@arm.com 11:1c1ebd0324fa 68 /* Function: mode
rolf.meyer@arm.com 11:1c1ebd0324fa 69 * Set the input pin mode
rolf.meyer@arm.com 11:1c1ebd0324fa 70 *
rolf.meyer@arm.com 11:1c1ebd0324fa 71 * Variables:
simon.ford@mbed.co.uk 18:b3c9f16cbb96 72 * mode - PullUp, PullDown, PullNone, OpenDrain
rolf.meyer@arm.com 11:1c1ebd0324fa 73 */
rolf.meyer@arm.com 11:1c1ebd0324fa 74 void mode(PinMode pull);
rolf.meyer@arm.com 11:1c1ebd0324fa 75
rolf.meyer@arm.com 11:1c1ebd0324fa 76 #ifdef MBED_OPERATORS
rolf.meyer@arm.com 11:1c1ebd0324fa 77 /* Function: operator=
rolf.meyer@arm.com 11:1c1ebd0324fa 78 * A shorthand for <write>
rolf.meyer@arm.com 11:1c1ebd0324fa 79 */
simon.ford@mbed.co.uk 18:b3c9f16cbb96 80 DigitalInOut& operator= (int value) {
simon.ford@mbed.co.uk 18:b3c9f16cbb96 81 write(value);
simon.ford@mbed.co.uk 18:b3c9f16cbb96 82 return *this;
simon.ford@mbed.co.uk 18:b3c9f16cbb96 83 }
simon.ford@mbed.co.uk 18:b3c9f16cbb96 84
simon.ford@mbed.co.uk 18:b3c9f16cbb96 85 DigitalInOut& operator= (DigitalInOut& rhs) {
simon.ford@mbed.co.uk 18:b3c9f16cbb96 86 write(rhs.read());
simon.ford@mbed.co.uk 18:b3c9f16cbb96 87 return *this;
simon.ford@mbed.co.uk 18:b3c9f16cbb96 88 }
simon.ford@mbed.co.uk 18:b3c9f16cbb96 89
rolf.meyer@arm.com 11:1c1ebd0324fa 90 /* Function: operator int()
rolf.meyer@arm.com 11:1c1ebd0324fa 91 * A shorthand for <read>
rolf.meyer@arm.com 11:1c1ebd0324fa 92 */
simon.ford@mbed.co.uk 18:b3c9f16cbb96 93 operator int() {
simon.ford@mbed.co.uk 18:b3c9f16cbb96 94 return read();
simon.ford@mbed.co.uk 18:b3c9f16cbb96 95 }
rolf.meyer@arm.com 11:1c1ebd0324fa 96 #endif
rolf.meyer@arm.com 11:1c1ebd0324fa 97
rolf.meyer@arm.com 11:1c1ebd0324fa 98 #ifdef MBED_RPC
rolf.meyer@arm.com 11:1c1ebd0324fa 99 virtual const struct rpc_method *get_rpc_methods();
rolf.meyer@arm.com 11:1c1ebd0324fa 100 static struct rpc_class *get_rpc_class();
rolf.meyer@arm.com 11:1c1ebd0324fa 101 #endif
rolf.meyer@arm.com 11:1c1ebd0324fa 102
rolf.meyer@arm.com 11:1c1ebd0324fa 103 protected:
rolf.meyer@arm.com 11:1c1ebd0324fa 104
simon.ford@mbed.co.uk 18:b3c9f16cbb96 105 PinName _pin;
simon.ford@mbed.co.uk 18:b3c9f16cbb96 106 LPC_GPIO_TypeDef *_gpio;
simon.ford@mbed.co.uk 18:b3c9f16cbb96 107 uint32_t _mask;
rolf.meyer@arm.com 11:1c1ebd0324fa 108
rolf.meyer@arm.com 11:1c1ebd0324fa 109 };
rolf.meyer@arm.com 11:1c1ebd0324fa 110
rolf.meyer@arm.com 11:1c1ebd0324fa 111 } // namespace mbed
rolf.meyer@arm.com 11:1c1ebd0324fa 112
rolf.meyer@arm.com 11:1c1ebd0324fa 113 #endif