Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - The MCU teacher asked us to control the LED lights to increase the brightness slowly.
The MCU teacher asked us to control the LED lights to increase the brightness slowly.
//Variables are too messy. Let me give you an example.

# include & ltreg52.h & gt// contains header files, which generally do not need to be changed. The header file contains definitions of special function registers.

sbit led0=p 1^0; //Define LED to P 1.0 port with sbit keyword. LED is an arbitrarily defined symbol, which is easy to remember.

Invalid delay (unsigned integer t); //Function declaration

/* -

major function

- */

Invalid master (invalid)

{

Unsigned integer period =600, PWM _ LOW = 0;; //Define the period and assign a value.

While (1) // main loop

{

led 0 = 1;

Delay (60000); //deliberately delay, you can see the process of extinguishing.

for(PWM _ LOW = 1; PWM _ LOW< cycle; PWM_LOW++){ //PWM_LOW stands for low level.

//level time, in which the duration of low level is accumulated from 1 to the value of CYCLE, that is, 600 times.

LED0 = 0// lights the led.

Delay (PWM _ low); //The delay length is increased from 1 to 599 in 600 cycles.

led 0 = 1; //Turn off the LED

Delay (period -PWM _ low level); //Delay length, 600 cycles reduced from 599 to 1.

}

LED0 = 0

for(PWM _ LOW = CYCLE- 1; PWM _ LOW & gt0; PWM _ LOW-){// and the process of gradual phase change.

LED0 = 0

Delay (PWM _ low);

led 0 = 1;

Delay (period -PWM _ low level);

}

//Add other programs that need to work all the time in the main loop.

}

}

/* -

Delay function, the input parameter is unsigned integer t, and there is no return value.

Unsigned int is an unsigned integer variable whose value range is

0~65535

- */

Invalid delay (unsigned integer t)

{

while(-t);

}