0

I'm trying to enable on an AD9833 (it's a DDS that can generate waves) using a Raspberry Pi 3 B+.

I just want to know how to create a clock or master clock from the GPIO4 (GPCLK) of a Raspberry Pi B+ but I have no idea on how to do it.

  • 2
    Welcome to Raspberry Pi! Please take the tour and visit the helpcenter to see how things work here. Could you please provide additional relevant information, e.g. that clocks desired properties such as frequency, accuracy... – Ghanima Jun 18 at 19:40
  • Possible duplicate of How can i turn an GPIO to its alternate function? – Roger Jones Jun 19 at 9:35
  • I usually do not read the question carefully. I get the main points, and rephrase question. I almost never use GPIOs, to make it easy to transfer circuits to other hardware. In other words, this question has nothing to do with the GPIO alternate function, which I never use (except I2C, SPI, UART pins, which I never use for GPIO purposes). Anyway, see Appendix C and D about the non GPIO 25MHz very very stable quartz clock. I using this high precision clock for my midi DIY controller. Unstable, temperature drifting RPI GPIO based clock would damage my very sensitive ears! :) – tlfong01 Jun 19 at 10:07
  • I do not think a pi clock would be that stable, you are probably better of using a gps PPS. – Dr_Bunsen Jun 19 at 11:02
  • @Franck, you asked how to create a master clock from GPIO4 (GPCLK) . I missed this part because you aim is to use the AD9833 ADS. If you are using a cheapy module from AliEpress, then there is already a 25MHz built in, no need to get the master clock from Pi. And you may need to use a bulky coaxial cable to carry the clock signal to your ADS. But if you still wish to use the GPIO clock, I might need to read the datasheet and see if I am able to do it. – tlfong01 Jun 19 at 12:03
-2

Question

  1. How to enable AD9833?

  2. How to clock it?

Answer

Two good references are:

  1. The AN1070 app notes, where there is an example of frequency calculations. You also need to study very carefully the functions of the pins Master Clock, Frame Sync (See Appendix A below)

  2. Arduino forums. The Arduino guys have more experience on this chip, because Arduino is MCU, not SBC, therefore it is simpler to do real time clocking etc. Of course you might like to start with the much more powerful STM32, especially if you are playing with VHF sig gen things.

Update 2019jun19hkt2122

I used a scope to display the master clock waveform at Ref (pin #1) and found the 25MHz signal OK.

ad9833 master clock

ad9833 schematic

I also study the timing requirements, particular the fsync thing. I found that the minimum requirement is of the order of 5nS, which is nothing if my SPI speed of 400 kHz and output frequency 20 kHz are so low. So I should ignore fsync for my Micky Mouse Midi controller project.

fsync timing

ad9833 control reg

ad9833 data write

/ to continue, ...

References

1. AD9833 Programmable Waveform Generator Data Sheet - Analog Devices

2. AD9833 App Note AN1070

3. AliExpress CMCJU9833 Module - US$3

4. TaoBao CMCJU9833 Module - ¥25

5. Programming DDS AD9833 signal generator (Arduino Forum Discussion, Read 11,933 times)

6. Waveform Generator for Raspberry Pi 3B+ (AD9850, ICL8038)

7. ProtoSupplies AD9833 Function Generator Module - US$15

8. Simple code to drive AD9833 DDS chip - mjoldfield 2018oct26

9. Poor Man’s Signal Generator - Peter ScarGill 2017nov30

10. AD9833 Waveform Generator DIY - 2015sep

Appendices

Appendix A - AD9833 Master Clock Maximum Frequency

ad9833 mclock

Appendix B - Arduino Style STM32 - ST Electronics

stm32 arduino

Appendix C - AD8933 Sig Gen Module Setup Picture

ad8933 sig gen

Appendix D - AD8933 Master Clock Schematic

ad8933 master clock

Appendix E - Python program to drive AD9833 - by mjoldfield 2018oct26

Simple code to drive AD9833 DDS chip - mjoldfield 2018oct26

# 
#   Toy code for simple control of the AD9833 DDS chip
# 
#   Cut down heavily from https://github.com/MajicDesigns/MD_AD9833
#     - The SPI code was lifted almost verbatim then manually
#       converted to python
# 
#   Copyright (C) 2018 M J Oldfield
#   
#   This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU Lesser General Public
#   License as published by the Free Software Foundation; either
#   version 2.1 of the License, or (at your option) any later version.
#   
#   This library is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   Lesser General Public License for more details.
#   
#   You should have received a copy of the GNU Lesser General Public
#   License along with this library; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
# 

import gpiozero

class AD9833:

    def __init__(self, data, clk, fsync):
        self.dataPin  = gpiozero.OutputDevice(pin = data)
        self.clkPin   = gpiozero.OutputDevice(pin = clk)
        self.fsyncPin = gpiozero.OutputDevice(pin = fsync)

        self.fsyncPin.on()
        self.clkPin.on()
        self.dataPin.off()

        self.clk_freq = 25.0e6

    def set_freq(self, f):
        flag_b28  = 1 << 13
        flag_freq = 1 << 14

        scale = 1 << 28
        n_reg = int(f * scale / self.clk_freq)

        n_low = n_reg         & 0x3fff
        n_hi  = (n_reg >> 14) & 0x3fff

        self.send16(flag_b28)
        self.send16(flag_freq | n_low)
        self.send16(flag_freq | n_hi)

    def send16(self, n):
        self.fsyncPin.off()

        mask = 1 << 15
        for i in range(0, 16):

            self.dataPin.value = bool(n & mask)
            self.clkPin.off()
            self.clkPin.on()

            mask = mask >> 1

        self.dataPin.off()
        self.fsyncPin.on()

ad = AD9833(10, 11, 8)
while 1:
    for f in range(10,10000):
        ad.set_freq(f)
  • 1
    Not sure how this answers the OPs question on getting the GPCLK pins to work on the Pi. – Roger Jones Jun 19 at 9:38
  • I agree. As I said, the OP needs to study the datasheet and app notes to clarify things. I confess I did not point out that what the OP asks about GPIO pin to supply clock to the sig gen does not make sense, because the AD8933 needs 25Mhz clock which should not be provided by Rpi. I am now showing the hardware setup and see if I can also show later how to use the 25MHz clock which is buiit in the module. wait and see the updated picture I am going to paste soon. – tlfong01 Jun 19 at 9:50
  • @Roger Jones, The white square in Appendix D is the crystal clock. I forgot to boast that I am a rich guy with over 50 such quartz clocks in my junk box. Each of them is worth about 5 yuan, but they worth more than stupid diamonds and jewels in my hearts, (well sadly not in my VIP's heart) . Anyway, time for me to go out for supper. See you late evening or tomorrow. – tlfong01 Jun 19 at 10:12
  • The GPCLK pins on the Pi can be connected to the BCM2835 clock sources (both PLL and xtal) so can output a variety of frequencies (1GHz down to 19.2MHz) and can also be further divided in software. The OP is asking how to enable this function. – Roger Jones Jun 19 at 10:12
  • I see. I am sorry that I missed this big point. I must apologize to OP as well. I have not studied the inside of the Rpi. I tend to think the 1Gz is made of capacitors and inductors, not high class quartz. Even they are quartz, I need not use them, because my AD8933 already has a quartz, EXACTLY 25MHz, which I don't think Rpi can do. One other thing is that I am connecting long cables from Rpi to the midi controller. Long cables carrying 25MHz is not a good idea. After all I must thank you for telling me the Rpi clock, something that I did not know that I did not know. See you later. – tlfong01 Jun 19 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.