-
Notifications
You must be signed in to change notification settings - Fork 2
/
max261.c
76 lines (60 loc) · 1.82 KB
/
max261.c
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
65
66
67
68
69
70
71
72
73
74
75
76
// max261 command & control
#include <avr/io.h>
#include <util/delay_basic.h>
uint8_t filtermode[2] ;
// MAX261 data bits 0/1 are on PD01
// MAX261 address bits 0123 are on PD2345
// WR signal is on PD7.
// The filters should operate in
// mode 3 (LP,HP,BP) (2 binary)
// initialize hardware
// just make sure PD7 is 1.
void init_max261() {
DDRD=0xff ; // all PD pins are outputs
PORTD |= (1<<PD7) ;
}
// do a logic-low pulse on the WR signal of the filter chip
// this is located on PD7.
inline void wr_pulse() {
PORTD &= 0b01111111 ;
_delay_loop_1(6) ;
PORTD |= 0b10000000 ;
// _delay_loop_1(6) ;
}
// set filter mode/configuration
void set_mode(uint8_t mode, uint8_t filter) {
filtermode[filter]=mode;
}
// write filter frequency (f) to filter bank A/B (ab)
void max261_write_f(uint8_t f, uint8_t ab) {
uint8_t addr=0; // register location 0-3
uint8_t pd=0; // ioport data word
f = f << 2 ;
f |= filtermode[ab];
for (addr = 0 ; addr < 4 ; addr++ ) { // addresses 0-3 are Q data values
pd=addr << 2 ; // shift address up two bits
pd |= (f & 0b00000011) ; // add the two data bits
pd |= (ab << 5) ; // add filter select bit to A3
PORTD &= 0b11000000 ;
PORTD |= pd ;
wr_pulse();
f = f >> 2 ; // shift down data for next cycle
}
}
// write filter Q value (q) to filter A/B (ab)
void max261_write_q(uint8_t q, uint8_t ab) {
uint8_t addr=0; // register location 0-3
uint8_t pd=0; // ioport data word
if ( q == 0 ) {
q = 1; // prevent total filter shutdown if Q=0 in filter A.
}
for (addr = 4 ; addr < 8 ; addr++ ) { // addresses 4-7 are Q data values
pd=addr << 2 ; // shift address up two bits
pd |= (q & 0b00000011) ; // add the two data bits
pd |= (ab << 5) ; // add filter select bit to A3
PORTD &= 0b11000000 ;
PORTD |= pd ;
wr_pulse();
q = q >> 2 ; // shift down data for next cycle
}
}