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 26th November 2007, 03:43 AM   (permalink)
Experienced Member
 
Andy1845c is just really niceAndy1845c is just really niceAndy1845c is just really nice
Send a message via MSN to Andy1845c Send a message via Skype™ to Andy1845c
Default PIC PWM outputs?

I have been googling for a while now, and I haven't been able to find what I am looking for.

What is a PWM output on a PIC? I assume any pic can do PWM, but one with a PWM output makes it easier?

I am looking for a a program to simply fade one I/O port on and off gently. I want to use it to control christmas lights and as a starting point in learning to use PWM.

I don't need it to be adjustible after programming (would be fun to add in the future though)

Is somthing like this somwhat simple, or does it take a fair amount of programming know how to make work? Maybe over my head at the moment?

All I have been able to find for the most part are motor contollers and color wash codes. And some are in C or Basic, and I only have a basic beginner knowledge of ASM.

Anyone have any advice or know where I could find a very basic PWM program to play with?
__________________
This message transmitted on 100% recycled electrons
Andy1845c is offline   Reply With Quote
Old 26th November 2007, 03:52 AM   (permalink)
Experienced Member
 
futz is a name known to allfutz is a name known to allfutz is a name known to allfutz is a name known to allfutz is a name known to allfutz is a name known to all
Default

Quote:
Originally Posted by Andy1845c
Is somthing like this somwhat simple, or does it take a fair amount of programming know how to make work? Maybe over my head at the moment?
It's not too tough to do. Read the datasheet very carefully, as always, when setting something like this up.

Quote:
Anyone have any advice or know where I could find a very basic PWM program to play with?
Here's my simple newbie PWM program for a 16F628:
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

Last edited by futz; 26th November 2007 at 04:11 AM.
futz is online now   Reply With Quote
Old 26th November 2007, 08:17 PM   (permalink)
Experienced Member
 
justDIY is a jewel in the roughjustDIY is a jewel in the rough
Default

futz's example uses the pic's internal CCP module aka hardware pwm. If you have the budget, check out the 16F737 (or 767), they have three independent CCP modules, making it nice for rgb color mixing applications. hardware pwm also offers the benefit of freeing up the CPU and not using any interrupts, so your pic can do other stuff, not having to waste cycles on generating the pulse-train.

On the other hand, there are many asm examples out there for soft-pwm, where interrupts are used to generate a pulse-train. the advantage to soft-pwm is cheaper chips can be used, and you can get more pwm outputs. an inexpensive 12f683 has only one hard-pwm output, but can do five soft-pwm outputs.
__________________
If you don't have a planet, what good are gold bars?

want to contact me directly? gmail gordonthree
check out my project website: http://projects.dimension-x.net
Favorite numbers:
09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
justDIY is offline   Reply With Quote
Old 26th November 2007, 09:50 PM   (permalink)
Experienced Member
 
Andy1845c is just really niceAndy1845c is just really niceAndy1845c is just really nice
Send a message via MSN to Andy1845c Send a message via Skype™ to Andy1845c
Default

Thanks for the replys. Right now I don't care how I accomplish the PWM, but its interesting to know there are two ways to go about it.

Will futz's code fade in and out, or just hold at a fixed duty cycle? I really need to spend some time learning ASM better.
__________________
This message transmitted on 100% recycled electrons
Andy1845c is offline   Reply With Quote
Old 26th November 2007, 10:19 PM   (permalink)
Experienced Member
 
futz is a name known to allfutz is a name known to allfutz is a name known to allfutz is a name known to allfutz is a name known to allfutz is a name known to all
Default

Quote:
Originally Posted by Andy1845c
Will futz's code fade in and out, or just hold at a fixed duty cycle? I really need to spend some time learning ASM better.
It's just an example. You'd have to modify it to suit your purpose. It constantly varies the PWM duty cycle from 0 (off) to 255 (full on), over and over. Tis just a demo, not supposed to be useful.
futz is online now   Reply With Quote
Old 26th November 2007, 10:30 PM   (permalink)
Experienced Member
 
Wingmax is just really niceWingmax is just really niceWingmax is just really nice
Default

Quote:
Originally Posted by Andy1845c

I am looking for a a program to simply fade one I/O port on and off gently. I want to use it to control christmas lights and as a starting point in learning to use PWM.

I don't need it to be adjustible after programming (would be fun to add in the future though)

Is somthing like this somwhat simple, or does it take a fair amount of programming know how to make work? Maybe over my head at the moment?

All I have been able to find for the most part are motor contollers and color wash codes. And some are in C or Basic, and I only have a basic beginner knowledge of ASM.

If all you want is just to fade one I/O port, you don't need to use PWM. You can simply just use a delay routine. Send the port pin high, delay for certain time and send the port pin low. Just refer to Nigel's tutorial, you'll find the answer you need. But whatever you do don't use Timer0, or you'll upset him badly.
__________________
May the force be with you.


My project: Simple White Line Follower

http://au.youtube.com/watch?v=8Z_MmrdH4oc

http://i271.photobucket.com/albums/j...nefollower.jpg
Wingmax is offline   Reply With Quote
Old 27th November 2007, 02:03 AM   (permalink)
Experienced Member
 
Andy1845c is just really niceAndy1845c is just really niceAndy1845c is just really nice
Send a message via MSN to Andy1845c Send a message via Skype™ to Andy1845c
Default

Should futz's code actually output on a pin? RB3? I can't get it to work and can't get my head around how to PWM with a pic. I have been googling for over 2 hours and have seen all kinds of PWM programs, but most have more to them then just PWM and that confuses me.

