void BUZZER_Handler( void ) { switch ( Buzzer_Mode ) { case BUZZER_PLAYMUSIC : if ( Buzzer_Counter <= 0 ) { /* Play next note*/ BUZZER_PlayNote(); } else { /* Continue the current note*/ Buzzer_Counter--; } break; case BUZZER_SHORTBEEP : if ( Buzzer_Counter++ >= WEIGHTED_TIME( BUZZER_SHORTBEEP_DURATION ) ) { BUZZER_SetMode( BUZZER_OFF ); } break; case BUZZER_LONGBEEP : if ( Buzzer_Counter++ >= WEIGHTED_TIME( BUZZER_LONGBEEP_DURATION ) ) { BUZZER_SetMode( BUZZER_OFF ); } break; } }
NODEBUG2 void BUZZER_Init( void ) { #if !AUDIO_AVAIL GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIOB clock */ RCC_PERIPH_GPIO_CLOCK_CMD( RCC_APB2Periph_GPIOB, ENABLE ); /* GPIOB Configuration: TIM3 3in Output */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( GPIOB, &GPIO_InitStructure ); /* TIM3 Configuration ------------------------------------------------------*/ /* TIM3CLK = 18 MHz, Prescaler = 0x0, TIM3 counter clock = 18 MHz */ /* CC update rate = TIM3 counter clock / (2* CCR_Val) ~= 750 Hz */ /* Enable TIM3 clock */ RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE ); TIM_DeInit( TIM3 ); TIM_TimeBaseStructInit( &TIM_TimeBaseStructure ); TIM_OCStructInit( &TIM_OCInitStructure ); /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 0xFFFF; TIM_TimeBaseStructure.TIM_Prescaler = 0x0; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit( TIM3, &TIM_TimeBaseStructure ); /* Output Compare Toggle Mode configuration: Channel3 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; /* in FWLib v1.0 : TIM_OCInitStructure.TIM_Channel = TIM_Channel_3;*/ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; /* now in in FWLib v2.0*/ TIM_OCInitStructure.TIM_Pulse = CCR_Val; TIM_OC3Init( TIM3, &TIM_OCInitStructure ); /* changed against FWLib v2.0*/ TIM_OC3PreloadConfig( TIM3, TIM_OCPreload_Disable ); BUZZER_SetFrequency( 440 ); #endif #if AUDIO_AVAIL /* RQ : init done by Audio init*/ #endif BUZZER_SetMode( BUZZER_OFF ); }
void BUZZER_PlayMusic( const u8* melody ) { u8 c; u8 default_id = 0; u16 default_val = 0; DefaultOctave = OCT_880; /* Default for the default Octave.*/ DefaultDuration = 4; /* Default for the default Duration.*/ DefaultBeats = 63; CurrentMelody = melody; CurrentMelodySTART = melody; while ( *CurrentMelody != RTTTL_SEP ) { if ( *CurrentMelody == 0 ) { return; } /* Discard the melody name.*/ CurrentMelody++; } /* Now read the defaults if any.*/ for ( ++CurrentMelody; *CurrentMelody != RTTTL_SEP; CurrentMelody++ ) { if ( *CurrentMelody == 0 ) { return; } /* Discard any blank.*/ while ( *CurrentMelody == ' ' ) { CurrentMelody++; } c = *CurrentMelody; if ( c == RTTTL_SEP ) { break; } if (( c >= 'a' ) && ( c <= 'z' ) ) { c += ( 'A' - 'a' ); } if (( c >= 'A' ) && ( c <= 'Z' ) ) { default_id = c; continue; } if (( c >= '0' ) && ( c <= '9' ) ) { default_val *= 10; default_val += ( c - '0' ); c = * ( CurrentMelody + 1 ); if (( c >= '0' ) && ( c <= '9' ) ) { continue; } if ( default_id == 'D' ) { DefaultDuration = default_val; } else if ( default_id == 'O' ) { DefaultOctave = default_val - 5; if ( DefaultOctave > OCT_7040 ) DefaultOctave = OCT_440; } else if ( default_id == 'B' ) { DefaultBeats = default_val; /* Check minimum BPM, so that the counter can be a 16-bit value */ if (( DefaultBeats < 16 ) || ( DefaultBeats > 500 ) ) DefaultBeats = 63; } default_val = 0; default_id = 0; } } BUZZER_SetMode( BUZZER_PLAYMUSIC ); }
void BUZZER_PlayNote( void ) { u8 duration = DefaultDuration; u8 c; /* Discard blank characters*/ while ( *CurrentMelody == ' ' ) { CurrentMelody++; } /* Check whether a duration is present.*/ if (( *CurrentMelody > '0' ) && ( *CurrentMelody < '9' ) ) { duration = *CurrentMelody++ - '0'; if (( *CurrentMelody > '0' ) && ( *CurrentMelody < '9' ) ) { duration *= 10; duration += ( *CurrentMelody++ - '0' ); } } #if __RCSTM8__ // Compute the number of milliseconds of the given note // Buzzer_Counter = 32L * 60 * 1000 / duration / DefaultBeats; // This is the THEORETICAL value Buzzer_Counter = 35000 / DefaultBeats * 10 / duration; // This is the value that matches CircleOS3 #else // TODO verify the code and rewrite for efficiency Buzzer_Counter = (( 32 / duration ) * 256L * 32L ) / DefaultBeats; Buzzer_Counter *= ( SYSTEM_CLOCK / 12000L ); Buzzer_Counter /= 1000L; /*FL081103 divide in two stages to keep precision*/ #endif /* Read the note*/ c = *CurrentMelody++; if (( c >= 'a' ) && ( c <= 'z' ) ) { c += ( 'A' - 'a' ); } if ( c == 'P' ) { note = NOTE_PAUSE; } else if (( c >= 'A' ) && ( c <= 'G' ) ) { note = ( c - 'A' ) + NOTE_LA; if ( *CurrentMelody == '#' ) { note |= 0x8; CurrentMelody++; } } octave = DefaultOctave; c = *CurrentMelody; if (( c >= '5' ) && ( c <= '8' ) ) { octave = OCT_440 + ( c - '5' ); CurrentMelody++; } BUZZER_SetFrequency(( Note_Freq[note] *( 1 << octave ) ) ); /* Handle special duration */ if ( *CurrentMelody == '.' ) { /* Dotted note: Add half duration */ Buzzer_Counter += ( Buzzer_Counter / 2 ); CurrentMelody++; } /* Discard delimiter */ while (( c = *CurrentMelody++ ) != 0 ) { if ( c == ',' ) break; } /* Check end of melody */ if ( c == 0 ) { CurrentMelody = 0; BUZZER_SetMode( BUZZER_OFF ); } }
static void PlayMusic( void ) { u8 duration = DefaultDuration; u8 c; // Discard blank characters while ( *CurrentMelody == ' ') { CurrentMelody++; } // Check whether a duration is present. if ( (*CurrentMelody > '0') && (*CurrentMelody < '9') ) { duration = *CurrentMelody++ - '0'; if ( (*CurrentMelody > '0') && (*CurrentMelody < '9') ) { duration *= 10; duration += (*CurrentMelody++ - '0'); } } Buzzer_Counter = ( (32/duration) * 256L * 32L) / DefaultBeats; Buzzer_Counter*= (RCC_ClockFreq.SYSCLK_Frequency / 12000000L); //Adapt to HCLK1 //read the note c = *CurrentMelody++; if ( (c >= 'a') && (c <= 'z') ) { c+=('A'-'a'); } if ( c == 'P' ) { note = NOTE_PAUSE; } else if ( (c >= 'A') && (c <= 'G') ) { note = (c - 'A') + NOTE_LA; if ( *CurrentMelody == '#' ) { note|=0x8; CurrentMelody++; } } octave = DefaultOctave; c = *CurrentMelody; if ( (c>= '5') && (c<= '8') ) { octave = OCT_440 + (c-'5'); CurrentMelody++; } BUZZER_SetFrequency ( (Note_Freq [ note ] * (1<<octave))); //discard delimiter and ignore special duration while ( (c = *CurrentMelody++) != 0 ) { if ( c==',') break; } if ( *(CurrentMelody-1)==0 ) { CurrentMelody = 0; } if ( c == 0 ) { BUZZER_SetMode ( BUZZER_OFF ); } }