-2

I'm trying to generate note for example Do , do's frequency is 523. I wrote some codes but, i did not work Systick 8 mhz

void note1(void){ // Note Do    

    for (int i = 0; i < 523; i++){

        GPIOE->ODR = 0x4000;
        delay_ms(1);
        GPIOE->ODR = 0x0000;
        delay_ms(1);
    }
}

How can we solve this problem ?

EasyMx Pro v7

I'm calling the function like that

void button_handler(void)
{
    note1();

    // Clear pending bit depending on which one is pending
    if (EXTI->PR & (1 << 0)){

        EXTI->PR = (1 << 0);
    }
    else if (EXTI->PR & (1 << 1)){

        EXTI->PR = (1 << 1);
    }       
}

523 times sending 1 and 0 and delay_ms 1 = 1 ms

1000 = 1 sec

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115

2 Answers2

2

On STM32 (as I can see you have it) you have timers which can be configured as PWM output. So use timer, set period and prescaler values according to your needed frequency and set duty cycle on channel to 50%.

If you need 523Hz PWM output, then set your timer PWM to 523Hz using prescaler and period value:

timer_overflow_frequency = timer_input_clock / 
                           (prescaler_value + 1) / 
                           (period_value + 1) ;

Then, for your output channel set value half of timer period value.

For standard peripheral library, tutorial can be used from here: https://stm32f4-discovery.net/2014/05/stm32f4-stm32f429-discovery-pwm-tutorial/

Link from unwind for Cube https://electronics.stackexchange.com/questions/179546/getting-pwm-to-work-on-stm32f4-using-sts-hal-libraries

Community
  • 1
  • 1
unalignedmemoryaccess
  • 7,246
  • 2
  • 25
  • 40
  • This is a very good answer, considering the quality of the question. Still, I think recommending StdPeriphLib isn't optimal, [here is a PWM question using Cube instead](http://electronics.stackexchange.com/questions/179546/getting-pwm-to-work-on-stm32f4-using-sts-hal-libraries). – unwind Mar 28 '17 at 11:23
  • That depends on app, but more optimal than SPL can only be LL library (new ST library). I will add your link to the post. – unalignedmemoryaccess Mar 28 '17 at 11:25
  • I meant "optimal" as in "a good suggestion"; StdPeriphLib is obsolete and should not be used as far as I know, so learning it as a newcomer is not very productive, in my opinion. – unwind Mar 28 '17 at 11:26
  • For STM32F4xx it is absolutelly not absolete, because we still have most designs on this driver. Then here is HAL with Cube interaction (little heavy and processing tool coverintg everything for you) and then there are HAL additional librariey called LL (Low-Level) which are based on static-inline technique in C. They are replacement for SPL. – unalignedmemoryaccess Mar 28 '17 at 11:28
  • The STM32Cube is bloatware designed to lock you in to using ST's parts by making it deceptively simple to get a lot of stuff working quickly. It is certainly not optimal. Perhaps not in this simple example, but in general using STM32Cube forces a lot of quite high-level design decisions on you that you might otherwise choose not to take, such as what RTOS you shall use or even whether you will use an RTOS at all.. – Clifford Mar 28 '17 at 20:12
  • I can agree it is not the best software, that's why there will be release of LL libraries for all versions. For some of them it is already available. I appreciate your opinion @Clifford, on the other hand, I would propose you to say more on ST community if you didn't already so we can do better software. – unalignedmemoryaccess Mar 28 '17 at 20:14
0

You appear to have a fundamental misunderstanding. In your code note1(), the value 523 will affect only the duration of the note, nit its frequency. With 1ms high, 1ms low repeated 523 times you will generate a tone of approximately 500Hz for approximately 1.43 seconds. I say "approximately" because there will be some small overhead in the loop other then the time delays.

A time delay resolution 1ms is insufficient to generate an accurate tone in that manner. To do it in the manner you have, each delay would need to be 1/2f seconds, so for 523Hz approximately 956ms. The loop iteration count would need to be ft, so for say .25 seconds, 131 iterations.

However if button_handler() is as it appears to be an interrupt handler, you really should not be spending 1.46 seconds in an interrupt handler!

In any event this is an extraordinarily laborious, CPU intensive and inaccurate method of generating a specific frequency. The STM32 on your board is well endowed with hardware timers with direct GPIO output that will generate the frequency you need accurately with zero software over head. Even if none of the timers map to a suitable GPIO output that you need to use, ou can still get one to generate an interrupt at 1/2f and toggle the pin in the interrupt handler. Either way that will leave the processor free to do useful stuff while the tone is being output.

Clifford
  • 88,407
  • 13
  • 85
  • 165