-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathad9833.py
64 lines (51 loc) · 1.59 KB
/
ad9833.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# By Kipling Crossing
class AD9833(object):
# Clock Frequency
ClockFreq = 25000000
freq = 10000
shape_word = 0x2000
def __init__(self, spi, ss):
self.spi = spi
self.ss = ss
# function for splitting hex into high and low bits
def _bytes(self, integer):
return divmod(integer, 0x100)
def _send(self, data):
high, low = self._bytes(data)
self.ss.low()
self.spi.send(high)
self.spi.send(low)
self.ss.high()
def set_freq(self, freq):
self.freq = freq
def set_type(self, inter):
if inter == 1:
self.shape_word = 0x2020
elif inter == 2:
self.shape_word = 0x2002
else:
self.shape_word = 0x2000
@property
def shape_type(self):
if self.shape_word == 0x2020:
return "Square"
elif self.shape_word == 0x2002:
return "Triangle"
else:
return "Sine"
def send(self):
# Calculate frequency word to send
word = hex(int(round((self.freq*2**28)/self.ClockFreq)))
# Split frequency word onto its seperate bytes
MSB = (int(word, 16) & 0xFFFC000) >> 14
LSB = int(word, 16) & 0x3FFF
# Set control bits DB15 = 0 and DB14 = 1; for frequency register 0
MSB |= 0x4000
LSB |= 0x4000
self._send(0x2100)
# Set the frequency
self._send(LSB) # lower 14 bits
self._send(MSB) # Upper 14 bits
# Set the shape
# square: 0x2020, sin: 0x2000, triangle: 0x2002
self._send(self.shape_word)