![](/media/cache/profiles/7810ccd41bf26faaa2c4e1f20db70a71.50x50_q85.png)
A test program for the TLV320, Generates some sine waves.
Dependencies: I2S TLV320 mbed sinelookup
main.cpp@1:f1ddb053b49a, 2012-09-19 (annotated)
- Committer:
- p07gbar
- Date:
- Wed Sep 19 13:19:09 2012 +0000
- Revision:
- 1:f1ddb053b49a
- Parent:
- 0:63b7102aab3e
Tested
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
p07gbar | 0:63b7102aab3e | 1 | #include "mbed.h" |
p07gbar | 0:63b7102aab3e | 2 | #include "sinelookup.h" |
p07gbar | 0:63b7102aab3e | 3 | #include "TLV320.h" |
p07gbar | 0:63b7102aab3e | 4 | #include "I2S.h" |
p07gbar | 0:63b7102aab3e | 5 | |
p07gbar | 0:63b7102aab3e | 6 | #define SAMPLERATE 48000 |
p07gbar | 0:63b7102aab3e | 7 | |
p07gbar | 0:63b7102aab3e | 8 | |
p07gbar | 0:63b7102aab3e | 9 | TLV320 codec(p9, p10); |
p07gbar | 0:63b7102aab3e | 10 | I2S i2s(I2S_TRANSMIT, p5, p6, p7); |
p07gbar | 0:63b7102aab3e | 11 | |
p07gbar | 0:63b7102aab3e | 12 | AnalogIn aIn(p19); |
p07gbar | 0:63b7102aab3e | 13 | AnalogIn vol(p20); |
p07gbar | 0:63b7102aab3e | 14 | |
p07gbar | 0:63b7102aab3e | 15 | float phase_l; |
p07gbar | 0:63b7102aab3e | 16 | float phase_r; |
p07gbar | 0:63b7102aab3e | 17 | int minibuf[16]; |
p07gbar | 0:63b7102aab3e | 18 | float skipper; |
p07gbar | 0:63b7102aab3e | 19 | float volume; |
p07gbar | 0:63b7102aab3e | 20 | float phase_kill; |
p07gbar | 0:63b7102aab3e | 21 | |
p07gbar | 0:63b7102aab3e | 22 | |
p07gbar | 0:63b7102aab3e | 23 | extern "C" void HardFault_Handler() |
p07gbar | 0:63b7102aab3e | 24 | { |
p07gbar | 0:63b7102aab3e | 25 | error("Hard Fault!\n"); |
p07gbar | 0:63b7102aab3e | 26 | } |
p07gbar | 0:63b7102aab3e | 27 | |
p07gbar | 0:63b7102aab3e | 28 | |
p07gbar | 0:63b7102aab3e | 29 | void play(void) |
p07gbar | 0:63b7102aab3e | 30 | { |
p07gbar | 0:63b7102aab3e | 31 | int to_write = i2s.max_fifo_points() - (i2s.fifo_points()); |
p07gbar | 0:63b7102aab3e | 32 | for(int i = 0; i < to_write; i+=2) { |
p07gbar | 0:63b7102aab3e | 33 | minibuf[i] = int(float(sine16lookup[int(phase_l)])*volume); |
p07gbar | 0:63b7102aab3e | 34 | //minibuf[i] = int(sine16lookup[int(phase_l)]); |
p07gbar | 0:63b7102aab3e | 35 | minibuf[i+1] = int(float(sine16lookup[int(phase_r)])*volume); |
p07gbar | 0:63b7102aab3e | 36 | phase_l+=skipper; |
p07gbar | 0:63b7102aab3e | 37 | phase_r = phase_l + phase_kill; |
p07gbar | 0:63b7102aab3e | 38 | while(phase_l >= SINE16LENGTH) { |
p07gbar | 0:63b7102aab3e | 39 | phase_l -= SINE16LENGTH; |
p07gbar | 0:63b7102aab3e | 40 | } |
p07gbar | 0:63b7102aab3e | 41 | |
p07gbar | 0:63b7102aab3e | 42 | while(phase_r >= SINE16LENGTH) { |
p07gbar | 0:63b7102aab3e | 43 | phase_r -= SINE16LENGTH; |
p07gbar | 0:63b7102aab3e | 44 | } |
p07gbar | 0:63b7102aab3e | 45 | while(phase_r< 0) phase_r += SINE16LENGTH; |
p07gbar | 0:63b7102aab3e | 46 | } |
p07gbar | 0:63b7102aab3e | 47 | i2s.write(minibuf, to_write); |
p07gbar | 0:63b7102aab3e | 48 | } |
p07gbar | 0:63b7102aab3e | 49 | |
p07gbar | 0:63b7102aab3e | 50 | /* main */ |
p07gbar | 0:63b7102aab3e | 51 | int main() |
p07gbar | 0:63b7102aab3e | 52 | { |
p07gbar | 0:63b7102aab3e | 53 | skipper = (aIn*49)+1; |
p07gbar | 0:63b7102aab3e | 54 | |
p07gbar | 0:63b7102aab3e | 55 | codec.power(true); |
p07gbar | 0:63b7102aab3e | 56 | codec.frequency(SAMPLERATE); |
p07gbar | 0:63b7102aab3e | 57 | codec.wordsize(16); |
p07gbar | 0:63b7102aab3e | 58 | codec.master(false); |
p07gbar | 0:63b7102aab3e | 59 | codec.headphone_volume(0.5); |
p07gbar | 0:63b7102aab3e | 60 | codec.start(); |
p07gbar | 0:63b7102aab3e | 61 | |
p07gbar | 0:63b7102aab3e | 62 | i2s.frequency(SAMPLERATE); |
p07gbar | 0:63b7102aab3e | 63 | i2s.wordsize(16); |
p07gbar | 0:63b7102aab3e | 64 | i2s.stereomono(I2S_STEREO); |
p07gbar | 0:63b7102aab3e | 65 | i2s.masterslave(I2S_MASTER); |
p07gbar | 0:63b7102aab3e | 66 | i2s.attach(&play); |
p07gbar | 0:63b7102aab3e | 67 | i2s.start(); |
p07gbar | 0:63b7102aab3e | 68 | |
p07gbar | 0:63b7102aab3e | 69 | while(1) |
p07gbar | 0:63b7102aab3e | 70 | { |
p07gbar | 0:63b7102aab3e | 71 | skipper = (aIn*30)+1; |
p07gbar | 0:63b7102aab3e | 72 | |
p07gbar | 0:63b7102aab3e | 73 | volume = 1; |
p07gbar | 0:63b7102aab3e | 74 | phase_kill = (vol-0.5)*400; |
p07gbar | 0:63b7102aab3e | 75 | |
p07gbar | 0:63b7102aab3e | 76 | //printf("Skipper:%f, %i, %i\n\r",skipper, to_write,i2s.fifo_level()); |
p07gbar | 0:63b7102aab3e | 77 | |
p07gbar | 0:63b7102aab3e | 78 | /*if(flag) |
p07gbar | 0:63b7102aab3e | 79 | { |
p07gbar | 0:63b7102aab3e | 80 | printf("."); |
p07gbar | 0:63b7102aab3e | 81 | flag = false; |
p07gbar | 0:63b7102aab3e | 82 | }*/ |
p07gbar | 0:63b7102aab3e | 83 | } |
p07gbar | 0:63b7102aab3e | 84 | |
p07gbar | 0:63b7102aab3e | 85 | } |