Skip to content

Instantly share code, notes, and snippets.

@SaheblalBagwan
Created May 14, 2016 07:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SaheblalBagwan/2d9ab8cbf840407b7248fef40fc56e21 to your computer and use it in GitHub Desktop.
Save SaheblalBagwan/2d9ab8cbf840407b7248fef40fc56e21 to your computer and use it in GitHub Desktop.
#include<pic16f877a.h>
void delay(int cnt) {
while (cnt--);
}
int main() {
char dutyCycle = 0;
TRISC = 0x00; // Configure PORTC as output(RC2-PWM1, RC1-PWM2)
CCP1CON = 0x0F; // Select the PWM mode.
PR2 = 100; // Set the Cycle time to 100 for varying the duty cycle from 0-100
CCPR1L = 50; // By default set the dutyCycle to 50
TMR2ON = 1; //Start the Timer for PWM generation
while (1) {
for (dutyCycle = 0; dutyCycle < 100; dutyCycle++) // Keep increasing the dutyCyle to increase the brightness of LED
{
CCPR1L = dutyCycle;
delay(10000);
}
for (dutyCycle = 100; dutyCycle > 0; dutyCycle--) // Keep reducing the dutyCyle to decrease the brightness of LED
{
CCPR1L = dutyCycle;
delay(10000);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment