Skip to content

Instantly share code, notes, and snippets.

@eamanu
Created January 23, 2017 02:54
Show Gist options
  • Save eamanu/27329e78cfc3b72fbe8072b2e4449725 to your computer and use it in GitHub Desktop.
Save eamanu/27329e78cfc3b72fbe8072b2e4449725 to your computer and use it in GitHub Desktop.
/*
* main.c
*
* Created on: Jan 22, 2017
* Author: eamanu
*/
#include <avr/io.h>
#include <util/delay.h>
void InitPWM()
{
TCCR0A |=(1<<WGM00)|(1<<WGM01)|(1<<COM0A1);
/*set de CS0*/
TCCR0B |= (1<<CS00);
//Set OC0 PIN as output. It is pin 13 of Atmega2560
DDRB |= (1<<PB7);
}
/******************************************************************
Sets the duty cycle of output.
Arguments
---------
duty: Between 0 - 255
0= 0%
255= 100%
The Function sets the duty cycle of pwm output generated on OC0 PIN
The average voltage on this output pin will be
duty
Vout= ------ x 5v
255
*********************************************************************/
void SetPWMOutput(uint8_t duty)
{
OCR0A=duty;
}
/*Put a waiting*/
void Wait()
{
_delay_loop_2(3200);
}
int main()
{
uint8_t brightness=0;
//Initialize PWM Channel 0
InitPWM();
//Do this forever
while(1)
{
//Now Loop with increasing brightness
for(brightness=0;brightness<255;brightness++)
{
//Now Set The Brighness using PWM
SetPWMOutput(brightness);
//Now Wait For Some Time
Wait();
}
//Now Loop with decreasing brightness
for(brightness=255;brightness>0;brightness--)
{
//Now Set The Brighness using PWM
SetPWMOutput(brightness);
//Now Wait For Some Time
Wait();
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment