[SOLVED] generator on a timer with an interval of 25 nanoseconds ?

acronis
Fri Dec 15, 2017 12:39 pm
Tell me a simple code example for the squarewave generator on a timer with an interval of 25 nanoseconds (25 MHz)
Board STM32F407 BLACK.

Did so, but at least 1 microsecond. It should be faster !

void setup()
{
timer.pause();

timer.setPeriod(1);
timer.setChannel1Mode(TIMER_OUTPUT_COMPARE);
timer.setCompare(TIMER_CH1, 1);
timer.attachCompare1Interrupt(timer_interrrupts);
timer.refresh();
timer.resume();
}

void timer_interrrupts()
{
if (digitalRead(PA2) == true)
digitalWrite (PA2, LOW);
else
digitalWrite (PA2, HIGH);
}


stevestrong
Fri Dec 15, 2017 4:32 pm
You should use directly the PWM output signal of timer instead of digital port output.
Add this line to setup() to generate PWM signal on a timer channel pin:
pinMode(OUTPUT_TIMER_CHANNEL_PIN, PWM);

RogerClark
Fri Dec 15, 2017 7:07 pm
25 Nano seconds is not 25MHz, it’s 40Mhz

You would probably need to set the clock PLL so the board ran at 160MHz so the timer can have a divider of 4.


acronis
Sun Dec 17, 2017 4:20 am
Hello stevestrong !
I included the line pinMode(PA1, PWM) in setup().

What code is necessary to enter into
void timer_interrrupts()
{
???
}

for this to work ?


acronis
Sun Dec 17, 2017 4:26 am
Hello RogerClark !

Whether correctly I understood You that it is necessary to do so ?
timer.setPrescaleFactor(4);

If not, tell me how to do it. I really need the PWM frequency is much faster than 1 MHz!


RogerClark
Sun Dec 17, 2017 11:29 am
Depends on what resolution PWM you need

E.g. on the F103, with 8 bit resolution, max PWM rate will be 72/256 = approx 280 Khz is max PWM rate

On the F4 with 160 Mhz clock rate and 8 bit resolution PWM that only gives you a PWM freq 625 kHz

None of these boards will give you a PWM of period of 1Mhz. Except perhaps if you extremely overclock the processor to 256Mhz, and I’m not sure if anyone has successfully done that.

I think you’ll need to use something like a FPGA to achieve the sort of performance you are looking for


acronis
Sun Dec 17, 2017 2:27 pm
I understand You correctly, that the more frequency of 625 kHz at the timer without overclocking out any ways ?
I need a simple generator of rectangular pulses

And without the use of PWM it is possible to obtain a high speed of generation ?


victor_pv
Sun Dec 17, 2017 3:30 pm
Yes, you can obtain Mhz output in a pin by setting the reload value to 1, prescaler to 0, and timer compare to either 0 and 1.

stevestrong
Sun Dec 17, 2017 9:05 pm
The problem seems to be on the F4 side in the Roger’s (and my) repo.
I am investigating the issue, but it seems that major rework is necessary.

RogerClark
Sun Dec 17, 2017 9:11 pm
@acronis

You have written conflicting requirements.

You said 25nS interval. Which is possible using he F4 processor.

You also said 1MHz PWM, which is not possible ( assuming 8 bit PWM resolution)

I think you need to explain what you are trying to do, and why….


acronis
Sun Dec 17, 2017 11:13 pm
Good afternoon everyone.

I need to generate a square signal of 1 bit (output either 0 or 1 ) with high frequency using a timer.
The frequency of generation needs the most high , more than 1 MHz

Image https://yadi.sk/i/O_BCSslH3Qheba
( Tell me on what site to upload images so that they appear on the forum in the topic)


stevestrong
Mon Dec 18, 2017 1:25 am
Try this:
#define timer Timer5

#define TIMER5_CH1_PIN PA0

void setup()
{
pinMode(TIMER5_CH1_PIN, PWM);
gpio_set_af_mode(TIMER5_CH1_PIN, 2);

timer.pause();
timer.setPrescaleFactor(1); // use 42MHz internal clock
timer.setOverflow(2); // output ~21MHz signal (48ns period)
timer.setCompare(TIMER_CH1, 1); // 50% duty cycle: 24ns low + 24ns high
timer.refresh();
timer.resume();
}


RogerClark
Mon Dec 18, 2017 4:55 am
OK

So what is needed is a 20Mhz square wave.


acronis
Mon Dec 18, 2017 1:02 pm
Hello stevestrong !

I am very grateful for Your invaluable help in the study of this beautiful microcontroller STM32F407 !
It is not a rectangular generator, but I think this is what I need and will work fine from me !

Code I liked it, but I have a few questions for You:
1. I have not found information on the forum about the line gpio_set_af_mode(TIMER5_CH1_PIN, 2);
What does it do and what does the digit 2 ?

2. Is it possible to assign a different pin out ? For example PA2 ? I tried but I not working. How can I change ?

