node-red-contrib-waveshare-shield-adda-11010
Loading...
Searching...
No Matches
ads1256 Namespace Reference

Functions

 output_json ()
 
 parse_args (raw_args)
 
 rdata ()
 
 read_fsc_reg ()
 
 read_ofc_reg ()
 
 rreg (register)
 
 selfcal ()
 
 sync ()
 
 twos_complement_to_integer_24bit (twos_complement)
 
 wakeup ()
 
 wreg (register, data)
 
 write_adcon_reg ()
 
 write_drate_reg ()
 
 write_mux_reg ()
 
 write_status_reg ()
 

Variables

int cs = 22
 
int drdy = 17
 
 max_speed_hz
 
str mode = 'analog'
 
int output = -1
 
 register_address
 
 register_data
 
 spi = spidev.SpiDev()
 

Function Documentation

◆ output_json()

ads1256.output_json ( )
Prints a JSON of the register_data for development and testing.

Definition at line 147 of file ads1256.py.

147def output_json():
148 """Prints a JSON of the register_data for development and testing."""
149 print(json.dumps(register_data, sort_keys=True, indent=4))
150
151

◆ parse_args()

ads1256.parse_args ( raw_args)
Parses the args into ready to write register_data.

Definition at line 152 of file ads1256.py.

152def parse_args(raw_args):
153 """Parses the args into ready to write register_data."""
154 # First parse the CLI.
155 parser = argparse.ArgumentParser()
156 parser.add_argument("--mode", type=str, default="analog",
157 help="Mode: analog, calibrationFSC, calibrationOFC, digitalIn, digitalOut default analog")
158 parser.add_argument("--port_p", type=str, default="0",
159 help="Positive Analog Input Port: 0 thru 8 default 0")
160 parser.add_argument("--port_n", type=str, default="8",
161 help="Negative Analog Input Port: 0 thru 8 default 8 (AINCOM)")
162 parser.add_argument("--gain", type=str, default="1",
163 help="Input Amp Gain: 1, 2, 4, 8, 16, 32, 64 default 1")
164 parser.add_argument("--rate", type=str, default="2.5",
165 help="Data Rate in samples per second: 2.5, 5, 10, 15, 25, 30, 50, 60, 100, 500, 1000, 2000, 3750, 7500, 15000, 30000 default 2.5")
166 parser.add_argument("--buffer", type=str, default="off",
167 help="Input Buffer: off, on default off")
168 parser.add_argument("--sdcs", type=str, default="off",
169 help="SDCS sensor direct current source microamps: off, 0.5, 2, 10 default off")
170 parser.add_argument("--pin", type=str, default="D0",
171 help="Specify pin for modes digitalIn and digitalOut: D0, D1, D2, D3 default D0")
172 parser.add_argument("--output", type=str, default="-1",
173 help="Absence means input mode, or specify output for modes digitalIn and digitalOut: 0, 1 default input mode")
174 parser.add_argument("-t", "--test", action="store_true",
175 help="Print JSON of register_data")
176 args = parser.parse_args(raw_args)
177
178 # Save global configs
179 global mode
180 mode = args.mode
181 global output
182 output = args.output
183
184 # Process the STATUS register, including the BUFEN analog input buffer property.
185 register_data['status'] = 0x00
186 if (args.buffer == 'on'):
187 register_data['status'] = 0x02
188 if (args.buffer == 'off'):
189 register_data['status'] = 0x00
190
191 # Process the ADCON register, including PGA gain and SDCS property.
192 register_data['adcon'] = 0x20 # Default clock out rate setting
193 if (args.gain == '1'):
194 register_data['adcon'] += 0x00
195 if (args.gain == '2'):
196 register_data['adcon'] += 0x01
197 if (args.gain == '4'):
198 register_data['adcon'] += 0x02
199 if (args.gain == '8'):
200 register_data['adcon'] += 0x03
201 if (args.gain == '16'):
202 register_data['adcon'] += 0x04
203 if (args.gain == '32'):
204 register_data['adcon'] += 0x05
205 if (args.gain == '64'):
206 register_data['adcon'] += 0x06
207 if (args.sdcs == 'off'):
208 register_data['adcon'] += 0x00
209 if (args.sdcs == '0.5'):
210 register_data['adcon'] += 0x08
211 if (args.sdcs == '2'):
212 register_data['adcon'] += 0x10
213 if (args.sdcs == '10'):
214 register_data['adcon'] += 0x18
215
216 # Process the DRATE register, including the rate property.
217 register_data['drate'] = 0x03
218 if (args.rate == '30000'):
219 register_data['drate'] = 0xF0
220 if (args.rate == '15000'):
221 register_data['drate'] = 0xE0
222 if (args.rate == '7500'):
223 register_data['drate'] = 0xD0
224 if (args.rate == '3750'):
225 register_data['drate'] = 0xC0
226 if (args.rate == '2000'):
227 register_data['drate'] = 0xB0
228 if (args.rate == '1000'):
229 register_data['drate'] = 0xA1
230 if (args.rate == '500'):
231 register_data['drate'] = 0x92
232 if (args.rate == '100'):
233 register_data['drate'] = 0x82
234 if (args.rate == '60'):
235 register_data['drate'] = 0x72
236 if (args.rate == '50'):
237 register_data['drate'] = 0x63
238 if (args.rate == '30'):
239 register_data['drate'] = 0x53
240 if (args.rate == '25'):
241 register_data['drate'] = 0x43
242 if (args.rate == '15'):
243 register_data['drate'] = 0x33
244 if (args.rate == '10'):
245 register_data['drate'] = 0x23
246 if (args.rate == '5'):
247 register_data['drate'] = 0x13
248 if (args.rate == '2.5'):
249 register_data['drate'] = 0x03
250
251 # Process the MUX register, including the port.
252 # register_data['mux'] = 0x08
253 register_data['mux'] = (int(args.port_p) << 4) | int(args.port_n)
254
255 # Process the gpio_direction_bitmask and gpio_status_bitmask registers, including the pin property.
256 register_data['gpio_direction_bitmask'] = 0x10
257 register_data['gpio_status_bitmask'] = 0x01
258 if (args.pin == 'D0'):
259 register_data['gpio_direction_bitmask'] = 0x10
260 register_data['gpio_status_bitmask'] = 0x01
261 if (args.pin == 'D1'):
262 register_data['gpio_direction_bitmask'] = 0x20
263 register_data['gpio_status_bitmask'] = 0x02
264 if (args.pin == 'D2'):
265 register_data['gpio_direction_bitmask'] = 0x40
266 register_data['gpio_status_bitmask'] = 0x04
267 if (args.pin == 'D3'):
268 register_data['gpio_direction_bitmask'] = 0x80
269 register_data['gpio_status_bitmask'] = 0x08
270
271 # Optionally output register state for development purposes.
272 if args.test:
273 output_json()
274 exit()
275
276
277# Functions that read or write to the device.
278# Device commands are run by functions named after the datasheet provided device command names.
279# The datasheet names are all caps, Python names are lowercase, other than that issue, they match.
280# The complete list of commands is on datasheet page 34 table 24.
281
282

◆ rdata()

ads1256.rdata ( )
Run RDATA command as explained on datasheet page 34.

Definition at line 313 of file ads1256.py.

313def rdata():
314 """Run RDATA command as explained on datasheet page 34."""
315 while True:
316 if (GPIO.input(drdy) == 0):
317 break
318 GPIO.output(cs, 0)
319 spi.writebytes([0x01])
320 buf = spi.readbytes(3)
321 GPIO.output(cs, 1)
322 read = (buf[0] << 16) & 0xff0000
323 read |= (buf[1] << 8) & 0xff00
324 read |= (buf[2]) & 0xff
325 return twos_complement_to_integer_24bit(read)
326
327

◆ read_fsc_reg()

ads1256.read_fsc_reg ( )
Read the FSC calibration value.

Definition at line 342 of file ads1256.py.

342def read_fsc_reg():
343 """Read the FSC calibration value."""
344 # See datasheet page 33 to read the raw byte values.
345 # FSC per datasheet pg 24 is an unsigned 24 bit integer.
346 register_data['fsc'] = 0x000000
347 register_data['fsc'] += (rreg(register_address['fsc2']) << 16)
348 register_data['fsc'] += (rreg(register_address['fsc1']) << 8)
349 register_data['fsc'] += rreg(register_address['fsc0'])
350
351

◆ read_ofc_reg()

ads1256.read_ofc_reg ( )
Read the OFC calibration value.

Definition at line 352 of file ads1256.py.

352def read_ofc_reg():
353 """Read the OFC calibration value."""
354 # See datasheet page 33 to read the raw byte values.
355 # OFC per datasheet pg 24 is a twos complement 24 bit signed integer.
356 ofc_twos_complement = 0x000000
357 ofc_twos_complement += (rreg(register_address['ofc2']) << 16)
358 ofc_twos_complement += (rreg(register_address['ofc1']) << 8)
359 ofc_twos_complement += rreg(register_address['ofc0'])
360 register_data['ofc'] = twos_complement_to_integer_24bit(ofc_twos_complement)
361
362

◆ rreg()

ads1256.rreg ( register)
Run RREG command as explained on datasheet page 36.

Definition at line 283 of file ads1256.py.

283def rreg(register):
284 """Run RREG command as explained on datasheet page 36."""
285 GPIO.output(cs, 0)
286 firstbyte = 0x10 + register
287 secondbyte = 0x01
288 spi.writebytes([firstbyte, secondbyte])
289 data = spi.readbytes(1)
290 GPIO.output(cs, 1)
291 return data[0]
292
293

◆ selfcal()

ads1256.selfcal ( )
Run SELFCAL command as explained on datasheet page 36.

