Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 3rd December 2007, 02:50 AM   (permalink)
Question PWM Smoothing Coding Problem

Hi! I’m working with a simple delay loop that can generate a PWM part (without a PWM module).

My target is to fade a LED smoothly.

I have a pair of tested values. By loading one of these values & call Main routine it will show the LED dim or full brightness.

What I need is to change DIM values to BRIGHT values nicely (linear) to make the LED fade.

Any method greatly appreciated.

Thanks


Code:
LED is very DIM by loading these values

;DIM		movlw	.255
;		movwf	SaveONtime
;		movlw	.1
;		movwf	saveONcount
;		movlw	.1
;		movwf	SaveOFFtime
;		movlw	.40
;		movwf	saveOFFcount


LED is fully BRIGHT by loading these values

;BRIGHT	movlw	.1
;		movwf	SaveONtime
;		movlw	.40
;		movwf	saveONcount
;		movlw	.255
;		movwf	SaveOFFtime
;		movlw	.1
;		movwf	saveOFFcount


Main		call	ON
		call	OFF
		goto	 Main


ON		movlw	0xff		;turn on leds
		movwf	PORTB			
		movf	SaveONtime,W		
		movwf	ONtime
		movf	saveONcount,W
		movwf	ONcount						
loopON		incfsz	ONtime,F	;count up the on time
		goto	loopON
		decfsz	ONcount,F
		goto	$-3
		return


OFF		movlw	0x00		;turn off leds
		movwf	PORTB			
		movf	SaveOFFtime,W			
		movwf	OFFtime
		movf	saveOFFcount,W
		movwf	OFFcount						
loopOFF		incfsz	OFFtime,F	;count up the off time
		goto	loopOFF
		decfsz	OFFcount,F
		goto	$-3
		return
Suraj143 is offline   Reply With Quote
Old 3rd December 2007, 09:51 AM   (permalink)
Default

Try something like this. Timer 2 is good for generating the interrupt which takes care of the PWM. Then gradually change the value of ledPwm variable from 0x00 (led off) to 0xFF (led 100% on) to fade the LED.

Code:
                ; define variables
                ;
                cblock 0x20
                                ledPwm          ; LED PWM value
                                pwmCntr         ; PWM 'ramp' counter
                                pwmWkg          ; working variable
                endc
 
 
                ; call this from a timer driven interrupt 
                ; every 40uS for 98Hz refresh.
 
_pwm            incfsz          pwmCntr,F       
                goto            _noRollover
_resetPWM       incf            pwmCntr,F
                movfw           ledPwm          
                movwf           pwmWkg
                incfsz          pwmWkg,W
                bcf             PORTA, LED
_noRollover     incf            pwmWkg,f        
                skpnz
                bsf             PORTA, LED
 
                ; exit ISR here
                ; ---------------------------------------
 
 
_loop           ; software fade delay code here
                incf            ledPwm,F
                incfsz          ledPwm,W
                goto            _loop
                ; stop when ledPWM == 0xFF
__________________
Pete
picprojects.org.uk

Last edited by geko; 3rd December 2007 at 09:55 AM.
geko is offline   Reply With Quote
Old 3rd December 2007, 10:48 AM   (permalink)
Default

Thanks geko I'll try your coding I'm working hard with my coding to get a smooth fade but I couldn't.

Now I'll check with your one.

Thanks for the help.
Suraj143 is offline   Reply With Quote
Old 5th December 2007, 04:30 AM   (permalink)
Default

Maybe this is smooth enough?

http://www.youtube.com/watch?v=isftM5mUqRU

And this is multi-smooth:

http://www.youtube.com/watch?v=aeHFavqIe7U
wschroeder is offline   Reply With Quote
Old 5th December 2007, 04:38 AM   (permalink)
Default

Quote:
Originally Posted by wschroeder
Maybe this is smooth enough?

http://www.youtube.com/watch?v=isftM5mUqRU

And this is multi-smooth:

http://www.youtube.com/watch?v=aeHFavqIe7U
Yes my friend that smooth is good.I'm doing from assembly language.Is that your circuit?
Do you have any coding ideas to share?
Suraj143 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
Keil C problem sarang1_in Micro Controllers 3 27th February 2005 05:05 AM
Problem; Random pic performance timothyjackson Micro Controllers 7 19th February 2005 06:19 PM
Big problem!! zezito Electronic Projects Design/Ideas/Reviews 4 29th October 2004 02:05 AM
Analog output PLC CQM1H problem goekgoek General Electronics Chat 0 24th June 2003 08:07 AM
Ballast / Igniter Problem O B 1 KNooB Electronic Projects Design/Ideas/Reviews 1 6th March 2003 09:22 PM



All times are GMT. The time now is 12:30 AM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.