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.

Problem with PWM for brightness controlled LED

Status
Not open for further replies.

c36041254

Member
Hi there,
I'm trying to make an LED blinking program with controlled brightness by PWM, I'm using 16f690 low pin count demo board of Microchip, four LEDs connected at PORTC (<0-3>), Here is my code, please ignore those routines labled timer<1-4> I really messed up with that but that will give you an idea of what I'm trying to do.........
Code:
List p=16f690
	#include<p16f690.inc>
	__CONFIG _MCLRE_ON & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
	ERRORLEVEL-302
;**********************************************************************
	cblock 0x20
		temp
		d1
		d2
		don
		doff
		dis
	endc
;***********************************************************************
	org 0x00
	goto main
;***********************************************************************
main:
portset:
	bsf STATUS,RP0
	clrf TRISA 
	clrf TRISB
	clrf TRISC
	movlw b'00000111'
	movwf OPTION_REG
	bcf STATUS,RP0
start:
	call bright
	movlw b'00000000'
	movwf PORTC
	call delay
	goto start
delay:
	decfsz d1
	goto delay
	decfsz d2
	goto delay
	return
bright:
	movlw d'56'
	movwf don
	movlw b'00000001'
	movwf PORTC
	call delayon
	movlw d'200'
	movwf doff
	movlw b'00000000'
	movwf PORTC
	call delayoff
timer1:
	btfss INTCON,T0IF
	goto timer1
	banksel OPTION_REG
	bsf OPTION_REG,T0CS
	banksel INTCON
	bcf INTCON,T0IF
timer2:
	banksel OPTION_REG
	bcf OPTION_REG,T0CS
	btfss INTCON,T0IF
	goto timer2
	bsf OPTION_REG,T0CS
	banksel INTCON
	bcf INTCON,T0IF
timer3:
	banksel OPTION_REG
	bcf OPTION_REG,T0CS
	btfss INTCON,T0IF
	goto timer3
	bsf OPTION_REG,T0CS
	banksel INTCON
	bcf INTCON,T0IF
timer4:
	banksel OPTION_REG
	bcf OPTION_REG,T0CS
	btfss INTCON,T0IF
	goto timer4
	bsf OPTION_REG,T0CS
	banksel INTCON
	bcf INTCON,T0IF
	return
	
delayon:
	decfsz don
	goto delayon
	return
delayoff:
	decfsz doff
	goto delayoff
	return
	end
now I know the problem (I think so) and the solution too, but don't know how to implement it. The problem is that the "bright" routine.........
Code:
bright:
	movlw d'56'
	movwf don
	movlw b'00000001'
	movwf PORTC
	call delayon
	movlw d'200'
	movwf doff
	movlw b'00000000'
	movwf PORTC
	call delayoff
SHOULD KEEP RUNNING during the "delay" period

Code:
delay:
	decfsz d1
	goto delay
	decfsz d2
	goto delay
	return
, now I can either use TIMER set for time equal to delay and loop the "bright" until the T0IF flag get set (that is what I tried with those ugly timer<1-4> routine, ps ignore them)...............
OR,
I can compare the value of "d2" with comparator but, that too uses timer values to compare and the "delay " routine is of 24mSec so don't know how to do these both.
If any one have good links (with .ASM not C) to learn comparator and PWM then please guide me. I'll really appreciate any help.
 
Hi,
How many LED are there you want them PWM controlled?
You can use hardware PWM available in the microcontroller.
For proper delay routine, you can refer to to get the flashing frequency. While for the PWM frequency or duty cycle, your code is okay. In fact you can use only one delay routine for the PWM on and off, just put different value into the counting register.
 
Hi bananasiong,
you said my code is o.k. but is it so with those timer<1-4> routine or excluding them (I did tell to ignore them ), , and yes using PWM is exactly for which I need an easy tutorial because I did notice and refer to that PWM/COMPARATOR module of the PIC on data sheet but I couldn't make much out of it I mean the PWM gives output to PORT while I need "internal output" ,software PWM, is that possible?

EDIT:
How one can make delay of more than 65536uSec with TIMER0 ?
btw there are four LEDs
HTML:
four LEDs connected at PORTC (<0-3>
 
Last edited:
This is my latest code ...
Code:
List p=16f690
	#include<p16f690.inc>
	__CONFIG _MCLRE_ON & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
	ERRORLEVEL-302
;**********************************************************************
	cblock 0x20
		temp
		d1
		d2
		don
		doff
		dis
	endc
;***********************************************************************
	org 0x00
	goto main
;***********************************************************************
main:
portset:
	bsf STATUS,RP0
	clrf TRISA 
	clrf TRISB
	clrf TRISC
	movlw b'00000111'
	movwf OPTION_REG
	bcf STATUS,RP0
start:
	banksel INTCON
	btfss INTCON,T0IF
	goto start
	bcf INTCON,T0IF
	banksel PORTC
	call bright
	movlw b'00000000'
	movwf PORTC
	call delay
	clrf dis
	movlw d'56'
	movwf doff
	movlw d'200'
	movwf don
	goto start
delay:
	decfsz d1
	goto delay
	decfsz d2
	goto delay
	return
bright:
	incf dis,f
	movlw b'00000001'
	movwf PORTC
	call delayon
	movlw b'00000000'
	movwf PORTC
	call delayoff
	btfss dis,0x03
	goto start
	return
delayon:
	decfsz don
	goto delayon
	return
delayoff:
	decfsz doff
	goto delayoff
	return
	end
The LED does blink with low brightness but in middle it flickers very fast a while in every blink.
 
Last edited:
O.K. I solved it, but stlil would like to know if there is an easier method.

Code:
 List p=16f690
	#include<p16f690.inc>
	__CONFIG _MCLRE_ON & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
	ERRORLEVEL-302
;**********************************************************************
	cblock 0x20
		temp
		d1
		d2
		don
		doff
		dis
	endc
;***********************************************************************
	org 0x00
	goto main
;***********************************************************************
main:
portset:
	bsf STATUS,RP0
	clrf TRISA 
	clrf TRISB
	clrf TRISC
	bcf STATUS,RP0
start:					; THE BASIC FUNDA OF THIS PROGRAM IS THAT LOOP
	call bright			;"bright" SHOULD KEEP RUNNING DURING "delay" OR IN OTHER
	movlw b'00000000'	;WORDS EQUAL TO THAT TIME PERIOD WHICH IS USED IN HERE.
	movwf PORTC
	call delay
	goto start
delay:
	decfsz d1
	goto delay
	decfsz d2
	goto delay
	return
bright:
	incf dis,f
	movlw b'00000001'
	movwf PORTC
	call delayons
	movlw b'00000000'
	movwf PORTC
	call delayoffs
	btfss dis,0x00
	goto bright
	btfss dis,0x01
	goto bright
	btfss dis,0x02
	goto bright
	btfss dis,0x03
	goto bright
	btfss dis,0x04
	goto bright
	btfss dis,0x05
	goto bright	
	btfss dis,0x06
	goto bright
	btfss dis,0x07
	goto bright
	clrf dis
	return
delayons:
	movlw d'36'
	movwf don
delayon:
	decfsz don
	goto delayon
	return
delayoffs:
	movlw d'220'
	movwf doff
delayoff:
	decfsz doff
	goto delayoff
	return
	end
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top