![](/media/cache/profiles/P1050323.jpg.50x50_q85.jpg)
MCP3208 and Ticker class test
main.cpp@0:657424dc6103, 2017-06-09 (annotated)
- Committer:
- ryood
- Date:
- Fri Jun 09 01:14:11 2017 +0000
- Revision:
- 0:657424dc6103
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ryood | 0:657424dc6103 | 1 | #include "mbed.h" |
ryood | 0:657424dc6103 | 2 | #include "mcp3208.h" |
ryood | 0:657424dc6103 | 3 | |
ryood | 0:657424dc6103 | 4 | #define SAMPLING_RATE (96000) |
ryood | 0:657424dc6103 | 5 | #define SAMPLING_PERIOD (1.0f/SAMPLING_RATE) |
ryood | 0:657424dc6103 | 6 | |
ryood | 0:657424dc6103 | 7 | DigitalOut checkPin(D2); |
ryood | 0:657424dc6103 | 8 | |
ryood | 0:657424dc6103 | 9 | void isr() |
ryood | 0:657424dc6103 | 10 | { |
ryood | 0:657424dc6103 | 11 | checkPin = 1; |
ryood | 0:657424dc6103 | 12 | wait_us(1); |
ryood | 0:657424dc6103 | 13 | checkPin = 0; |
ryood | 0:657424dc6103 | 14 | } |
ryood | 0:657424dc6103 | 15 | |
ryood | 0:657424dc6103 | 16 | int main() |
ryood | 0:657424dc6103 | 17 | { |
ryood | 0:657424dc6103 | 18 | SPI spiM(SPI_MOSI, SPI_MISO, SPI_SCK); |
ryood | 0:657424dc6103 | 19 | spiM.frequency(4000000); |
ryood | 0:657424dc6103 | 20 | MCP3208 mcp3208_0(spiM, D10); |
ryood | 0:657424dc6103 | 21 | MCP3208 mcp3208_1(spiM, D9); |
ryood | 0:657424dc6103 | 22 | |
ryood | 0:657424dc6103 | 23 | float v0[8]; |
ryood | 0:657424dc6103 | 24 | float v1[8]; |
ryood | 0:657424dc6103 | 25 | |
ryood | 0:657424dc6103 | 26 | Ticker t; |
ryood | 0:657424dc6103 | 27 | t.attach(&isr, SAMPLING_PERIOD); |
ryood | 0:657424dc6103 | 28 | |
ryood | 0:657424dc6103 | 29 | for (;;) { |
ryood | 0:657424dc6103 | 30 | for (int i = 0; i < 8; i++) { |
ryood | 0:657424dc6103 | 31 | v0[i] = mcp3208_0.read_input(i); |
ryood | 0:657424dc6103 | 32 | } |
ryood | 0:657424dc6103 | 33 | for (int i = 0; i < 8; i++) { |
ryood | 0:657424dc6103 | 34 | v1[i] = mcp3208_1.read_input(i); |
ryood | 0:657424dc6103 | 35 | } |
ryood | 0:657424dc6103 | 36 | |
ryood | 0:657424dc6103 | 37 | printf("Device0\t"); |
ryood | 0:657424dc6103 | 38 | for (int i = 0; i < 8; i++) { |
ryood | 0:657424dc6103 | 39 | printf("%.3f\t", v0[i]); |
ryood | 0:657424dc6103 | 40 | } |
ryood | 0:657424dc6103 | 41 | printf("\r\n"); |
ryood | 0:657424dc6103 | 42 | printf("Device1\t"); |
ryood | 0:657424dc6103 | 43 | for (int i = 0; i < 8; i++) { |
ryood | 0:657424dc6103 | 44 | printf("%.3f\t", v1[i]); |
ryood | 0:657424dc6103 | 45 | } |
ryood | 0:657424dc6103 | 46 | printf("\r\n"); |
ryood | 0:657424dc6103 | 47 | |
ryood | 0:657424dc6103 | 48 | wait(0.2); |
ryood | 0:657424dc6103 | 49 | } |
ryood | 0:657424dc6103 | 50 | } |