Skip to content

Commit 4d0d9e6

Browse files
committed
update.
1 parent b322915 commit 4d0d9e6

File tree

3 files changed

+72
-114
lines changed

3 files changed

+72
-114
lines changed

examples/CS5530_basic/CS5530_basic.ino

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ https://github.com/yasir-shahzad/CS5530
1919

2020
#include "Arduino.h"
2121

22-
#include "CS5530.h"
22+
#include <CS5530.h>
2323
#include "SPI.h"
2424
#include <avr/pgmspace.h>
2525

@@ -34,40 +34,47 @@ void setup()
3434

3535
if (cell.reset())
3636
Serial.println("CS5530 Initialized Successfully");
37-
else
38-
Serial.println("Starting CS5530 failed");
37+
else {
38+
Serial.println("CS5530 Initialization Failed");
39+
while (1);
40+
}
41+
3942

4043
// cell.CS5530_Write_Reg(CMD_GAIN_WRITE, 0x3);
4144

42-
uint32_t tmp = cell.getRegister(ConfigRead);
45+
// uint32_t tmp = cell.getRegister(Command::ConfigRead);
46+
uint32_t tmp = cell.getRegister(static_cast<uint8_t>(Command::ConfigRead));
47+
4348
Serial.print("CONFIG Register:");
4449
Serial.println(tmp, BIN);
4550

4651
// uint32_t tmpdata = REG_CONFIG_UNIPOLAR | REG
4752

48-
cell.setRegister(ConfigWrite, CS5530_UNIPOLAR);
53+
cell.setRegister(static_cast<uint8_t>(Command::ConfigWrite), CS5530_UNIPOLAR);
4954

50-
cell.convert(CONTINUED_CONVERSION, 1, 1, (int)WORD_RATE_3200SPS);
55+
// cell.convert(CONTINUED_CONVERSION, 1, 1, (int)WORD_RATE_3200SPS);
5156

52-
uint32_t cmpl = cell.calculateTwoComplement(0xFFFFFFFF);
57+
uint32_t cmpl = cell.calculateTwoComplement(0xFFFFFFFF);
5358

54-
cell.writeByte(CMD_CONVERSION_CONTINU);
55-
cell.setRegister(CMD_OFFSET_WRITE, cmpl);
59+
//cell.writeByte(ContinuousConversion);
60+
cell.setRegister(OffsetWrite, cmpl);
5661
}
5762

