Menu

all about electronic and microcontrollers

Sunday, October 17, 2010

Lesson nr.07-PWM & Push Button

Hardware setup:
In this experiment we will use the CCP1 as a PWM to control the power to a L.E.D. PWM stands for the Pulse Width Modulation where the width of a digital waveform is varied to control the power delivered to a load (in this case an L.E.D). The underlying principle in the whole process is that the average power delivered is directly proportional to the modulation duty cycle. The term duty cycle describes the proportion of on time to the regular interval or period of time; a low duty cycle corresponds to low power, because the power is off for most of the time. Duty cycle is expressed in percent, 100% being fully on and 0% means off.


Trough  hardware configuration of the microcontroller we will deduct to use the PORT RB3 as PWM output.
Button S1 and (pull-up) resistance with value of 4.7 K will provide the manual reset mode to the microcontroller.
Buttons S2 and S3 will vary the width form of our signal.
ICSP connector provides the connection between the programmer (PicKit 2) and microcontroller (PIC16F628A).



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.07:
'          PWM & Push Button
'  Written by:
'          Aureliu Raducu Macovei, 2010.
'  Description:
'          In this experiment we will show how to work with module CCP1 as PWM mode.
'          The PORT RB3 is used for this experiment (as output, for a led)
'          and RA6-RA7 (as imput, for buttons).
'  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 last_duty, current_duty;
void main()                 // main;
{
 CMCON |= 7;                // turn off analogue comparator and make PORTA to digital I/O;
 trisb3_bit = 0;            // make rb3 as output;
 portb = 0;                 // turn off LEDs on PORTB;
 current_duty = 0;          // Initial value of variable current_duty;
 last_duty = 0;             // Reset variable last_duty;
 pwm1_init(5000);           // PWM module initialization (5KHz);
 pwm1_start();              // Start PWM1 module;
 pwm1_set_duty(last_duty);
 do {
     while(last_duty>0<255){
                            if (Button(&PORTA, 6,1,0))
                            last_duty = last_duty +2;       // If the button connected to 
                                                            // RA0 is pressed
                            Pwm1_Set_Duty(last_duty);
                            delay_ms(100);

                            if (Button(&PORTA, 7,1,0))
                            last_duty = last_duty -2;       // If the pressed button is 
                                                            // connected to RA1
                            Pwm1_Set_Duty(last_duty);
                            delay_ms(100);
                            }
     delay_ms(200);                       // 200mS delay
     } while(1);                          // Do forever;
}                                         // End.

5 comments:

  1. iti multumesc mult pt aceste tutoriale

    ReplyDelete
  2. RO: Ma bucur foarte mult sa vad ca sunt de folos aceste tutoriale. Site-ul este intr-o dezvoltare continua, in functie de timpul meu liber.

    EN: I am very glad to see that these tutorials are helpful. The site is in continuous development, depending on my spare time.

    ReplyDelete
  3. Is this source code can be translated to be implemented in LPCXpresso LPC1769 arm microcontroller???

    ReplyDelete
  4. Ducule m-ai dat pe spate , te-am gasit (descoperit) cam tirziu , incerc sa mai invat cite ceva din lectiile tale cu toate ca sunt un electronist amator de 48 de ani. Sunt fascinat de microcontrolere. Salutare si sa fii iubit__PS. sa ai timp cit mai mult , sa nu crezi ca treci neobsevat___cu respect dan(barlad)__esti destept prietene daca scrii softuri pentru mc

    ReplyDelete

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