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_Init( void ) { GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIOB clock */ RCC_APB2PeriphClockCmd( 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; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR_Val; TIM_OC3Init( TIM3, &TIM_OCInitStructure ); TIM_OC3PreloadConfig( TIM3, TIM_OCPreload_Disable ); BUZZER_SetFrequency( 440 ); /* Enable TIM3 IT */ TIM_ITConfig( TIM3, TIM_IT_CC3, ENABLE ); Buzzer_Mode = BUZZER_OFF; }
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 ); } }