+ Reply to Thread
Page 1 of 3
1 2 3 Last
Results 1 to 15 of 35

Thread: PIC PWM outputs?

  1. #1
    Friend of the Electrons Andy1845c Good Andy1845c Good Andy1845c Good
    Join Date
    Apr 2006
    Location
    Southern Minnesota
    Posts
    1,156

    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


  2. #2
    futz Excellent futz Excellent futz Excellent futz Excellent futz Excellent futz Excellent futz Excellent
    Join Date
    Sep 2007
    Location
    Vancouver, B.C.
    Posts
    1,980

    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.

    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.

  3. #3
    justDIY Good justDIY Good
    Join Date
    Mar 2005
    Location
    Michigan, USA
    Posts
    1,725

    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

  4. #4
    Friend of the Electrons Andy1845c Good Andy1845c Good Andy1845c Good
    Join Date
    Apr 2006
    Location
    Southern Minnesota
    Posts
    1,156

    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

  5. #5
    futz Excellent futz Excellent futz Excellent futz Excellent futz Excellent futz Excellent futz Excellent
    Join Date
    Sep 2007
    Location
    Vancouver, B.C.
    Posts
    1,980

    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.

  6. #6
    Wingmax Excellent Wingmax Excellent Wingmax Excellent Wingmax Excellent
    Join Date
    Sep 2007
    Location
    Australia
    Posts
    163

    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

  7. #7
    Friend of the Electrons Andy1845c Good Andy1845c Good Andy1845c Good
    Join Date
    Apr 2006
    Location
    Southern Minnesota
    Posts
    1,156

    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

  8. #8
    3v0
    3v0 is offline
    3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent
    Join Date
    Jul 2006
    Location
    USA
    Posts
    6,464
    Blog Entries
    11

    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.

  9. #9
    Gayan Soyza Excellent Gayan Soyza Excellent Gayan Soyza Excellent Gayan Soyza Excellent Gayan Soyza Excellent Gayan Soyza Excellent
    Join Date
    Oct 2006
    Location
    Colombo
    Posts
    1,664
    Blog Entries
    1

    Default

    Here is another one done in 628

    http://sandiding.tripod.com/pwm.html

  10. #10
    Friend of the Electrons Andy1845c Good Andy1845c Good Andy1845c Good
    Join Date
    Apr 2006
    Location
    Southern Minnesota
    Posts
    1,156

    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

  11. #11
    Help us help you blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent
    Join Date
    Jan 2007
    Location
    Toronto, Canada
    Posts
    10,705
    Blog Entries
    5

    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/

  12. #12
    Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent
    Join Date
    Jan 2005
    Location
    Michigan, USA
    Posts
    2,521

    Default

    Andy, what PIC device are you using Sir?

  13. #13
    Friend of the Electrons Andy1845c Good Andy1845c Good Andy1845c Good
    Join Date
    Apr 2006
    Location
    Southern Minnesota
    Posts
    1,156

    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

  14. #14
    Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent
    Join Date
    Jan 2005
    Location
    Michigan, USA
    Posts
    2,521

    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)?

  15. #15
    Friend of the Electrons Andy1845c Good Andy1845c Good Andy1845c Good
    Join Date
    Apr 2006
    Location
    Southern Minnesota
    Posts
    1,156

    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

+ Reply to Thread
Page 1 of 3
1 2 3 Last

Similar Threads

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

Tags for this Thread