MCPcopy Create free account
hub / github.com/RobTillaart/Arduino / analogRead

Method analogRead

libraries/PCF8591/PCF8591.cpp:91–140  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

89
90
91uint8_t PCF8591::analogRead(uint8_t channel, uint8_t mode)
92{
93 if (mode > 3)
94 {
95 _error = PCF8591_MODE_ERROR;
96 return 0;
97 }
98 _control &= 0b01000100; // clear all except flags
99 _control |= (mode << 4);
100
101 _error = PCF8591_CHANNEL_ERROR;
102 switch(mode)
103 {
104 case 0:
105 if (channel > 3) return 0;
106 _control |= channel;
107 break;
108 case 1:
109 if (channel > 2) return 0;
110 _control |= (channel << 4);
111 break;
112 case 2:
113 if (channel > 2) return 0;
114 _control |= (channel << 4);
115 break;
116 case 3:
117 if (channel > 1) return 0;
118 _control |= (channel << 4);
119 break;
120 default:
121 return 0;
122 }
123 _error = PCF8591_OK;
124
125 // NOTE: one must read two values to get an up to date value.
126 // Page 8 datasheet.
127 _wire->beginTransmission(_address);
128 _wire->write(_control);
129 _error = _wire->endTransmission(); // default == 0 == PCF8591_OK
130 if (_error != 0) return PCF8591_I2C_ERROR;
131
132 if (_wire->requestFrom(_address, (uint8_t)2) != 2)
133 {
134 _error = PCF8591_I2C_ERROR;
135 return _adc[channel]; // known last value
136 }
137 _wire->read();
138 _adc[channel] = _wire->read();
139 return _adc[channel];
140}
141
142
143uint8_t PCF8591::analogRead4()

Callers

nothing calls this directly

Calls 2

writeMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected