Skip to content

Commit c208679

Browse files
committed
mcp4728: Add initial support for the mcp4728 i2c dac chip
Signed-off-by: Kevin O'Connor <[email protected]>
1 parent 0da064c commit c208679

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

config/example-extras.cfg

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,36 @@
639639
# to not scale the 'wiper_x' parameters.
640640

641641

642+
# Statically configured MCP4728 digital-to-analog converter connected
643+
# via I2C bus (one may define any number of sections with an "mcp4728"
644+
# prefix).
645+
#[mcp4728 my_dac]
646+
#i2c_mcu: mcu
647+
# The name of the micro-controller that the MCP4451 chip is
648+
# connected to. The default is "mcu".
649+
#i2c_address:
650+
# The i2c address that the chip is using on the i2c bus. The default
651+
# is zero.
652+
#channel_a:
653+
#channel_b:
654+
#channel_c:
655+
#channel_d:
656+
# The value to statically set the given MCP4728 channel to. This is
657+
# typically set to a number between 0.0 and 1.0 with 1.0 being the
658+
# highest voltage and 0.0 being the lowest voltage. However, the
659+
# range may be changed with the 'scale' parameter (see below). If a
660+
# channel is not specified then it is left unconfigured.
661+
#scale:
662+
# This parameter can be used to alter how the 'channel_x' parameters
663+
# are interpreted. If provided, then the 'channel_x' parameters
664+
# should be between 0.0 and 'scale'. This may be useful when the
665+
# MCP4728 is used to set stepper voltage references. The 'scale' can
666+
# be set to the equivalent stepper amperage if the MCP4728 were at
667+
# its highest voltage, and then the 'channel_x' parameters can be
668+
# specified using the desired amperage value for the stepper. The
669+
# default is to not scale the 'channel_x' parameters.
670+
671+
642672
# Configure an SX1509 I2C to GPIO expander. Due to the delay incurred
643673
# by I2C communication you should NOT use SX1509 pins as stepper enable,
644674
# step or dir pins or any other pin that requires fast bit-banging. They

config/generic-printrboard.cfg

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,11 @@ max_velocity: 300
7474
max_accel: 3000
7575
max_z_velocity: 5
7676
max_z_accel: 100
77+
78+
# Use the following on a Printrboard RevF to control stepper current.
79+
#[mcp4728 stepper_current_dac]
80+
#scale: 2.327
81+
#channel_a: 1.3 # Extruder
82+
#channel_b: 1.1 # stepper_z
83+
#channel_c: 1.1 # stepper_y
84+
#channel_d: 1.1 # stepper_x

klippy/extras/mcp4728.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# MCP4728 dac code
2+
#
3+
# Copyright (C) 2018 Kevin O'Connor <[email protected]>
4+
#
5+
# This file may be distributed under the terms of the GNU GPLv3 license.
6+
import bus
7+
8+
class mcp4728:
9+
def __init__(self, config):
10+
self.i2c = bus.MCU_I2C_from_config(config, default_addr=0)
11+
scale = config.getfloat('scale', 1., above=0.)
12+
# Configure registers
13+
for i, name in enumerate('abcd'):
14+
val = config.getfloat('channel_%s' % (name,), None,
15+
minval=0., maxval=scale)
16+
if val is not None:
17+
self.set_dac(i, int(val * 4095. / scale + .5))
18+
def set_dac(self, dac, value):
19+
self.i2c.i2c_write([0x40 | (dac << 1),
20+
(value >> 8) & 0x0f, value & 0xff])
21+
22+
def load_config_prefix(config):
23+
return mcp4728(config)

0 commit comments

Comments
 (0)