Embedded之Introduction

 

1  Embedded system

  An embedded system is a combination of computer hardware and software, and perhaps additional mechanical or other parts, designed to perform a specific function. (E.g. microwave oven)

 

2  Infinite loop

  One of the most fundamental differences between programs developed for embedded systems and those written for other computer platforms is that the embedded programs almost always end with an infinite loop.

  The infinite loop is necessary because the embedded software's job is never done. It is intended to be run until either the world comes to an end or the board is reset, whichever happens first.

 

3  Blinking LED

 1 /********************************************************************** 

 2  * 

 3  * Function:    main() 

 4  * 

 5  * Description: Blink the green LED once a second. 

 6  *  

 7  * Notes:       This outer loop is hardware-independent.  However,  

 8  *              it depends on two hardware-dependent functions. 

 9  * 

10  * Returns:     This routine contains an infinite loop. 

11  * 

12  **********************************************************************/ 

13 void main(void) 

14 { 

15     while (1) 

16     { 

17         toggleLed(LED_GREEN);      /* Change the state of the LED.    */ 

18         delay(500);                /* Pause for 500 milliseconds.     */ 

19     } 

20 }

 

  3.1  toggleLed

 1 /********************************************************************** 

 2  * 

 3  * Function:    toggleLed() 

 4  * 

 5  * Description: Toggle the state of one or both LEDs. 

 6  * 

 7  * Notes:       This function is specific to Arcom's Target188EB board. 

 8  * 

 9  * Returns:     None defined. 

10  * 

11  **********************************************************************/ 

12  

13 #define LED_GREEN   0x40        /* The green LED is controlled by bit 6.*/ 

14 #define P2LTCH      0xFF5E      /* The offset of the P2LTCH register. */ 

15 

16 void toggleLed(unsigned char ledMask) 

17 { 

18     asm { 

19         mov dx, P2LTCH          /* Load the address of the register.  */ 

20         in  al, dx              /* Read the contents of the register. */ 

21  

22         mov ah, ledMask         /* Move the ledMask into a register.  */ 

23         xor al, ah              /* Toggle the requested bits.         */ 

24  

25         out dx, al              /* Write the new register contents.   */ 

26     }; 

27  

28 }   /* toggleLed() */ 

 

  3.2  delay

 1 /********************************************************************** 

 2  * 

 3  * Function:    delay() 

 4  * 

 5  * Description: Busy-wait for the requested number of milliseconds. 

 6  * 

 7  * Notes:       The number of decrement-and-test cycles per millisecond 

 8  *              was determined through trial and error.  This value is 

 9  *              dependent upon the processor type and speed. 

10  * 

11  * Returns:     None defined. 

12  * 

13  **********************************************************************/ 

14  

15 void delay(unsigned int nMilliseconds) 

16 { 

17     #define CYCLES_PER_MS 260 /* Number of decrement-and-test cycles. */ 

18  

19     unsigned long nCycles = nMilliseconds * CYCLES_PER_MS; 

20  

21     while (nCycles--); 

22  

23 }   /* delay() */ 

 

你可能感兴趣的:(EMBED)