0% found this document useful (0 votes)
79 views2 pages

#Include #Include : Programmer's Notepad - Main.c

This C code configures the timer/counter control register on an AVR microcontroller to generate a pulse-width modulated (PWM) output on pin PB3. It defines functions to initialize PWM settings, set the PWM duty cycle, and delay in a loop. The main function uses these to gradually increase and decrease the brightness of the PWM output from 0 to 255, simulating fading an LED on and off.

Uploaded by

Laksh Man
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views2 pages

#Include #Include : Programmer's Notepad - Main.c

This C code configures the timer/counter control register on an AVR microcontroller to generate a pulse-width modulated (PWM) output on pin PB3. It defines functions to initialize PWM settings, set the PWM duty cycle, and delay in a loop. The main function uses these to gradually increase and decrease the brightness of the PWM output from 0 to 255, simulating fading an LED on and off.

Uploaded by

Laksh Man
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Programmer's Notepad - main.

//***********///

#include <avr/io.h>
#include <util/delay.h>
void InitPWM()
()
{
/*
TCCR0 - Timer Counter Control Register (TIMER0)
----------------------------------------------BITS DESCRIPTION
NO:
NAME
DESCRIPTION
-------------------------BIT 7 : FOC0
Force Output Compare [Not used in this example]
BIT 6 : WGM00 Wave form generartion mode [SET to 1]
BIT 5 : COM01 Compare Output Mode
[SET to 1]
BIT 4 : COM00 Compare Output Mode
[SET to 0]
BIT
BIT
BIT
BIT

3
2
1
0

:
:
:
:

WGM01
CS02
CS01
CS00

Wave form generation mode [SET to 1]


Clock Select
[SET to 0]
Clock Select
[SET to 0]
Clock Select
[SET to 1]

The above settings are for


-------------------------Timer Clock = CPU Clock (No Prescalling)
Mode
= Fast PWM
PWM Output = Non Inverted
*/
TCCR0|=(
|=(1<<
)|(1<<
)|(1<<
)|(1<<
);
|=( <<WGM00)|(
<<
)|( <<WGM01)|(
<<
)|( <<COM01)|(
<<
)|( <<CS00);
<<
//Set OC0 PIN as output. It is
DDRB|=(
|=(1<<
);
|=( <<PB3);
<<

PB3 on ATmega16 ATmega32

}
/******************************************************************
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
Vout=

duty
------ x 5v
255

This can be used to control the brightness of LED or Speed of Motor.


*********************************************************************/
void SetPWMOutput(
(uint8_t duty)
)
{
OCR0=
=duty;
;
}
/********************************************************************
Simple Wait Loop
Page 1, 22-03-2013 - 13:12:15

Programmer's Notepad - main.c

*********************************************************************/
void Wait()
()
{
_delay_loop_2(
(30000);
);
}
void main()
()
{
uint8_t brightness=
=0;
;
//Initialize PWM Channel 0
InitPWM();
();
//Do this forever
while(
(1)
)
{

for(
(brightness=
=255;
;brightness>=
>=0;
=brightness-25)
)
>= ;brightness=
{
SetPWMOutput(
(brightness);
);
_delay_ms(
(100);
);
}

Page 2, 22-03-2013 - 13:12:15

You might also like