LED / PWM

Have you got the greatest 48 bit multiplier ever conceived? Prove it - post your code here.

Moderator: phalanx

PDubya
Posts: 18
Joined: Fri May 19, 2006 11:45 am

LED / PWM

Post by PDubya » Thu May 25, 2006 12:14 pm

Just learning PIC programming, and can't find an example of LED fading using PWM with a PIC. Anyone have a simple example of just fading a single LED?

Thanks so much!

kkjensen
Posts: 3
Joined: Fri May 26, 2006 5:57 pm

Try MikroBasic

Post by kkjensen » Fri May 26, 2006 6:04 pm

Hi there,

It wasn't that long ago when I was in the same boat as yourself!

I started using a development tool called Mikrobasic which lets me program in a much easier language than assembly and it has a lot of built in libraries available so you can skip reinventing the wheel and get on with your projects. I know in the PWM library they have an example using a LED like what you're trying to do.

I'd really recommend it...I spent a lot of time studying assembly code and now I'm actually able to write code on my own. They also have a very active forum if you have any questions. Just google "mikroelektronika" and you'll find their website.

Good luck!

User avatar
ohararp
Posts: 307
Joined: Sun May 01, 2005 8:43 pm
Location: Dayton, OH
Contact:

Post by ohararp » Fri May 26, 2006 6:11 pm

Mikrobasic good...www.picbasic.org better. Highly recommended. You can download the lite version for free with a 50 line code limit.
Regards Ryan :-)
$25 SMT Kapton Stencils - http://www.ohararp.com/
US Distributor of PDS - http://www.ohararp.com/compilers.html

riden
Support Volunteer
Posts: 1754
Joined: Fri Jun 03, 2005 10:41 pm
Location: Illinois

Post by riden » Fri May 26, 2006 6:29 pm

ohararp wrote:Mikrobasic good...www.picbasic.org better. Highly recommended. You can download the lite version for free with a 50 line code limit.
I agree completely. Mikrobasic is good and I think the free version allows building potentially bigger programs than you can with the demo Proton+ compiler. But for PIC BASIC development, Proton+ (picbasic.org) rules. It took me some time to decide which compiler to buy, but I have absolutely no regrets. It is a very good compiler, is frequently updated, and has just about anything you would need to build programs quickly including floating point math. It also comes with a demo copy of Proteus ISIS so you can simulate many aspects of your program. Check out the demo, I think that you'll be impressed.

However, you might want to take kkjensen's advice and try Mikrobasic. It comes with many example programs including an LED blinker and has the PWM support you want to fade LEDs. They also have a really nice free book on programming the PIC so it is hard not the like the product.

aa3yv
Posts: 45
Joined: Sun Apr 30, 2006 11:05 am

Post by aa3yv » Wed May 31, 2006 9:24 pm

If using one of the chips with hardware pwm module I think you have to set pwm output to whatever frequency you need and then change duty cycle to set the brightness. for example on 16f628 PR2 and the tmr2 prescaler set the period and CCPR1L and bits 5-4 of CCP1CON control the duty cycle. Should be as easy as assigning values to those registers using your favorite programming language.

Kuroi Kenjin
Support Volunteer
Posts: 523
Joined: Mon Jan 30, 2006 4:19 pm
Location: Cleveland area (Ohio)
Contact:

Post by Kuroi Kenjin » Thu Jun 01, 2006 2:59 am

