![](/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@7:983886b6ada9, 2017-02-17 (annotated)
- Committer:
- mcx
- Date:
- Fri Feb 17 18:15:42 2017 +0000
- Revision:
- 7:983886b6ada9
- Parent:
- 5:9568cfe486dd
- Child:
- 8:4c941f8c6d73
Added timing loop counter
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 | 5:9568cfe486dd | 4 | MCP3208 input1(PA_7, PA_6, PA_5, PA_4); //MCP3208(PinName mosi, PinName miso, PinName clk, PinName cs) |
mcx | 7:983886b6ada9 | 5 | //MCP3208 input1(PB_5, PB_4, PB_3, PA_4); |
mcx | 7:983886b6ada9 | 6 | |
mcx | 7:983886b6ada9 | 7 | //MCP3208 input1(p5, p6, p7, p8); |
mcx | 0:6c41db68c857 | 8 | |
mcx | 5:9568cfe486dd | 9 | //Serial pc(dp16,dp15); |
mcx | 0:6c41db68c857 | 10 | // Serial pc(p9,p10); |
mcx | 5:9568cfe486dd | 11 | Serial pc(USBTX, USBRX); |
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 | 0:6c41db68c857 | 25 | // Credit: Erik Olieman |
mcx | 1:cf620653d56f | 26 | // http://developer.mbed.org/questions/5149/Serial-port-on-LPC1114-is-slow/ |
mcx | 0:6c41db68c857 | 27 | void intToString(char *buffer, int value) |
mcx | 0:6c41db68c857 | 28 | { |
mcx | 0:6c41db68c857 | 29 | int temp; |
mcx | 0:6c41db68c857 | 30 | temp = value / 1000; |
mcx | 0:6c41db68c857 | 31 | buffer[0] = temp + '0'; |
mcx | 0:6c41db68c857 | 32 | value = value - temp * 1000; |
mcx | 0:6c41db68c857 | 33 | |
mcx | 0:6c41db68c857 | 34 | temp = value / 100; |
mcx | 0:6c41db68c857 | 35 | buffer[1] = temp + '0'; |
mcx | 0:6c41db68c857 | 36 | value = value - temp * 100; |
mcx | 0:6c41db68c857 | 37 | |
mcx | 0:6c41db68c857 | 38 | temp = value / 10; |
mcx | 0:6c41db68c857 | 39 | buffer[2] = temp + '0'; |
mcx | 0:6c41db68c857 | 40 | value = value - temp * 10; |
mcx | 0:6c41db68c857 | 41 | |
mcx | 0:6c41db68c857 | 42 | temp = value / 1; |
mcx | 0:6c41db68c857 | 43 | buffer[3] = temp + '0'; |
mcx | 0:6c41db68c857 | 44 | value = value - temp * 1; |
mcx | 0:6c41db68c857 | 45 | |
mcx | 0:6c41db68c857 | 46 | buffer[4] = '\0'; |
mcx | 0:6c41db68c857 | 47 | } |
mcx | 0:6c41db68c857 | 48 | |
mcx | 0:6c41db68c857 | 49 | bool tickerActivated = false; |
mcx | 0:6c41db68c857 | 50 | |
mcx | 0:6c41db68c857 | 51 | void log_data() |
mcx | 0:6c41db68c857 | 52 | { |
mcx | 0:6c41db68c857 | 53 | tickerActivated = true; |
mcx | 0:6c41db68c857 | 54 | } |
mcx | 0:6c41db68c857 | 55 | |
mcx | 0:6c41db68c857 | 56 | int main() |
mcx | 0:6c41db68c857 | 57 | { |
mcx | 4:1267e8fa59a3 | 58 | int tt = 0; |
mcx | 4:1267e8fa59a3 | 59 | t.start(); |
mcx | 4:1267e8fa59a3 | 60 | |
mcx | 7:983886b6ada9 | 61 | pc.baud(921600); |
mcx | 0:6c41db68c857 | 62 | pc.printf("Working!!\n\r"); |
mcx | 0:6c41db68c857 | 63 | |
mcx | 7:983886b6ada9 | 64 | datalog.attach_us(&log_data,1000); // 1000us = 1ms |
mcx | 0:6c41db68c857 | 65 | |
mcx | 1:cf620653d56f | 66 | while(1) { |
mcx | 1:cf620653d56f | 67 | intToString(datastr0,input1.binary(0)); |
mcx | 1:cf620653d56f | 68 | intToString(datastr1,input1.binary(1)); |
mcx | 1:cf620653d56f | 69 | intToString(datastr2,input1.binary(2)); |
mcx | 1:cf620653d56f | 70 | intToString(datastr3,input1.binary(3)); |
mcx | 1:cf620653d56f | 71 | intToString(datastr4,input1.binary(4)); |
mcx | 1:cf620653d56f | 72 | intToString(datastr5,input1.binary(5)); |
mcx | 0:6c41db68c857 | 73 | |
mcx | 4:1267e8fa59a3 | 74 | // Timing methods to verify 1 ms loop time / 1 kHz update speed |
mcx | 4:1267e8fa59a3 | 75 | // tt = t.read_ms(); |
mcx | 4:1267e8fa59a3 | 76 | tt++; |
mcx | 4:1267e8fa59a3 | 77 | if(tt>=10000) { |
mcx | 4:1267e8fa59a3 | 78 | tt = 0; |
mcx | 4:1267e8fa59a3 | 79 | } |
mcx | 4:1267e8fa59a3 | 80 | intToString(datastr6,tt); |
mcx | 4:1267e8fa59a3 | 81 | |
mcx | 0:6c41db68c857 | 82 | if(tickerActivated == true) { |
mcx | 0:6c41db68c857 | 83 | tickerActivated = false; |
mcx | 0:6c41db68c857 | 84 | pc.printf("%s,%s,%s,%s,%s,%s\n\r",datastr0,datastr1,datastr2,datastr3,datastr4,datastr5); |
mcx | 0:6c41db68c857 | 85 | } |
mcx | 4:1267e8fa59a3 | 86 | |
mcx | 0:6c41db68c857 | 87 | } |
mcx | 0:6c41db68c857 | 88 | } |
mcx | 0:6c41db68c857 | 89 | |
mcx | 0:6c41db68c857 | 90 |