echo '' ;

PIC Interface – PWM

 

Functions

  • pwm_init()
  • pwm_dutycycle(unsigned int duty)

Example : Generate predefined PWM using four buttons

Code

  • Define Port-B as input and RB0, RB1, RB2 and RB3 used as button input
  • Generate PWM using PWM functions

Note  :  UN-command the line depend on your preferred compiler.

//www.ArunEworld.com/embedded/microchip/pic_mcu/
#include<pic.h> // for Hi-Tech Compiler

#define _XTAL_FREQ 4000000

//short currduty1=16;
void init()
{
    PORTB=0X00;
    TRISB=0X0f;
}

void pwm_dutycycle(unsigned int duty)
{
    CCPR1L=duty;
    CCP1CON&=0xCF;
    CCP1CON=(CCP1CON|(0X30&(duty<<4)));
}

void pwm_init()
{
    TRISC=0X00;
    CCP1CON=0X0C;
    PR2=96;
    TMR2=0;
    T2CON=0x7B;
    pwm_dutycycle(0);
    T2CON=0X7F;
}

void delay(unsigned int de) {   while(de--);    }

void main()
{
    pwm_init();
    while(1)
    {
        if(RB0) {   /*delay(100);*/ pwm_dutycycle(0);   }
        if(RB1) {   /*delay(100);*/ pwm_dutycycle(64);  }
        if(RB2) {   /*delay(100);*/ pwm_dutycycle(128); }
        if(RB3) {   /*delay(100);*/ pwm_dutycycle(255); }
    }   
} 

 

Result :

 


Next :

previous :


 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading