Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How can i smooth a PWM fading led?

Status
Not open for further replies.

whiz115

Member
Hi!

I decided to get some experience on uCs, so yesterday i did my first programmer a JDM..i also bought an 16F628A and i started with a blinking led :D and then i advanced to a PWM fading led :p now what i want to do is to make the led fade in and out smooth because it's jerky enough.

The code is from this site
**broken link removed**

what should i change?
Also is there any other code which explains what's going on? i have some very basic knowledge on 8086 assembly from past years but it doesn't help much because the instructions are totaly different.
 
Try changing the code to,
Code:
;-------------------------- asteapta puls de 1ms
	movlw	0
uu	movwf	CCPR1L
	call	pause
	incfsz	CCPR1L,w
	goto	uu
	movlw	0xff
Dw	movwf	CCPR1L
	call	Pause
	decfsz	CCPR1L,w
	goto	Dw
	goto	uu
		
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
set_timer

This should make for a very gradual change. The value in CCPR1L is the brightness of the LED.

Mike.
 
now it's very slow...

on your code i did a minor change to "movlw 0xFF" with "movlw 0x02"
i saw a difference but it wasn't significant... it is a little bit faster
but when it is about to fade out it gets sudden.
 
can someobody please help me? this is my first try on microcontrolers and i want to understand few things...

thanks! :)
 
This will be 4 times faster. I have also commented it so you should be able to follow what it's doing.
Code:
;-------------------------- asteapta puls de 1ms
Speed 	equ	.04
	movlw	0		;start at zero
uu	movwf	CCPR1L		;put W into CCP
	call	pause		
	movlw	Speed		;put speed in W
	addwf	CCPR1L,w	;and add it to CCP value - result in W
	btfss	STATUS,Z	;if W=0 then skip next line
	goto	uu		;go back and do it again
	movlw	.256-Speed	;0x100-speed = full brightness
Dw	movwf	CCPR1L		;into CCP
	call	Pause
	movlw	Speed		;put Speed in W
	addwf	CCPR1L,w	;and add it to CCP value - result in W
	btfss	STATUS,Z	;if W=0 then skip next line
	goto	Dw		;loop until LED is out
	goto	uu		;Start all over again
		
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
set_timer

You can change the value of Speed but it must be a power of 2. I.E 2,4,8,16 etc

Hope that helps.

Mike.
 
It's almost perfect now!! :) but i've just noticed that this program i got from that site only fades in... it doesn't fade out. :D

i guess i must change something to your code and then copy/paste it one more time underneath the first and then make a loop all of that portion right?

but what should i change
 
Whoops,

Made a mistake, this should now fade both ways.

Code:
;-------------------------- asteapta puls de 1ms
Speed 	equ	.04
	movlw	0		;start at zero
uu	movwf	CCPR1L		;put W into CCP
	call	pause		
	movlw	Speed		;put speed in W
	addwf	CCPR1L,w	;and add it to CCP value - result in W
	btfss	STATUS,Z	;if W=0 then skip next line
	goto	uu		;go back and do it again
	movlw	.256-Speed	;0x100-speed = full brightness
Dw	movwf	CCPR1L		;into CCP
	call	Pause
	movlw	.256-Speed	;put -Speed in W
	addwf	CCPR1L,w	;and add it to CCP value - result in W
	btfss	STATUS,Z	;if W=0 then skip next line
	goto	Dw		;loop until LED is out
	goto	uu		;Start all over again
		
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
set_timer

Mike.
 
now it fades out faster than it fades in, but it's ok i don't mind much for that :)

thanks for your help Pommie!

P.S if you know any good sites about Assembly and they are apropriate for begginers, i'm interested!
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top