@@ -39,6 +39,7 @@ Version Modified By Date Comments
3939
4040
4141unsigned long int count_duration=0 ;
42+ volatile bool no_stop = false ;
4243uint8_t pin_sound=0 ;
4344
4445
@@ -52,11 +53,16 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration)
5253 float per=float (1 )/frequency;
5354 time_per=per/0.000008 ;
5455 unsigned int duty=time_per/2 ;
55- float mil=float (duration)/1000 ;
56- if (per>mil)
57- count_duration=1 ;
56+ if (duration > 0 ){
57+ no_stop = false ;
58+ float mil=float (duration)/1000 ;
59+ if (per>mil)
60+ count_duration=1 ;
61+ else
62+ count_duration= mil/per;
63+ }
5864 else
59- count_duration= mil/per ;
65+ no_stop = true ;
6066
6167 // Configure PWM
6268 static uint16_t seq_values[]={0 };
@@ -129,29 +135,41 @@ extern "C"{
129135
130136void PWM0_IRQHandler (void ){
131137 nrf_pwm_event_clear (NRF_PWM0, NRF_PWM_EVENT_PWMPERIODEND);
132- count_duration--;
133- if (count_duration == 0 )
134- noTone (pin_sound);
138+ if (!no_stop){
139+ count_duration--;
140+ if (count_duration == 0 )
141+ noTone (pin_sound);
142+ else
143+ nrf_pwm_task_trigger (NRF_PWM0, NRF_PWM_TASK_SEQSTART0);
144+ }
135145 else
136- nrf_pwm_task_trigger (NRF_PWM0, NRF_PWM_TASK_SEQSTART0);
146+ nrf_pwm_task_trigger (NRF_PWM0, NRF_PWM_TASK_SEQSTART0);
137147}
138148
139149void PWM1_IRQHandler (void ){
140150 nrf_pwm_event_clear (NRF_PWM1, NRF_PWM_EVENT_PWMPERIODEND);
141- count_duration--;
142- if (count_duration == 0 )
143- noTone (pin_sound);
151+ if (!no_stop){
152+ count_duration--;
153+ if (count_duration == 0 )
154+ noTone (pin_sound);
155+ else
156+ nrf_pwm_task_trigger (NRF_PWM1, NRF_PWM_TASK_SEQSTART0);
157+ }
144158 else
145- nrf_pwm_task_trigger (NRF_PWM1, NRF_PWM_TASK_SEQSTART0);
159+ nrf_pwm_task_trigger (NRF_PWM1, NRF_PWM_TASK_SEQSTART0);
146160}
147161
148162void PWM2_IRQHandler (void ){
149163 nrf_pwm_event_clear (NRF_PWM2, NRF_PWM_EVENT_PWMPERIODEND);
150- count_duration--;
151- if (count_duration == 0 )
152- noTone (pin_sound);
164+ if (!no_stop){
165+ count_duration--;
166+ if (count_duration == 0 )
167+ noTone (pin_sound);
168+ else
169+ nrf_pwm_task_trigger (NRF_PWM2, NRF_PWM_TASK_SEQSTART0);
170+ }
153171 else
154- nrf_pwm_task_trigger (NRF_PWM2, NRF_PWM_TASK_SEQSTART0);
172+ nrf_pwm_task_trigger (NRF_PWM2, NRF_PWM_TASK_SEQSTART0);
155173}
156174
157175#ifdef __cplusplus
0 commit comments