echo '' ;

Archives

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 :


 

ESP8266 NodeMCU Project – Imperial March Ringtone Play


Playing the Imperial March ringtone on an ESP8266 NodeMCU involves using the NodeMCU’s capabilities to generate sound or control an external sound module. One common method is using a piezo buzzer to generate tones. Here’s a basic example of “ESP8266 Imperial March Ringtone play” using the NodeMCU and a piezo buzzer to play the Imperial March:

Read more… →