See commit words
Dependencies: SDFileSystem_high_spi_freq mbed wave_player_pwm_and_dac
main.cpp@0:205c27289cf4, 2014-12-05 (annotated)
- Committer:
- lenhardt
- Date:
- Fri Dec 05 02:34:45 2014 +0000
- Revision:
- 0:205c27289cf4
This is Wheel Beats working with ADC, playing through PWMout for a ; headphone/speaker audio jack when you hit the force sensitive resistor squares. Oh, and it reads it off an SD card at an spi frequency of 25MHz.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lenhardt | 0:205c27289cf4 | 1 | #include "mbed.h" |
lenhardt | 0:205c27289cf4 | 2 | #include "SDFileSystem.h" |
lenhardt | 0:205c27289cf4 | 3 | #include "wave_player.h" |
lenhardt | 0:205c27289cf4 | 4 | |
lenhardt | 0:205c27289cf4 | 5 | SPI spi(p11, p12, p13); // mosi(out), miso(in), sclk(clock) |
lenhardt | 0:205c27289cf4 | 6 | DigitalOut cs(p14); // cs (the chip select signal) |
lenhardt | 0:205c27289cf4 | 7 | Serial pc(USBTX, USBRX); // tx, rx ( the usb serial communication ) |
lenhardt | 0:205c27289cf4 | 8 | |
lenhardt | 0:205c27289cf4 | 9 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
lenhardt | 0:205c27289cf4 | 10 | PwmOut PWMout(p21); |
lenhardt | 0:205c27289cf4 | 11 | AnalogOut DACout(p18); |
lenhardt | 0:205c27289cf4 | 12 | AnalogIn fsr(p15); |
lenhardt | 0:205c27289cf4 | 13 | wave_player waver(&DACout,&PWMout); |
lenhardt | 0:205c27289cf4 | 14 | FILE *wave_file; |
lenhardt | 0:205c27289cf4 | 15 | |
lenhardt | 0:205c27289cf4 | 16 | int main() { |
lenhardt | 0:205c27289cf4 | 17 | // Setup the spi for 7 bit data, high steady state clock, |
lenhardt | 0:205c27289cf4 | 18 | // second edge capture, with a 1MHz clock rate |
lenhardt | 0:205c27289cf4 | 19 | spi.format(7,0); |
lenhardt | 0:205c27289cf4 | 20 | spi.frequency(1000000); |
lenhardt | 0:205c27289cf4 | 21 | |
lenhardt | 0:205c27289cf4 | 22 | // Set PWMout period |
lenhardt | 0:205c27289cf4 | 23 | PWMout.period(1.0/400000.0); |
lenhardt | 0:205c27289cf4 | 24 | |
lenhardt | 0:205c27289cf4 | 25 | // notify the user that we are starting with the ADC communication |
lenhardt | 0:205c27289cf4 | 26 | pc.printf("Starting ADC interaction\n\n\n"); |
lenhardt | 0:205c27289cf4 | 27 | |
lenhardt | 0:205c27289cf4 | 28 | // lets just do this forever |
lenhardt | 0:205c27289cf4 | 29 | while (1) { |
lenhardt | 0:205c27289cf4 | 30 | |
lenhardt | 0:205c27289cf4 | 31 | for (int i = 0 ; i < 2; i++){ |
lenhardt | 0:205c27289cf4 | 32 | // Select the device by seting chip select low |
lenhardt | 0:205c27289cf4 | 33 | cs = 0; |
lenhardt | 0:205c27289cf4 | 34 | |
lenhardt | 0:205c27289cf4 | 35 | // sending the 6 bits + 1 bit to ignore the null bit |
lenhardt | 0:205c27289cf4 | 36 | // coming from the device, so the data that is sent is 1100000 |
lenhardt | 0:205c27289cf4 | 37 | spi.write(0x60 + (i*4)); |
lenhardt | 0:205c27289cf4 | 38 | |
lenhardt | 0:205c27289cf4 | 39 | // now the device sends back the readings 12 bits, 7 bits at a time |
lenhardt | 0:205c27289cf4 | 40 | uint8_t high = spi.write(0x00); |
lenhardt | 0:205c27289cf4 | 41 | uint8_t low = spi.write(0x00); |
lenhardt | 0:205c27289cf4 | 42 | |
lenhardt | 0:205c27289cf4 | 43 | // shift out the right bits |
lenhardt | 0:205c27289cf4 | 44 | low = ( high << 5 ) | (low >> 2); |
lenhardt | 0:205c27289cf4 | 45 | high = high >> 3; |
lenhardt | 0:205c27289cf4 | 46 | |
lenhardt | 0:205c27289cf4 | 47 | // shift and or the result together |
lenhardt | 0:205c27289cf4 | 48 | int value = ( high << 8 ) | low; |
lenhardt | 0:205c27289cf4 | 49 | |
lenhardt | 0:205c27289cf4 | 50 | // and voila we have the value and we can print it for the user |
lenhardt | 0:205c27289cf4 | 51 | //pc.printf("sensor %d value = %u\n", i, value); |
lenhardt | 0:205c27289cf4 | 52 | switch (i){ |
lenhardt | 0:205c27289cf4 | 53 | case 0: |
lenhardt | 0:205c27289cf4 | 54 | if (value > 200) { |
lenhardt | 0:205c27289cf4 | 55 | pc.printf("\tX\r"); |
lenhardt | 0:205c27289cf4 | 56 | wave_file=fopen("/sd/highhat.wav","r"); |
lenhardt | 0:205c27289cf4 | 57 | waver.play(wave_file); |
lenhardt | 0:205c27289cf4 | 58 | fclose(wave_file); |
lenhardt | 0:205c27289cf4 | 59 | } |
lenhardt | 0:205c27289cf4 | 60 | break; |
lenhardt | 0:205c27289cf4 | 61 | case 1: |
lenhardt | 0:205c27289cf4 | 62 | if (value > 200){ |
lenhardt | 0:205c27289cf4 | 63 | pc.printf("\t\t\t\tX\r"); |
lenhardt | 0:205c27289cf4 | 64 | wave_file=fopen("/sd/snare.wav","r"); |
lenhardt | 0:205c27289cf4 | 65 | waver.play(wave_file); |
lenhardt | 0:205c27289cf4 | 66 | fclose(wave_file); |
lenhardt | 0:205c27289cf4 | 67 | } |
lenhardt | 0:205c27289cf4 | 68 | break; |
lenhardt | 0:205c27289cf4 | 69 | } |
lenhardt | 0:205c27289cf4 | 70 | |
lenhardt | 0:205c27289cf4 | 71 | // Deselect the device |
lenhardt | 0:205c27289cf4 | 72 | cs = 1; |
lenhardt | 0:205c27289cf4 | 73 | } |
lenhardt | 0:205c27289cf4 | 74 | |
lenhardt | 0:205c27289cf4 | 75 | // delay some time before reading again |
lenhardt | 0:205c27289cf4 | 76 | wait_us(100); |
lenhardt | 0:205c27289cf4 | 77 | } |
lenhardt | 0:205c27289cf4 | 78 | } |