You are on page 1of 7

/* PWMext.

c
*
* Created: 9/28/2013 11:25:16 PM
*

Author: GANESH SELVARAJ

*/
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
void InitPWM()
{
TCCR0|=(1<<WGM00)|(1<<WGM01)|(1<<COM01)|(0<<CS02)|(0<<CS01)|(1<<CS00);
TCCR1A|=(1<<COM1A1)|(1<<COM1B1)|(1<<WGM11);
TCCR1B|=(1<<WGM13)|(1<<WGM12)|(0<<CS12)|(0<<CS11)|(1<<CS10);
ICR1=255;
DDRB|=(1<<PB3);
DDRD|=(1<<PD5)|(1<<PD4);
OCR0=0;
OCR1A=0;
OCR1B=0;
}
void Waiting(int j) // simple delay function
{
uint8_t i;
for(i=0;i<j;i++)
_delay_ms(200);
}
void SetADC()
{
ADMUX|=(1<<REFS0)|(1<<ADLAR); //8 bit conversion
ADCSRA=(1<<ADEN)|(7<<ADPS0);
}
uint16_t ReadADC(uint8_t ch)
{
//Select ADC Channel ch must be 0-7
ch=ch&0b00000111;
ADMUX&=0b11100000;
ADMUX|=ch;
//Start Single conversion

ADCSRA|=(1<<ADSC);
//Wait for conversion to complete
while(!(ADCSRA & (1<<ADIF)));
//Clear ADIF by writing one to it
ADCSRA|=(1<<ADIF);
return(ADCH); //only higher byte is returned
}
void main()
{
SetADC();
InitPWM();
while(1)
{
OCR0=ReadADC(1);
OCR1A=ReadADC(2);
OCR1B=ReadADC(3);
}

Acelore
#include<avr/io.h>
#include <util/delay.h>
#include<adc_lib.h>
#include<lcd.h>

void main()

{
int adc0;
ADCinit();
LCDinit();
LCDclr();
LCDcursorOFF();
char ch[16];

while(1)
{
adc0=read_adc(0);
sprintf(ch,"X: %d",adc0);

//reading X axis data


//converting int to char

LCDGotoXY(0,0);
LCDdisplay(ch);
LCDsendChar(' ');

adc0=read_adc(1);

//reading Y axis data

sprintf(ch,"Y: %d",adc0);
LCDGotoXY(0,10);
LCDdisplay(ch);
LCDsendChar(' ');

adc0=read_adc(2);
sprintf(ch,"Z: %d",adc0);

//reading Z axis data

LCDGotoXY(1,5);
LCDdisplay(ch);
LCDsendChar(' ');

_delay_ms(500);

//a half-second delay

}
}

Program to use fast PWM (Pulse Width Modulation) Mode of AVR


microcontroller Timer
#include<avr/io.h>
#include<util/delay.h>
#include<avr/interrupt.h>
#define FREQ 12000000
#define duty_cycle 50 // duty cycle require
#define prescaler 8
#define OCR_value ((duty_cycle*256)/100) //OCR value calculation
void t0_pwm_init(void);
void t2_fastpwm_init(void);
int main()
{
t0_pwm_init();
t2_fastpwm_init();
sei();
while(1);
}
void t0_pwm_init() // initialization for Phase Correct PWM signal using
timer 0
{
// WGM0[1:0]= 01, for Phase Correct PWM mode
// COM0[1:0]= 10, to select non inveting mode

// CS0[2:0] =010. for prescaler=8


TCCR0=(1<<WGM00)|(2<<COM00)|(2<<CS00);
DDRB|=(1<<PB3); // selcet OC0 as output pin
TIMSK|=(1<<OCIE0); //enable Output compare interrupt
}
void t2_fastpwm_init() // initialization for Phase Correct PWM signal using
timer 2
{
// WGM2[1:0]= 11, for Fast PWM mode
// COM2[1:0]= 10, to select non inveting mode
// CS2[2:0] =010. for prescaler=8
TCCR2=(1<<WGM20)|(1<<WGM21)|(2<<COM20)|(2<<CS20);
DDRD|=(1<<PD7); // selcet OC2 as output pin
TIMSK|=(1<<OCIE2); //enable Output compare interrupt
}
ISR(TIMER0_COMP_vect) // interrupt subroutine
{
OCR0=OCR_value; // put OCR value
}
ISR(TIMER2_COMP_vect) // interrupt subroutine
{
OCR2=OCR_value; // put OCR value
}

// Program to use Phase Correct PWM (Pulse Width Modulation) Mode of


AVR microcontroller Timer
#include<avr/io.h>
#include<util/delay.h>
#include<avr/interrupt.h>
#define FREQ 12000000
#define duty_cycle 75 // duty cycle require
#define prescaler 1
#define OCR_value ((duty_cycle*256)/100)

void t0_pwm_init(void);
int main()
{
t0_pwm_init();
sei(); // enable global interrupt
while(1);
}
void t0_pwm_init() //// intiatialize of timer0
{
// WGM0[1:0]= 01, for Phase Correct PWM mode
// COM0[1:0]= 10, to select non inveting mode
// CS0[2:0] =001. for no prescaler
TCCR0=(1<<WGM00)|(2<<COM00)|(1<<CS00);
DDRB|=(1<<PB3); // selcetOC0 as output pin
TIMSK|=(1<<OCIE0); //enable Output compare interrupt
}

ISR(TIMER0_COMP_vect) // interrupt subroutine


{
OCR0=(uint8_t)OCR_value; // put OCR value
}

140,548-Reads

Browse 4 Million+ Electrical &


Electronics Products

Pick the right product for your next design project with "Mouser
Store". Mouser Electronics brings broadest range of semiconductors and
electronic components from over 500 industry leading suppliers.
Mouser specializes in the rapid introduction of new products and technologies for
design engineers and buyers that includes semiconductors, interconnects,
passives, and electro-mechanical components. Providing the best possible
service with the flexibility of not requiring a minimum order and same-day
shipping, Mouser has it all.

Temperature Sensors
(7,586)

Integrated Circuits
ICs (288,369)
Multimeters &
Voltmeters (813)

Current Sensors (742)

Resistors (332,210)

Potentiometers &
Trimmers (26,588)

Encoders (4,497)

Relays (32,934)

7 Segment Displays
(151)

Transformers (6,455)

Capacitors (354,282)

Proximity Sensors
(6,731)
Pressure Sensors
(10,544)

- Processors & Controllers


(41,046)
Motors & Drives (1,362)

When you need the right part right now, think of Mouser
Check out the store now!
Thank you
Team EG

You might also like