by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"

Dependencies:   mbed

Committer:
robt
Date:
Sun Jun 16 15:41:23 2013 +0000
Revision:
0:a618e508f7e5
by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"

Who changed what in which revision?

UserRevisionLine numberNew contents of line
robt 0:a618e508f7e5 1 /* Program Example 14.3: Uses digital input and output using control registers, and flashes an LED. LEDS connect to mbed pins 25 and 26. Switch input to pin 9.
robt 0:a618e508f7e5 2 */
robt 0:a618e508f7e5 3 // function prototypes
robt 0:a618e508f7e5 4 void delay(void);
robt 0:a618e508f7e5 5 //Define Digital I/O registers
robt 0:a618e508f7e5 6 #define FIO0DIR0 (*( volatile unsigned char *)(0x2009C000))
robt 0:a618e508f7e5 7 #define FIO0PIN0 (*( volatile unsigned char *)(0x2009C014))
robt 0:a618e508f7e5 8 #define FIO2DIR0 (*(volatile unsigned char *)(0x2009C040))
robt 0:a618e508f7e5 9 #define FIO2PIN0 (*(volatile unsigned char *)(0x2009C054))
robt 0:a618e508f7e5 10 //some variables
robt 0:a618e508f7e5 11 char a;
robt 0:a618e508f7e5 12 char b;
robt 0:a618e508f7e5 13 char i;
robt 0:a618e508f7e5 14
robt 0:a618e508f7e5 15 int main() {
robt 0:a618e508f7e5 16 FIO0DIR0=0x00; // set all bits of port 0 byte 0 to input
robt 0:a618e508f7e5 17 FIO2DIR0=0xFF; // set port 2 byte 0 to output
robt 0:a618e508f7e5 18 while(1) {
robt 0:a618e508f7e5 19 if (FIO0PIN0&0x01==1){ // bit test port 0 pin 0 (mbed pin 9)
robt 0:a618e508f7e5 20 a=0x01; // this reverses the order of LED flashing
robt 0:a618e508f7e5 21 b=0x02; // based on the switch position
robt 0:a618e508f7e5 22 }
robt 0:a618e508f7e5 23 else {
robt 0:a618e508f7e5 24 a=0x02;
robt 0:a618e508f7e5 25 b=0x01;
robt 0:a618e508f7e5 26 }
robt 0:a618e508f7e5 27 FIO2PIN0 |= a;
robt 0:a618e508f7e5 28 delay();
robt 0:a618e508f7e5 29 FIO2PIN0 &= ~a;
robt 0:a618e508f7e5 30 delay();
robt 0:a618e508f7e5 31
robt 0:a618e508f7e5 32 for (i=1;i<=3;i++){
robt 0:a618e508f7e5 33 FIO2PIN0 |= b;
robt 0:a618e508f7e5 34 delay();
robt 0:a618e508f7e5 35 FIO2PIN0 &= ~b;
robt 0:a618e508f7e5 36 delay();
robt 0:a618e508f7e5 37 }
robt 0:a618e508f7e5 38 } //end while loop
robt 0:a618e508f7e5 39 }
robt 0:a618e508f7e5 40
robt 0:a618e508f7e5 41 void delay(void){ //delay function.
robt 0:a618e508f7e5 42 int j; //loop variable j
robt 0:a618e508f7e5 43 for (j=0; j<1000000; j++) {
robt 0:a618e508f7e5 44 j++;
robt 0:a618e508f7e5 45 j--; //waste time
robt 0:a618e508f7e5 46 }
robt 0:a618e508f7e5 47 }