![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Code to use read data from MCP3208 ADC via SPI and output it via serial using the LPC1114.
main.cpp@0:6c41db68c857, 2015-02-19 (annotated)
- Committer:
- mcx
- Date:
- Thu Feb 19 00:03:51 2015 +0000
- Revision:
- 0:6c41db68c857
- Child:
- 1:cf620653d56f
Working!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mcx | 0:6c41db68c857 | 1 | #include "mbed.h" |
mcx | 0:6c41db68c857 | 2 | #include "mcp3208.h" |
mcx | 0:6c41db68c857 | 3 | // #include "BufferedSerial.h" |
mcx | 0:6c41db68c857 | 4 | |
mcx | 0:6c41db68c857 | 5 | // #include <string> |
mcx | 0:6c41db68c857 | 6 | // using namespace std; |
mcx | 0:6c41db68c857 | 7 | |
mcx | 0:6c41db68c857 | 8 | MCP3208 input1(dp2, dp1, dp6, dp9); //MCP3208(PinName mosi, PinName miso, PinName clk, PinName cs) |
mcx | 0:6c41db68c857 | 9 | // MCP3208 input1(p5, p6, p7, p8); |
mcx | 0:6c41db68c857 | 10 | |
mcx | 0:6c41db68c857 | 11 | Serial pc(dp16,dp15); //(USBTX, USBRX) |
mcx | 0:6c41db68c857 | 12 | // Serial pc(p9,p10); |
mcx | 0:6c41db68c857 | 13 | |
mcx | 0:6c41db68c857 | 14 | Ticker datalog; |
mcx | 0:6c41db68c857 | 15 | |
mcx | 0:6c41db68c857 | 16 | char datastr0[5]; |
mcx | 0:6c41db68c857 | 17 | char datastr1[5]; |
mcx | 0:6c41db68c857 | 18 | char datastr2[5]; |
mcx | 0:6c41db68c857 | 19 | char datastr3[5]; |
mcx | 0:6c41db68c857 | 20 | char datastr4[5]; |
mcx | 0:6c41db68c857 | 21 | char datastr5[5]; |
mcx | 0:6c41db68c857 | 22 | |
mcx | 0:6c41db68c857 | 23 | // Credit: Erik Olieman |
mcx | 0:6c41db68c857 | 24 | void intToString(char *buffer, int value) |
mcx | 0:6c41db68c857 | 25 | { |
mcx | 0:6c41db68c857 | 26 | int temp; |
mcx | 0:6c41db68c857 | 27 | temp = value / 1000; |
mcx | 0:6c41db68c857 | 28 | buffer[0] = temp + '0'; |
mcx | 0:6c41db68c857 | 29 | value = value - temp * 1000; |
mcx | 0:6c41db68c857 | 30 | |
mcx | 0:6c41db68c857 | 31 | temp = value / 100; |
mcx | 0:6c41db68c857 | 32 | buffer[1] = temp + '0'; |
mcx | 0:6c41db68c857 | 33 | value = value - temp * 100; |
mcx | 0:6c41db68c857 | 34 | |
mcx | 0:6c41db68c857 | 35 | temp = value / 10; |
mcx | 0:6c41db68c857 | 36 | buffer[2] = temp + '0'; |
mcx | 0:6c41db68c857 | 37 | value = value - temp * 10; |
mcx | 0:6c41db68c857 | 38 | |
mcx | 0:6c41db68c857 | 39 | temp = value / 1; |
mcx | 0:6c41db68c857 | 40 | buffer[3] = temp + '0'; |
mcx | 0:6c41db68c857 | 41 | value = value - temp * 1; |
mcx | 0:6c41db68c857 | 42 | |
mcx | 0:6c41db68c857 | 43 | buffer[4] = '\0'; |
mcx | 0:6c41db68c857 | 44 | } |
mcx | 0:6c41db68c857 | 45 | |
mcx | 0:6c41db68c857 | 46 | bool tickerActivated = false; |
mcx | 0:6c41db68c857 | 47 | |
mcx | 0:6c41db68c857 | 48 | void log_data() |
mcx | 0:6c41db68c857 | 49 | { |
mcx | 0:6c41db68c857 | 50 | tickerActivated = true; |
mcx | 0:6c41db68c857 | 51 | } |
mcx | 0:6c41db68c857 | 52 | |
mcx | 0:6c41db68c857 | 53 | int main() |
mcx | 0:6c41db68c857 | 54 | { |
mcx | 0:6c41db68c857 | 55 | pc.baud(921600); |
mcx | 0:6c41db68c857 | 56 | pc.printf("Working!!\n\r"); |
mcx | 0:6c41db68c857 | 57 | |
mcx | 0:6c41db68c857 | 58 | datalog.attach_us(&log_data,1000); // 1000us = 1ms |
mcx | 0:6c41db68c857 | 59 | |
mcx | 0:6c41db68c857 | 60 | intToString(datastr0,input1.binary(0)); |
mcx | 0:6c41db68c857 | 61 | intToString(datastr1,input1.binary(1)); |
mcx | 0:6c41db68c857 | 62 | intToString(datastr2,input1.binary(2)); |
mcx | 0:6c41db68c857 | 63 | intToString(datastr3,input1.binary(3)); |
mcx | 0:6c41db68c857 | 64 | intToString(datastr4,input1.binary(4)); |
mcx | 0:6c41db68c857 | 65 | intToString(datastr5,input1.binary(5)); |
mcx | 0:6c41db68c857 | 66 | |
mcx | 0:6c41db68c857 | 67 | while(1) { |
mcx | 0:6c41db68c857 | 68 | if(tickerActivated == true) { |
mcx | 0:6c41db68c857 | 69 | tickerActivated = false; |
mcx | 0:6c41db68c857 | 70 | pc.printf("%s,%s,%s,%s,%s,%s\n\r",datastr0,datastr1,datastr2,datastr3,datastr4,datastr5); |
mcx | 0:6c41db68c857 | 71 | } |
mcx | 0:6c41db68c857 | 72 | } |
mcx | 0:6c41db68c857 | 73 | } |
mcx | 0:6c41db68c857 | 74 | |
mcx | 0:6c41db68c857 | 75 |