![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ads_converter for the a ads1015 using all mbed
Dependencies: ADS1015 USBDevice mbed
main.cpp@0:e9b3484aed88, 2015-12-10 (annotated)
- Committer:
- NunchukPiledrivers
- Date:
- Thu Dec 10 19:11:44 2015 +0000
- Revision:
- 0:e9b3484aed88
Final code - Punlished by paul
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
NunchukPiledrivers | 0:e9b3484aed88 | 1 | #include <mbed.h> |
NunchukPiledrivers | 0:e9b3484aed88 | 2 | #include <Adafruit_ADS1015.h> |
NunchukPiledrivers | 0:e9b3484aed88 | 3 | using namespace std; |
NunchukPiledrivers | 0:e9b3484aed88 | 4 | /*************************************** |
NunchukPiledrivers | 0:e9b3484aed88 | 5 | LED 1 = Standy by mode |
NunchukPiledrivers | 0:e9b3484aed88 | 6 | LED 2 = Reading Message |
NunchukPiledrivers | 0:e9b3484aed88 | 7 | LED 3 = Wrighting Message |
NunchukPiledrivers | 0:e9b3484aed88 | 8 | From The Keyboard |
NunchukPiledrivers | 0:e9b3484aed88 | 9 | Cell Zero == 3 is if they are talking to us |
NunchukPiledrivers | 0:e9b3484aed88 | 10 | Second character 0 == start |
NunchukPiledrivers | 0:e9b3484aed88 | 11 | 1 == stop |
NunchukPiledrivers | 0:e9b3484aed88 | 12 | ***************************************/ |
NunchukPiledrivers | 0:e9b3484aed88 | 13 | /************************************** |
NunchukPiledrivers | 0:e9b3484aed88 | 14 | Initial Parameters |
NunchukPiledrivers | 0:e9b3484aed88 | 15 | ***************************************/ |
NunchukPiledrivers | 0:e9b3484aed88 | 16 | I2C i2c(p28, p27); //pins 27/28 for I2C interface |
NunchukPiledrivers | 0:e9b3484aed88 | 17 | Adafruit_ADS1015 ads(&i2c); |
NunchukPiledrivers | 0:e9b3484aed88 | 18 | Serial pc(USBTX, USBRX); // tx, rx |
NunchukPiledrivers | 0:e9b3484aed88 | 19 | DigitalOut led(LED1); // LED 1 for state 1 |
NunchukPiledrivers | 0:e9b3484aed88 | 20 | DigitalOut led2(LED2); // LED 2 for state 2 |
NunchukPiledrivers | 0:e9b3484aed88 | 21 | DigitalOut led3(LED3); // LED 3 for state 3 |
NunchukPiledrivers | 0:e9b3484aed88 | 22 | CAN can(p30, p29); //p30(rx) and p29(tx) |
NunchukPiledrivers | 0:e9b3484aed88 | 23 | LocalFileSystem local("local"); //Output file location |
NunchukPiledrivers | 0:e9b3484aed88 | 24 | CANMessage msg; |
NunchukPiledrivers | 0:e9b3484aed88 | 25 | char state; |
NunchukPiledrivers | 0:e9b3484aed88 | 26 | FILE *fp = fopen("/local/out.txt", "w"); //File initialization |
NunchukPiledrivers | 0:e9b3484aed88 | 27 | int i = 0; |
NunchukPiledrivers | 0:e9b3484aed88 | 28 | /************************************* |
NunchukPiledrivers | 0:e9b3484aed88 | 29 | Function Status keeps checking the bus waiting for the KeyBoard Subgroup |
NunchukPiledrivers | 0:e9b3484aed88 | 30 | To tell us to start reading from the blackbox. |
NunchukPiledrivers | 0:e9b3484aed88 | 31 | *************************************/ |
NunchukPiledrivers | 0:e9b3484aed88 | 32 | void get_state() |
NunchukPiledrivers | 0:e9b3484aed88 | 33 | { |
NunchukPiledrivers | 0:e9b3484aed88 | 34 | can.read(msg); |
NunchukPiledrivers | 0:e9b3484aed88 | 35 | char Cell_One = msg.data[0]; |
NunchukPiledrivers | 0:e9b3484aed88 | 36 | char Cell_Two = msg.data[1]; |
NunchukPiledrivers | 0:e9b3484aed88 | 37 | if (Cell_One == '3' && Cell_Two == '0') { |
NunchukPiledrivers | 0:e9b3484aed88 | 38 | pc.printf("Reading \r \n"); |
NunchukPiledrivers | 0:e9b3484aed88 | 39 | state = 2; |
NunchukPiledrivers | 0:e9b3484aed88 | 40 | } else if (Cell_One == '3' && Cell_Two == '1') { |
NunchukPiledrivers | 0:e9b3484aed88 | 41 | pc.printf("Writing \r \n"); |
NunchukPiledrivers | 0:e9b3484aed88 | 42 | state = 3; |
NunchukPiledrivers | 0:e9b3484aed88 | 43 | i = 0; |
NunchukPiledrivers | 0:e9b3484aed88 | 44 | } else |
NunchukPiledrivers | 0:e9b3484aed88 | 45 | pc.printf("Waiting \r \n"); |
NunchukPiledrivers | 0:e9b3484aed88 | 46 | state = state; |
NunchukPiledrivers | 0:e9b3484aed88 | 47 | |
NunchukPiledrivers | 0:e9b3484aed88 | 48 | } |
NunchukPiledrivers | 0:e9b3484aed88 | 49 | int main() |
NunchukPiledrivers | 0:e9b3484aed88 | 50 | { |
NunchukPiledrivers | 0:e9b3484aed88 | 51 | state = 1; |
NunchukPiledrivers | 0:e9b3484aed88 | 52 | pc.baud(9600); |
NunchukPiledrivers | 0:e9b3484aed88 | 53 | pc.printf("\r \n"); |
NunchukPiledrivers | 0:e9b3484aed88 | 54 | pc.printf("\r \n"); |
NunchukPiledrivers | 0:e9b3484aed88 | 55 | pc.printf("Hello! \r \n"); |
NunchukPiledrivers | 0:e9b3484aed88 | 56 | pc.printf("Note: ADC Range +/- 6.144 (1 bit = 3mV/ADS1015, 0.1875mV/ ADS1115)\r \n"); |
NunchukPiledrivers | 0:e9b3484aed88 | 57 | pc.printf("Waitng.... \r \n"); |
NunchukPiledrivers | 0:e9b3484aed88 | 58 | uint16_t adc0; // channel coming from the black box |
NunchukPiledrivers | 0:e9b3484aed88 | 59 | float conversion_factor = .003; |
NunchukPiledrivers | 0:e9b3484aed88 | 60 | can.attach(&get_state , CAN::RxIrq); |
NunchukPiledrivers | 0:e9b3484aed88 | 61 | while (1) { |
NunchukPiledrivers | 0:e9b3484aed88 | 62 | |
NunchukPiledrivers | 0:e9b3484aed88 | 63 | if (state == 2) { |
NunchukPiledrivers | 0:e9b3484aed88 | 64 | adc0 = ads.readADC_SingleEnded(0); // read channel 0 |
NunchukPiledrivers | 0:e9b3484aed88 | 65 | pc.printf("AIN0: %f\r\n", adc0*conversion_factor); // print channel 0 |
NunchukPiledrivers | 0:e9b3484aed88 | 66 | fprintf(fp, "%f\r\n", adc0*conversion_factor); |
NunchukPiledrivers | 0:e9b3484aed88 | 67 | led = 0; |
NunchukPiledrivers | 0:e9b3484aed88 | 68 | led2 = 1; |
NunchukPiledrivers | 0:e9b3484aed88 | 69 | led3 = 0; |
NunchukPiledrivers | 0:e9b3484aed88 | 70 | wait(0.01); //100 hz |
NunchukPiledrivers | 0:e9b3484aed88 | 71 | } else if (state == 3) { |
NunchukPiledrivers | 0:e9b3484aed88 | 72 | fclose(fp); |
NunchukPiledrivers | 0:e9b3484aed88 | 73 | led = 0; |
NunchukPiledrivers | 0:e9b3484aed88 | 74 | led2 = 0; |
NunchukPiledrivers | 0:e9b3484aed88 | 75 | led3 = 1; |
NunchukPiledrivers | 0:e9b3484aed88 | 76 | if(i == 0) { |
NunchukPiledrivers | 0:e9b3484aed88 | 77 | pc.printf("File Complete \r \n"); |
NunchukPiledrivers | 0:e9b3484aed88 | 78 | i++; |
NunchukPiledrivers | 0:e9b3484aed88 | 79 | } |
NunchukPiledrivers | 0:e9b3484aed88 | 80 | state = 1; |
NunchukPiledrivers | 0:e9b3484aed88 | 81 | } else { |
NunchukPiledrivers | 0:e9b3484aed88 | 82 | led = 1; |
NunchukPiledrivers | 0:e9b3484aed88 | 83 | led2 = 0; |
NunchukPiledrivers | 0:e9b3484aed88 | 84 | led3 = 0; |
NunchukPiledrivers | 0:e9b3484aed88 | 85 | } |
NunchukPiledrivers | 0:e9b3484aed88 | 86 | } |
NunchukPiledrivers | 0:e9b3484aed88 | 87 | } |