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.

Stupid question

Status
Not open for further replies.

GForce2010

New Member
As the title says i expect this is going to be a stupid question by most of your standards.

Basically i've made my own light strip using 12V SMT LEDS which is being controlled by a PIC16f628.

Now i can easily program in all kinds of light patterns by flashing the LEDS but what i really want to do now is be able to dim them. After having a look on here and google i learned about the exsitance of PWM. So i know how the theroy works but now need to put that into practise. Problem being is i haven't been able to find anywhere where i can get a nice simple walkthrough of how to make this work. Also i'm not even sure if that's something i can do with the PIC i'm using at the moment.

Any help or pointers to a site with more info that a can use would be greatly appreciated.

Also if you need to know anything more about my circuit i'll be happy to supply all the info i have.

Once again a apoligise if this is a stupid question but we all had to start somewhere.

Thanks.
 
Here's a very simple asm program I wrote that shows how to initialize and use PWM on a 628. The 628 only has one channel of PWM on the RB3/CCP1 pin. If you need more than one pin you'll have to write your own software PWM for it.
Code:
	processor 16f628
	include "P16F628.inc"
	__config _HS_OSC & _WDT_OFF & _LVP_OFF

	cblock	0x20		;start of general purpose registers
	d1,d2,d3,count
	endc

	org		0x0000

init	clrf	count
	movlw	0x07			;turn comparators off
	movwf	CMCON
	bsf 	STATUS,RP0		;bank 1
	movlw	0x00			;all pins outputs
	movwf	TRISA
	movwf	TRISB
	movlw	0xff			;set period
	movwf	PR2
	bcf 	STATUS,RP0		;bank 0

main	movlw	0x80			;set duty cycle
	movwf	CCPR1L
	movlw	0x04			;set timer2 prescale and enable it
	movwf	T2CON
	movlw	0x0c			;set bottom 2 pwm bits and enable pwm
	movwf	CCP1CON
	call	delay

mloop	movf	count,w			;cycles msb 8 bits of pwm from 0 to 255
	movwf	CCPR1L			;over and over
	call	delay
	incfsz	count
	goto	mloop
reset	clrf	count
	goto	mloop	

delay	movlw	0x2d			;around 0.1 second or a bit less
	movwf	d1
	movlw	0xe7
	movwf	d2
	movlw	0x01
	movwf	d3
delay_0	decfsz	d1, f
	goto	dd2
	decfsz	d2, f
dd2	goto	dd3
	decfsz	d3, f
dd3	goto	delay_0
	return

	end
 
Well my circuit is designed that each pin of port B is connected to a transistor that connects a group of LEDs to ground.

So if i wanted to be able to dim each group induvidualy would i have to set up a different PWM on each pin? And how would i go about doing this.

Think i might have to try and find a book or site where i can increase my knowlege of assembly.
 
off the top of my head, you could 'and' the output of the each of the portB pins with the pwm signal, and use the output of the and gates to drive the transistors. it would be very crude and require the logic chips, im quite sure someone will chime in with a much neater solution.

an alternative would be to add a transistor (high enough rated to pass the current for ALL leds being on) between the ground and the what is currently the ground connection for the leds.

use pwm to control this transistor, it should equally dim all the leds
 
Well to be totally honest most of that just went over my head.

think i might have to postpone this untill i learn a bit more about the progame code.

Does anyone have any suggested reading material which will help me get a better understanding of assembly?
 
The best way to understand it is to first understand PWM. Imagine a LED flashing on and off. If it is on the same amount of time it is off, and you flash it quickly enough (Say at 100 times a second), then it will appear to be dimmed. So you could do something like this on portB:

76543210 <---PortB bits

11110000
11110000
11010000
11000000
10000000
10000000
10000000
10000000

So if you cycled though the table above 100 times a second or more; then LED7, the one on bit 7 of portB, would appear to be at full brightness. Led6 would be at 50% brightness, Led5 at 25%, Led4 at 37.5%, and the rest would be off. You should be able to do this on any PIC with an 8bit port like portB.
 
Status
Not open for further replies.

Latest threads

Back
Top