Microcontroller 51 - the LED lights

We first look at the schematics LED lights

 

 

 FIG LED is our LED lights, then the left diode VCC, then we just put the right goes low P2x can light the corresponding two) #include " reg52.h " // defined in this file SCM some special function registers


LED the sbit = P2 ^ 0 ;        // the P0.0 port of microcontroller as defined led. sbit defined variable bit special function register, we can operate on P20, here corresponding to the first LED. 

/ * **************************** ****************************** 
* function name: main 
* function: The main function 
* input: None 
* output: None 
* ************************************************** *************************** * / 
void main () 
{ 
    LED = 0 ;     // P2.0 port is set to a low level, then the LED light will light 
    the while ( 1); // the while loop, the microcontroller and we usually do not write a program, we need to keep the microcontroller an infinite loop, which is to write code that must happen while loop        
}

Compiled .hex file, then you can burn up.

If you want a second lights, then we only need to sbit led2 = P2 ^ 1; then this led2 operate on it.

If I want all the lights are on, I need to use a lot of sbit to define the eight ports?

Is not actually required, we can use this to operate #define LED P2 P2 port.

#include " reg52.h "     // This file defines the special function registers of the microcontroller 
#define the LED P2;        // the microcontroller port P2 is defined as the LED 


void main () 
{ 
    the LED = 0x 0 ;     // 0x0 16 hexadecimal, binary shall be changed to 00 million, which represents the P2 ^ 0 0 assigned, the assignment P2 ^ 1 0 ......
 while(1);
}

8 led lights so bright on the whole, if I just want a few of which lights, then the LED assignment can be, for example, says that 1011 0010,0 corresponding LED lights, 1 means no light, into hexadecimal as 0xb2.

LED = 0xb2; b assigned to the upper four bits of the LED, i.e. P2 ^ 7 ~ P2 ^ 4,2 assigned to the fourth, the first result is a 1,3,4,7 led lights.

 

 

 

 (Not a semicolon after the #define statement)

Guess you like

Origin www.cnblogs.com/Tayl0r-Swift/p/12441565.html