4656 views|17 replies

86

Posts

0

Resources
The OP
 

The microcontroller has its own PWM to control the brightness of the LED light. How to write it? ? [Copy link]

 

The register configuration has been configured. I just started learning microcontrollers and just came into contact with PWM. Now the register configuration is done, but how can the LED turn on and off later? I don't know how to write it.

I also know that the time that the high level occupies the entire cycle is the duty cycle. But how should it be written? ? ? ? ? ? ? ? ? ? Help! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !

This post is from 51mcu

Latest reply

Your PWM changes the switching time of the driving circuit within a unit of time, thereby adjusting the voltage of the driving circuit. The voltage changes the brightness of the lamp.   Details Published on 2019-12-9 09:39
 

650

Posts

8

Resources
2
 

Add a while (1) loop, change the duty cycle from 0 to 100 every 100ms, and the light will normally change from on to off.

This post is from 51mcu

Comments

I tried it. It didn't work. I changed the value manually.  Details Published on 2019-10-17 17:47
 
 
 

86

Posts

0

Resources
3
 
29447945 Published on 2019-10-17 17:26 Add a while (1) loop, change the duty cycle at intervals of a certain period of time, such as 100ms, from 0 to 100 duty cycle, the light will normally change from bright to dark

I tried it. It didn't work. I changed the value manually.

This post is from 51mcu

Comments

Was a delay added?  Details Published on 2019-10-17 21:34
 
 
 

650

Posts

8

Resources
4
 
Myself One Hundred Years Later posted on 2019-10-17 17:47 I tried it. It didn't work. I changed the value manually
Did you add a delay?
This post is from 51mcu

Comments

No addition  Details Published on 2019-10-18 09:06
 
 
 

4005

Posts

0

Resources
5
 

Just modify the duty cycle register

This post is from 51mcu
 
 
 

4005

Posts

0

Resources
6
 

Is the configuration register locked?

This post is from 51mcu

Comments

Can I just edit this part?  Details Published on 2019-10-18 09:07
 
 
 

86

Posts

0

Resources
7
 

No addition

This post is from 51mcu

Comments

Each time you change the duty cycle, you have to delay it, otherwise you won't be able to see the change at all.  Details Published on 2019-10-18 09:10
 
 
 

86

Posts

0

Resources
8
 
huo_hu posted on 2019-10-18 08:16 Is the configuration register locked?

Can I just edit this part?

image.png (17.95 KB, downloads: 0)

image.png
This post is from 51mcu
 
 
 

650

Posts

8

Resources
9
 

Each time you change the duty cycle, you have to delay it, otherwise you won't be able to see the change at all.

This post is from 51mcu

Comments

[attachimg]438865[/attachimg] This is the register configuration I changed. Does it take a delay to change the duty cycle each time? ? ?  Details Published on 2019-10-18 09:38
[attachimg]438865[/attachimg] This is the register configuration I changed. Does it take a delay to change the duty cycle each time? ? ?  Details Published on 2019-10-18 09:26
 
 
 

86

Posts

0

Resources
10
 
29447945 Published on 2019-10-18 09:10 Every time you change the duty cycle, you need to delay it, otherwise you can't see the change process at all

This is the register configuration I just changed.

Does each change of duty cycle require a delay?

This post is from 51mcu
 
 
 

86

Posts

0

Resources
11
 
29447945 Published on 2019-10-18 09:10 Every time you change the duty cycle, you need to delay it, otherwise you can't see the change process at all

Then I put it in the main function, and the while loop is fine. I also declared variables for the period and duty cycle. Changing the values in the main function should have an effect. But I didn't declare the change.

This post is from 51mcu

Comments

Here is the idea, you need to implement these functions yourself. void main() { int pwm_value = 0; pwm_init(); while(1) { set_pwm(pwm_value ); pwm_value += 10; if(pwm_value >= 100) pwm_value  Details Published on 2019-10-18 09:50
 
 
 

650

Posts

8

Resources
12
 
Myself a hundred years later posted on 2019-10-18 09:38 Then I put it in the main function, while looping all the time, right? And I declared variables for period and duty cycle, and then in the main function...

Here is the idea, you have to implement these functions yourself.

void main()

{

int pwm_value = 0;

pwm_init();

while(1)

{

set_pwm(pwm_value );

pwm_value += 10;

if(pwm_value >= 100)

pwm_value = 0;

delay_10ms();

}

}

This post is from 51mcu

Comments

OK, thanks  Details Published on 2019-10-18 11:24
OK, thanks  Details Published on 2019-10-18 10:04
 
 
 

86

Posts

0

Resources
13
 
29447945 Published on 2019-10-18 09:50 Here is the idea, you have to implement these functions yourself. void main() { int pwm_value = 0; pwm_init(); ...

OK, thanks

This post is from 51mcu
 
 
 

86

Posts

0

Resources
14
 
29447945 Published on 2019-10-18 09:50 Here is the idea, you have to implement these functions yourself. void main() { int pwm_value = 0; pwm_init(); ...

If I don't set the function at the beginning, and I directly change the duty cycle value, will the brightness of the LED lamp change? ? ? ? ? ? ?

This post is from 51mcu

Comments

Of course, you have to set the timer first, and then change the duty cycle. If you don't set the timer, it won't work. The delay time can be extended.  Details Published on 2019-10-18 12:17
 
 
 

650

Posts

8

Resources
15
 
Myself a hundred years later posted on 2019-10-18 11:24 If I don't set the function at the beginning, and I directly change the value of the duty cycle, will the brightness of the LED light change? ??? ??? ??? ???

Of course, you have to set the timer first, and then change the duty cycle. If you don't set the timer, it won't work. The delay time can be extended.

This post is from 51mcu

Comments

The timer is set, but now I have another question. I use an oscilloscope to see the high and low levels. The main question is, after I configure the register, do I still need to use io to control the LED light? Or can PWM directly turn on the LED? If it can be turned on directly, why doesn't the LED light up when I burn the program? I also enable PWM.  Details Published on 2019-10-18 13:42
 
 
 

86

Posts

0

Resources
16
 
29447945 Published on 2019-10-18 12:17 Of course, you have to set the timer first, and then change the duty cycle before you can use it. If you don't set the timer, it won't work. The delay time can be extended a bit

The timer has been set, but now I have another question. I used an oscilloscope to see high and low levels.

The main thing is whether I need to use io to control the LED light after I configure the register.

Or can PWM directly dim the LED? If it can be dimmed directly, why doesn't the LED light up after I program it? I also turned on the PWM enable.

This post is from 51mcu
 
 
 

2

Posts

0

Resources
17
 

PWM can directly brighten and dim the LED, which is related to the driving lamp circuit.

This post is from 51mcu
 
 
 

2

Posts

0

Resources
18
 

Your PWM changes the switching time of the driving circuit within a unit of time, thereby adjusting the voltage of the driving circuit. The voltage changes the brightness of the lamp.

This post is from 51mcu
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list