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 16F628A - Need a bit of guidance

Status
Not open for further replies.
Before you know it, microcontrollers will seem easy and everytime someone describes an electronic problem to you, you will instantly start trying to think how a microcontroller could solve it!
 
Also, pay attention to the way people format their code. If you write new code and want to share it with others, it is very important that you structure it in a way that keeps it readable, not to mention if you reuse code you wrote a year ago, you want to be able to understand everything you worked on.

Don't forget, comment everything!
 
Alright! Figured it out! I took the basic idea from the code posted above and here's what I got:

Code:
Dec
		call	MasterDelay
		DECFSZ	CCPR1L,1
		goto	Dec

Inc	call	MasterDelay
		incf    CCPR1L,1                        
		xorwf	CCPR1L,d'50'                                  
      skpz                                                     
      goto	Inc
		goto	Dec

Note that I switched the prevous XORLW to an XORWF and adjusted the code accordingly. I found this a bit overall easier to work with. 'MasterDelay' is a short [5 mS] delay.

Once the PWM is set up, this will run in a continous loop! It works flawlessly, and with a super-bright blue LED it gives a very pleasent effect :D

Now, the big question...

Is it possable to set brightness levels for other pins? Check this out:

https://www.anothercoilgunsite.com/vid-lightsaber.wmv
**broken link removed**

Thats some serous color shifting going on there. What boggles me, is that he is using ALL pins except V+ and ground. Must be where the Internal Oscillator comes into play. You can also see that he has different brightness levels flying around like crazy, not just on RB3, but all of them. Has anyone attempted anything like this? Thoughts to how it is done?

First thing that comes to mind is muxing, but I dunno...
 
It's done with software PWM, EPE did a project the other year with LED's fading all over the place - the only snag is it takes so much of the PIC's power, hardware PWM takes none.

I actually tried the EPE project on my tutorial hardware, it worked really well!.
 
I'm taking it that what you just said was, instead of letting the built-in PWM take control, it's all software controlled?
 
Tipp said:
I'm taking it that what you just said was, instead of letting the built-in PWM take control, it's all software controlled?

Yes, you take a pin HIGH, wait a bit, take it LOW again, wait a bit, and repeat. How long you wait each time determines the mark/space ratio.

The clever bit is doing it on multiple pins independently, 'apparently' at the same time.

One method is to use timer interrupts, and a series of counters in the interrupt routine. Preset the counters for the brightness you want, and turn all the LED's ON, each time the interrupt routine runs it decrements each counter in turn, until they reach zero, then turns that LED OFF. Once the interrupt routine has run the maximum number of times, the original counter values are reset, and it starts again.

But it's a balancing act between number of bits used for the PWM, and the repetition frequency - it's obviously essential that the interrupt routine is faster than the time between interrupts. It's also a problem that you don't have much spare processing power left.

I've done it in the past for two PWM channels feeding motors on a robot, and also done software RS232 to receive radio commands via 433MHz modules, plus IR obstacle detection - all at the same time. But it was a very busy little 16C84, back before the 16F628 came out!.
 
Hmm...sounds like something worth playing around with! :D If LED shifting was all I wanted to do with it, then it would be adiquite processing indeed :D

Timer interrupts sounds relitivly familiar :p I will set up a simple test program and try it out!

Any other ideas/comments?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top