Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 12:52 28 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : PicoMite sine waves?

     Page 1 of 5    
Author Message
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 07:03am 19 Jun 2023
Copy link to clipboard 
Print this post

For sine wave generation is there an advantage to using PLAY TONE over PLAY SOUND, given that I need to use PLAY SOUND to get the other waveforms anyway?

Obviously, if I use PLAY TONE then both  channels will produce sine waves. That's fine, I can use that. If I use PLAY SOUND I can have a different waveform and output level on each channel, but if I can get better sine waves from PLAY TONE then I'll use that as an option.

In this application outputs will always be switched on or off, with nothing appreciable (DIN input scanning) happening while the tone(s) are being generated.

@ matherp
Is the 20kHz limit something that's set in stone or could it be extended a bit given that just about any PicoMite will overclock to 200MHz or higher? Just wondering about using it to test filters, although I'm not expecting to get anywhere close to 50kHz. 25kHz or 30kHz would be very nice though.

I might try a better system later (DMA - PIO - 10-bit or 12-bit R2R, which will work to much higher frequencies) but I haven't got the PCB space for this project. I'm re-using the little case from a bricked PSU module.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4247
Posted: 07:21am 19 Jun 2023
Copy link to clipboard 
Print this post

With PWM you can redesign the analog filter into a more complex one to give you real 0Hz..20kHz. With 44kHz PWM you cannot reliably get anything above 20kHz.

With PWM you (Peter) may be able to run the PWM at 100kHz, but then you sacrifice resolution. Going back to 8 bit or so.

With DAC you may, but it will cost you CPU power ...

Volhout
Edited 2023-06-19 17:22 by Volhout
PicomiteVGA PETSCII ROBOTS
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 07:31am 19 Jun 2023
Copy link to clipboard 
Print this post

I'm using the MCP4822 DAC for this, so I have 12-bit output. The CPU load isn't going to be the limit, I don't think, but the rise/fall times of the DAC and any output buffer I come up with certainly will! :) There's not much space so it's likely to be a unity gain lm358. This isn't pretending to be lab quality equipment but I may as well get out of it what I can.

It's all a bit wierd, of course, be ing me.
I only have physical space for a ST7789 240x240 display, a rotary encoder and a button. That's a hard limit. They have to line up with positions that are more or less in line with the original PSU controls so I can use the original front - even though I'm using pad board and not a PCB. That gives me one other board the same size to fit a RP2040-Zero, the DAC, simple RC filter, buffer and terminals. I'm a glutton for punishment. :)
Edited 2023-06-19 17:36 by Mixtel90
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4247
Posted: 07:38am 19 Jun 2023
Copy link to clipboard 
Print this post

  Mixtel90 said  I'm using the MCP4822 DAC for this, so I have 12-bit output. The CPU load isn't going to be the limit, I don't think, but the rise/fall times of the DAC and any output buffer I come up with certainly will! :) There's not much space so it's likely to be a unity gain lm358. This isn't pretending to be lab quality equipment but I may as well get out of it what I can.


If this is going to be a test tool, you may want to use a different opamp. The LM358 features cross-over distortion (when the output goes from push to pull current end reverse).

Volhout
PicomiteVGA PETSCII ROBOTS
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 09:45am 19 Jun 2023
Copy link to clipboard 
Print this post

I only mentioned the LM358 because I know I have a few. :) It will probably have to be a similar packaged dual though. And it'll have to be something I can find in my bits box. lol I have a NE5532, I think. May have to run it at 5V though.

EDIT:
Interesting. I didn't know that LM358 distortion depends on output load.. It strikes me that 3K9 or 3K3 on the output may not be too bad if I can't find a better op amp in my stash.
Edited 2023-06-19 20:02 by Mixtel90
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
barewires
Newbie

Joined: 13/04/2015
Location: United Kingdom
Posts: 30
Posted: 02:30pm 19 Jun 2023
Copy link to clipboard 
Print this post

In the 90s I used to pass my electronic magazines to my father and he always asked me if I understood what guru Don Lancaster was writing. I was into PIC microcontrollers and learned a lot from the magazines. One thing that intrigued me then was the Magic Sinewaves articles. Time to go retro and look at the website (with its 'fruit salad' colours.)
https://www.tinaja.com/magsn01.shtml
https://www.tinaja.com/msquant/retry_m/xxxx.shtml
https://www.tinaja.com/mssamp1.shtml
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 03:24pm 19 Jun 2023
Copy link to clipboard 
Print this post

Interesting (and very clever) stuff once you get past the colour scheme! :) I'm not going to even pretend that I understand the maths behind it. lol

Not really in keeping with this particular project though as it looks like I'd need silly clock rates to get 20kHz, but great for sub-audio and low audio frequencies.

--------------------------------

More research...
I'm not sure that the MCP4822 can get above 19.5kHz anyway, if both channels are being updated. Even if it's possible for the PicoMite to push the data out on SPI it seems to be a limitation of the DAC itself. Updating a single channel would, in theory anyway, allow up to 39kHz.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4247
Posted: 10:50am 20 Jun 2023
Copy link to clipboard 
Print this post

Mick,

Magic sinewaves are only really interesting in power electronics to limit switching losses. You limit the number of transitions (switching losses) to a minimum. At the cost of filtering.

Just look at Picomite PWM audio: you switch at 44kHz and have 11 bit time resolution. The 11 bit is similar to the 65 dB THD for the designed harmonics (number "n") they advocate for the magic sinewaves.