Maybe i'm just jumping too far ahead. Nothing is making any sense
__________________
This message transmitted on 100% recycled electrons
Andy1845c is offline   Reply With Quote
Old 27th November 2007, 02:23 AM   (permalink)
3v0
Moderator
 
Blog Entries: 3
3v0 is a splendid one to behold3v0 is a splendid one to behold3v0 is a splendid one to behold3v0 is a splendid one to behold3v0 is a splendid one to behold3v0 is a splendid one to behold3v0 is a splendid one to behold
Default

Quote:
Originally Posted by Andy1845c
.... I have been googling for over 2 hours and have seen all kinds of PWM programs, but most have more to them then just PWM and that confuses me.
I was teaching this in school today
PWM in its most simple (not confusing) form. This is C but the concept is the same in any language.

Untested off the top of the head sort of code follows....
Code:
 
int i;
int percent;
 
percent = 52;  //  % time on
while(1)   // loop forever
{
    for (i=0; i>100; i++)
    {
        if (i < percent)
        {
              ra0 = 1; // turn bit on
        }
        else
        {
             ra0 = 0; // turn bit off
        }
        delay_ms(1); //some sort of delay here, determines freq
}
 
-------------------- OR --------------------------
 
while(1) // loop forever
{
    ra0 = 1; // turn bit on
 
    for (i=0; i>100; i++)
    {
        if (i == percent)
        {
              ra0 = 0; // turn bit off
        }
        delay_ms(1); // some sort of delay here, determines freq
    }
}
You can use timers and other hardware to unload the processor but the above works.

Last edited by 3v0; 27th November 2007 at 07:01 AM.
3v0 is offline   Reply With Quote
Old 27th November 2007, 02:59 AM   (permalink)
Experienced Member
 
Blog Entries: 1
Gayan Soyza is a glorious beacon of lightGayan Soyza is a glorious beacon of lightGayan Soyza is a glorious beacon of lightGayan Soyza is a glorious beacon of lightGayan Soyza is a glorious beacon of light
Default

Here is another one done in 628

http://sandiding.tripod.com/pwm.html
__________________
Gayan
Forum Supporter
Gayan Soyza is offline   Reply With Quote
Old 27th November 2007, 04:26 PM   (permalink)
Experienced Member
 
Andy1845c is just really niceAndy1845c is just really niceAndy1845c is just really nice
Send a message via MSN to Andy1845c Send a message via Skype™ to Andy1845c
Default

Quote:
Originally Posted by Gayan Soyza
Here is another one done in 628

http://sandiding.tripod.com/pwm.html
I tried this one, and couldn't get the led to do anything I think i need to back up and learn some basics before I attemp PWM.
__________________
This message transmitted on 100% recycled electrons
Andy1845c is offline   Reply With Quote
Old 27th November 2007, 05:11 PM   (permalink)
Experienced Member
 
Blog Entries: 4
blueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to behold
Send a message via Skype™ to blueroomelectronics
Default

Check out the toy car project, it uses PWM to drive a toy cars motor. On the projects section of my site.
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com
blueroomelectronics is online now   Reply With Quote
Old 28th November 2007, 11:44 AM   (permalink)
Experienced Member
Mike, K8LH is a name known to allMike, K8LH is a name known to allMike, K8LH is a name known to allMike, K8LH is a name known to allMike, K8LH is a name known to allMike, K8LH is a name known to all
Default

Andy, what PIC device are you using Sir?
Mike, K8LH is offline   Reply With Quote
Old 28th November 2007, 02:48 PM   (permalink)
Experienced Member
 
Andy1845c is just really niceAndy1845c is just really niceAndy1845c is just really nice
Send a message via MSN to Andy1845c Send a message via Skype™ to Andy1845c
Default

I'm using the 16F628A. So futz's and Gayan's code should work. I'm not sure what I am doing wrong. I haven't tried Bill's code yet. I don't know if I have a 16F88 around or not.

I think i'm just trying to run before I can walk.
__________________
This message transmitted on 100% recycled electrons
Andy1845c is offline   Reply With Quote
Old 28th November 2007, 03:17 PM   (permalink)
Experienced Member
Mike, K8LH is a name known to allMike, K8LH is a name known to allMike, K8LH is a name known to allMike, K8LH is a name known to allMike, K8LH is a name known to allMike, K8LH is a name known to all
Default

Ok. Are you using the 4 MHz internal oscillator? Do you have the LED connected to the RB3/CCP1 pin? If not, what pin(s)?
Mike, K8LH is offline   Reply With Quote
Old 28th November 2007, 04:32 PM   (permalink)
Experienced Member
 
Andy1845c is just really niceAndy1845c is just really niceAndy1845c is just really nice
Send a message via MSN to Andy1845c Send a message via Skype™ to Andy1845c
Default

I am using _HS_OSC. Would that be correct?

I have the LED on RB3. I have tried both ways, but RB3 should be sourcing power, right?
__________________
This message transmitted on 100% recycled electrons
Andy1845c is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
Quik PIC Programming kit Krumlink General Electronics Chat 5 27th January 2008 11:27 PM
Problems switchin relay with PIC Andy1845c General Electronics Chat 5 17th November 2007 06:13 PM
High ADC sampling rate PIC, 18F needed? bananasiong Micro Controllers 24 28th October 2007 12:13 PM
Four PIC with One LCD.. meera83 Micro Controllers 13 20th September 2007 06:40 AM
Circuit design for keyfob receiver's TTL outputs for current Guerrero General Electronics Chat 6 18th April 2005 02:11 AM



All times are GMT. The time now is 02:36 AM.


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