Lab 1 Program C
Fork of mbed by
DigitalOut.h@27:7110ebee3484, 2011-11-29 (annotated)
- Committer:
- emilmont
- Date:
- Tue Nov 29 14:59:27 2011 +0000
- Revision:
- 27:7110ebee3484
- Parent:
- 18:b3c9f16cbb96
- Child:
- 43:aff670d0d510
New Libraries 11.11
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon.ford@mbed.co.uk | 0:82220227f4fa | 1 | /* mbed Microcontroller Library - DigitalOut |
emilmont | 27:7110ebee3484 | 2 | * Copyright (c) 2006-2011 ARM Limited. All rights reserved. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 3 | */ |
simon.ford@mbed.co.uk | 0:82220227f4fa | 4 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 5 | #ifndef MBED_DIGITALOUT_H |
simon.ford@mbed.co.uk | 0:82220227f4fa | 6 | #define MBED_DIGITALOUT_H |
simon.ford@mbed.co.uk | 0:82220227f4fa | 7 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 8 | #include "platform.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 9 | #include "PinNames.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 10 | #include "PeripheralNames.h" |
simon.ford@mbed.co.uk | 0:82220227f4fa | 11 | #include "Base.h" |
simon.ford@mbed.co.uk | 0:82220227f4fa | 12 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 13 | namespace mbed { |
simon.ford@mbed.co.uk | 0:82220227f4fa | 14 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 15 | /* Class: DigitalOut |
simon.ford@mbed.co.uk | 0:82220227f4fa | 16 | * A digital output, used for setting the state of a pin |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 17 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 18 | * Example: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 19 | * > // Toggle a LED |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 20 | * > #include "mbed.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 21 | * > |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 22 | * > DigitalOut led(LED1); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 23 | * > |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 24 | * > int main() { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 25 | * > while(1) { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 26 | * > led = !led; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 27 | * > wait(0.2); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 28 | * > } |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 29 | * > } |
simon.ford@mbed.co.uk | 0:82220227f4fa | 30 | */ |
simon.ford@mbed.co.uk | 0:82220227f4fa | 31 | class DigitalOut : public Base { |
simon.ford@mbed.co.uk | 0:82220227f4fa | 32 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 33 | public: |
simon.ford@mbed.co.uk | 0:82220227f4fa | 34 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 35 | /* Constructor: DigitalOut |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 36 | * Create a DigitalOut connected to the specified pin |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 37 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 38 | * Variables: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 39 | * pin - DigitalOut pin to connect to |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 40 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 41 | DigitalOut(PinName pin, const char* name = NULL); |
simon.ford@mbed.co.uk | 0:82220227f4fa | 42 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 43 | /* Function: write |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 44 | * Set the output, specified as 0 or 1 (int) |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 45 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 46 | * Variables: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 47 | * value - An integer specifying the pin output value, |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 48 | * 0 for logical 0 and 1 (or any other non-zero value) for logical 1 |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 49 | */ |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 50 | void write(int value) { |
emilmont | 27:7110ebee3484 | 51 | |
emilmont | 27:7110ebee3484 | 52 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
emilmont | 27:7110ebee3484 | 53 | |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 54 | if(value) { |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 55 | _gpio->FIOSET = _mask; |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 56 | } else { |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 57 | _gpio->FIOCLR = _mask; |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 58 | } |
emilmont | 27:7110ebee3484 | 59 | |
emilmont | 27:7110ebee3484 | 60 | #elif defined(TARGET_LPC11U24) |
emilmont | 27:7110ebee3484 | 61 | |
emilmont | 27:7110ebee3484 | 62 | if(value) { |
emilmont | 27:7110ebee3484 | 63 | LPC_GPIO->SET[_index] = _mask; |
emilmont | 27:7110ebee3484 | 64 | } else { |
emilmont | 27:7110ebee3484 | 65 | LPC_GPIO->CLR[_index] = _mask; |
emilmont | 27:7110ebee3484 | 66 | } |
emilmont | 27:7110ebee3484 | 67 | #endif |
emilmont | 27:7110ebee3484 | 68 | |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 69 | } |
simon.ford@mbed.co.uk | 0:82220227f4fa | 70 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 71 | /* Function: read |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 72 | * Return the output setting, represented as 0 or 1 (int) |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 73 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 74 | * Variables: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 75 | * returns - An integer representing the output setting of the pin, |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 76 | * 0 for logical 0 and 1 for logical 1 |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 77 | */ |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 78 | int read() { |
emilmont | 27:7110ebee3484 | 79 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 80 | return ((_gpio->FIOPIN & _mask) ? 1 : 0); |
emilmont | 27:7110ebee3484 | 81 | #elif defined(TARGET_LPC11U24) |
emilmont | 27:7110ebee3484 | 82 | return ((LPC_GPIO->PIN[_index] & _mask) ? 1 : 0); |
emilmont | 27:7110ebee3484 | 83 | #endif |
emilmont | 27:7110ebee3484 | 84 | |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 85 | } |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 86 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 87 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 88 | #ifdef MBED_OPERATORS |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 89 | /* Function: operator= |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 90 | * A shorthand for <write> |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 91 | */ |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 92 | DigitalOut& operator= (int value) { |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 93 | write(value); |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 94 | return *this; |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 95 | } |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 96 | |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 97 | DigitalOut& operator= (DigitalOut& rhs) { |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 98 | write(rhs.read()); |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 99 | return *this; |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 100 | } |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 101 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 102 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 103 | /* Function: operator int() |
simon.ford@mbed.co.uk | 0:82220227f4fa | 104 | * A shorthand for <read> |
simon.ford@mbed.co.uk | 0:82220227f4fa | 105 | */ |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 106 | operator int() { |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 107 | return read(); |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 108 | } |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 109 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 110 | #endif |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 111 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 112 | #ifdef MBED_RPC |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 113 | virtual const struct rpc_method *get_rpc_methods(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 114 | static struct rpc_class *get_rpc_class(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 115 | #endif |
simon.ford@mbed.co.uk | 0:82220227f4fa | 116 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 117 | protected: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 118 | |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 119 | PinName _pin; |
emilmont | 27:7110ebee3484 | 120 | |
emilmont | 27:7110ebee3484 | 121 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 122 | LPC_GPIO_TypeDef *_gpio; |
emilmont | 27:7110ebee3484 | 123 | #elif defined(TARGET_LPC11U24) |
emilmont | 27:7110ebee3484 | 124 | int _index; |
emilmont | 27:7110ebee3484 | 125 | #endif |
emilmont | 27:7110ebee3484 | 126 | |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 127 | uint32_t _mask; |
simon.ford@mbed.co.uk | 18:b3c9f16cbb96 | 128 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 129 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 130 | }; |
simon.ford@mbed.co.uk | 0:82220227f4fa | 131 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 132 | } // namespace mbed |
simon.ford@mbed.co.uk | 0:82220227f4fa | 133 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 134 | #endif |