Definition at line 303 of file ads1256.py.

303def selfcal():
304 """Run SELFCAL command as explained on datasheet page 36."""
305 GPIO.output(cs, 0)
306 spi.writebytes([0xf0])
307 GPIO.output(cs, 1)
308 while True:
309 if (GPIO.input(drdy) == 0):
310 break
311
312

◆ sync()

ads1256.sync ( )
Run SYNC command as explained on datasheet page 37.

Definition at line 328 of file ads1256.py.

328def sync():
329 """Run SYNC command as explained on datasheet page 37."""
330 GPIO.output(cs, 0)
331 spi.writebytes([0xfc])
332 GPIO.output(cs, 1)
333
334

◆ twos_complement_to_integer_24bit()

ads1256.twos_complement_to_integer_24bit ( twos_complement)
Convert a 24 bit twos complement value to a plain Python integer.

Definition at line 386 of file ads1256.py.

386def twos_complement_to_integer_24bit(twos_complement):
387 """Convert a 24 bit twos complement value to a plain Python integer."""
388 result = twos_complement
389 if (twos_complement & (1 << 23)):
390 result = twos_complement & ~(1 << 24)
391 result = -(1 << 24) + result
392 return result
393
394

◆ wakeup()

ads1256.wakeup ( )
Run WAKEUP command as explained on datasheet page 37.

Definition at line 335 of file ads1256.py.

335def wakeup():
336 """Run WAKEUP command as explained on datasheet page 37."""
337 GPIO.output(cs, 0)
338 spi.writebytes([0xff])
339 GPIO.output(cs, 1)
340
341

◆ wreg()

ads1256.wreg ( register,
data )
Run WREG command as explained on datasheet page 36.

Definition at line 294 of file ads1256.py.

294def wreg(register, data):
295 """Run WREG command as explained on datasheet page 36."""
296 GPIO.output(cs, 0)
297 firstbyte = 0x50 + register
298 secondbyte = 0x01
299 spi.writebytes([firstbyte, secondbyte, data])
300 GPIO.output(cs, 1)
301
302

◆ write_adcon_reg()

ads1256.write_adcon_reg ( )
Write the ADCON register.  See datasheet page 31.

Definition at line 363 of file ads1256.py.

363def write_adcon_reg():
364 """Write the ADCON register. See datasheet page 31."""
365 wreg(register_address['adcon'], register_data['adcon'])
366
367

◆ write_drate_reg()

ads1256.write_drate_reg ( )
Write the DRATE register.  See datasheet page 32.

Definition at line 368 of file ads1256.py.

368def write_drate_reg():
369 """Write the DRATE register. See datasheet page 32."""
370 wreg(register_address['drate'], register_data['drate'])
371
372

◆ write_mux_reg()

ads1256.write_mux_reg ( )
Write the MUX register.  See datasheet page 31.

Definition at line 373 of file ads1256.py.

373def write_mux_reg():
374 """Write the MUX register. See datasheet page 31."""
375 wreg(register_address['mux'], register_data['mux'])
376
377

◆ write_status_reg()

ads1256.write_status_reg ( )
Write the STATUS register.  See datasheet page 30.

Definition at line 378 of file ads1256.py.

378def write_status_reg():
379 """Write the STATUS register. See datasheet page 30."""
380 wreg(register_address['status'], register_data['status'])
381
382
383# Functions that convert
384
385

Variable Documentation

◆ cs

int ads1256.cs = 22

Definition at line 114 of file ads1256.py.

◆ drdy

int ads1256.drdy = 17

Definition at line 115 of file ads1256.py.

◆ max_speed_hz

ads1256.max_speed_hz

Definition at line 406 of file ads1256.py.

◆ mode

ads1256.mode = 'analog'

Definition at line 118 of file ads1256.py.

◆ output

int ads1256.output = -1

Definition at line 120 of file ads1256.py.

◆ register_address

ads1256.register_address
Initial value:
1= dict([('status', 0x00),
2 ('mux', 0x01),
3 ('adcon', 0x02),
4 ('drate', 0x03),
5 ('gpio', 0x04),
6 ('ofc0', 0x05),
7 ('ofc1', 0x06),
8 ('ofc2', 0x07),
9 ('fsc0', 0x08),
10 ('fsc1', 0x09),
11 ('fsc2', 0x0a)])

Definition at line 122 of file ads1256.py.

◆ register_data

ads1256.register_data
Initial value:
1= dict([('adcon', 0x00),
2 ('drate', 0x00),
3 ('fsc', 0x000000),
4 ('gpio_direction_bitmask', 0x00),
5 ('gpio_status_bitmask', 0x00),
6 ("mux", 0x00),
7 ('ofc', 0x000000),
8 ('status', 0x00)])

Definition at line 134 of file ads1256.py.

◆ spi

ads1256.spi = spidev.SpiDev()

Definition at line 404 of file ads1256.py.