Anthony Barison
/
GPIO_Hello_World
An example of how to initialize p20 as GPIO.
main.cpp@1:f0b14fe7874e, 2012-08-30 (annotated)
- Committer:
- a_baris
- Date:
- Thu Aug 30 12:06:20 2012 +0000
- Revision:
- 1:f0b14fe7874e
- Parent:
- 0:4652951bde46
GPIO Hello World
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
a_baris | 1:f0b14fe7874e | 1 | /* Copyright (c) 2012 Anthony Barison, MIT License |
a_baris | 1:f0b14fe7874e | 2 | * |
a_baris | 1:f0b14fe7874e | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
a_baris | 1:f0b14fe7874e | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
a_baris | 1:f0b14fe7874e | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
a_baris | 1:f0b14fe7874e | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
a_baris | 1:f0b14fe7874e | 7 | * furnished to do so, subject to the following conditions: |
a_baris | 1:f0b14fe7874e | 8 | * |
a_baris | 1:f0b14fe7874e | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
a_baris | 1:f0b14fe7874e | 10 | * substantial portions of the Software. |
a_baris | 1:f0b14fe7874e | 11 | * |
a_baris | 1:f0b14fe7874e | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
a_baris | 1:f0b14fe7874e | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
a_baris | 1:f0b14fe7874e | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
a_baris | 1:f0b14fe7874e | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
a_baris | 1:f0b14fe7874e | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
a_baris | 1:f0b14fe7874e | 17 | */ |
a_baris | 1:f0b14fe7874e | 18 | |
a_baris | 0:4652951bde46 | 19 | /* |
a_baris | 0:4652951bde46 | 20 | This simple program is written has an example to show how pins of |
a_baris | 0:4652951bde46 | 21 | mbed board can be programmed without the high-level abstraction of the |
a_baris | 0:4652951bde46 | 22 | class DigitalOut. |
a_baris | 0:4652951bde46 | 23 | |
a_baris | 0:4652951bde46 | 24 | References: |
a_baris | 0:4652951bde46 | 25 | http://www.nxp.com/documents/user_manual/UM10360.pdf |
a_baris | 0:4652951bde46 | 26 | |
a_baris | 0:4652951bde46 | 27 | Author: Anthony Barison |
a_baris | 0:4652951bde46 | 28 | First release on: 30/08/2012 |
a_baris | 0:4652951bde46 | 29 | Last release on: 30/08/2012 |
a_baris | 0:4652951bde46 | 30 | */ |
a_baris | 0:4652951bde46 | 31 | |
a_baris | 0:4652951bde46 | 32 | #include <stdio.h> |
a_baris | 0:4652951bde46 | 33 | |
a_baris | 0:4652951bde46 | 34 | /* pin 20 on the mbed board is memory mapped to port P1.31 of LPC1768 |
a_baris | 0:4652951bde46 | 35 | which I have defined as preprocessor directive. The value can be used as a mask to |
a_baris | 0:4652951bde46 | 36 | read and write the status of "p20" */ |
a_baris | 0:4652951bde46 | 37 | |
a_baris | 0:4652951bde46 | 38 | #define PIN_20 0x80000000 /* this is the mask to be used in referring to p20 on the mbed board */ |
a_baris | 0:4652951bde46 | 39 | |
a_baris | 0:4652951bde46 | 40 | #define FIO1DIR (*((volatile unsigned int *) 0x2009C020)) /* output enable register */ |
a_baris | 0:4652951bde46 | 41 | #define FIO1CLR (*((volatile unsigned int *) 0x2009C03C)) /* clear output data register */ |
a_baris | 0:4652951bde46 | 42 | #define FIO1SET (*((volatile unsigned int *) 0x2009C038)) /* set output data register */ |
a_baris | 0:4652951bde46 | 43 | |
a_baris | 0:4652951bde46 | 44 | //volatile unsigned int *ClearOutputDataReg = (unsigned int *)0xfffff434; |
a_baris | 0:4652951bde46 | 45 | |
a_baris | 0:4652951bde46 | 46 | int main() |
a_baris | 0:4652951bde46 | 47 | { |
a_baris | 0:4652951bde46 | 48 | int state = 0; /* state variable used as store of user input */ |
a_baris | 0:4652951bde46 | 49 | |
a_baris | 0:4652951bde46 | 50 | /* p20 initialization */ |
a_baris | 0:4652951bde46 | 51 | FIO1DIR = PIN_20; /* enable p20 as an output pin */ |
a_baris | 0:4652951bde46 | 52 | FIO1CLR = PIN_20; /* set 0 to p20 as a default value */ |
a_baris | 0:4652951bde46 | 53 | |
a_baris | 0:4652951bde46 | 54 | printf( "\r\nGPIO\r\nPossible inputs\r\n" ); |
a_baris | 0:4652951bde46 | 55 | printf( "0\tset p20 to 0\r\n" ); |
a_baris | 0:4652951bde46 | 56 | printf( "1\tset p20 to 0\r\n" ); |
a_baris | 0:4652951bde46 | 57 | printf( "-1\tterminate program\r\n\r\n" ); |
a_baris | 0:4652951bde46 | 58 | |
a_baris | 0:4652951bde46 | 59 | |
a_baris | 0:4652951bde46 | 60 | while(1) { |
a_baris | 0:4652951bde46 | 61 | |
a_baris | 0:4652951bde46 | 62 | |
a_baris | 0:4652951bde46 | 63 | /* read user input from terminal */ |
a_baris | 0:4652951bde46 | 64 | printf( "State = "); |
a_baris | 0:4652951bde46 | 65 | scanf( "%i", &state ); |
a_baris | 0:4652951bde46 | 66 | printf( "%i\r\n", state); |
a_baris | 0:4652951bde46 | 67 | |
a_baris | 0:4652951bde46 | 68 | |
a_baris | 0:4652951bde46 | 69 | /* */ |
a_baris | 0:4652951bde46 | 70 | if( state == 0 ) |
a_baris | 0:4652951bde46 | 71 | FIO1CLR = PIN_20; /* p20 = 2 */ |
a_baris | 0:4652951bde46 | 72 | else if (state == 1) |
a_baris | 0:4652951bde46 | 73 | FIO1SET = PIN_20; /* p20 = 1 */ |
a_baris | 0:4652951bde46 | 74 | else if (state == -1) |
a_baris | 0:4652951bde46 | 75 | return 0; /* terminate program */ |
a_baris | 0:4652951bde46 | 76 | else |
a_baris | 0:4652951bde46 | 77 | continue; |
a_baris | 0:4652951bde46 | 78 | } |
a_baris | 0:4652951bde46 | 79 | } |