Yup it is just that easy. Hint: the 2 stray bits are the least significant bits I believe.... took me a while when I did a dimmer to check if it was working, why it did 4 ramp ups throughout its range. I had them as the most significant.. hehe.
[url=http://kuroikenjin.boldlygoingnowhere.org/]my website[/url] (Last Updated 5/25/2008 - Bug: noticed that images may not load on first hits)

riden
Support Volunteer
Posts: 1754
Joined: Fri Jun 03, 2005 10:41 pm
Location: Illinois

Post by riden » Thu Jun 01, 2006 7:09 am

Not to diminish the importance of knowing the nitty gritty of PWM management, but if one is first starting out, using a high level language can reduce the learning curve.

MikroBasic code example:

Code: Select all

Pwm_Init(1000)     ' Set the PWM frequency to 1000 hz
Pwm_Change_Duty(127) ' 0=0%  127=50% 255=100% duty cycle
Pwm_Start ' Start outputing the pulse stream
Delay_ms(1000)  ' Wait a second
Pwm_Stop; ' Turn the stream off
And if you want to know how the magic is done, you can look at the ASM files created by the compiler.

PDubya
Posts: 18
Joined: Fri May 19, 2006 11:45 am

Post by PDubya » Wed Jun 07, 2006 12:42 pm

I've tried doing basic LED blinking with MikroBasic, but it puts characters at the beginning of each line of assembly that cause it not to be able to be compiled to .hex.

I must be missing something.

Kuroi Kenjin
Support Volunteer
Posts: 523
Joined: Mon Jan 30, 2006 4:19 pm
Location: Cleveland area (Ohio)
Contact:

Post by Kuroi Kenjin » Wed Jun 07, 2006 3:23 pm

MicroBasic should compile it full. The assembly code with line numbers and opcodes in front is the listing file. This is used for your reference only (aka debugging the basic on the lowest level). Look for the a HEX, COF, COD, or ELF file to import into Mplabs or whatever your programmer software is. I believe the Mikroelektronika lines of compilers use HEX.
[url=http://kuroikenjin.boldlygoingnowhere.org/]my website[/url] (Last Updated 5/25/2008 - Bug: noticed that images may not load on first hits)

aa3yv
Posts: 45
Joined: Sun Apr 30, 2006 11:05 am

another pwm code - works with picclite

Post by aa3yv » Fri Jun 09, 2006 11:51 am

another pwm code - works with picclite

Code: Select all

 #include <htc.h>
#include "delay.h"

__CONFIG(0x3F31);

//init
  CMCON = 0x7; //disable comparators
  OPTION	= 0b10000011;
  TRISA	= 0x0;
  TRISB	= 0x0;
  ei();	// Global interrupts enabled

  //set pwm 1000 Hz = 1 mS
 * PWM registers configuration
 * Fosc = 4000000 Hz
 * Fpwm = 1000.00 Hz (Requested : 1000 Hz)
 * Resolution is 10 bits
 * Prescaler is 4
 * Ensure that your PWM pin is configured as digital output
 * see more details on http://www.micro-examples.com/
 * this source code is provided 'as this',
 * use it at your own risks
 */
 PR2 = 0b11111001 ;
 T2CON = 0b00000101 ;
 CCPR1L = 0b01111100 ;
 CCP1CON = 0b00111100 ;

 while (1) {
        //marry x-mas	
	CCPR1L = rand();
       DelayMs(rand());
} 

cheesy
Posts: 43
Joined: Thu May 25, 2006 7:54 pm

Post by cheesy » Wed Jun 14, 2006 1:52 am

PWM fading was one of my very first microcontroller projects a couple months ago! I originally wrote it for an MSP430 but it was easy enough to port to a PIC.

This fades in and out (like the sleep indicator on my PowerBook!). It doesn't use anything fancy, just a bunch of for loops...

Code: Select all

void main(void)
{
    unsigned int i, j, k, step;
    
    TRISCbits.TRISC0 = 0;
    
    step = 1;
    j = 0;
    
    while (1)
    {
        for(; j < 20 && j >= 0; j += step)
        {
            for (k = 0; k < 10; k++)
            {
                PORTCbits.RC0 = 1;
                for (i = j; i != 0; i--);
                PORTCbits.RC0 = 0;
                for (i = 100-j; i != 0; i--);
            }
        }
        step *= -1;
        j += step;
    }
}
It maxes out at 20% duty cycle since with my LED that's when it appeared fully on. That may vary with your LED/resistor/microcontroller (adjust the "j < 20")

With a PIC18 running at 1MHz it fades in and out in about 2 seconds. Different processor speeds will give different rates (adjust the "k < 10")

Of course you must also change PORTCbits.RC0 (and TRISCbits.TRISC0) if you're not using bit 0 of port C.

I used MPLAB C18 to compile.

Enjoy!
-tom

jesrandall
Posts: 21
Joined: Mon Jun 15, 2009 7:32 am
Location: Austin, Texas
Contact:

Post by jesrandall » Tue Jun 16, 2009 10:51 am

If you're new to programming in general or just learning you might want to write your own PWM code, like Tom suggested, instead of using the microcontroller's built in PWM hardware. Especially since many of the cheaper chips don't have these. The built in PWM module of the microcontroller can have glitchy startups when power is first applied...

Also, writing your own PWM logic will ultimately give your more detailed control over it's operation and higher possible frequency rates. I suggest writing it in assembly and considering using interrupts.
Jesse Randall
Electronic Engineer
Design Coordinator
http://www.NovaConceptions.com

falingtrea
Posts: 126
Joined: Mon Dec 31, 2007 3:54 pm
Location: Montgomery, MN

Post by falingtrea » Wed Jun 17, 2009 10:37 am

Here is another method of modulation that you might want to look at. It's called MIBAM (Mirror Image Bit Angle Modulation). This is easier to manage from the interrupt standpoint, and the example code is written in ME labs PICBASIC.
Tim Barr

User avatar
ohararp
Posts: 307
Joined: Sun May 01, 2005 8:43 pm
Location: Dayton, OH
Contact:

Post by ohararp » Fri Jun 19, 2009 6:05 am

I have a project where I am PWM'ing a high power led through a zetex driver. I have noticed that the led brightness does not appear to be a linear function. I am wondering if anyone knows what kind of mathematical function would describe the brightness of the led relative to the duty cycle. To me it appears to probably be an exponential growth type of curve.
Regards Ryan :-)
$25 SMT Kapton Stencils - http://www.ohararp.com/
US Distributor of PDS - http://www.ohararp.com/compilers.html

User avatar
leon_heller
Support Volunteer
Posts: 5734
Joined: Sun May 01, 2005 11:20 am
Location: St. Leonards-on-Sea, E. Sussex, UK.

Post by leon_heller » Fri Jun 19, 2009 6:15 am

The visual system has a logarithmic response, like most biological sensor systems.

Leon
Leon Heller
G1HSM

Post Reply