In magic sinewaves 60Hz for n=10 the harmonics at 660Hz is strong.
Where in PWM all harmonics are down up to 12kHz (with the low pass filter).

The big difference is in switching losses in the power stage.

With n=10 the magic sinewave has 20 transitions per quadrant = 80 transitions per cycle = (at 60Hz) 4800 transitions per second.
With n=10 you need a low pass filter with rejection at 660Hz (1 decade above fundamental (60Hz)). A simple LC filter may not suffice.

With 44kHz PWM there are 44100*2 = 88200 transitions per second, almost 20x more. So 20x more losses.
But your filter for 60Hz would need to suppress 44kHz, and that is almost 3 decades higher. Much simpler filter.

I hope this confuses the hell out of you... ;) ;)

Volhout
Edited 2023-06-20 20:51 by Volhout
PicomiteVGA PETSCII ROBOTS
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 11:18am 20 Jun 2023
Copy link to clipboard 
Print this post

Given that my natural state is "confused" I didn't notice any difference whatsoever. :)

Thanks. :)
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9129
Posted: 11:41am 20 Jun 2023
Copy link to clipboard 
Print this post

  Quote  I'm not sure that the MCP4822 can get above 19.5kHz anyway,


Well 200KHz actually
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 02:58pm 20 Jun 2023
Copy link to clipboard 
Print this post

That's nice. :)

Any real advantage to using PLAY TONE over PLAY SOUND to get sine waves?
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9129
Posted: 03:16pm 20 Jun 2023
Copy link to clipboard 
Print this post

  Quote  Any real advantage to using PLAY TONE over PLAY SOUND to get sine waves?


More volume
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 03:32pm 20 Jun 2023
Copy link to clipboard 
Print this post

Thanks. :)
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 06:45pm 20 Jun 2023
Copy link to clipboard 
Print this post

but the voltage will not go over 3.3V after the filter? connecting filter out to a-d pin.. blow it?
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 07:09pm 20 Jun 2023
Copy link to clipboard 
Print this post

It's not quite that clear. I'm not using PWM in this case. The DAC has a maximum peak output of 2.048 volts. That's set by an internal voltage reference. The audio is produced by a series of numbers fed to it rapidly over an SPI link. these are converted into an analogue signal in hardware.

For your oscilloscope you want to go the other way, converting an analogue waveform into digital. Luckily you have some A-D converters in the RP2040. You could use an external A-D converter for better quality but you'd better be prepared to write some C to handle it.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
hitsware2

Guru

Joined: 03/08/2019
Location: United States
Posts: 713
Posted: 09:19pm 20 Jun 2023
Copy link to clipboard 
Print this post

Out of curiosity ...
Could one use the ' PORT '
command with a R - 2R network
on 8 outputs fed the numbers ?
my site
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 09:58pm 20 Jun 2023
Copy link to clipboard 
Print this post

Yes, it'll work fine. The max frequency is a bit limited from BASIC though.

There is a project on the web where he sets up an array (using Python IIRC, but it doesn't matter). A PIO state machine pulls data out of its incoming pipe and shoves it out onto pins. When the pipe is empty a DMA is triggered and more data is shoved into the pipe. Actually, two DMA channels flip-flop continuously so the data rate stays constant. I might have a go. I have some 1K8 0.1% 0815 resistors to make the R2R ladder (10-bit, I think).
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
hitsware2

Guru

Joined: 03/08/2019
Location: United States
Posts: 713
Posted: 11:08pm 20 Jun 2023
Copy link to clipboard 
Print this post

  Mixtel90 said  
The max frequency is a bit limited from BASIC though.

More so than SPI ?
my site
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4247
Posted: 06:18am 21 Jun 2023
Copy link to clipboard 
Print this post

Mmbasic is fully equiped to do this. Use a ring buffer and dma to pio.
PicomiteVGA PETSCII ROBOTS
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 06:59am 21 Jun 2023
Copy link to clipboard 
Print this post

I suspect it would be quite a bit slower but it would be interesting to try it.

Just my theories - nothing proven and nothing guaranteed to be factual:

It's not just the speed. Suppose you are working from a table stored in an array. The array would hold one full cycle of the output wave. You output from a(0) to a(n) but now you have to reset the pointer to zero as the array isn't circular. As the frequency increases you start to see that reset time as a discontinuity in the waveform. You can reduce this problem by triggering each port write using an interrupt, but the usual system used by MMBasic won't do, it has to be a hardware interrupt as the timing is critical. It may also be possible to reduce it by using two array pointers so that a circular array is simulated.

Additionally, this system may be workable but you will almost certainly want to do other things in the program, not just generate the waveform. The interrupt system would let you do that but you wouldn't have a lot of time unless your processor is very fast.

To get a fairly rough staircase you probably want to do about 32 writes per cycle. that's 8 steps per quarter waveform. At 1kHz you will need to write 32,000 times per second. I don't think this will work at more than a couple of kHz unless you reduce the resolution a lot.

End of theories.

The project I described above gets round all of the above. Timing is independent of everything except the system clock as the DMA channels and PIO are only clocked from that and aren't connected to the CPU cores. While a waveform is being generated the CPUs can do other things. The user program is only used to stuff the waveform array and start the system running. The array can be of any size, I suspect, so complex waveforms can be generated. Maximum output frequency can go up to over 10MHz and is crystal locked. Using 2 DMA channels allows for them to be reset alternately so there is no waveform discontinuity.

I might try this or something similar.
Edited 2023-06-21 17:01 by Mixtel90
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
     Page 1 of 5    
Print this page
© JAQ Software 2024