3. Is it possible to obtain even greater frequency generator, for example 84 or 100 or 168 MHz ?

I would be very grateful if You could help me with these newbie .


ahull
Mon Dec 18, 2017 1:21 pm
[acronis – Mon Dec 18, 2017 1:02 pm] –

3. Is it possible to obtain even greater frequency generator, for example 84 or 100 or 168 MHz ?

Probably not directly, but there are several ways you can double the frequencies you *can* generate, so long as you don’t mind adding a bit of extra hardware, for example with a couple of flip flops.

Image

.. although I’m not sure if the humble CD4528 would be fast enough for your application, so you may have to do a bit of googling to find something suitable.

You could also look at the ad9851 dds signal generator, controlled by the STM32F103 of course :D

https://www.ebay.com/sch/i.html?_sacat= … dds&_frs=1

What is the purpose of this square wave?


acronis
Mon Dec 18, 2017 1:28 pm
Hi ahull.

Thanks for Your hint, but I’m not good friends with the construction of schemes and I don’t want to use any external components , and I want to only microcontroller .
The more code that has prompted dear sevestrong works fine.
Just want to deal with those questions that I wrote that I understood as with it to work correctly.

We are developing an interesting project to measure the percent alcohol.
To modify the desired generation.
First generated signal , and then analyze the response by the ADC of the microcontroller.
By the way ADC has me very helped sevestrong


edogaldo
Mon Dec 18, 2017 1:32 pm
[acronis – Mon Dec 18, 2017 1:02 pm] –
3. Is it possible to obtain even greater frequency generator, for example 84 or 100 or 168 MHz ?

If you just need a square wave, it should be possible to to output the system clock (up to 168MHz) via the MCO pins but I can’t provide you the technical details..

Cheers, E.


acronis
Mon Dec 18, 2017 1:35 pm
If you just need a square wave, it should be possible to to output the system clock (up to 168MHz) via the MCO pins but I can’t provide you the technical details..

And where can I see information ?


acronis
Mon Dec 18, 2017 1:47 pm
If anyone is interested, the measurement results look like this
https://yadi.sk/i/C05IOvNy3Qijun

stevestrong
Mon Dec 18, 2017 1:56 pm
[acronis – Mon Dec 18, 2017 1:02 pm] –
It is not a rectangular generator, but I think this is what I need and will work fine from me !

Well, it must be a square-wave signal, because it is a digital output PWM signal from timer 5 compare channel 1.

[acronis – Mon Dec 18, 2017 1:02 pm] –
gpio_set_af_mode(TIMER5_CH1_PIN, 2);

acronis
Mon Dec 18, 2017 2:04 pm
Well, it must be a square-wave signal, because it is a digital output PWM signal from timer 5 compare channel 1.

hmmm . But it’s not .Why …

With the oscilloscope

https://yadi.sk/i/Ly1g8RBe3QiqfN


acronis
Mon Dec 18, 2017 2:10 pm
InitMCO1();

acronis
Sun Feb 18, 2018 10:22 am
Hello steve

I updated the library today to your last on F4.
on the previous version, this code worked fine – but now gives an error

invalid conversion from 'int' to 'gpio_af_mode' [-fpermissive]


stevestrong
Sun Feb 18, 2018 11:07 am
Use GPIO_AFMODE_TIM3_5 instead of value 2
gpio_set_af_mode(TIMER5_CH1_PIN, GPIO_AFMODE_TIM3_5);

acronis
Sun Feb 18, 2018 11:43 am
Steve. These options are compiled normally but the code

#include <Adafruit_TFTLCD_16bit_STM32.h>
Adafruit_TFTLCD_16bit_STM32 tft;

#define timer Timer5
#define TIMER5_CH1_PIN PA0

void setup()
{
tft.reset();
tft.begin(0x9341);
tft.setTextSize(1);
tft.setRotation(1);
tft.setTextColor(BLUE);
tft.fillScreen(BLACK);

pinMode(TIMER5_CH1_PIN, PWM);

// gpio_set_af_mode(TIMER5_CH1_PIN,GPIO_AFMODE_TIM3_5);

gpio_set_af_mode(TIMER5_CH1_PIN, (gpio_af_mode)2);

timer.pause();
timer.setPrescaleFactor(1); // use 42MHz internal clock
timer.setOverflow(2); // output ~21MHz signal (48ns period)
timer.setCompare(TIMER_CH1, 1); // 50% duty cycle: 24ns low + 24ns high
timer.refresh();
timer.resume();

}

void loop() {
tft.println("Test");
}


stevestrong
Sun Feb 18, 2018 12:12 pm
Oops, sorry.
My previous commit erroneously removed the predefined Timer instances.
I re-added them now.
Please check out my repo again.
(In the future please pay attention to the warnings printed by the Arduino IDE in the message window!)

acronis
Sun Feb 18, 2018 12:32 pm
Steve’s great !

Everything works as before !
Thank You!


Leave a Reply

Your email address will not be published. Required fields are marked *