Download What`s a Microcontroller Text
Transcript
Chapter #8: Frequency and Sound · Page 253 ' -----[ Subroutine - Find Equals Character ]----------------------------FindEquals: DO READ RTTTL_File + counter, char counter = counter + 1 LOOP UNTIL char = "=" ' ' ' ' ' Go through characters in RTTTL file looking for '='. Increment counter until '=' is found, then return. RETURN ' ' ' ' ' ' -----[ Subroutine - Read Tempo from RTTTL Header ]---------------------Each keyboard character has a unique number called an ASCII value. The characters 0, 1, 2,...9 have ASCII values of 48, 49, 50,...57. You can always convert from the character representing a digit to to its value by subtracting 48 from the variable storing the digit. You can examine this by comparing DEBUG DEC 49 and DEBUG 49. GetTempo: default_b = 0 DO READ RTTTL_File + counter, char IF char = ":" THEN default_b = default_b / 10 counter = counter + 1 EXIT ENDIF default_b = default_b + char - 48 counter = counter + 1 default_b = default_b * 10 LOOP UNTIL char = ":" RETURN ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' Parse RTTTL file for Tempo. Convert characters to digits by subtracting 48 from each character's ASCII value. Iteratively multiply each digit by 10 if there is another digit, then add the most recent digit to one's column. For example, the string "120" is (1 X 10 X 10) + (2 X 10) + 0. The '1' is converted first, then multiplied by 10. The '2' is then converted/added. 0 is converted/added, done. ' -----[ Subroutine - Look up Octave ]-----------------------------------ProcessOctave: READ RTTTL_File + counter, char SELECT char CASE "5" TO "8" noteOctave = char - "0" counter = counter + 1 CASE ELSE noteOctave = default_o ENDSELECT IF default_o = 0 THEN default_o = noteOctave ENDIF ' ' ' ' ' ' ' ' ' ' ' ' ' ' Octave may or may not be included in a given note because any note that is played in the default octave does not specify the octave. If a char from '5' to '8' then use it, else use default_o. Characters are converted to digits by subtracting '0', which is the same as subtracting 48. The first time this subroutine is called, default_o is 0.