![]() | ![]() | ![]() |
| | |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
| | Thread Tools | Display Modes |
| | (permalink) |
| Experienced Member | 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 |
| | |
| | (permalink) | ||
| Experienced Member | Quote:
Quote:
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. | ||
| | |
| | (permalink) |
| Experienced Member | 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 |
| | |
| | (permalink) |
| Experienced Member | 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 |
| | |
| | (permalink) | |
| Experienced Member | Quote:
| |
| | |
| | (permalink) | |
| Experienced Member | Quote:
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 | |
| | |
| | (permalink) |
| Experienced Member | 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 |
| | |
| | (permalink) | |
| Moderator | Quote:
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
}
} Last edited by 3v0; 27th November 2007 at 07:01 AM. | |
| | |
| | (permalink) |
| Experienced Member |
__________________ Gayan Forum Supporter |
| | |
| | (permalink) | |
| Experienced Member | Quote:
__________________ This message transmitted on 100% recycled electrons | |
| | |
| | (permalink) |
| Experienced Member | Check out the toy car project, it uses PWM to drive a toy cars motor. On the projects section of my site. ![]() |
| | |
| | (permalink) |
| Experienced Member | Andy, what PIC device are you using Sir? |
| | |
| | (permalink) |
| Experienced Member | 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 |
| | |
| | (permalink) |
| Experienced Member | 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)? |
| | |
| | (permalink) |
| Experienced Member | 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 |
| | |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
| |
| | ||||
| 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 |