Wiblocks --- NCO

NCO/NCO.h

00001 
00002 
00003 
00004 
00005 #ifndef __NCO_H__
00006 #define __NCO_H__
00007 
00008 #include <inttypes.h>
00009 
00013 
00014 #define NCO_ADDR_LEN 6
00015 #define NCO_NUM_PTS 64
00016 #define NCO_LAST_PT 63
00017 static unsigned char nco_pts[] = {
00018     128, 140, 152, 165, 176, 188, 198, 208,
00019     218, 226, 234, 240, 245, 250, 253, 254,
00020     255, 254, 253, 250, 245, 240, 234, 226,
00021     218, 208, 198, 188, 176, 165, 152, 140,
00022     128, 115, 103,  90,  79,  67,  57,  47,
00023      37,  29,  21,  15,  10,   5,   2,   1,
00024       0,   1,   2,   5,  10,  15,  21,  29,
00025      37,  47,  57,  67,  79,  90, 103, 115};
00026 
00030 
00031 #define NCO_NUM_CHS 4
00032 
00037 #define NCO_ADDR_MASK 0x3F
00038 
00042 
00043 #define NCO_CH0_CONTROL ((0<<1) | (0x01))
00044 #define NCO_CH1_CONTROL ((1<<1) | (0x01))
00045 #define NCO_CH2_CONTROL ((2<<1) | (0x01))
00046 #define NCO_CH3_CONTROL ((3<<1) | (0x01))
00047 
00051 
00052 #define NCO_CLOCK_NONE    ((0<<CS22) | (0<<CS21) | (0<<CS20))
00053 #define NCO_CLOCK_DIV1    ((0<<CS22) | (0<<CS21) | (1<<CS20))
00054 #define NCO_CLOCK_DIV8    ((0<<CS22) | (1<<CS21) | (0<<CS20))
00055 #define NCO_CLOCK_DIV32   ((0<<CS22) | (1<<CS21) | (1<<CS20))
00056 #define NCO_CLOCK_DIV64   ((1<<CS22) | (0<<CS21) | (0<<CS20))
00057 #define NCO_CLOCK_DIV128  ((1<<CS22) | (0<<CS21) | (1<<CS20))
00058 #define NCO_CLOCK_DIV256  ((1<<CS22) | (1<<CS21) | (0<<CS20))
00059 #define NCO_CLOCK_DIV1024 ((1<<CS22) | (1<<CS21) | (1<<CS20))
00060 
00065 
00066 #define NCO_DAC_LOAD  PB2
00067 #define NCO_DAC_LATCH PB1
00068 
00074 
00075 #define NCO_SAMPLING_RATE 3750 // Hz
00076 #define NCO_NUM_UNUSED_BITS 32 - NCO_ADDR_LEN - 1
00077 #define NCO_SCALE 0x04000000
00078 #define NCO_OCR2A 99
00079 
00091 
00092 struct nco_struct {
00093   union {
00094     struct {
00095       unsigned UNUSED1 : 16;
00096       unsigned UNUSED2 : NCO_NUM_UNUSED_BITS - 16;
00097       unsigned round   : 1;
00098       unsigned address : NCO_ADDR_LEN;
00099     } acc_bits;
00100     unsigned long acc_long;
00101   } acc;                 // phase accumulator
00102   unsigned long inc;     // phase increment
00103   unsigned char control; // control byte for the TLV5620
00104 };
00105 extern struct nco_struct nco[NCO_NUM_CHS-1];
00106 
00111 
00112 #define nco_init_ch(i,control) { nco[i].acc.acc_long = 0; nco[i].inc = 0; nco[i].control = control;}
00113 #define nco_inc_phase_acc(i) { nco[i].acc.acc_long += nco[i].inc; }
00114 
00119 
00120 #define nco_addr(i)  (nco[i].acc.acc_bits.address)
00121 #define nco_round(i) (nco[i].acc.acc_bits.round)
00122 #define nco_point(i) (nco_pts[ nco_addr(i) ])
00123 
00127 
00128 void nco_set_freq(unsigned char ch, double freq);
00129 void nco_inc(void);
00130 void nco_init(void);
00131 void nco_update(void);
00132 
00133 #endif // __NCO_H__