PDA

View Full Version : Generating sine wave with avr



dheeraj91
06-04-2011, 07:51 AM
I wanted to generate a sine wave with an atmega32.After i read a post saying to give the value of the sin(t) to the pwm output so that the sine wave can be generated.I tried doing so but it didnt work.

Please help me.I want to learn how to generate a sine wave using the atmega32 or atmega8

maddy_nish
06-05-2011, 08:21 PM
can u pls elaborate on your problem?? Just post your code..

dheeraj91
06-06-2011, 06:40 AM
I tried using
for(n=0;n<256;n++)
{
OCR1A=sin(2*pi*n/256)
}

avinash_g
06-06-2011, 08:55 AM
@dheeraj91

Have you really did that ? or you are misguiding peoples?

Every one knows the value of sin ranges from -1 to 1 only

sin(2*pi*n/256) will never give a value more than 1!

That value must be scaled to the resolution of PWM that is if PWM is 8 bit it must be multiplied by 255. If pwm resolution is 10 bit then this factor is 1023.

Also any experience programmer will pre calcute the sine table(as you can each time you are only generating same 256 discreet value, so it is wiser to pregenerate sine table using some scripting language on PC) This sine table will be stored in Flash memory occupying only 256 bytes (if pwm resolotion is 8 bit). This is far less than the object code of the bulky math lib. Also the will code execute much faster.

here is a real code



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

DESCRIPTION: A Simple program to genarate some random

musical tone.

MCU: Atmel AVR ATmega32s

CPU SPEED: 16Mhz

LOW FUSE: 0XFF
HIGH FUSE: 0XC9

NOTE: Connect a 8ohm speaker between GND and PB3.


AUTHOR: Avinash Gupta
DATE: Mar 18, 2010

WEB: www.eXtremeElectronics.co.in


*** THIS PROJECT IS PROVIDED FOR EDUCATION/HOBBY USE ONLY ***

*** NO PROTION OF THIS WORK CAN BE USED IN COMMERIAL ***
*** APPLICATION WITHOUT WRITTEN PERMISSION FROM THE AUTHOR ***

EVERYONE IS FREE TO POST/PUBLISH THIS ARTICLE IN
PRINTED OR ELECTRONIC FORM IN FREE/PAID WEBSITES/MAGAZINES/BOOKS
IF PROPER CREDIT TO ORIGINAL AUTHOR IS MENTIONED WITH LINKS TO
ORIGINAL ARTICLE

COPYRIGHT (C) 2008-2010 EXTREME ELECTRONICS INDIA.

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


#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>

#include "sin.h"

/*

Function To Initialize TIMER0 In Fast
PWWM Mode.

*/
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 3 : WGM01 Wave form generartion mode [SET to 1]
BIT 2 : CS02 Clock Select [SET to 0]
BIT 1 : CS01 Clock Select [SET to 0]
BIT 0 : CS00 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<<WGM00)|(1<<WGM01)|(1<<COM01)|(1<<CS00);

//Set OC0 PIN as output. It is PB3 on ATmega16 ATmega32

DDRB|=(1<<PB3);
}

void main()
{
uint8_t i;

InitPWM();

while(1)
{

uint8_t delay,n;

for(delay=1;delay<=50;delay++)
{
for(n=0;n<(51-delay);n++)
{
for(i=0;i<=254;i++)
{
OCR0=pgm_read_byte_near(sine+i);
_delay_loop_2(delay);

}
}
}
for(delay=50;delay>=2;delay--)
{
for(n=0;n<(51-delay);n++)
{
for(i=0;i<=254;i++)
{
OCR0=pgm_read_byte_near(sine+i);
_delay_loop_2(delay);
}
}
}

}
}




And its sounds like this


http://www.youtube.com/embed/lUhQU8gyG9M

shri9890
03-28-2014, 03:25 PM
I dint get need of using 'n'..
please rply