Codigo Mozart
Codigo Mozart
Codigo Mozart
#include "P16F84.INC" ; Include header file CBLOCK 0x10 tempo tptrl tptrh note length pitch temp dl1 dl2 ENDC ; Temporary storage
ORG 0 goto initialise ORG 4 goto toggle ; Register set up: clrw movwf PORTA movwf PORTB STATUS,RP0 TRISB ; Zero. ; Ensure PORTB is zero before we enable it ; Ensure PORTB is zero before we enable it ; Select Bank 1 ; Port B is outputs
. bsf clrf
; Set up timer 0 for tone generation movlw 0x02 ; Prescaler on, internal clocking, divide by 4 movwf bcf OPTION_REG STATUS,RP0 ; Back to bank 0
wait wloop
for a button PORTB PORTA,0 playtune0 PORTA,1 playtune1 wloop tunes 0x13 PORTB 0x50 tempo tune0 / 0x100 tptrh tune0 % 0x100 tptrl playtune 0x23
;Routines to play the playtune0 movlw movwf movlw movwf movlw movwf movlw movwf goto playtune1 movlw
PORTB 0xA0 tempo tune1 / 0x100 tptrh tune1 % 0x100 tptrl playtune ; ; ; ; ; ; ; Lookup note from tune table Store the note code Bit 7 set => end of tune Back to waiting for a button press Increment the tune pointer Get the note length Store it
;Subroutine to play a tune playtune call gettunedata movwf note btfsc note,7 goto wait incf tptrl,F call gettunedata movwf length
; Play the stored note ; The tune table supports two octaves and 8 note lengths. playnote btfsc note,6 ; Bit 6 set => rest goto playwait ; Silence ensues... movf note,W ; Retrieve the note andlw 0x3F ; Mask off the pitch index call pitchtable ; and look it up in the pitchtable movwf pitch ; Transfer the value to Timer 0 movwf TMR0 bcf INTCON,T0IF ; Clear Timer Interrupt bsf INTCON,T0IE ; Enable interrupts for Timer 0 bsf INTCON,GIE playwait movf length,W ; Retrieve the note length movwf dl1 ; and store it in delay counter 1 loop1 movf tempo,W ; This value sets the timing resolution movwf dl2 loop2 nop ; Inner delay loop delay nop decfsz dl2,F goto loop2 decfsz dl1,F ; Outer delay loop goto loop1 bcf INTCON,T0IE ; Timer interrupts off ;Fetch next note/length pair: incf tptrl,F btfsc STATUS,Z incf tptrh,F goto playtune ; ; ; ; Increment tune ptr Test for low byte rollover Inc high byte Loop for next note
; Routine to fetch data from the tune tables gettunedata movf tptrh,W ; Set up PCLATH for the jump movwf PCLATH movf tptrl,W ; Get low address of tptr movwf PCL ; Jump off to the table entry ; Pitch lookup function pitchtable clrf PCLATH addwf PCL,F retlw 0x11 retlw 0x1F
retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw
0x2B 0x37 0x42 0x4D 0x57 0x61 0x6A 0x72 0x7A 0x81 0x89 0x8F 0x96 0x9C 0xA1 0xA7 0xAC 0xB0 0xB5 0xB9 0xBD 0xC1 0xC4 0xC8 0xCB 0xCE 0xD1 0xD3 0xD6 0xD8 0xDA 0xDC 0xDE 0xE0 0xE2
;D ;D# ;E ;F ;F# ;G ;G# ;A (440) ;Bb ;B ;C6 // Midi Code 72 ;Db ;D ;Eb ;E ;F ;F# ;G ;Ab ;A (880) ;Bb ;B ;C7 // Midi Code 84 ;Db ;D ;Eb ;E ;F ;F# ;G ;Ab ;A (1760) ;Bb ; ;C8 // Midi Code 96
;Interrupt service - toggle the drive polarity to the sounder ;Note that this does not preserve the status bits - be careful, ;and that there is an assumption we are addressing bank 0. toggle movwf temp movf pitch,W movwf TMR0 movf PORTB,W xorlw 0x0F movwf PORTB bcf INTCON,T0IF movf temp,w retfie ;Tune Tables ;Must be on even byte boundary org 0x0080 ; Tune 0 Data ; Bach 'Badinerie' from Suite No 2 tune0 retlw 0x17 ;B
retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw
0x78 0x1A 0x3C 0x17 0x3C 0x12 0x78 0x17 0x3C 0x12 0x3C 0x0E 0x78 0x12 0x3C 0x0E 0x3C 0x0B 0xF0 0x06 0x3C 0x0B 0x3C 0x0E 0x3C 0x0B 0x3C 0x0D 0x3C 0x0B 0x3C 0x0D 0x3C 0x0B 0x3C 0x0A 0x3C 0x0D 0x3C 0x10 0x3C 0x0D 0x3C 0x0E 0x78 0x0B 0x78 0x17 0x78 0x1A 0x3C 0x17 0x3C 0x12 0x78 0x17 0x3C 0x12 0x3C 0x0E
;240 ;D ;120 ;B ;120 ;F# ;240 ;B ;120 ;F# ;120 ;D ;240 ;F# ;120 ;D ;120 ;B ;480 ;F# ;120 ;B ;120 ;D ;120 ;B ;120 ;C# ;120 ;B ;120 ;C# ;120 ;B ;120 ;A# ;120 ;C# ;120 ;E ;120 ;C# ;120 ;D ;240 ;B ;240 ;B ;240 ;D ;120 ;B ;120 ;F# ;240 ;B ;120 ;F# ;120 ;D
retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw
0x78 0x12 0x3C 0x0E 0x3C 0x0B 0xF0 0x0E 0x78 0x0E 0x78 0x0E 0x78 0x0E 0x78 0x17 0x78 0x0E 0x78 0x10 0x3C 0x0E 0x3C 0x0D 0x78 0x12 0x78 0x12 0x78 0x12 0x78 0x12 0x78 0x1A 0x78 0x12 0x78 0x12 0x3C 0x14 0x3C 0x11 0x78 0x0D 0x3C 0x12 0x3C 0x15 0x3C 0x12 0x3C 0x14 0x3C 0x12 0x3C 0x14 0x3C 0x12 0x3C 0x11
;240 ;F# ;120 ;D ;120 ;B ;480 ;D ;240 ;D ;240 ;D ;240 ;D ;240 ;B ;240 ;D ;240 ;E ;60 ;D ;30 ;C# ;240 ;F# ;240 ;F# ;240 ;F# ;240 ;F# ;240 ;D ;240 ;F# ;220 ;F# ;30 ;G# ;65 ;F ;270 ;C# ;120 ;F# ;120 ;A ;120 ;F# ;120 ;G# ;120 ;F# ;120 ;G# ;120 ;F# ;120 ;F
retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw
0x3C 0x14 0x3C 0x17 0x3C 0x14 0x3C 0x15 0x3C 0x14 0x3C 0x15 0x3C 0x14 0x3C 0x12 0x3C 0x15 0x3C 0x12 0x3C 0x11 0x3C 0x12 0x3C 0x17 0x3C 0x12 0x3C 0x11 0x39 0x12 0x3C 0x19 0x3E 0x12 0x3C 0x11 0x3C 0x12 0x39 0x1A 0x3E 0x12 0x3C 0x11 0x3C 0x12 0x3C 0x1A 0x3C 0x19 0x3C 0x17 0x3C 0x19 0x3C 0x15 0x3C 0x14
;120 ;G# ;120 ;B ;120 ;G# ;120 ;A ;120 ;G# ;120 ;A ;120 ;G# ;120 ;F# ;120 ;A ;120 ;F# ;120 ;F ;120 ;F# ;120 ;B ;120 ;F# ;120 ;F ;115 ;F# ;120 ;C# ;125 ;F# ;120 ;F ;120 ;F# ;115 ;D ;125 ;F# ;120 ;F ;120 ;F# ;120 ;D ;120 ;C# ;120 ;B ;120 ;C# ;120 ;A ;120 ;G#
retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw
0x3C 0x12 0x3C 0x15 0x78 0x14 0x3C 0x12 0x3C 0x12 0xE0 0xFF 0
; Tune 1 ; Mozart Eine Klein Nachtmusik tune1 retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw 0x13 0x43 0x40 0x4D 0x0E 0x18 0x40 0x1D 0x13 0x3E 0x40 0x48 0x0E 0x1B 0x40 0x18 0x13 0x15 0x40 0x19 0x0E 0x15 0x40 0x15 0x13 0x17 0x40 0x18 0x17 0x17 0x40 0x1B 0x1A 0x3B 0x40 0x83 0x18 0x47 0x40 0x4A ;G ;538 ;REST ;617 ;D ;193 ;REST ;232 ;G ;498 ;REST ;582 ;D ;218 ;REST ;192 ;G ;168 ;REST ;207 ;D ;168 ;REST ;172 ;G ;188 ;REST ;197 ;B ;188 ;REST ;222 ;D ;478 ;REST ;1052 ;C ;568 ;REST ;597
retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw
0x15 0x15 0x40 0x1D 0x18 0x3C 0x40 0x52 0x15 0x16 0x40 0x16 0x18 0x1C 0x40 0x12 0x15 0x16 0x40 0x18 0x12 0x18 0x40 0x14 0x15 0x1D 0x40 0x18 0x0E 0x39 0x40 0x82 0x13 0x27 0x40 0x3E 0x13 0x6B 0x40 0x1E 0x17 0x31 0x15 0x21 0x40 0x07 0x13 0x16 0x40 0x18 0x13 0x16 0x15 0x13 0x13 0x12 0x12 0x10 0x40 0x22
;A ;168 ;REST ;237 ;C ;483 ;REST ;657 ;A ;183 ;REST ;177 ;C ;228 ;REST ;147 ;A ;183 ;REST ;197 ;F# ;198 ;REST ;167 ;A ;238 ;REST ;192 ;D ;458 ;REST ;1042 ;G ;313 ;REST ;497 ;G ;858 ;REST ;242 ;B ;395 ;A ;268 ;REST ;57 ;G ;183 ;REST ;197 ;G ;180 ;A ;155 ;G ;145 ;F# ;133 ;REST ;272
retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw
0x12 0x68 0x40 0x1F 0x15 0x2C 0x18 0x1B 0x40 0x14 0x12 0x13 0x40 0x19 0x15 0x2F 0x13 0x13 0x40 0x22 0x13 0x78 0x40 0x1D 0x17 0x2A 0x40 0x03 0x15 0x22 0x40 0x0C 0x13 0x16 0x40 0x1A 0x13 0x19 0x15 0x12 0x13 0x11 0x12 0x12 0x40 0x1F 0x12 0x5B 0x40 0x27 0x15 0x2D 0x18 0x1A 0x40 0x13 0x12 0x14 0x40 0x19
;F# ;833 ;REST ;252 ;A ;355 ;C ;218 ;REST ;162 ;F# ;158 ;REST ;202 ;A ;380 ;G ;158 ;REST ;272 ;G ;963 ;REST ;237 ;B ;343 ;REST ;27 ;A ;278 ;REST ;102 ;G ;183 ;REST ;212 ;G ;205 ;A ;150 ;G ;140 ;F# ;148 ;REST ;252 ;F# ;728 ;REST ;317 ;A ;365 ;C ;208 ;REST ;152 ;F# ;163 ;REST ;207
retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw
0x13 0x14 0x40 0x19 0x13 0x13 0x40 0x1A 0x13 0x1C 0x12 0x15 0x10 0x1E 0x12 0x12 0x13 0x18 0x40 0x16 0x13 0x10 0x40 0x23 0x17 0x17 0x15 0x0D 0x13 0x1F 0x15 0x0F 0x17 0x18 0x40 0x1A 0x17 0x10 0x40 0x20 0x1A 0x19 0x18 0x0F 0x17 0x1F 0x18 0x10 0x1A 0x59 0x40 0x7B 0x0E 0xBA 0x10 0x7C 0x40 0x48 0x0E 0x19
;G ;163 ;REST ;207 ;G ;153 ;REST ;212 ;G ;225 ;F# ;175 ;E ;240 ;F# ;150 ;G ;198 ;REST ;177 ;G ;128 ;REST ;287 ;B ;190 ;A ;105 ;G ;255 ;A ;125 ;B ;193 ;REST ;212 ;B ;128 ;REST ;257 ;D ;200 ;C ;125 ;B ;250 ;C ;135 ;D ;713 ;REST ;987 ;D ;1495 ;E ;998 ;REST ;577 ;D ;205
retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw
0x0C 0x16 0x40 0x31 0x0C 0x14 0x40 0x4C 0x0C 0x17 0x0B 0x12 0x40 0x39 0x0B 0x13 0x40 0x43 0x0B 0x17 0x09 0x13 0x40 0x37 0x09 0x16 0x40 0x41 0x07 0x2E 0x06 0x33 0x04 0x2D 0x40 0x01 0x06 0x35 0x40 0x02 0x07 0x16 0x40 0x50 0x09 0x10 0x40 0x4C 0x0B 0x0E 0x40 0xB4 0x0E 0xBA 0x10 0x82 0x40 0x39 0x0E 0x2D
;C ;183 ;REST ;397 ;C ;163 ;REST ;612 ;C ;190 ;B ;148 ;REST ;457 ;B ;153 ;REST ;537 ;B ;190 ;A ;153 ;REST ;447 ;A ;178 ;REST ;522 ;G ;375 ;F# ;410 ;E ;363 ;REST ;12 ;F# ;428 ;REST ;22 ;G ;178 ;REST ;642 ;A ;133 ;REST ;612 ;B ;113 ;REST ;1442 ;D ;1495 ;E ;1043 ;REST ;462 ;D ;360
retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw
0x0C 0x16 0x40 0x1E 0x0C 0x10 0x40 0x1F 0x0C 0x10 0x40 0x1C 0x0C 0x2C 0x0B 0x13 0x40 0x1E 0x0B 0x0D 0x40 0x23 0x0B 0x10 0x40 0x1E 0x0B 0x2F 0x09 0x13 0x40 0x1B 0x09 0x13 0x40 0x1E 0x09 0x10 0x40 0x23 0x07 0x2B 0x06 0x2F 0x04 0x31 0x06 0x35 0x40 0x05 0x07 0xB4 0xFF END
;C ;178 ;REST ;242 ;C ;128 ;REST ;252 ;C ;133 ;REST ;227 ;C ;355 ;B ;153 ;REST ;247 ;B ;108 ;REST ;282 ;B ;133 ;REST ;247 ;B ;380 ;A ;158 ;REST ;222 ;A ;153 ;REST ;242 ;A ;128 ;REST ;282 ;G ;350 ;F# ;380 ;E ;395 ;F# ;428 ;REST ;47 ;G ;468