@ -34,7 +34,7 @@
# if ENABLED(DAC_STEPPER_CURRENT)
# if ENABLED(DAC_STEPPER_CURRENT)
uint16_t mcp4728_values [ 4 ] ;
uint16_t mcp4728_values [ XYZE ] ;
/**
/**
* Begin I2C , get current values ( input register and eeprom ) of mcp4728
* Begin I2C , get current values ( input register and eeprom ) of mcp4728
@ -42,16 +42,13 @@ uint16_t mcp4728_values[4];
void mcp4728_init ( ) {
void mcp4728_init ( ) {
Wire . begin ( ) ;
Wire . begin ( ) ;
Wire . requestFrom ( int ( DAC_DEV_ADDRESS ) , 24 ) ;
Wire . requestFrom ( int ( DAC_DEV_ADDRESS ) , 24 ) ;
while ( Wire . available ( ) ) {
while ( Wire . available ( ) ) {
int deviceID = Wire . read ( ) ;
char deviceID = Wire . read ( ) ,
int hiByte = Wire . read ( ) ;
hiByte = Wire . read ( ) ,
int loByte = Wire . read ( ) ;
loByte = Wire . read ( ) ;
int isEEPROM = ( deviceID & 0B00001000 ) > > 3 ;
if ( ! ( deviceID & 0x08 ) )
int channel = ( deviceID & 0B00110000 ) > > 4 ;
mcp4728_values [ ( deviceID & 0x30 ) > > 4 ] = word ( ( hiByte & 0x0F ) , loByte ) ;
if ( isEEPROM ! = 1 ) {
mcp4728_values [ channel ] = word ( ( hiByte & 0B00001111 ) , loByte ) ;
}
}
}
}
}
@ -71,8 +68,8 @@ uint8_t mcp4728_analogWrite(uint8_t channel, uint16_t value) {
uint8_t mcp4728_eepromWrite ( ) {
uint8_t mcp4728_eepromWrite ( ) {
Wire . beginTransmission ( DAC_DEV_ADDRESS ) ;
Wire . beginTransmission ( DAC_DEV_ADDRESS ) ;
Wire . write ( SEQWRITE ) ;
Wire . write ( SEQWRITE ) ;
for ( uint8_t channel = 0 ; channel < = 3 ; channel + + ) {
for ( uint8_t channel = 0 ; channel < COUNT ( channel ) ; channel + + ) {
Wire . write ( DAC_STEPPER_VREF < < 7 | 0 < < 5 | DAC_STEPPER_GAIN < < 4 | highByte ( mcp4728_values [ channel ] ) ) ;
Wire . write ( DAC_STEPPER_VREF < < 7 | DAC_STEPPER_GAIN < < 4 | highByte ( mcp4728_values [ channel ] ) ) ;
Wire . write ( lowByte ( mcp4728_values [ channel ] ) ) ;
Wire . write ( lowByte ( mcp4728_values [ channel ] ) ) ;
}
}
return Wire . endTransmission ( ) ;
return Wire . endTransmission ( ) ;
@ -83,7 +80,7 @@ uint8_t mcp4728_eepromWrite() {
*/
*/
uint8_t mcp4728_setVref_all ( uint8_t value ) {
uint8_t mcp4728_setVref_all ( uint8_t value ) {
Wire . beginTransmission ( DAC_DEV_ADDRESS ) ;
Wire . beginTransmission ( DAC_DEV_ADDRESS ) ;
Wire . write ( VREFWRITE | value < < 3 | value < < 2 | value < < 1 | value ) ;
Wire . write ( GAINWRITE | ( value ? 0x0F : 0x00 ) ) ;
return Wire . endTransmission ( ) ;
return Wire . endTransmission ( ) ;
}
}
/**
/**
@ -91,7 +88,7 @@ uint8_t mcp4728_setVref_all(uint8_t value) {
*/
*/
uint8_t mcp4728_setGain_all ( uint8_t value ) {
uint8_t mcp4728_setGain_all ( uint8_t value ) {
Wire . beginTransmission ( DAC_DEV_ADDRESS ) ;
Wire . beginTransmission ( DAC_DEV_ADDRESS ) ;
Wire . write ( GAINWRITE | value < < 3 | value < < 2 | value < < 1 | value ) ;
Wire . write ( GAINWRITE | ( value ? 0x0F : 0x00 ) ) ;
return Wire . endTransmission ( ) ;
return Wire . endTransmission ( ) ;
}
}
@ -105,13 +102,22 @@ uint16_t mcp4728_getValue(uint8_t channel) { return mcp4728_values[channel]; }
* Return Vout
* Return Vout
*
*
uint16_t mcp4728_getVout ( uint8_t channel ) {
uint16_t mcp4728_getVout ( uint8_t channel ) {
uint32_t vref = 2048 ;
uint32_t vref = 2048 ,
uint32_t vOut = ( vref * mcp4728_values [ channel ] * ( _DAC_STEPPER_GAIN + 1 ) ) / 4096 ;
vOut = ( vref * mcp4728_values [ channel ] * ( _DAC_STEPPER_GAIN + 1 ) ) / 4096 ;
if ( vOut > defaultVDD ) vOut = defaultVDD ;
if ( vOut > defaultVDD ) vOut = defaultVDD ;
return vOut ;
return vOut ;
}
}
*/
*/
/* Returns DAC values as a 0-100 percentage of drive strength */
uint16_t mcp4728_getDrvPct ( uint8_t channel ) { return uint16_t ( 100.0 * mcp4728_values [ channel ] / ( DAC_STEPPER_MAX ) + 0.5 ) ; }
/* Recieves all Drive strengths as 0-100 percent values, updates DAC Values array and calls fastwrite to update the DAC */
void mcp4728_setDrvPct ( int16_t pct [ XYZE ] ) {
LOOP_XYZE ( i ) mcp4728_values [ i ] = 0.01 * pct [ i ] * ( DAC_STEPPER_MAX ) ;
mcp4728_fastWrite ( ) ;
}
/**
/**
* FastWrite input register values - All DAC ouput update . refer to DATASHEET 5.6 .1
* FastWrite input register values - All DAC ouput update . refer to DATASHEET 5.6 .1
* DAC Input and PowerDown bits update .
* DAC Input and PowerDown bits update .
@ -119,7 +125,7 @@ uint16_t mcp4728_getVout(uint8_t channel) {
*/
*/
uint8_t mcp4728_fastWrite ( ) {
uint8_t mcp4728_fastWrite ( ) {
Wire . beginTransmission ( DAC_DEV_ADDRESS ) ;
Wire . beginTransmission ( DAC_DEV_ADDRESS ) ;
for ( uint8_t channel = 0 ; channel < = 3 ; channel + + ) {
for ( uint8_t channel = 0 ; channel < COUNT ( channel ) ; channel + + ) {
Wire . write ( highByte ( mcp4728_values [ channel ] ) ) ;
Wire . write ( highByte ( mcp4728_values [ channel ] ) ) ;
Wire . write ( lowByte ( mcp4728_values [ channel ] ) ) ;
Wire . write ( lowByte ( mcp4728_values [ channel ] ) ) ;
}
}