Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Is there any way to generate sine wave modulated PWM (sPWM) with an ATmega microcontroller?

Status
Not open for further replies.

Arsh008

Newbie
Newbie level 1
Joined
Jun 18, 2021
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
9
Hi, dears,
I want to generate a 1Hz (sPWM) to fire the full-bridge MOSFET gates with an ATmega 16 microcontroller. Is there any way to achieve this goal?
I appreciate your useful comments.
Best Regards.
 

Just google "atmel spwm", several code base hits.

Another easy single chip approach -



Regards, Dana.
 
Last edited:

In 2006 I made a PWM "little tones player" using a small AtTiny13 unit, based on a Sin Table on flash.
You can define the frequency easily, and obtain the sinewave output based on PWM, so, if you don't filter the PWM you still have the digital output that can control the MosFET. The code is in AVR assembly. I know some people have issues with assembly program, anyway I am attaching the source file. You can easily modify the code, assembly using the free AVRASM and program the chip using AVRDUDE with an USBASP dongle. Ooops, Edaboard only allows me to attach image files. So, sorry, posting the assembly source below.

Code:
;--------------------------------------------------------
; File: Sinewave.asm
; December 21 2006
; Wagner Lipnharski (wagner@ustr.net)
; processor Atmel AVR AtTiny13
;
; Description: Example of how to use the fast PWM of the
;       ATtiny13 to generate "sine-wave" signal.
;
;       The PWM output requires filtering to shape
;       the sine wave form.  The filter may be composed
;       by a simply 10k resistor connected to the PB1 pin
;       to a 100nF capacitor to ground. This output may
;       be boosted by an operational amplifier.
;
;---------------------------------------------------------
;
; You must program AtTiny13 Fuse:
; Internal RC Oscillator 9.6MHz Start-Up time: 14CK + 64ms
;
;---------------------------------------------------------

.include "tn13def.inc"        

.Def    Temp1          = R15
.Def    Temp           = R16
.Def    Temp2          = R17

.Def    TablePtr       = R18

.Def    Tone1          = R19
.Def    Tone1Temp      = R20

.Def    Tempo          = R21

.Def    SineCntr       = R22
.Def    SineCntrTmp    = R23

.Def    Skipper        = R24
.Def    KeyNumber      = R25        ; Key A5=0, Key B7=15

.Def    PlayerL        = R26
.Def    PlayerH        = R27



;Initialization

.cseg
.org $000
            Rjmp    Init


;***********************************************
; MUSIC TO PLAY - Change this Table
;***********************************************
        ; [Zero, KeyNumber, Tempo, Pause]
        ; KeyNumber 0-15
        ; Tempo 1-50 (aprox 200ms each)
        ; Pause 1-50 (aprox 5ms each)
        ; End table with 255,255,255,255

Music:
    ; We wish you a Merry Christmas
    .db        0,6,4,3
    .db        0,9,4,4
    .db        0,9,2,3
    .db        0,10,2,3
    .db        0,9,2,3
    .db        0,8,2,3
    .db        0,7,4,3
    .db        0,7,2,15

    .db        0,7,4,3
    .db        0,10,4,4
    .db        0,10,2,3
    .db        0,11,2,3
    .db        0,10,2,3
    .db        0,9,2,3
    .db        0,8,4,3
    .db        0,6,2,15

    .db        0,6,4,3
    .db        0,11,4,4
    .db        0,11,2,3
    .db        0,12,2,3
    .db        0,11,2,3
    .db        0,10,2,3
    .db        0,9,4,3
    .db        0,7,2,15
    .db        0,6,2,3
    .db        0,6,2,3
    .db        0,7,4,3
    .db        0,10,4,3
    .db        0,8,4,3
    .db        0,9,8,80

    ; NOEL First Part
    .db    0,4,2,3        ; Key=4, Tempo=2, Pause=3
    .db    0,3,2,3        ; Key=3, Tempo=2, Pause=3
    .db    0,2,6,3
    .db    0,3,2,3
    .db    0,4,2,3
    .db    0,5,2,3
    .db    0,6,6,15
    .db    0,7,2,3
    .db    0,8,2,3
    .db    0,9,4,3
    .db    0,8,4,3
    .db    0,7,4,3
    .db    0,6,6,30
    .db    0,9,4,3
    .db    0,8,4,3
    .db    0,7,4,3
    .db    0,6,4,3
    .db    0,7,4,3
    .db    0,8,4,3
    .db    0,9,4,3
    .db    0,6,4,3
    .db    0,5,4,3
    .db    0,4,4,30

    ; NOEL Second Part
    .db    0,4,2,3        
    .db    0,3,2,3        
    .db    0,2,6,3
    .db    0,3,2,3
    .db    0,4,2,3
    .db    0,5,2,3
    .db    0,6,6,15
    .db    0,9,2,3
    .db    0,8,2,3
    .db    0,9,4,3
    .db    0,8,4,3
    .db    0,7,4,3
    .db    0,6,6,30
    .db    0,9,4,3
    .db    0,8,4,3
    .db    0,7,4,3
    .db    0,6,4,3
    .db    0,7,4,15
    .db    0,8,2,3
    .db    0,9,4,3
    .db    0,6,4,3
    .db    0,5,4,3
    .db    0,4,4,80

    .db    255,255,255,255  ; <=== End of Music to Play 


;***********************************************
; INITIALIZATION
;***********************************************

INIT:

; Set PB0 and PB1 as output

        Ldi     Temp, (1<<PB0) | (1<<PB1)    
        Out     DDRB, Temp

