Used to hold all my \"snippets\" I create in forum support.

Dependencies:   mbed

Committer:
AjK
Date:
Thu Feb 10 11:20:12 2011 +0000
Revision:
0:98784e7c6163
1.0 Initial release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 0:98784e7c6163 1 /**
AjK 0:98784e7c6163 2 * @file forum1828.cpp
AjK 0:98784e7c6163 3 * @see http://mbed.org/forum/mbed/topic/1828
AjK 0:98784e7c6163 4 * @code
AjK 0:98784e7c6163 5 #include "mbed.h"
AjK 0:98784e7c6163 6
AjK 0:98784e7c6163 7 #define P0_22_SEL_MASK ~(3UL << 12)
AjK 0:98784e7c6163 8 #define P0_22_SET_MASK (1UL << 22)
AjK 0:98784e7c6163 9 #define P0_22_CLR_MASK ~(p15_SET_MASK)
AjK 0:98784e7c6163 10 #define P0_22_AS_OUTPUT LPC_PINCON->PINSEL1&=P0_22_SEL_MASK;LPC_GPIO0->FIODIR|=P0_22_SET_MASK
AjK 0:98784e7c6163 11 #define P0_22_AS_INPUT LPC_GPIO0->FIOMASK &= P0_22_CLR_MASK;
AjK 0:98784e7c6163 12 #define P0_22_SET LPC_GPIO0->FIOSET = P0_22_SET_MASK
AjK 0:98784e7c6163 13 #define P0_22_CLR LPC_GPIO0->FIOCLR = P0_22_SET_MASK
AjK 0:98784e7c6163 14 #define P0_22_IS_SET (bool)(LPC_GPIO0->FIOPIN & P0_22_SET_MASK)
AjK 0:98784e7c6163 15 #define P0_22_IS_CLR !(P0_22_IS_SET)
AjK 0:98784e7c6163 16 #define P0_22_MODE(x) LPC_PINCON->PINMODE1&=P0_22_SEL_MASK;LPC_PINCON->PINMODE1|=((x&0x3)<<12)
AjK 0:98784e7c6163 17
AjK 0:98784e7c6163 18 DigitalOut myled(LED1);
AjK 0:98784e7c6163 19 DigitalOut led2(LED2);
AjK 0:98784e7c6163 20 InterruptIn mybool(P0_22);
AjK 0:98784e7c6163 21
AjK 0:98784e7c6163 22 void led2on(void) {
AjK 0:98784e7c6163 23 led2 = 1;
AjK 0:98784e7c6163 24 }
AjK 0:98784e7c6163 25
AjK 0:98784e7c6163 26 void led2off(void) {
AjK 0:98784e7c6163 27 led2 = 0;
AjK 0:98784e7c6163 28 }
AjK 0:98784e7c6163 29
AjK 0:98784e7c6163 30 int main() {
AjK 0:98784e7c6163 31
AjK 0:98784e7c6163 32 // Setup InterruptIn as normal.
AjK 0:98784e7c6163 33 mybool.mode(PullUp);
AjK 0:98784e7c6163 34 mybool.rise(&led2on);
AjK 0:98784e7c6163 35 mybool.fall(&led2off);
AjK 0:98784e7c6163 36
AjK 0:98784e7c6163 37 // Now cheat, manually switch it to an output.
AjK 0:98784e7c6163 38 P0_22_AS_OUTPUT;
AjK 0:98784e7c6163 39
AjK 0:98784e7c6163 40 while(1) {
AjK 0:98784e7c6163 41 myled = 1;
AjK 0:98784e7c6163 42 P0_22_SET; // Set the bool and led2on() is called.
AjK 0:98784e7c6163 43 wait(0.2);
AjK 0:98784e7c6163 44
AjK 0:98784e7c6163 45 myled = 0;
AjK 0:98784e7c6163 46 P0_22_CLR; // Clear the bool and led2off() is called.
AjK 0:98784e7c6163 47 wait(0.2);
AjK 0:98784e7c6163 48 }
AjK 0:98784e7c6163 49 }
AjK 0:98784e7c6163 50
AjK 0:98784e7c6163 51 * @endcode
AjK 0:98784e7c6163 52 */
AjK 0:98784e7c6163 53
AjK 0:98784e7c6163 54 #ifdef FORUM_1828
AjK 0:98784e7c6163 55
AjK 0:98784e7c6163 56 /**
AjK 0:98784e7c6163 57 * @see http://mbed.org/forum/mbed/topic/1828
AjK 0:98784e7c6163 58 */
AjK 0:98784e7c6163 59
AjK 0:98784e7c6163 60 #include "mbed.h"
AjK 0:98784e7c6163 61
AjK 0:98784e7c6163 62 #define P0_22_SEL_MASK ~(3UL << 12)
AjK 0:98784e7c6163 63 #define P0_22_SET_MASK (1UL << 22)
AjK 0:98784e7c6163 64 #define P0_22_CLR_MASK ~(p15_SET_MASK)
AjK 0:98784e7c6163 65 #define P0_22_AS_OUTPUT LPC_PINCON->PINSEL1&=P0_22_SEL_MASK;LPC_GPIO0->FIODIR|=P0_22_SET_MASK
AjK 0:98784e7c6163 66 #define P0_22_AS_INPUT LPC_GPIO0->FIOMASK &= P0_22_CLR_MASK;
AjK 0:98784e7c6163 67 #define P0_22_SET LPC_GPIO0->FIOSET = P0_22_SET_MASK
AjK 0:98784e7c6163 68 #define P0_22_CLR LPC_GPIO0->FIOCLR = P0_22_SET_MASK
AjK 0:98784e7c6163 69 #define P0_22_IS_SET (bool)(LPC_GPIO0->FIOPIN & P0_22_SET_MASK)
AjK 0:98784e7c6163 70 #define P0_22_IS_CLR !(P0_22_IS_SET)
AjK 0:98784e7c6163 71 #define P0_22_MODE(x) LPC_PINCON->PINMODE1&=P0_22_SEL_MASK;LPC_PINCON->PINMODE1|=((x&0x3)<<12)
AjK 0:98784e7c6163 72
AjK 0:98784e7c6163 73 DigitalOut myled(LED1);
AjK 0:98784e7c6163 74 DigitalOut led2(LED2);
AjK 0:98784e7c6163 75 InterruptIn mybool(P0_22);
AjK 0:98784e7c6163 76
AjK 0:98784e7c6163 77 void led2on(void) {
AjK 0:98784e7c6163 78 led2 = 1;
AjK 0:98784e7c6163 79 }
AjK 0:98784e7c6163 80
AjK 0:98784e7c6163 81 void led2off(void) {
AjK 0:98784e7c6163 82 led2 = 0;
AjK 0:98784e7c6163 83 }
AjK 0:98784e7c6163 84
AjK 0:98784e7c6163 85 int main() {
AjK 0:98784e7c6163 86
AjK 0:98784e7c6163 87 // Setup InterruptIn as normal.
AjK 0:98784e7c6163 88 mybool.mode(PullUp);
AjK 0:98784e7c6163 89 mybool.rise(&led2on);
AjK 0:98784e7c6163 90 mybool.fall(&led2off);
AjK 0:98784e7c6163 91
AjK 0:98784e7c6163 92 // Now cheat, manually switch it to an output.
AjK 0:98784e7c6163 93 P0_22_AS_OUTPUT;
AjK 0:98784e7c6163 94
AjK 0:98784e7c6163 95 while(1) {
AjK 0:98784e7c6163 96 myled = 1;
AjK 0:98784e7c6163 97 P0_22_SET; // Set the bool and led2on() is called.
AjK 0:98784e7c6163 98 wait(0.2);
AjK 0:98784e7c6163 99
AjK 0:98784e7c6163 100 myled = 0;
AjK 0:98784e7c6163 101 P0_22_CLR; // Clear the bool and led2off() is called.
AjK 0:98784e7c6163 102 wait(0.2);
AjK 0:98784e7c6163 103 }
AjK 0:98784e7c6163 104 }
AjK 0:98784e7c6163 105
AjK 0:98784e7c6163 106 #endif