Melody Player Author: Slav Slavov Sliven email: ell@sliven.osf.acad.bg Flow Chart: begin APPLICATION OPERATION : This application generates a melody. It was a little bit difficult to place the tables because of the program counter's 8 bits, but I found a way to do this. There are 8 melodies programmed in the PIC12C509 device. When you push the button a melody is played. Every melody finishes with " 0x0" which is the mark to stop playing. When the button is pressed again the next melody is played. There are two software generators of 7.5Hz and 100KHz. The first defines the continuation of the played note and the second gives the frequency of the played note. The 7.5Hz generator uses TMR0 and two flags (Flag.0 and Flag.1) to make the frequency. The 100KHz generator uses 10 instruction cycles to make the frequency. Here is a table of the used frequencies: B 0xCA 0x33 A# 0xD6 0x36 A 0xE4 0x39 G# 0xF0 0x3C G 0xFF 0x40 F# 0x44 F E D# 0x50 D C# 0xB4 C code is used for pause code 0x00 is used for stop mark Waiting for button press Increment the melody counter yes Is the counter >7? no melody counter = 0 note counter = 0 label: get note (Melody counter, note counter) no Is the note 0? yes begin yes Is the note 1? no play pause play note increment note counter label Microchip Technology Incorporated, has been granted a nonexclusive, worldwide license to reproduce, publish and distribute all submitted materials, in either original or edited form. The author has affirmed that this work is an original, unpublished work and that he/she owns all rights to such work. All property rights, such as patents, copyrights and trademarks remain with author. 1997 Microchip Technology Inc. DS40160A/4_018-page 1
Graphical hardware representation: 5V 3W/8ohm PIC12C509 GP1 1K b e C KT805AM GND GP0 Switch GND Bill of Materials (BOM): Part# PIC12C509 Switch Speaker Resistor 1K KT805AM Manufacturer MICROCHIP DS40160A/4_018-page 2 1997 Microchip Technology Inc.
APPENDIX A: SOURCE CODE ;**************************************************************** ; MLDY509.ASM ; ;**************************************************************** LIST p=12c509 #include "..\inc\p12c509.inc" config _WDT_OFF & _IntRC_OSC & _MCLRE_OFF & _CP_OFF RAM equ 0x07 ;Begining of RAM NumSongs equ.8 Out equ GPIO Speaker equ 2 In equ GPIO Btn equ 0 cblock RAM endc org Counter Flag Dta Melody Note Count1 Count2 0x00 begin ProgLp: ProgLoop ;------------------------------------------- GetNote: bcf bcf rlf bcf bcf bcf bcf STATUS,C Melody,W Melody1 Melody2 Melody3 Melody4 Melody5 Melody6 Melody7 1997 Microchip Technology Inc. DS40160A/4_018-page 3
DS40160A/4_018-page 4 1997 Microchip Technology Inc. Melody8 ;---------------------------------------- ; PINCO ;---------------------------------------- Melody1: 0xCA 0xCC 0xD6 0 ;--------------------------------------------- ; MENDELSON ;--------------------------------------------- Melody2:
1997 Microchip Technology Inc. DS40160A/4_018-page 5 0x0 ; A MELODY I
DS40160A/4_018-page 6 1997 Microchip Technology Inc. Melody3: 0x0 ; ENGLISH WALTZ Melody4:
0x0 begin: org 0x100 movwf OSCCAL ;calibrating the internal oscillator clrf movlw GPIO B'00111011' 1997 Microchip Technology Inc. DS40160A/4_018-page 7
main: TRIS movlw OPTION clrf GPIO B'10000111' Melody In,Btn $-1 Loop clrf Count1 movlw.52 movwf Count2 decfsz Count1,F $-1 Loop1: decfsz incf movlw subwf clrf clrf call movwf incf clrf call Count2,F Loop In,Btn main In,Btn main Melody,F NumSongs Melody,w STATUS,C Melody Note GetNote Dta Dta,F STATUS,Z main Note,F Flag ProgLp Loop1 ;--------------------------------------------------- ; Software cycle for 10 us that implements ; the hardware generator of 100KHz. Also a timer ; for about 7.5Hz is made for the continuation of the ; notes that are played. ; ;--------------------------------------------------- ProgLoop: Dta,W movwf Counter xorlw 1 btfss STATUS,Z Out,Speaker ;sets the output to high ;------------------------------------- ;100KHz (10us) generator ProgLoop1: DS40160A/4_018-page 8 1997 Microchip Technology Inc.
btfss TMR0,7 Flag,0 Flag,0 ProgDelay1 TMR0,W STATUS,Z Flag,1 BackProgDelay1: decfsz Counter,F ProgLoop1 Prog2 ProgDelay1: BackProgDelay1 ; ;-------------------------------------- Prog2: movwf Dta,W Counter bcf Out,Speaker ;sets the output to low ProgLoop2: btfss TMR0,7 Flag,0 Flag,0 ProgDelay2 TMR0,W STATUS,Z Flag,1 BackProgDelay2: ProgDelay2: ProgLoop3 decfsz btfss return clrf Counter,F ProgLoop2 ProgLoop3 BackProgDelay2 Flag,1 ProgLoop Flag,7 Flag Flag,7 ProgLoop org 0x200 ; SLEEP BABE SLEEP Melody5: 1997 Microchip Technology Inc. DS40160A/4_018-page 9
DS40160A/4_018-page 10 1997 Microchip Technology Inc.
1997 Microchip Technology Inc. DS40160A/4_018-page 11 0x0 ; A MELODY II Melody6: 0x84 0xB4 0xB4 0xB4 0x50 0x50 0x50
DS40160A/4_018-page 12 1997 Microchip Technology Inc. 0x0 ; FINAL COUNTDOWN Melody7: 0x0
1997 Microchip Technology Inc. DS40160A/4_018-page 13 ; IN THE MORNING Melody8:
0x0 END DS40160A/4_018-page 14 1997 Microchip Technology Inc.