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.

PWM Smoothing Coding Problem

Status
Not open for further replies.

Suraj143

Active Member
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
 
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
 
Last edited:
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.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top