Menu

all about electronic and microcontrollers

Sunday, October 17, 2010

Lesson nr.08-Using PWM to control LED brightness

Hardware setup:
Like in the last Lesson, nr.07, we will work with CCP1 in PWM mode, to control the luminous intensity of a L.E.D, but here we will not use buttons to vary the width form of our signal. The pulse width will automatically vary between 0% and 100% and vice versa.
Button S1 and (pull-up) resistance with value of 4.7 K will provide the manual reset mode to the microcontroller.



Circuit Diagram:
For those who want to build it on their own breadboard or other platform, here is the electronic scheme built in Eagle Cad, free version:



Software:
Here is the C program written for MikroC PRO for PIC 2010 (version v4.15).
/*
'*******************************************************************************
'  Lesson nr.08:
'          Using PWM to control LED brightness
'  Written by:
'          Aureliu Raducu Macovei, 2010.
'  Description:
'          In this experiment we will work with module CCP1 as PWM mode, to 
'          control LED brightness. PORT RB.3 will be used for this experiment.
'  Test configuration:
'    MCU:                        PIC16F628A
'    Test.Board:                 WB-106 Breadboard 2420 dots
'    SW:                         MikroC PRO for PIC 2010 (version v4.15)
'  Configuration Word
'    Oscillator:                 INTOSC:I/O on RA.6, I/O on RA.7
'    Watchdog Timer:             OFF
'    Power up Timer:             Disabled
'    Master Clear Enable:        Enabled
'    Browun Out Detect:          Enabled
'    Low Voltage Program:        Disabled
'    Data EE Read Protect:       Disabled
'    Code Protect:               OFF
'*******************************************************************************
*/
unsigned short i;

void main()             // Main;
{
 TRISB3_bit = 0;        // RB.3 is output;
 PORTB = 0;             // Set PORTB to 0;

 Pwm1_Init(5000);       // Initialize PWM module at 5KHz;
 Pwm1_Start();          // Start PWM;

 do {
     while(i<255){                        // As long as i<255;
                  i = i+1;                // Increment i with 1;
                  Pwm1_Set_Duty(i);       // Set pwm duty;
                  Delay_ms(8);            // 5ms delay;
                  }
     Pwm1_Stop;                           // Stop pwm;
     Delay_ms(5);                         // 5ms delay;
     Pwm1_Start;                          // Start pwm;
     Delay_ms(5);                         // 5ms delay;
     
     while(i>0){                          // As long as i>0;
                i = i-1;                  // decrement i with 1;
                Pwm1_Set_Duty(i);         // Set pwm duty;
                Delay_ms(5);              // 5ms delay;
                }
     Pwm1_Stop;                           // Stop pwm;
     Delay_ms(100);                       // 100ms delay;
     } while(1);                          // Do forever;
}                                         // End.

6 comments:

  1. Hey,
    Which is the maximum value that "Delay_ms(x)" should accept ?
    Is not necessary to configure one of the available timers to make it work?

    Thank you

    ReplyDelete
  2. Hello buddy,
    I'll answer your questions in the order you wrote:
    1. Do not remember exactly, but you can get by successive tests.
    2. It's not necessary.

    Kind Regards,
    ducu

    ReplyDelete
  3. dear sir following my code is no running action i want to make chaser with comet tail effect


    #include
    #define FADE_RATE 12
    #define INITIAL_WIDTH 128
    void fade()
    {
    unsigned char rate = FADE_RATE;
    unsigned char pulse_width = INITIAL_WIDTH;
    unsigned char count;
    while(pulse_width){
    PORTB= 0b11111111;
    for(count=0; count>1);
    }
    for(b=0x40; b; b>>=1){
    PORTB = b;
    fade(b<<1);
    }
    }
    }

    jaya19995@gamil.com

    ReplyDelete
  4. Hello
    I wanted to ask if you can please explain to me if I want to use more LEDs , such as a sequence of LED back and forth with effect PWM ?

    ReplyDelete
  5. hello
    could show how you use pic16f628A's ADC_READ()AN0 AN1 ?

    ReplyDelete
  6. Thank youu for writing this

    ReplyDelete

If you do not understand something, or if you make some aplication helped by this blog, let me know.