![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Mej
Dependencies: MCP3208_SWSPI SWSPI mbed
Fork of MCP3208_STM32 by
main.cpp@5:9568cfe486dd, 2016-12-02 (annotated)
- Committer:
- mcx
- Date:
- Fri Dec 02 16:41:16 2016 +0000
- Revision:
- 5:9568cfe486dd
- Parent:
- 4:1267e8fa59a3
- Child:
- 7:983886b6ada9
Modified to update at 9600 baud every 0.1s
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 | 0:6c41db68c857 | 5 | // MCP3208 input1(p5, p6, p7, p8); |
mcx | 0:6c41db68c857 | 6 | |
mcx | 5:9568cfe486dd | 7 | //Serial pc(dp16,dp15); |
mcx | 0:6c41db68c857 | 8 | // Serial pc(p9,p10); |
mcx | 5:9568cfe486dd | 9 | Serial pc(USBTX, USBRX); |
mcx | 0:6c41db68c857 | 10 | |
mcx | 0:6c41db68c857 | 11 | Ticker datalog; |
mcx | 4:1267e8fa59a3 | 12 | Timer t; |
mcx | 0:6c41db68c857 | 13 | |
mcx | 0:6c41db68c857 | 14 | char datastr0[5]; |
mcx | 0:6c41db68c857 | 15 | char datastr1[5]; |
mcx | 0:6c41db68c857 | 16 | char datastr2[5]; |
mcx | 0:6c41db68c857 | 17 | char datastr3[5]; |
mcx | 0:6c41db68c857 | 18 | char datastr4[5]; |
mcx | 0:6c41db68c857 | 19 | char datastr5[5]; |
mcx | 0:6c41db68c857 | 20 | |
mcx | 4:1267e8fa59a3 | 21 | char datastr6[5]; |
mcx | 4:1267e8fa59a3 | 22 | |
mcx | 0:6c41db68c857 | 23 | // Credit: Erik Olieman |
mcx | 1:cf620653d56f | 24 | // http://developer.mbed.org/questions/5149/Serial-port-on-LPC1114-is-slow/ |
mcx | 0:6c41db68c857 | 25 | void intToString(char *buffer, int value) |
mcx | 0:6c41db68c857 | 26 | { |
mcx | 0:6c41db68c857 | 27 | int temp; |
mcx | 0:6c41db68c857 | 28 | temp = value / 1000; |
mcx | 0:6c41db68c857 | 29 | buffer[0] = temp + '0'; |
mcx | 0:6c41db68c857 | 30 | value = value - temp * 1000; |
mcx | 0:6c41db68c857 | 31 | |
mcx | 0:6c41db68c857 | 32 | temp = value / 100; |
mcx | 0:6c41db68c857 | 33 | buffer[1] = temp + '0'; |
mcx | 0:6c41db68c857 | 34 | value = value - temp * 100; |
mcx | 0:6c41db68c857 | 35 | |
mcx | 0:6c41db68c857 | 36 | temp = value / 10; |
mcx | 0:6c41db68c857 | 37 | buffer[2] = temp + '0'; |
mcx | 0:6c41db68c857 | 38 | value = value - temp * 10; |
mcx | 0:6c41db68c857 | 39 | |
mcx | 0:6c41db68c857 | 40 | temp = value / 1; |
mcx | 0:6c41db68c857 | 41 | buffer[3] = temp + '0'; |
mcx | 0:6c41db68c857 | 42 | value = value - temp * 1; |
mcx | 0:6c41db68c857 | 43 | |
mcx | 0:6c41db68c857 | 44 | buffer[4] = '\0'; |
mcx | 0:6c41db68c857 | 45 | } |
mcx | 0:6c41db68c857 | 46 | |
mcx | 0:6c41db68c857 | 47 | bool tickerActivated = false; |
mcx | 0:6c41db68c857 | 48 | |
mcx | 0:6c41db68c857 | 49 | void log_data() |
mcx | 0:6c41db68c857 | 50 | { |
mcx | 0:6c41db68c857 | 51 | tickerActivated = true; |
mcx | 0:6c41db68c857 | 52 | } |
mcx | 0:6c41db68c857 | 53 | |
mcx | 0:6c41db68c857 | 54 | int main() |
mcx | 0:6c41db68c857 | 55 | { |
mcx | 4:1267e8fa59a3 | 56 | int tt = 0; |
mcx | 4:1267e8fa59a3 | 57 | t.start(); |
mcx | 4:1267e8fa59a3 | 58 | |
mcx | 4:1267e8fa59a3 | 59 | pc.baud(9600); |
mcx | 0:6c41db68c857 | 60 | pc.printf("Working!!\n\r"); |
mcx | 0:6c41db68c857 | 61 | |
mcx | 5:9568cfe486dd | 62 | datalog.attach_us(&log_data,100000); // 1000us = 1ms |
mcx | 0:6c41db68c857 | 63 | |
mcx | 1:cf620653d56f | 64 | while(1) { |
mcx | 1:cf620653d56f | 65 | intToString(datastr0,input1.binary(0)); |
mcx | 1:cf620653d56f | 66 | intToString(datastr1,input1.binary(1)); |
mcx | 1:cf620653d56f | 67 | intToString(datastr2,input1.binary(2)); |
mcx | 1:cf620653d56f | 68 | intToString(datastr3,input1.binary(3)); |
mcx | 1:cf620653d56f | 69 | intToString(datastr4,input1.binary(4)); |
mcx | 1:cf620653d56f | 70 | intToString(datastr5,input1.binary(5)); |
mcx | 0:6c41db68c857 | 71 | |
mcx | 4:1267e8fa59a3 | 72 | // Timing methods to verify 1 ms loop time / 1 kHz update speed |
mcx | 4:1267e8fa59a3 | 73 | // tt = t.read_ms(); |
mcx | 4:1267e8fa59a3 | 74 | tt++; |
mcx | 4:1267e8fa59a3 | 75 | if(tt>=10000) { |
mcx | 4:1267e8fa59a3 | 76 | tt = 0; |
mcx | 4:1267e8fa59a3 | 77 | } |
mcx | 4:1267e8fa59a3 | 78 | intToString(datastr6,tt); |
mcx | 4:1267e8fa59a3 | 79 | |
mcx | 0:6c41db68c857 | 80 | if(tickerActivated == true) { |
mcx | 0:6c41db68c857 | 81 | tickerActivated = false; |
mcx | 0:6c41db68c857 | 82 | pc.printf("%s,%s,%s,%s,%s,%s\n\r",datastr0,datastr1,datastr2,datastr3,datastr4,datastr5); |
mcx | 0:6c41db68c857 | 83 | } |
mcx | 4:1267e8fa59a3 | 84 | |
mcx | 0:6c41db68c857 | 85 | } |
mcx | 0:6c41db68c857 | 86 | } |
mcx | 0:6c41db68c857 | 87 | |
mcx | 0:6c41db68c857 | 88 |