Timeline for Writing Data through Pin at certain frequency
Current License: CC BY-SA 4.0
14 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jun 4, 2019 at 14:25 | vote | accept | FoldFence | ||
Jun 4, 2019 at 12:08 | answer | added | Edgar Bonet | timeline score: 2 | |
Jun 4, 2019 at 7:11 | comment | added | FoldFence | Ah ok I understand, thanks for the answers! @EdgarBonet that would be nice if you would publish it. I don't know if the SPI solution would work with the frdm-klz25 board but I will try to use the timer interrupt solution and if this don't fit I give it a try, thanks! And I don't must do the sigma delta modulation, I have done it on python on the Host-PC and I use the Arduino to send the Data to a KLZ25, basically the Arduino and the Host-PC together simulates a Microphone and on the KLZ25 I will do an MFCC calcuation after I streamed the PDM-Data from the PC to the Arduino. | |
Jun 3, 2019 at 18:55 | comment | added | Edgar Bonet | 16 CPU cycles is a very short time. You won't be able to do much other than the delta-sigma algorithm for generating the PDM output. Some time ago I wrote a program that does almost what you are asking for. It reads data in a "shift register" fashion and outputs it as PDM at 615 kHz with a CPU running at 8 MHz. That's 13 CPU cycles per PDM cycle. It's intended to run on an ATtiny13A and should be easy to adapt to an Uno. It's based on a carefully timed assembly loop, with no interrupts. Tell me if you are interested in me publishing it. | |
Jun 3, 2019 at 15:51 | comment | added | Sim Son |
You might also be able to emulate the desired behaviour with one of the atmegas interfaces (e.g. SPI master). The essence of what Gerben mentioned is that you should use the available hardware because as you already found out handling this stuff in loop() won't work at 1 MHz.
|
|
Jun 3, 2019 at 15:27 | comment | added | Gerben | The timer is for example used to PWM an LED. This timer runs parallel to your code. The timer has a counter that it increments every clock cycle. You can select a TOP value, so that, when it reaches this value, it reset the counter to zero. It also has a compare registers. You can instruct the timer that, once the counter reaches the value in the compare register it should set one of the output pins LOW. You can also instruct it to set the output pin HIGH when it's set back to zero. So by combining this the output will be HIGH for 1µs, followed by a number of µs LOW, depending on value of TOP. | |
Jun 3, 2019 at 15:14 | comment | added | Gerben |
DigitalWrite itself is super slow. It takes a lot of cycles. loop itself also takes a few cycle to get called. You could use direct port manipulation instead, which is a lot faster. But to run at 1Mhz, you only have 16 cycles to toggle the pin and run the loop, leaving you with nearly no time left to do anything else. Also other interrupts could occur and slow your loop at "random" intervals, giving you a unstable 1Mhz signal. That's why I'm suggesting using timers, as they run in parallel.
|
|
Jun 3, 2019 at 15:02 | comment | added | FoldFence | PDM means pulse density modulation, basically I write 1 or 0 with 1Mhz frequency. @Gerben What do you mean by TOP value ? Does the change of the timer changes also the loop speed because I don't get how to write in 1Mhz if the loop is slower. | |
Jun 3, 2019 at 15:00 | history | edited | FoldFence | CC BY-SA 4.0 |
added 314 characters in body
|
Jun 3, 2019 at 14:35 | review | Close votes | |||
Jun 13, 2019 at 14:22 | |||||
Jun 3, 2019 at 14:21 | comment | added | Gerben | Use one of the timers on the UNO. Those are the ones that are used for PWM. Set the prescaler to /8. Set the mode to fast-pwm. Set the output pin to no-inverting mode. Set OCxA/OCxB to 2 (since the timer runs at 2Mhz). Then change the TOP value based on the PDM value you want to send. | |
Jun 3, 2019 at 14:20 | comment | added | MichaelT | What does the acronym PDM mean? It would help to state it in your question. | |
Jun 3, 2019 at 14:10 | review | First posts | |||
Jun 3, 2019 at 14:20 | |||||
Jun 3, 2019 at 14:07 | history | asked | FoldFence | CC BY-SA 4.0 |