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