Mej
Dependencies: MCP3208_SWSPI SWSPI mbed
Fork of MCP3208_STM32 by
main.cpp@11:0c855a410d68, 2017-04-15 (annotated)
- Committer:
- johnla
- Date:
- Sat Apr 15 01:48:22 2017 +0000
- Revision:
- 11:0c855a410d68
- Parent:
- 10:a22ee39e2282
- Child:
- 12:a5672b85b4b5
Meh;
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 | |
johnla | 11:0c855a410d68 | 4 | MCP3208 input1(PA_7, PA_6, PA_5, PA_4); //MCP3208(PinName mosi, PinName miso, PinName clk, PinName cs) |
johnla | 11:0c855a410d68 | 5 | //MCP3208 input1(PB_5, PB_4, PB_3, PA_11); |
mcx | 7:983886b6ada9 | 6 | //MCP3208 input1(p5, p6, p7, p8); |
mcx | 0:6c41db68c857 | 7 | |
mcx | 5:9568cfe486dd | 8 | //Serial pc(dp16,dp15); |
mcx | 0:6c41db68c857 | 9 | // Serial pc(p9,p10); |
johnla | 11:0c855a410d68 | 10 | //Serial pc(USBTX, USBRX); |
johnla | 11:0c855a410d68 | 11 | Serial pc(PA_3, PA_1); |
mcx | 0:6c41db68c857 | 12 | |
mcx | 0:6c41db68c857 | 13 | Ticker datalog; |
mcx | 4:1267e8fa59a3 | 14 | Timer t; |
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 | 4:1267e8fa59a3 | 23 | char datastr6[5]; |
mcx | 4:1267e8fa59a3 | 24 | |
mcx | 8:4c941f8c6d73 | 25 | int data0; |
mcx | 8:4c941f8c6d73 | 26 | int data1; |
mcx | 8:4c941f8c6d73 | 27 | int data2; |
mcx | 8:4c941f8c6d73 | 28 | int data3; |
mcx | 8:4c941f8c6d73 | 29 | int data4; |
mcx | 8:4c941f8c6d73 | 30 | int data5; |
mcx | 8:4c941f8c6d73 | 31 | |
mcx | 0:6c41db68c857 | 32 | // Credit: Erik Olieman |
mcx | 1:cf620653d56f | 33 | // http://developer.mbed.org/questions/5149/Serial-port-on-LPC1114-is-slow/ |
mcx | 0:6c41db68c857 | 34 | void intToString(char *buffer, int value) |
mcx | 0:6c41db68c857 | 35 | { |
mcx | 0:6c41db68c857 | 36 | int temp; |
mcx | 0:6c41db68c857 | 37 | temp = value / 1000; |
mcx | 0:6c41db68c857 | 38 | buffer[0] = temp + '0'; |
mcx | 0:6c41db68c857 | 39 | value = value - temp * 1000; |
mcx | 0:6c41db68c857 | 40 | |
mcx | 0:6c41db68c857 | 41 | temp = value / 100; |
mcx | 0:6c41db68c857 | 42 | buffer[1] = temp + '0'; |
mcx | 0:6c41db68c857 | 43 | value = value - temp * 100; |
mcx | 0:6c41db68c857 | 44 | |
mcx | 0:6c41db68c857 | 45 | temp = value / 10; |
mcx | 0:6c41db68c857 | 46 | buffer[2] = temp + '0'; |
mcx | 0:6c41db68c857 | 47 | value = value - temp * 10; |
mcx | 0:6c41db68c857 | 48 | |
mcx | 0:6c41db68c857 | 49 | temp = value / 1; |
mcx | 0:6c41db68c857 | 50 | buffer[3] = temp + '0'; |
mcx | 0:6c41db68c857 | 51 | value = value - temp * 1; |
mcx | 0:6c41db68c857 | 52 | |
mcx | 0:6c41db68c857 | 53 | buffer[4] = '\0'; |
mcx | 0:6c41db68c857 | 54 | } |
mcx | 0:6c41db68c857 | 55 | |
mcx | 0:6c41db68c857 | 56 | bool tickerActivated = false; |
mcx | 0:6c41db68c857 | 57 | |
mcx | 0:6c41db68c857 | 58 | void log_data() |
mcx | 0:6c41db68c857 | 59 | { |
mcx | 0:6c41db68c857 | 60 | tickerActivated = true; |
mcx | 0:6c41db68c857 | 61 | } |
mcx | 0:6c41db68c857 | 62 | |
mcx | 0:6c41db68c857 | 63 | int main() |
mcx | 0:6c41db68c857 | 64 | { |
mcx | 4:1267e8fa59a3 | 65 | int tt = 0; |
mcx | 4:1267e8fa59a3 | 66 | t.start(); |
mcx | 4:1267e8fa59a3 | 67 | |
mcx | 7:983886b6ada9 | 68 | pc.baud(921600); |
johnla | 11:0c855a410d68 | 69 | //pc.baud(9600); |
mcx | 0:6c41db68c857 | 70 | pc.printf("Working!!\n\r"); |
mcx | 0:6c41db68c857 | 71 | |
mcx | 7:983886b6ada9 | 72 | datalog.attach_us(&log_data,1000); // 1000us = 1ms |
mcx | 0:6c41db68c857 | 73 | |
mcx | 1:cf620653d56f | 74 | while(1) { |
mcx | 8:4c941f8c6d73 | 75 | data0 = input1.binary(0); |
mcx | 8:4c941f8c6d73 | 76 | data1 = input1.binary(1); |
mcx | 8:4c941f8c6d73 | 77 | data2 = input1.binary(2); |
mcx | 8:4c941f8c6d73 | 78 | data3 = input1.binary(3); |
mcx | 8:4c941f8c6d73 | 79 | data4 = input1.binary(4); |
mcx | 8:4c941f8c6d73 | 80 | data5 = input1.binary(5); |
mcx | 8:4c941f8c6d73 | 81 | |
mcx | 1:cf620653d56f | 82 | intToString(datastr0,input1.binary(0)); |
mcx | 1:cf620653d56f | 83 | intToString(datastr1,input1.binary(1)); |
mcx | 1:cf620653d56f | 84 | intToString(datastr2,input1.binary(2)); |
mcx | 1:cf620653d56f | 85 | intToString(datastr3,input1.binary(3)); |
mcx | 1:cf620653d56f | 86 | intToString(datastr4,input1.binary(4)); |
mcx | 1:cf620653d56f | 87 | intToString(datastr5,input1.binary(5)); |
mcx | 0:6c41db68c857 | 88 | |
mcx | 4:1267e8fa59a3 | 89 | // Timing methods to verify 1 ms loop time / 1 kHz update speed |
mcx | 4:1267e8fa59a3 | 90 | // tt = t.read_ms(); |
mcx | 4:1267e8fa59a3 | 91 | tt++; |
mcx | 4:1267e8fa59a3 | 92 | if(tt>=10000) { |
mcx | 4:1267e8fa59a3 | 93 | tt = 0; |
mcx | 4:1267e8fa59a3 | 94 | } |
mcx | 4:1267e8fa59a3 | 95 | intToString(datastr6,tt); |
mcx | 4:1267e8fa59a3 | 96 | |
mcx | 0:6c41db68c857 | 97 | if(tickerActivated == true) { |
mcx | 0:6c41db68c857 | 98 | tickerActivated = false; |
mcx | 8:4c941f8c6d73 | 99 | // pc.printf("%s,%s,%s,%s,%s,%s\n\r",datastr0,datastr1,datastr2,datastr3,datastr4,datastr5); |
mcx | 10:a22ee39e2282 | 100 | pc.printf("%04d,%04d,%04d,%04d,%04d,%04d\n\r",data0,data1,data2,data3,data4,data5); |
mcx | 8:4c941f8c6d73 | 101 | // pc.printf("%d\n\r",SPI1->DR); |
mcx | 8:4c941f8c6d73 | 102 | // pc.printf("%d\n\r",input1.binary(0)); |
mcx | 0:6c41db68c857 | 103 | } |
mcx | 4:1267e8fa59a3 | 104 | |
mcx | 0:6c41db68c857 | 105 | } |
mcx | 0:6c41db68c857 | 106 | } |
mcx | 0:6c41db68c857 | 107 | |
mcx | 0:6c41db68c857 | 108 |