Wave generator AD9833 with the Pyboard

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
Post Reply
User avatar
Kip
Posts: 31
Joined: Sat Dec 26, 2015 7:23 am

Wave generator AD9833 with the Pyboard

Post by Kip » Tue Mar 14, 2017 4:35 am

If anyone is interested, or need to produce waves up to 10 MHz, this might come in handy.

"The AD9833 is a low power, programmable waveform generator, capable of producing sine, triangular, and square wave outputs and has a 0 MHz to 12.5 MHz output frequency range"

The code may be found here:
https://github.com/KipCrossing/Micropython-AD9833

If you want to help improve the code, please contact me.

Kipling

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Wave generator AD9833 with the Pyboard

Post by pythoncoder » Tue Mar 14, 2017 7:54 am

Doubtless I'm being dumb here but I can't see where the AD9833 is getting its clock signal. In line 31 the code acquires the Pyboard's APB1 bus frequency but it's not clear how that electrical signal is connected to the device. Is it possible to get physical access to the APB1 bus clock?

Line 73 looks suspect. Send(0&0xC000) is the same as writing Send(0).
Peter Hinch
Index to my micropython libraries.

User avatar
Kip
Posts: 31
Joined: Sat Dec 26, 2015 7:23 am

Re: Wave generator AD9833 with the Pyboard

Post by Kip » Tue Mar 14, 2017 10:19 am

Good pick up Peter,

I was using the APB1 bus clock as an arbitrary rate. The AD9833 gets its clock reference via a timer into its MCLK pin. Unfautionately for me, the PCB that I bought does not have this MCLK pin and there is little information on what rate to use (but that's my problem). I've updated the code to include the timer; could you please check it for me to make sure I've gotten the number and channel correctly; and any other dumb thing I've done.

The line with Send(0&0xC000) is basically a placeholder for if I want to add control over the phase. I will leave it for now as it's basically harmless.

Let me know if you pick up anything else.

Thanks for your insight.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Wave generator AD9833 with the Pyboard

Post by pythoncoder » Wed Mar 15, 2017 9:40 am

I think you need to read the Pyboard timer docs http://docs.micropython.org/en/latest/p ... Timer.html. You can't get it to run at 160MHz: rates must be 168/N MHz where N is an integer. It might be able to run at 84MHz. I'm unsure what the maximum rate is, nor have I equipment capable of measuring those sort of frequencies. However you're well placed to do so as your AD9833 will divide it down. Start with a low clock rate and increase it. I'd be interested to hear the outcome.

I trust you're aware of the electrical issues with signals of that speed.
Peter Hinch
Index to my micropython libraries.

User avatar
Kip
Posts: 31
Joined: Sat Dec 26, 2015 7:23 am

Re: Wave generator AD9833 with the Pyboard

Post by Kip » Wed Mar 15, 2017 10:06 am

I'm well aware that it can't handle such crazy speeds. Have a look again, I chose 16 MHz. Perhaps you accidentally added a 0 when reading it?

In terms of the frequencies needing to fit 168/N, I don't see that anywhere in the documentation when using the freq argument. Perhaps this is common knowledge that I've missed? I can change it to 14 MHz (168MHz/12) to fit the model but I wanted to make sure that you are sure that this is the case.

Thanks again.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Wave generator AD9833 with the Pyboard

Post by Roberthh » Wed Mar 15, 2017 10:20 am

The little boards I've looked at typically had a built-in 25 MHz crystal. That means, you do not have to create MCLCK in your code. It should be printed as 25000 on the component (typecally a small part with a metal cap, very fine & faint print).

User avatar
Kip
Posts: 31
Joined: Sat Dec 26, 2015 7:23 am

Re: Wave generator AD9833 with the Pyboard

Post by Kip » Wed Mar 15, 2017 10:39 am

Good spot Robert.

I think I have the same board (little blue one without a MCLK pin). I do see 25 written on it. I used a trial and error method to find it out and I got approximately the same (a bit more than 25 MHz).

I decided to include it for those who have boards with the MCLK pin - not that I will use it necessarily.

That really helped, thanks.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Wave generator AD9833 with the Pyboard

Post by pythoncoder » Wed Mar 15, 2017 1:05 pm

Kip wrote:...I chose 16 MHz. Perhaps you accidentally added a 0 when reading it?
Apologies, you got me bang to rights! ;)
Kip wrote:...In terms of the frequencies needing to fit 168/N, I don't see that anywhere in the documentation when using the freq argument...
I suggest you look in the source tree at stmhal/timer.c line 259 compute_prescaler_period_from_freq(). It computes the prescalar and period to give the closest approximation to the required frequency. If you enter with a frequency of 16MHz it calculates the period as int(168/16) = 10 so I think you'll get 16.8MHz.
Peter Hinch
Index to my micropython libraries.

User avatar
Kip
Posts: 31
Joined: Sat Dec 26, 2015 7:23 am

Re: Wave generator AD9833 with the Pyboard

Post by Kip » Wed Mar 15, 2017 1:17 pm

I'll keep it in mind when choosing.

Thanks a lot, Peter, you've been a great help :D

Post Reply