Advertisement
Guest User

Tiny13Music

a guest
Dec 31st, 2011
3,913
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .include "tn13def.inc"
  2.  
  3. .def    voice1n     = R2
  4. .def    voice2n     = R3
  5. .def    voice3n     = R4
  6.  
  7. .def    voice1nn    = R5
  8. .def    voice2nn    = R6
  9. .def    voice3nn    = R7
  10.  
  11. .def    voice1ncl   = R8
  12. .def    voice1nch   = R9
  13. .def    voice2ncl   = R10
  14. .def    voice2nch   = R11
  15. .def    voice3ncl   = R12
  16. .def    voice3nch   = R13
  17.  
  18. .def    notecountl  = R14
  19. .def    notecounth  = R15
  20. .def    temp        = R16
  21. .def    temph       = R17
  22. .def    tempocounth = R18
  23. .def    tempocountl = R19
  24.  
  25. .def    voice1l     = R20
  26. .def    voice2l     = R21
  27. .def    voice3l     = R22
  28.  
  29. .def    outpin      = R23
  30.  
  31. .equ    XTAL = 4800000
  32. .equ    ProgSteps = 173 //Program steps in one cycle
  33. .equ    MaxFreq = XTAL / ProgSteps  
  34. .equ    Tempo = 160 // 1/4 per minute
  35. .equ    TempoCounter = (MaxFreq * 60 / 2) / Tempo
  36.  
  37. .cseg
  38.  
  39. .org    $000
  40.         rjmp    Init
  41. Init:
  42.         ldi     temp, (1<<PB3) | (1<<PB4)
  43.         out     DDRB, temp
  44. Start:
  45.         clr     tempocountl
  46.         clr     tempocounth
  47.         clr     notecountl
  48.         clr     notecounth
  49.         clr     voice1n
  50.         clr     voice2n
  51.         clr     voice3n
  52.         clr     voice1nn
  53.         clr     voice2nn
  54.         clr     voice3nn
  55.         clr     voice1ncl
  56.         clr     voice1nch
  57.         clr     voice2ncl
  58.         clr     voice2nch
  59.         clr     voice3ncl
  60.         clr     voice3nch
  61.         clr     voice1l
  62.         clr     voice2l
  63.         clr     voice3l
  64. MainLoop:
  65.         clr     outpin
  66.         subi    tempocountl,1
  67.         sbci    tempocounth,0
  68.         brcs    NewNote
  69.         nop
  70.         nop
  71.         ldi     temp,33
  72. Delay1:
  73.         dec     temp
  74.         brne    Delay1
  75.         rjmp    EndNewNote
  76. NewNote:
  77.         ldi     tempocountl, low(TempoCounter)
  78.         ldi     tempocounth, high(TempoCounter)
  79.         ldi     temp, 1
  80.         clr     temph
  81.         add     notecountl, temp
  82.         adc     notecounth, temph
  83.         mov     temph, voice1nch
  84.         mov     temp, voice1ncl
  85.         sub     temp, notecountl
  86.         sbc     temph, notecounth
  87.         brcs    LoadNoteVoice1
  88.         nop
  89.         ldi     temp,8
  90. Delay2:
  91.         dec     temp
  92.         brne    Delay2
  93.         rjmp    EndLdVoice1
  94. LoadNoteVoice1:
  95.         ldi     temp, low(Voice1*2) //load voice table
  96.         mov     ZL, temp
  97.         ldi     temp, high(Voice1*2)
  98.         mov     ZH, temp
  99.         clr     temph   //calculate offset
  100.         add     ZL, voice1nn
  101.         adc     ZH, temph
  102.         lpm
  103.         adc     R0, temph
  104.         brne    Cont1
  105.         rjmp    Start
  106. Cont1:
  107.         mov     temp,R0
  108.         andi    temp,0xE0
  109.         lsr     temp
  110.         lsr     temp
  111.         lsr     temp
  112.         lsr     temp
  113.         lsr     temp
  114.         add     voice1ncl, temp
  115.         adc     voice1nch, temph //temph must be 0
  116.         mov     temp,R0
  117.         andi    temp,0x1F
  118.         mov     voice1n,temp
  119.         inc     voice1nn
  120. EndLdVoice1:
  121.         mov     temph, voice2nch
  122.         mov     temp, voice2ncl
  123.         sub     temp, notecountl
  124.         sbc     temph, notecounth
  125.         brcs    LoadNoteVoice2
  126.         nop
  127.         ldi     temp,8
  128. Delay3:
  129.         dec     temp
  130.         brne    Delay3
  131.         rjmp    EndLdVoice2
  132. LoadNoteVoice2:
  133.         ldi     temp, low(Voice2*2) //load voice table
  134.         mov     ZL, temp
  135.         ldi     temp, high(Voice2*2)
  136.         mov     ZH, temp
  137.         clr     temph   //calculate offset
  138.         add     ZL, voice2nn
  139.         adc     ZH, temph
  140.         lpm
  141.         adc     R0, temph
  142.         brne    Cont2
  143.         rjmp    Start
  144. Cont2:
  145.         mov     temp,R0
  146.         andi    temp,0xE0
  147.         lsr     temp
  148.         lsr     temp
  149.         lsr     temp
  150.         lsr     temp
  151.         lsr     temp
  152.         add     voice2ncl, temp
  153.         adc     voice2nch, temph //temph must be 0
  154.         mov     temp,R0
  155.         andi    temp,0x1F
  156.         mov     voice2n,temp
  157.         inc     voice2nn
  158. EndLdVoice2:
  159.         mov     temph, voice3nch
  160.         mov     temp, voice3ncl
  161.         sub     temp, notecountl
  162.         sbc     temph, notecounth
  163.         brcs    LoadNoteVoice3
  164.         nop
  165.         ldi     temp,8
  166. Delay4:
  167.         dec     temp
  168.         brne    Delay4
  169.         rjmp    EndLdVoice3
  170. LoadNoteVoice3:
  171.         ldi     temp, low(Voice3*2) //load voice table
  172.         mov     ZL, temp
  173.         ldi     temp, high(Voice3*2)
  174.         mov     ZH, temp
  175.         clr     temph   //calculate offset
  176.         add     ZL, voice3nn
  177.         adc     ZH, temph
  178.         lpm
  179.         adc     R0, temph
  180.         brne    Cont3
  181.         rjmp    Start
  182. Cont3:
  183.         mov     temp,R0
  184.         andi    temp,0xE0
  185.         lsr     temp
  186.         lsr     temp
  187.         lsr     temp
  188.         lsr     temp
  189.         lsr     temp
  190.         add     voice3ncl, temp
  191.         adc     voice3nch, temph //temph must be 0
  192.         mov     temp,R0
  193.         andi    temp,0x1F
  194.         mov     voice3n,temp
  195.         inc     voice3nn
  196. EndLdVoice3:
  197. EndNewNote:
  198.         ldi     temp, low(NotesFreq*2) //load frequency table
  199.         mov     ZL, temp
  200.         ldi     temp, high(NotesFreq*2)
  201.         mov     ZH, temp
  202.         clr     temph   //calculate offset
  203.         add     ZL, voice1n
  204.         adc     ZH, temph      
  205.         subi    voice1l,1
  206.         brcs    Reload1
  207.         lpm
  208.         mov     temp,R0
  209.         ldi     temp, 2
  210.         sub     temp,voice1l
  211.         brcs    Bit1Off
  212.         inc     outpin
  213. Bit1Off:
  214.         rjmp    Endload1
  215. Reload1:
  216.         lpm
  217.         mov     voice1l,R0
  218.         nop
  219.         nop
  220.         nop
  221.         nop
  222.         nop
  223. Endload1:
  224.         ldi     temp, low(NotesFreq*2) //load frequency table
  225.         mov     ZL, temp
  226.         ldi     temp, high(NotesFreq*2)
  227.         mov     ZH, temp
  228.         clr     temph   //calculate offset
  229.         add     ZL, voice2n
  230.         adc     ZH, temph      
  231.         subi    voice2l,1
  232.         brcs    Reload2
  233.         lpm
  234.         mov     temp,R0
  235.         ldi     temp, 2
  236.         sub     temp,voice2l
  237.         brcs    Bit2Off
  238.         inc     outpin
  239. Bit2Off:
  240.         rjmp    Endload2
  241. Reload2:
  242.         lpm
  243.         mov     voice2l,R0
  244.         nop
  245.         nop
  246.         nop
  247.         nop
  248.         nop
  249. Endload2:
  250.         ldi     temp, low(NotesFreq*2) //load frequency table
  251.         mov     ZL, temp
  252.         ldi     temp, high(NotesFreq*2)
  253.         mov     ZH, temp
  254.         clr     temph   //calculate offset
  255.         add     ZL, voice3n
  256.         adc     ZH, temph      
  257.         subi    voice3l,1
  258.         brcs    Reload3
  259.         lpm
  260.         mov     temp,R0
  261.         ldi     temp,2
  262.         sub     temp,voice3l
  263.         brcs    Bit3Off
  264.         inc     outpin
  265. Bit3Off:
  266.         rjmp    Endload3
  267. Reload3:
  268.         lpm
  269.         mov     voice3l,R0
  270.         nop
  271.         nop
  272.         nop
  273.         nop
  274.         nop
  275. Endload3:
  276.         ori     outpin,0
  277.         breq    SetPin1
  278.         nop
  279. SetPin0:
  280.         sbi     PORTB, 3
  281.         cbi     PORTB, 4
  282.         rjmp    MainLoop
  283. SetPin1:
  284.         cbi     PORTB, 3
  285.         sbi     PORTB, 4
  286.         rjmp    MainLoop
  287.  
  288. Voice1:
  289.         .db 205, 207, 209, 77, 79, 81, 210, 208
  290.         .db 206, 205, 210, 208, 206, 205, 134, 72
  291.         .db 137, 75, 141, 77, 198, 193, 133, 70
  292.         .db 193, 133, 70, 210, 208, 206, 205, 210
  293.         .db 208, 206, 205, 134, 72, 137, 75, 141
  294.         .db 77, 198, 193, 133, 70, 193, 133, 70
  295.         .db 193, 195, 197, 65, 67, 69, 198, 192
  296.         .db 192, 0
  297. Voice2:
  298.         .db 213, 212, 213, 192, 192, 192, 192, 192
  299.         .db 213, 213, 213, 213, 213, 210, 209, 210
  300.         .db 192, 148, 85, 192, 148, 85, 192, 192
  301.         .db 192, 192, 213, 213, 213, 213, 213, 210
  302.         .db 209, 210, 192, 148, 85, 192, 148, 85
  303.         .db 192, 192, 213, 213, 192, 192, 192, 0
  304. Voice3:
  305.         .db 217, 215, 217, 192, 85, 52, 53, 82
  306.         .db 85, 52, 53, 82, 85, 52, 53, 82
  307.         .db 85, 52, 53, 82, 89, 55, 57, 85
  308.         .db 89, 55, 57, 85, 89, 55, 57, 85
  309.         .db 89, 55, 57, 85, 94, 62, 62, 60
  310.         .db 58, 89, 57, 57, 55, 53, 87, 55
  311.         .db 55, 57, 55, 85, 52, 53, 82, 45
  312.         .db 47, 49, 50, 52, 53, 55, 57, 87
  313.         .db 85, 45, 47, 49, 50, 52, 53, 55
  314.         .db 57, 87, 85, 85, 52, 53, 82, 85
  315.         .db 52, 53, 82, 85, 52, 53, 82, 85
  316.         .db 52, 53, 82, 89, 55, 57, 85, 89
  317.         .db 55, 57, 85, 89, 55, 57, 85, 89
  318.         .db 55, 57, 85, 94, 62, 62, 60, 58
  319.         .db 89, 57, 57, 55, 53, 87, 55, 55
  320.         .db 57, 55, 85, 52, 53, 82, 45, 47
  321.         .db 49, 50, 52, 53, 55, 57, 87, 85
  322.         .db 45, 47, 49, 50, 52, 53, 55, 57
  323.         .db 87, 85, 85, 52, 53, 82, 85, 52
  324.         .db 53, 82, 89, 55, 57, 85, 89, 55
  325.         .db 57, 85, 210, 192, 192, 0
  326.  
  327. .equ    fr_A2   = MaxFreq / 110
  328. .equ    fr_Hb2  = MaxFreq / 116.540940379522
  329. .equ    fr_H2   = MaxFreq / 123.470825314031
  330. .equ    fr_C3   = MaxFreq / 130.812782650299
  331. .equ    fr_Db3  = MaxFreq / 138.591315488436
  332. .equ    fr_D3   = MaxFreq / 146.832383958704
  333. .equ    fr_Eb3  = MaxFreq / 155.56349186104
  334. .equ    fr_E3   = MaxFreq / 164.813778456435
  335. .equ    fr_F3   = MaxFreq / 174.614115716502
  336. .equ    fr_Gb3  = MaxFreq / 184.997211355817
  337. .equ    fr_G3   = MaxFreq / 195.997717990875
  338. .equ    fr_Ab3  = MaxFreq / 207.652348789973
  339. .equ    fr_A3   = MaxFreq / 220
  340. .equ    fr_Hb3  = MaxFreq / 233.081880759045
  341. .equ    fr_H3   = MaxFreq / 246.941650628062
  342. .equ    fr_C4   = MaxFreq / 261.625565300599
  343. .equ    fr_Db4  = MaxFreq / 277.182630976872
  344. .equ    fr_D4   = MaxFreq / 293.664767917408
  345. .equ    fr_Eb4  = MaxFreq / 311.126983722081
  346. .equ    fr_E4   = MaxFreq / 329.62755691287
  347. .equ    fr_F4   = MaxFreq / 349.228231433004
  348. .equ    fr_Gb4  = MaxFreq / 369.994422711635
  349. .equ    fr_G4   = MaxFreq / 391.995435981749
  350. .equ    fr_Ab4  = MaxFreq / 415.304697579945
  351. .equ    fr_A4   = MaxFreq / 440
  352. .equ    fr_Hb4  = MaxFreq / 466.16376151809
  353. .equ    fr_H4   = MaxFreq / 493.883301256124
  354. .equ    fr_C5   = MaxFreq / 523.251130601198
  355. .equ    fr_Db5  = MaxFreq / 554.365261953745
  356. .equ    fr_D5   = MaxFreq / 587.329535834816
  357. .equ    fr_Eb5  = MaxFreq / 622.253967444162
  358. .equ    fr_E5   = MaxFreq / 659.255113825741
  359. .equ    fr_F5   = MaxFreq / 698.456462866008
  360. .equ    fr_Gb5  = MaxFreq / 739.98884542327
  361. .equ    fr_G5   = MaxFreq / 783.990871963499
  362. .equ    fr_Ab5  = MaxFreq / 830.609395159891
  363. .equ    fr_A5   = MaxFreq / 880
  364.  
  365. NotesFreq:
  366.         .db         0,  fr_D3,  fr_Eb3, fr_E3,  fr_F3,  fr_Gb3, fr_G3,  fr_Ab3
  367.         .db     fr_A3,  fr_Hb3, fr_H3, fr_C4,  fr_Db4,  fr_D4,  fr_Eb4, fr_E4
  368.         .db     fr_F4,  fr_Gb4, fr_G4,  fr_Ab4, fr_A4,  fr_Hb4, fr_H4,  fr_C5
  369.         .db     fr_Db5, fr_D5,  fr_Eb5, fr_E5,  fr_F5,  fr_Gb5, fr_G5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement