Results 1 to 5 of 5

Thread: Generating sine wave with avr

  1. #1
    Junior Member User
    Join Date
    Jan 2011
    Location
    Hyderabad
    Posts
    26

    Default Generating sine wave with avr

    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

  2. #2

    Default

    can u pls elaborate on your problem?? Just post your code..
    Last edited by maddy_nish; 06-05-2011 at 11:43 PM.

  3. #3
    Junior Member User
    Join Date
    Jan 2011
    Location
    Hyderabad
    Posts
    26

    Default

    I tried using
    for(n=0;n<256;n++)
    {
    OCR1A=sin(2*pi*n/256)
    }

  4. #4
    Senior Member T-1000
    Join Date
    Jun 2007
    Location
    Jamshedpur/Bhubaneswar
    Posts
    421

    Default

    @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

    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
    Last edited by avinash_g; 06-06-2011 at 09:03 AM.
    Avinash Gupta
    ************
    www.eXtremeElectronics.co.in

  5. #5

    Default @avinash

    I dint get need of using 'n'..
    please rply

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •