Library for MPC3424, it's my prototype library. work good.
MCP3424.cpp@0:4b472be1b25f, 2016-01-22 (annotated)
- Committer:
- irsanjul
- Date:
- Fri Jan 22 08:44:44 2016 +0000
- Revision:
- 0:4b472be1b25f
This is my library for IC ADC MPC3424
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
irsanjul | 0:4b472be1b25f | 1 | #include "MCP3424.h" |
irsanjul | 0:4b472be1b25f | 2 | |
irsanjul | 0:4b472be1b25f | 3 | extern Serial pc; |
irsanjul | 0:4b472be1b25f | 4 | |
irsanjul | 0:4b472be1b25f | 5 | MCP3424::MCP3424(PinName sda, PinName scl) : mcp3424_i2c(sda, scl) |
irsanjul | 0:4b472be1b25f | 6 | { |
irsanjul | 0:4b472be1b25f | 7 | mcp3424_i2c.frequency(MCP3424_FREQ); |
irsanjul | 0:4b472be1b25f | 8 | } |
irsanjul | 0:4b472be1b25f | 9 | |
irsanjul | 0:4b472be1b25f | 10 | MCP3424::~MCP3424(){ |
irsanjul | 0:4b472be1b25f | 11 | |
irsanjul | 0:4b472be1b25f | 12 | } |
irsanjul | 0:4b472be1b25f | 13 | |
irsanjul | 0:4b472be1b25f | 14 | bool MCP3424::Config(uint8_t channel, uint8_t resolution, bool mode, uint8_t PGA) |
irsanjul | 0:4b472be1b25f | 15 | { |
irsanjul | 0:4b472be1b25f | 16 | bool ok = false; |
irsanjul | 0:4b472be1b25f | 17 | char addr = 0x00; |
irsanjul | 0:4b472be1b25f | 18 | if(mcp3424_i2c.write(MCP3424_ADDR, &addr, 1) != 0) |
irsanjul | 0:4b472be1b25f | 19 | return 0; |
irsanjul | 0:4b472be1b25f | 20 | |
irsanjul | 0:4b472be1b25f | 21 | _PGA = PGA; |
irsanjul | 0:4b472be1b25f | 22 | _mode = mode; |
irsanjul | 0:4b472be1b25f | 23 | |
irsanjul | 0:4b472be1b25f | 24 | if(resolution != 12 && resolution != 14 && resolution != 16 && resolution != 18) |
irsanjul | 0:4b472be1b25f | 25 | { |
irsanjul | 0:4b472be1b25f | 26 | _resolution = 12; |
irsanjul | 0:4b472be1b25f | 27 | } |
irsanjul | 0:4b472be1b25f | 28 | else _resolution = resolution; |
irsanjul | 0:4b472be1b25f | 29 | |
irsanjul | 0:4b472be1b25f | 30 | _cfgbyte = 0; |
irsanjul | 0:4b472be1b25f | 31 | _cfgbyte = _cfgbyte<<2; |
irsanjul | 0:4b472be1b25f | 32 | _cfgbyte |= (channel-1); |
irsanjul | 0:4b472be1b25f | 33 | _cfgbyte = _cfgbyte<<1; |
irsanjul | 0:4b472be1b25f | 34 | _cfgbyte |= _mode; |
irsanjul | 0:4b472be1b25f | 35 | _cfgbyte = _cfgbyte<<2; |
irsanjul | 0:4b472be1b25f | 36 | _cfgbyte |= int((_resolution-12)/2); |
irsanjul | 0:4b472be1b25f | 37 | _cfgbyte = _cfgbyte<<2; |
irsanjul | 0:4b472be1b25f | 38 | _cfgbyte |= 0x00; |
irsanjul | 0:4b472be1b25f | 39 | |
irsanjul | 0:4b472be1b25f | 40 | char _send = (char)_cfgbyte; |
irsanjul | 0:4b472be1b25f | 41 | |
irsanjul | 0:4b472be1b25f | 42 | if(mcp3424_i2c.write(MCP3424_ADDR, &_send, 1) ==0) |
irsanjul | 0:4b472be1b25f | 43 | { |
irsanjul | 0:4b472be1b25f | 44 | ok = true; |
irsanjul | 0:4b472be1b25f | 45 | } |
irsanjul | 0:4b472be1b25f | 46 | else |
irsanjul | 0:4b472be1b25f | 47 | { |
irsanjul | 0:4b472be1b25f | 48 | ok = false; |
irsanjul | 0:4b472be1b25f | 49 | } |
irsanjul | 0:4b472be1b25f | 50 | return ok; |
irsanjul | 0:4b472be1b25f | 51 | } |
irsanjul | 0:4b472be1b25f | 52 | |
irsanjul | 0:4b472be1b25f | 53 | bool MCP3424::NewConversion() |
irsanjul | 0:4b472be1b25f | 54 | { |
irsanjul | 0:4b472be1b25f | 55 | char _send = (char)(_cfgbyte |= 128); |
irsanjul | 0:4b472be1b25f | 56 | if(mcp3424_i2c.write(MCP3424_ADDR, &_send, 1) != 0) |
irsanjul | 0:4b472be1b25f | 57 | return 0; |
irsanjul | 0:4b472be1b25f | 58 | else return 1; |
irsanjul | 0:4b472be1b25f | 59 | } |
irsanjul | 0:4b472be1b25f | 60 | |
irsanjul | 0:4b472be1b25f | 61 | bool MCP3424::IsConversionFinished() |
irsanjul | 0:4b472be1b25f | 62 | { |
irsanjul | 0:4b472be1b25f | 63 | if(_resolution!=18) |
irsanjul | 0:4b472be1b25f | 64 | { |
irsanjul | 0:4b472be1b25f | 65 | _RequestedByte = 3; |
irsanjul | 0:4b472be1b25f | 66 | } |
irsanjul | 0:4b472be1b25f | 67 | else _RequestedByte = 4; |
irsanjul | 0:4b472be1b25f | 68 | |
irsanjul | 0:4b472be1b25f | 69 | |
irsanjul | 0:4b472be1b25f | 70 | if(mcp3424_i2c.read(MCP3424_ADDR, _recvBuff, _RequestedByte)!= 0) return 0; |
irsanjul | 0:4b472be1b25f | 71 | |
irsanjul | 0:4b472be1b25f | 72 | for(_i = 0; _i < _RequestedByte; _i++) |
irsanjul | 0:4b472be1b25f | 73 | { |
irsanjul | 0:4b472be1b25f | 74 | _Buffer[_i] = _recvBuff[_i]; |
irsanjul | 0:4b472be1b25f | 75 | } |
irsanjul | 0:4b472be1b25f | 76 | |
irsanjul | 0:4b472be1b25f | 77 | _testvariable = _Buffer[_RequestedByte-1]>>7; |
irsanjul | 0:4b472be1b25f | 78 | |
irsanjul | 0:4b472be1b25f | 79 | return _testvariable; |
irsanjul | 0:4b472be1b25f | 80 | } |
irsanjul | 0:4b472be1b25f | 81 | |
irsanjul | 0:4b472be1b25f | 82 | |
irsanjul | 0:4b472be1b25f | 83 | long MCP3424::Measure() |
irsanjul | 0:4b472be1b25f | 84 | { |
irsanjul | 0:4b472be1b25f | 85 | _resultat=0; |
irsanjul | 0:4b472be1b25f | 86 | |
irsanjul | 0:4b472be1b25f | 87 | while(IsConversionFinished()==1); |
irsanjul | 0:4b472be1b25f | 88 | |
irsanjul | 0:4b472be1b25f | 89 | switch (_resolution) |
irsanjul | 0:4b472be1b25f | 90 | { |
irsanjul | 0:4b472be1b25f | 91 | case 12:{ |
irsanjul | 0:4b472be1b25f | 92 | _Buffer[0]&=0b00001111; |
irsanjul | 0:4b472be1b25f | 93 | _resultat = _Buffer[0]*256 + _Buffer[1]; |
irsanjul | 0:4b472be1b25f | 94 | |
irsanjul | 0:4b472be1b25f | 95 | if(_resultat>2048-1) |
irsanjul | 0:4b472be1b25f | 96 | { |
irsanjul | 0:4b472be1b25f | 97 | _resultat=_resultat-4096-1; |
irsanjul | 0:4b472be1b25f | 98 | } |
irsanjul | 0:4b472be1b25f | 99 | |
irsanjul | 0:4b472be1b25f | 100 | _resultat = _resultat*1000/_PGA; |
irsanjul | 0:4b472be1b25f | 101 | |
irsanjul | 0:4b472be1b25f | 102 | break; |
irsanjul | 0:4b472be1b25f | 103 | } |
irsanjul | 0:4b472be1b25f | 104 | case 14:{ |
irsanjul | 0:4b472be1b25f | 105 | _Buffer[0]&=0b00111111; |
irsanjul | 0:4b472be1b25f | 106 | _resultat = _Buffer[0]*256 + _Buffer[1]; |
irsanjul | 0:4b472be1b25f | 107 | |
irsanjul | 0:4b472be1b25f | 108 | if(_resultat>8192-1) |
irsanjul | 0:4b472be1b25f | 109 | { |
irsanjul | 0:4b472be1b25f | 110 | _resultat=_resultat-16384-1; |
irsanjul | 0:4b472be1b25f | 111 | } |
irsanjul | 0:4b472be1b25f | 112 | |
irsanjul | 0:4b472be1b25f | 113 | _resultat = _resultat*250/_PGA; |
irsanjul | 0:4b472be1b25f | 114 | |
irsanjul | 0:4b472be1b25f | 115 | break; |
irsanjul | 0:4b472be1b25f | 116 | } |
irsanjul | 0:4b472be1b25f | 117 | case 16:{ |
irsanjul | 0:4b472be1b25f | 118 | _resultat = _Buffer[0]*256 + _Buffer[1]; |
irsanjul | 0:4b472be1b25f | 119 | |
irsanjul | 0:4b472be1b25f | 120 | if(_resultat>32768-1) |
irsanjul | 0:4b472be1b25f | 121 | { |
irsanjul | 0:4b472be1b25f | 122 | _resultat=_resultat-65536-1; |
irsanjul | 0:4b472be1b25f | 123 | } |
irsanjul | 0:4b472be1b25f | 124 | |
irsanjul | 0:4b472be1b25f | 125 | _resultat = _resultat*62.5/_PGA; |
irsanjul | 0:4b472be1b25f | 126 | |
irsanjul | 0:4b472be1b25f | 127 | break; |
irsanjul | 0:4b472be1b25f | 128 | } |
irsanjul | 0:4b472be1b25f | 129 | case 18:{ |
irsanjul | 0:4b472be1b25f | 130 | _Buffer[0]&=0b00000011; |
irsanjul | 0:4b472be1b25f | 131 | _resultat = _Buffer[0]*512 + _Buffer[1]*256 + _Buffer[2]; |
irsanjul | 0:4b472be1b25f | 132 | |
irsanjul | 0:4b472be1b25f | 133 | if(_resultat>131072-1) |
irsanjul | 0:4b472be1b25f | 134 | { |
irsanjul | 0:4b472be1b25f | 135 | _resultat=_resultat-262144-1; |
irsanjul | 0:4b472be1b25f | 136 | } |
irsanjul | 0:4b472be1b25f | 137 | |
irsanjul | 0:4b472be1b25f | 138 | _resultat = _resultat*15.625/_PGA; |
irsanjul | 0:4b472be1b25f | 139 | |
irsanjul | 0:4b472be1b25f | 140 | break; |
irsanjul | 0:4b472be1b25f | 141 | } |
irsanjul | 0:4b472be1b25f | 142 | } |
irsanjul | 0:4b472be1b25f | 143 | return _resultat; |
irsanjul | 0:4b472be1b25f | 144 | |
irsanjul | 0:4b472be1b25f | 145 | } |