; Set PWM Mode
; Togle OC1A on Compare

        Ldi    Temp, (1<<COM0A1) |(1<<WGM00) |(1<<WGM01)
        Out    TCCR0A, Temp                        

; Set PWM Clock equal to Internal Clock

        Ldi     Temp,  (1<<CS00) 
        Out     TCCR0B, Temp                        

        Clr     TablePtr
        Clr     r18

;***********************************************
; WILL READ "MUSIC TO PLAY" AND PLAY IT
;***********************************************

MusicLoop:    
        Ldi     PlayerH,High(Music*2)
        Ldi     PlayerL,Low(Music*2)
MusicLoop1:
        Mov    ZL,PlayerL
        Mov    ZH,PlayerH
        Clr    Temp
        Adiw    ZL,1
        Lpm
        Mov    KeyNumber,R0
        Cpi    KeyNumber,255
        Breq    MusicLoop

        Adiw    ZL,1
        Lpm
        Mov    Tempo,R0

        Adiw    ZL,1
        Lpm
        Mov    Temp2,R0

        Push    Temp2
        Adiw    ZL,1
        Mov    PlayerL,ZL
        Mov    PlayerH,ZH
        Rcall    Get_Tone
        Pop    Temp2

        Rcall    Delay1    
        Rjmp    MusicLoop1

;***********************************************
; FIND PIANO KEY PROPERTIES (Frequency, etc)
;***********************************************

Get_Tone:    Ldi     ZH,High(KeysTable*2)    
        Ldi     ZL,Low(KeysTable*2)
        Lsl    KeyNumber
        Lsl    KeyNumber
        Inc    KeyNumber
        Clr    Temp

        Add     ZL,KeyNumber    
        Adc     ZH,Temp
        Lpm                
        Mov     Tone1,R0

        Adiw    ZL,1
        Lpm
        Mov    SineCntr,R0

        Adiw    ZL,1
        Lpm
        Mov    Skipper,R0

;***********************************************
; CREATE SINEWAVE AND TEMPO IT, According to Key
;***********************************************

Player1:    Clr    Temp1
Player2:    Mov    SineCntrTmp,SineCntr
Player3:    Mov    Tone1Temp,Tone1
        Rcall    Set_PWM

        Dec    Tone1Temp
        Brne    PC-1

        Dec    Temp1
        Brne    Player3
                
        Sub    SineCntrTmp,Skipper
        Brcc    Player3

        Dec    Tempo
        Brne    Player2

        Ret


Delay1:        Clr    Temp
        Dec    Temp
        Brne    Pc-1

        Dec    Temp1
        Brne    PC-3

        Dec    Temp2
        Brne    Pc-5

        Ret


Set_PWM:
        Ldi     ZH, High(sine_table*2)    
        Ldi     ZL, Low(sine_table*2)
          Clr    Temp
        Add     ZL, TablePtr    
        Adc     ZH, Temp
        Lpm                
        Out     OCR0A, R0
        Add     TablePtr,Skipper
        Ret

;***********************************************
; PIANO KEY PROPERTIES (Frequency, etc)
;***********************************************

KeysTable:
    
    ; 00, Tone, SineCntr, Skipper

    .db    00,22,44,1        ; A5
    .db    00,44,49,2        ; B5
    .db     00,65,53,3        ; C5
    .db    00,36,59,2        ; D5
    .db    00,88,66,5        ; E5
    .db    00,47,70,3        ; F5
    .db    00,89,79,6        ; G5
    .db    00,107,88,8        ; A6
    .db    00,95,99,8        ; B6
    .db    00,89,106,8        ; C6
    .db    00,79,119,8        ; D6
    .db    00,79,132,9        ; E6
    .db    00,74,141,9        ; F6
    .db    00,73,159,10        ; G6
    .db    00,41,176,8        ; A7
    .db    00,36,198,8        ; B7

;***********************************************
; SINEWAVE VALUES
;***********************************************

sine_table:
    .db 128,131,134,137,140,144,147,150,153,156,159,162,165,168,171,174
    .db 177,179,182,185,188,191,193,196,199,201,204,206,209,211,213,216
    .db 218,220,222,224,226,228,230,232,234,235,237,239,240,241,243,244
    .db 245,246,248,249,250,250,251,252,253,253,254,254,254,254,254,254
    .db 254,254,254,254,254,254,254,253,253,252,251,250,250,249,248,246
    .db 245,244,243,241,240,239,237,235,234,232,230,228,226,224,222,220
    .db 218,216,213,211,209,206,204,201,199,196,193,191,188,185,182,179
    .db 177,174,171,168,165,162,159,156,153,150,147,144,140,137,134,131
    .db 128,125,122,119,116,112,109,106,103,100,97,94,91,88,85,82
    .db 79,77,74,71,68,65,63,60,57,55,52,50,47,45,43,40
    .db 38,36,34,32,30,28,26,24,22,21,19,17,16,15,13,12
    .db 11,10,8,7,6,6,5,4,3,3,2,2,2,1,1,1
    .db 1,1,1,1,2,2,2,3,3,4,5,6,6,7,8,10
    .db 11,12,13,15,16,17,19,21,22,24,26,28,30,32,34,36
    .db 38,40,43,45,47,50,52,55,57,60,63,65,68,71,74,77
    .db 79,82,85,88,91,94,97,100,103,106,109,112,116,119,122,125
 
Last edited by a moderator:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top