5863
void loop()
5964
{
6065
int32_t recData = cell.getReading();
6166

67+
Serial.println(recData);
6268
if (recData > 0)
6369
{
6470
value = 0.97 * value + 0.03 * recData; // running average
65-
delay(5);
71+
Serial.println(value);
72+
// delay(5);
6673
}
6774

6875
if (millis() > startTime)
6976
{
70-
Serial.println(value);
77+
7178
// Serial.println (String((value-111683)/18) + " grms");
7279
startTime = millis() + 200;
7380
}

src/CS5530.cpp

Lines changed: 42 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@
88

99
CS5530::CS5530():
1010
_spiSettings(CS5530_DEFAULT_SPI_FREQUENCY, MSBFIRST, SPI_MODE0),
11-
_spi(&CS5530_DEFAULT_SPI), _ss(CS5530_SS)
11+
_spi(&CS5530_DEFAULT_SPI), _chipSelectPin(CS5530_SS)
1212
{
1313
_spi->begin();
1414
}
1515

1616
int CS5530::begin()
1717
{
18-
pinMode(_ss, OUTPUT);
19-
digitalWrite(_ss, HIGH);
18+
pinMode(_chipSelectPin, OUTPUT);
19+
digitalWrite(_chipSelectPin, HIGH);
2020
_spi->begin();
2121

2222
return 1;
2323
}
2424

2525
void CS5530::setPin(int ss)
2626
{
27-
_ss = ss;
27+
_chipSelectPin = ss;
2828
}
2929

3030
void CS5530::setSPI(SPIClass &spi)
@@ -54,7 +54,7 @@ bool CS5530::reset(void)
5454

5555
tmp = getRegister(ConfigRead);
5656
//valid reset will set RV to 1
57-
if (tmp & REG_CONFIG_RV) {
57+
if (tmp & RESET_VALID) {
5858
return true;
5959
}
6060

@@ -66,33 +66,31 @@ bool CS5530::reset(void)
6666
//x1, 2, 4, 8, 16, 32, 64 are avaialable
6767
bool CS5530::setGain(uint8_t gainValue)
6868
{
69-
if (gainValue > 1 << 6)
70-
gainValue = 1 << 6; //Error check
69+
if (gainValue > 1 << 6)
70+
gainValue = 1 << 6; // Error check
7171

72-
// Clear gain bits (bits 12, 13, 14, and 15)
73-
registerValue &= ~(0b1111 << 12);
72+
uint32_t value = getRegister(GainRead);
73+
// Clear gain bits (bits 12, 13, 14, and 15)
74+
value &= ~(0b1111 << 12);
75+
// Mask in new bits
76+
value |= (static_cast<uint32_t>(gainValue) << 12);
7477

75-
//Mask in new bits
76-
registerValue |= (static_cast<uint32_t>(gainValue) << 12);
77-
78-
// Return true to indicate success
79-
return true;
80-
//TODO:
81-
// return (setRegister(NAU7802_CTRL1, value));
78+
// Return true to indicate success
79+
return (setRegister(GainWrite, value));
8280
}
8381

8482
//Set the readings per second
8583
//10, 20, 40, 80, and 320 samples per second is available
86-
bool CS5530::setSampleRate(uint8_t rate)
84+
bool CS5530::setSampleRate(uint32_t rate)
8785
{
8886
if (rate > 0b111)
8987
rate = 0b111; //Error check
9088

91-
uint8_t value = getRegister(NAU7802_CTRL2);
89+
uint32_t value = getRegister(ConfigRead);
9290
value &= 0b10001111; //Clear CRS bits
93-
value |= rate << 4; //Mask in new CRS bits
91+
value |= rate << 11; //Mask in new CRS bits
9492

95-
return (setRegister(NAU7802_CTRL2, value));
93+
return (setRegister(ConfigWrite, value));
9694
}
9795

9896
//Call when scale is setup, level, at running temperature, with nothing on it
@@ -134,7 +132,7 @@ float CS5530::getCalibrationFactor()
134132

135133

136134
//Returns the y of y = mx + b using the current weight on scale, the cal factor, and the offset.
137-
float -CS5530::getWeight(bool allowNegativeWeights, uint8_t samplesToTake)
135+
float CS5530::getWeight(bool allowNegativeWeights, uint8_t samplesToTake)
138136
{
139137
int32_t onScale = getAverage(samplesToTake);
140138

@@ -151,10 +149,10 @@ float -CS5530::getWeight(bool allowNegativeWeights, uint8_t samplesToTake)
151149
return (weight);
152150
}
153151

154-
bool CS5530::setRegister(uint8_t reg, uint32_t dat)
152+
bool CS5530::setRegister(uint8_t command, uint32_t value)
155153
{
156-
writeByte(reg);
157-
write32(dat);
154+
writeByte(command);
155+
write32(value);
158156
return true;
159157
}
160158

@@ -225,117 +223,65 @@ bool CS5530::getBit(uint8_t bitNumber, uint8_t registerAddress)
225223
// }
226224

227225
void CS5530::writeByte(uint8_t data) {
228-
digitalWrite(_ss, LOW);
226+
digitalWrite(_chipSelectPin, LOW);
229227
_spi->beginTransaction(_spiSettings);
230-
_spi->transfer(data);
228+
_spi->transfer(data & 0xFF);
231229
_spi->endTransaction();
232-
digitalWrite(_ss, HIGH);
230+
digitalWrite(_chipSelectPin, HIGH);
233231
}
234232

235-
236-
// void CS5530::write32(uint32_t dat) {
237-
// uint8_t i;
238-
// uint8_t tmp;
239-
240-
// for(i=3; i>=0; i--) {
241-
// tmp = (uint8_t)( (dat >> (8*i)) & 0xff);
242-
// writeByte(tmp);
243-
// }
244-
// }
245-
246233
void CS5530::write32(uint32_t dat)
247234
{
248235
for (int8_t i = 3; i >= 0; i--) {
249236
writeByte(static_cast<uint8_t>((dat >> (8 * i)) & 0xFF));
250237
}
251238
}
252239

253-
// uint32_t CS5530::getRegister(uint8_t reg) {
254-
// uint32_t dat;
255-
// writeByte(reg);
256-
// dat = read32();
257-
258-
// return dat;
259-
// }
260-
261-
uint32_t CS5530::getRegister(uint8_t reg) {
262-
writeByte(reg);
240+
uint32_t CS5530::getRegister(uint8_t command) {
241+
writeByte(command);
263242
return read32();
264243
}
265244

266-
267-
// uint32_t CS5530::read32(void) {
268-
// uint32_t dat = 0;
269-
270-
// for (int i = 0; i < 4; i++) {
271-
// dat = (dat << 8) | readByte();
272-
// }
273-
274-
// return dat;
275-
// }
276-
277245
uint32_t CS5530::read32(void) {
278246
uint32_t result = 0;
279-
247+
280248
for (int i = 0; i < 4; i++) {
281-
result |= (static_cast<uint32_t>(readByte()) << (8 * i));
249+
result = (result << 8) | readByte();
282250
}
283-
251+
284252
return result;
285253
}
286254

287-
288-
// uint8_t CS5530::readByte(void) {
289-
// uint8_t dat = 0;
290-
// digitalWrite(_ss, LOW);
291-
// _spi->beginTransaction(_spiSettings);
292-
// dat = SPI.transfer(CMD_NULL);
293-
// _spi->endTransaction();
294-
// digitalWrite(_ss, HIGH);
295-
296-
// return dat;
297-
// }
298-
299-
300255
uint8_t CS5530::readByte() {
301256
uint8_t data = 0;
302257

303258
_spi->beginTransaction(_spiSettings);
304259
digitalWrite(_chipSelectPin, LOW);
305-
data = _spi->transfer(CMD_NULL);
260+
data = _spi->transfer(Null);
306261
digitalWrite(_chipSelectPin, HIGH);
307262
_spi->endTransaction();
308263

309264
return data;
310265
}
311266

312-
313267
bool CS5530::available(void)
314268
{
315269
if(digitalRead(12) == 0)
316-
{
317270
return true;
318-
}
319271

320272
return false;
321273
}
322274

323-
uint32_t CS5530::getReading()
275+
int32_t CS5530::getReading()
324276
{
325277
if (!available())
326-
{
327-
// Return -2 for busy status
328-
return -2;
329-
}
330-
331-
uint32_t rec_data = getRegister(CMD_NULL);
332-
if ((rec_data & REG_DATA_OF) == 0)
333-
{
278+
return -2; // Return -2 for busy status
279+
280+
uint32_t recData = getRegister(Null);
281+
if ((recData & REG_DATA_OF) == 0) {
334282
// Perform sign extension and return the result
335-
return static_cast<int32_t>(rec_data << 24) >> 24;
336-
}
337-
else
338-
{
283+
return static_cast<int32_t>(recData << 24) >> 24;
284+
} else {
339285
// Return -1 for overflow status
340286
return -1;
341287
}
@@ -426,14 +372,14 @@ uint8_t CS5530::convert(uint8_t convertType, uint8_t regNo, int wordRate)
426372
break;
427373
}
428374

429-
setRegister(CMD_WRITE_CFG_REG, cfgReg);
375+
setRegister(ConfigWrite, cfgReg);
430376
writeByte(cmd);
431377
printf("Conversion begins...\n");
432378

433-
sleep(10);
379+
delayMicroseconds(10); // waste some time
434380
u8 test = 0;
435-
read_byte(&test, 1);
436-
read_byte(convertResult, 4);
381+
// readByte(&test, 1);
382+
// readByte(convertResult, 4);
437383

438384
Serial.print("The raw result is:");
439385
Serial.println(finalResult, BIN);

src/CS5530.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#ifndef CS5530_H
1212
#define CS5530_H
1313

14+
#include <SPI.h>
15+
1416

1517
#define SYSTEM_OFFSET 3
1618
#define SYSTEM_GAIN 4
@@ -48,7 +50,7 @@ typedef enum {
4850
* @note This enumeration is based on the Tic Stepper Motor Controller User's Guide, available at
4951
*
5052
*/
51-
enum class Command : uint8_t
53+
typedef enum
5254
{
5355
OffsetRead = 0x09, ///< Read the offset value.
5456
OffsetWrite = 0x01, ///< Write the offset value.
@@ -63,7 +65,7 @@ enum class Command : uint8_t
6365
Sync1 = 0xFF, ///< Part of the serial port re-initialization sequence.
6466
Sync0 = 0xFE, ///< End of the serial port re-initialization sequence.
6567
Null = 0x00 ///< Clear a port flag and keep the converter in continuous conversion mode.
66-
};
68+
} Command;
6769

6870
typedef enum
6971
{
@@ -150,10 +152,10 @@ class CS5530
150152
float getWeight(bool allowNegativeWeights = false, uint8_t samplesToTake = 8); //Once you've set zero offset and cal factor, you can ask the library to do the calculations for you.
151153

152154
bool setGain(uint8_t gainValue); // Set the gain. x1, 2, 4, 8, 16, 32, 64, 128 are available
153-
bool setSampleRate(uint8_t rate); // Set the readings per second. 10, 20, 40, 80, and 320 samples per second is available
155+
bool setSampleRate(uint32_t rate); // Set the readings per second. 10, 20, 40, 80, and 320 samples per second is available
154156

155-
uint8_t getRegister(uint8_t registerAddress); //Get contents of a register
156-
bool setRegister(uint8_t registerAddress, uint32_t value); //Send a given value to be written to given address. Return true if successful
157+
uint32_t getRegister(uint8_t commnad); //Get contents of a register
158+
bool setRegister(uint8_t command, uint32_t value); //Send a given value to be written to given address. Return true if successful
157159

158160
uint8_t convert(uint8_t convertType, uint8_t regNo, int wordRate);
159161
uint8_t calibrate(uint8_t calibrateType, int cfgReg);
@@ -167,10 +169,13 @@ class CS5530
167169
private:
168170
SPISettings _spiSettings;
169171
SPIClass* _spi;
170-
int _ss;
172+
int _chipSelectPin;
173+
//y = mx+b
174+
int32_t _zeroOffset; //This is b
175+
float _calibrationFactor; //This is m. User provides this number so that we can output y when requested
171176

172177
uint8_t readByte();
173-
int32_t read32();
178+
uint32_t read32();
174179
void write32(uint32_t value);
175180
void writeByte(uint8_t value);
176181

0 commit comments

Comments
 (0)