This is test program of mcp3204. This is operating normally.
Dependencies: mbed MCP320x TextLCD
Nucleo_mcp3204test.cpp@0:55aac36c4f0f, 2016-01-28 (annotated)
- Committer:
- SK_ROBO_
- Date:
- Thu Jan 28 05:34:51 2016 +0000
- Revision:
- 0:55aac36c4f0f
- Child:
- 1:50f0c5749724
corrected the mistake of bit shift.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SK_ROBO_ | 0:55aac36c4f0f | 1 | #include "mbed.h" |
SK_ROBO_ | 0:55aac36c4f0f | 2 | |
SK_ROBO_ | 0:55aac36c4f0f | 3 | SPI spi(D11, D12, D13); // mosi(out), miso(in), sclk(clock) |
SK_ROBO_ | 0:55aac36c4f0f | 4 | DigitalOut cs(D10); // cs (the chip select signal) |
SK_ROBO_ | 0:55aac36c4f0f | 5 | |
SK_ROBO_ | 0:55aac36c4f0f | 6 | Serial pc(USBTX, USBRX); // tx, rx ( the usb serial communication ) |
SK_ROBO_ | 0:55aac36c4f0f | 7 | |
SK_ROBO_ | 0:55aac36c4f0f | 8 | int main() { |
SK_ROBO_ | 0:55aac36c4f0f | 9 | // Setup the spi for 7 bit data, high steady state clock, |
SK_ROBO_ | 0:55aac36c4f0f | 10 | // second edge capture, with a 1MHz clock rate |
SK_ROBO_ | 0:55aac36c4f0f | 11 | spi.format(7,0); |
SK_ROBO_ | 0:55aac36c4f0f | 12 | spi.frequency(1000000); |
SK_ROBO_ | 0:55aac36c4f0f | 13 | |
SK_ROBO_ | 0:55aac36c4f0f | 14 | // notify the user that we are starting with the ADC communication |
SK_ROBO_ | 0:55aac36c4f0f | 15 | pc.printf("Starting ADC interaction\n"); |
SK_ROBO_ | 0:55aac36c4f0f | 16 | |
SK_ROBO_ | 0:55aac36c4f0f | 17 | // lets just do this forever |
SK_ROBO_ | 0:55aac36c4f0f | 18 | while (1) { |
SK_ROBO_ | 0:55aac36c4f0f | 19 | |
SK_ROBO_ | 0:55aac36c4f0f | 20 | // Select the device by seting chip select low |
SK_ROBO_ | 0:55aac36c4f0f | 21 | cs = 0; |
SK_ROBO_ | 0:55aac36c4f0f | 22 | |
SK_ROBO_ | 0:55aac36c4f0f | 23 | // sending the 6 bits + 1 bit to ignore the null bit |
SK_ROBO_ | 0:55aac36c4f0f | 24 | // coming from the device, so the data that is sent is 1100000 |
SK_ROBO_ | 0:55aac36c4f0f | 25 | spi.write(0x60); |
SK_ROBO_ | 0:55aac36c4f0f | 26 | |
SK_ROBO_ | 0:55aac36c4f0f | 27 | // now the device sends back the readings 12 bits, 7 bits at a time |
SK_ROBO_ | 0:55aac36c4f0f | 28 | uint8_t high = spi.write(0x00); |
SK_ROBO_ | 0:55aac36c4f0f | 29 | uint8_t low = spi.write(0x00); |
SK_ROBO_ | 0:55aac36c4f0f | 30 | |
SK_ROBO_ | 0:55aac36c4f0f | 31 | // shift out the right bits |
SK_ROBO_ | 0:55aac36c4f0f | 32 | low = ( high << 5 ) | (low >> 2); |
SK_ROBO_ | 0:55aac36c4f0f | 33 | high = high >> 3; |
SK_ROBO_ | 0:55aac36c4f0f | 34 | |
SK_ROBO_ | 0:55aac36c4f0f | 35 | // shift and or the result together |
SK_ROBO_ | 0:55aac36c4f0f | 36 | int value = ( high << 7 ) | low; |
SK_ROBO_ | 0:55aac36c4f0f | 37 | |
SK_ROBO_ | 0:55aac36c4f0f | 38 | // and voila we have the value and we can print it for the user |
SK_ROBO_ | 0:55aac36c4f0f | 39 | pc.printf("sensor 0 value = %u\n", value); |
SK_ROBO_ | 0:55aac36c4f0f | 40 | |
SK_ROBO_ | 0:55aac36c4f0f | 41 | // Deselect the device |
SK_ROBO_ | 0:55aac36c4f0f | 42 | cs = 1; |
SK_ROBO_ | 0:55aac36c4f0f | 43 | |
SK_ROBO_ | 0:55aac36c4f0f | 44 | // delay some time before reading again |
SK_ROBO_ | 0:55aac36c4f0f | 45 | wait(1); |
SK_ROBO_ | 0:55aac36c4f0f | 46 | } |
SK_ROBO_ | 0:55aac36c4f0f | 47 | } |