Anthony Barison
/
GPIO_Hello_World
An example of how to initialize p20 as GPIO.
main.cpp@0:4652951bde46, 2012-08-30 (annotated)
- Committer:
- a_baris
- Date:
- Thu Aug 30 12:02:40 2012 +0000
- Revision:
- 0:4652951bde46
- Child:
- 1:f0b14fe7874e
GPIO hello world.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
a_baris | 0:4652951bde46 | 1 | /* |
a_baris | 0:4652951bde46 | 2 | This simple program is written has an example to show how pins of |
a_baris | 0:4652951bde46 | 3 | mbed board can be programmed without the high-level abstraction of the |
a_baris | 0:4652951bde46 | 4 | class DigitalOut. |
a_baris | 0:4652951bde46 | 5 | |
a_baris | 0:4652951bde46 | 6 | References: |
a_baris | 0:4652951bde46 | 7 | http://www.nxp.com/documents/user_manual/UM10360.pdf |
a_baris | 0:4652951bde46 | 8 | |
a_baris | 0:4652951bde46 | 9 | Author: Anthony Barison |
a_baris | 0:4652951bde46 | 10 | First release on: 30/08/2012 |
a_baris | 0:4652951bde46 | 11 | Last release on: 30/08/2012 |
a_baris | 0:4652951bde46 | 12 | */ |
a_baris | 0:4652951bde46 | 13 | |
a_baris | 0:4652951bde46 | 14 | #include <stdio.h> |
a_baris | 0:4652951bde46 | 15 | |
a_baris | 0:4652951bde46 | 16 | /* pin 20 on the mbed board is memory mapped to port P1.31 of LPC1768 |
a_baris | 0:4652951bde46 | 17 | which I have defined as preprocessor directive. The value can be used as a mask to |
a_baris | 0:4652951bde46 | 18 | read and write the status of "p20" */ |
a_baris | 0:4652951bde46 | 19 | |
a_baris | 0:4652951bde46 | 20 | #define PIN_20 0x80000000 /* this is the mask to be used in referring to p20 on the mbed board */ |
a_baris | 0:4652951bde46 | 21 | |
a_baris | 0:4652951bde46 | 22 | #define FIO1DIR (*((volatile unsigned int *) 0x2009C020)) /* output enable register */ |
a_baris | 0:4652951bde46 | 23 | #define FIO1CLR (*((volatile unsigned int *) 0x2009C03C)) /* clear output data register */ |
a_baris | 0:4652951bde46 | 24 | #define FIO1SET (*((volatile unsigned int *) 0x2009C038)) /* set output data register */ |
a_baris | 0:4652951bde46 | 25 | |
a_baris | 0:4652951bde46 | 26 | //volatile unsigned int *ClearOutputDataReg = (unsigned int *)0xfffff434; |
a_baris | 0:4652951bde46 | 27 | |
a_baris | 0:4652951bde46 | 28 | int main() |
a_baris | 0:4652951bde46 | 29 | { |
a_baris | 0:4652951bde46 | 30 | int state = 0; /* state variable used as store of user input */ |
a_baris | 0:4652951bde46 | 31 | |
a_baris | 0:4652951bde46 | 32 | /* p20 initialization */ |
a_baris | 0:4652951bde46 | 33 | FIO1DIR = PIN_20; /* enable p20 as an output pin */ |
a_baris | 0:4652951bde46 | 34 | FIO1CLR = PIN_20; /* set 0 to p20 as a default value */ |
a_baris | 0:4652951bde46 | 35 | |
a_baris | 0:4652951bde46 | 36 | printf( "\r\nGPIO\r\nPossible inputs\r\n" ); |
a_baris | 0:4652951bde46 | 37 | printf( "0\tset p20 to 0\r\n" ); |
a_baris | 0:4652951bde46 | 38 | printf( "1\tset p20 to 0\r\n" ); |
a_baris | 0:4652951bde46 | 39 | printf( "-1\tterminate program\r\n\r\n" ); |
a_baris | 0:4652951bde46 | 40 | |
a_baris | 0:4652951bde46 | 41 | |
a_baris | 0:4652951bde46 | 42 | while(1) { |
a_baris | 0:4652951bde46 | 43 | |
a_baris | 0:4652951bde46 | 44 | |
a_baris | 0:4652951bde46 | 45 | /* read user input from terminal */ |
a_baris | 0:4652951bde46 | 46 | printf( "State = "); |
a_baris | 0:4652951bde46 | 47 | scanf( "%i", &state ); |
a_baris | 0:4652951bde46 | 48 | printf( "%i\r\n", state); |
a_baris | 0:4652951bde46 | 49 | |
a_baris | 0:4652951bde46 | 50 | |
a_baris | 0:4652951bde46 | 51 | /* */ |
a_baris | 0:4652951bde46 | 52 | if( state == 0 ) |
a_baris | 0:4652951bde46 | 53 | FIO1CLR = PIN_20; /* p20 = 2 */ |
a_baris | 0:4652951bde46 | 54 | else if (state == 1) |
a_baris | 0:4652951bde46 | 55 | FIO1SET = PIN_20; /* p20 = 1 */ |
a_baris | 0:4652951bde46 | 56 | else if (state == -1) |
a_baris | 0:4652951bde46 | 57 | return 0; /* terminate program */ |
a_baris | 0:4652951bde46 | 58 | else |
a_baris | 0:4652951bde46 | 59 | continue; |
a_baris | 0:4652951bde46 | 60 | } |
a_baris | 0:4652951bde46 | 61 | } |