├── LICENSE ├── README.md ├── ad9833.py └── test.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Kipling Crossing 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Micropython-AD9833 2 | 3 | This script is written in python 3.x for interfacing the AD9833 with micropython microcontrollers over SPI. 4 | 5 | ## Usage 6 | 7 | Import 8 | 9 | ```python 10 | from ad9833 import AD9833 11 | from pyb import Pin 12 | from pyb import SPI 13 | ``` 14 | 15 | Choose SS pin 16 | 17 | ```python 18 | ss = Pin('X5', Pin.OUT_PP) 19 | ``` 20 | 21 | Choose SPI 22 | 23 | ```python 24 | spi = SPI(1, SPI.MASTER, baudrate=9600, polarity=1, phase=0,firstbit=SPI.MSB) 25 | ``` 26 | 27 | Takes 2 arguments: sbi and ss 28 | 29 | ```python 30 | wave = AD9833(spi,ss) 31 | ``` 32 | 33 | Set the frequency 34 | 35 | ```python 36 | wave.set_freq(14500) 37 | ``` 38 | 39 | Set the wave type: 0 for sin 1 for square 2 for triangle 40 | 41 | ```python 42 | wave.set_type(2) 43 | ``` 44 | 45 | Finally, send command to the AD9833 46 | 47 | ```python 48 | wave.send() 49 | ``` 50 | 51 | You can also get some useful information 52 | 53 | ```python 54 | print(wave.shape_type) 55 | print(wave.freq) 56 | ``` 57 | -------------------------------------------------------------------------------- /ad9833.py: -------------------------------------------------------------------------------- 1 | # By Kipling Crossing 2 | class AD9833(object): 3 | 4 | # Clock Frequency 5 | ClockFreq = 25000000 6 | freq = 10000 7 | shape_word = 0x2000 8 | 9 | def __init__(self, spi, ss): 10 | self.spi = spi 11 | self.ss = ss 12 | 13 | # function for splitting hex into high and low bits 14 | def _bytes(self, integer): 15 | return divmod(integer, 0x100) 16 | 17 | def _send(self, data): 18 | high, low = self._bytes(data) 19 | self.ss.low() 20 | 21 | self.spi.send(high) 22 | self.spi.send(low) 23 | 24 | self.ss.high() 25 | 26 | def set_freq(self, freq): 27 | self.freq = freq 28 | 29 | def set_type(self, inter): 30 | if inter == 1: 31 | self.shape_word = 0x2020 32 | elif inter == 2: 33 | self.shape_word = 0x2002 34 | else: 35 | self.shape_word = 0x2000 36 | 37 | @property 38 | def shape_type(self): 39 | if self.shape_word == 0x2020: 40 | return "Square" 41 | elif self.shape_word == 0x2002: 42 | return "Triangle" 43 | else: 44 | return "Sine" 45 | 46 | def send(self): 47 | # Calculate frequency word to send 48 | word = hex(int(round((self.freq*2**28)/self.ClockFreq))) 49 | 50 | # Split frequency word onto its seperate bytes 51 | MSB = (int(word, 16) & 0xFFFC000) >> 14 52 | LSB = int(word, 16) & 0x3FFF 53 | 54 | # Set control bits DB15 = 0 and DB14 = 1; for frequency register 0 55 | MSB |= 0x4000 56 | LSB |= 0x4000 57 | 58 | self._send(0x2100) 59 | # Set the frequency 60 | self._send(LSB) # lower 14 bits 61 | self._send(MSB) # Upper 14 bits 62 | # Set the shape 63 | # square: 0x2020, sin: 0x2000, triangle: 0x2002 64 | self._send(self.shape_word) 65 | -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | from ad9833 import AD9833 2 | 3 | # DUMMY classes for testing without board 4 | 5 | 6 | class SBI(object): 7 | def __init__(self): 8 | pass 9 | 10 | def send(self, data): 11 | print(data) 12 | 13 | 14 | class Pin(object): 15 | def __init__(self): 16 | pass 17 | 18 | def low(self): 19 | print(" 0") 20 | 21 | def high(self): 22 | print(" 1") 23 | 24 | 25 | # Code 26 | 27 | SBI1 = SBI() 28 | PIN3 = Pin() 29 | 30 | wave = AD9833(SBI1, PIN3) 31 | 32 | wave.set_freq(14500) 33 | wave.set_type(2) 34 | wave.send() 35 | print(wave.shape_type) 36 | --------------------------------------------------------------------------------