Using a double-DAC MAX519, with I2C

nbw

Senior Member
#1
Hi everyone, I have a simple project that I haven't quite managed to get working - I did a search on similar Maxim chips (517,518) but can't get sample code working there either.

It's a simple project, a large brass and copper art piece in the form of a goldfish. The electronic part is the eye, an RGB 1W LED - the colour of which I want to change depending on the room temperature. If it's a bit cold, say 14 deg C, the colour would be purple - blue and red LEDs on strong, no green. Over 28, just the red. In between, a mix of the colours.

To do that, I thought I'd use the PWM of my 14M2 for one LED, and a MAX519 dual DAC I found in my box of things for the other two. It's I2C.

The 519 has four AD points that you tie high to low, depending on the address you want to give. I have all mine set to high, so the address is %01011110 (the 010 leader is standard for these DAC chips, plus LSB = 0).

On the 2 DAC outputs, I have simple 5mm red and green LEDs with 470 ohm resistors to ground. These are just for testing so I can see if it's working.

I can't seem to get the DACs to work. The code is quite simple really (from what I see) - simply call the i2c address, then pass a 00,value or 01,value depending on which DAC channel I want. I've tried slow and fast i2c, also cranking up the internal 14m2 speed to 8 Mhz.

I've checked that pin 4 of the DAC is to 0V, and pin 12 to 5V. I have a 0.1uF cap across these pins. SCL and SDA are connected correctly, with 4k7 pull-up resistors.

I suspect there's something so simple, I've just missed it in a senior moment, perhaps. The picaxe itself is working fine, the other pins happily do what they should. Temp_read is just a byte which ultimately will have the temperature.

temp_read = 1
i2cslave %01011110, i2cfast, i2cbyte ' fire up the DAC MAX519
pause 50


max519_test:
writei2c $00,(temp_read) 'DAC Channel 0

b6 = 255 - temp_read
writei2c $01,(b6) 'DAC Channel 1

wait 2
temp_read = temp_read + 1

goto max519_test


... any suggestions?
I tried to attach the MAX519 datasheet here to save suggesters having to download it, but it's over 1Mb, so it failed. Here's the link though:

https://datasheets.maximintegrated.com/en/ds/MAX517-MAX519.pdf
 

premelec

Senior Member
#2
There are 4 PWMOUTs on 14M2 - you could use those straight with current driver. to LED.. You also might consider using a cluster of APA102s either in series or parallel - RGB LEDs proximity in these would help blend color. I have used other TI 8 channel DACs into FETs for current drivers to LEDs [using OP amps] but have no experience with the MAX parts.
 

nbw

Senior Member
#3
Oh, well I've learned something new today. I thought there was only one PWM module, but connected to a variety of pins?!!? But, checking... yes, I'm out of date. Using 3 of the PWMs on the 14m2 would work well, I have enough pins I/O for the rest. Would be interested to know what I'm doing wrong with the i2c and 519, but you've got me out of jail on this one! thanks Premelec
 

nbw

Senior Member
#4
Hmm odd. I connected three LEDs with current limiting resistors to PWM outputs B.2, B.4, C.2 on my 14m2. The ones on B.2 and B.4 function fine. The one on C.2 flashes when I PWM it - even with the other two out of the code. I'll check in the Manual if there's a limit on how many I can use.

#picaxe 14m2
#no_data

' initialise / define variables

symbol ORANGE = b.4
symbol GREEN = b.2
symbol RED = c.2

pwmout ORANGE, 49, 0
pwmout GREEN, 49, 0
pwmout RED, 49, 0

test:
for b0 = 5 to 195 step 5
pwmout GREEN, 49, b0
pwmout ORANGE, 49, b1
pwmout RED, 49, b0
pause 200
next

end

--> I actually somehow removed a line (not important here I think - where I said b1 = 200 - b0, so when 2 LEDs were ramping up, one would be ramping down.
 
Last edited:

nbw

Senior Member
#5
Also tried it on C.0. Noticed something interesting. In the above code loop the red LED flashes slowly at the start of the loop, then noticeably speeds up towards the end.
 

nbw

Senior Member
#6
Doing some research, multiple pwms has caused people issues in the past... I'm looking for some "simple" hpwm code to see if I can sneak a third pwm on my 14m2 that way.........
 

Hemi345

Senior Member
#9
Doing some research, multiple pwms has caused people issues in the past... I'm looking for some "simple" hpwm code to see if I can sneak a third pwm on my 14m2 that way.........
Give the following a try. It's a subset of some code I wrote to control an RGB light strip with an IR remote control (you'll need to edit the pin definitions to match your project):

Code:
#picaxe 14m2
#no_data
#terminal 4800

setfreq m4
symbol GreenPWM = C.0
symbol RedPWM = B.2
symbol BluePWM = B.4

symbol red = b2
symbol green = b3
symbol blue = b4

symbol curColor = b8
symbol baseColorSet = b9

init:
    red = 0
    blue = 0
    green = 0
    curColor = 0
    ;read from EEPROM stored RGB values
    'read 0,red,green,blue
   
    pwmout RedPWM,63,red
    pwmout GreenPWM,63,green
    pwmout BluePWM,63,blue
   
main:
    gosub cycleColors      
    goto main

white: ;nice white color
    pwmduty RedPWM,254 ;50
    pwmduty BluePWM,254 ;60
    pwmduty GreenPWM,254 ;180
    return
   
black:
    pwmduty RedPWM,0
    pwmduty BluePWM,0
    pwmduty GreenPWM,0
    return

cycleColors:
    gosub white
    pause 3000
    ;cycle red first
    gosub cycleRed
    pause 1000
    gosub cycleBlue
    pause 1000
    gosub cycleGreen
    pause 3000
    return
   
cycleRed:
    for red = 254 to 0 step -1
        pwmduty RedPWM,red
        ;pause 250
    next red
    for red = 0 to 254
        pwmduty RedPWM,red
        ;pause 250
    next red
    return
   
cycleGreen:
    for green = 254 to 0 step -1
        pwmduty GreenPWM,green
        ;pause 250
    next green
    for green = 0 to 254
        pwmduty GreenPWM,green
        ;pause 250
    next green
    return
   
cycleBlue:
    for blue = 254 to 0 step -1
        pwmduty BluePWM,blue
        ;pause 250
    next blue
    for blue = 0 to 254
        pwmduty BluePWM,blue
        ;pause 250
    next blue
    return
 

hippy

Technical Support
Staff member
#10
Doing some research, multiple pwms has caused people issues in the past
I recall the main problems were with X2 devices when using different PWMDIV options; issuing a later PWMOUT was resetting the PWMDIV setting of earlier.

I can't see any obvious reason why the code in Post #4 wouldn't work but haven't tried it myself.

It might be worth adjusting the PWMOUT command order to see if there is some interaction depending on order.

That code is issuing PWMOUT's every time but includes a reasonably long PAUSE so I would not have expected a lot of difference if using PWMDUTY as per Hemi345's code in Post #9.
 

nbw

Senior Member
#11
What do you have leg 13 (REF1) and leg 15 (REF0) connected to ?
I had those not connected; my understanding - maybe wrong? - was that they were used if you wanted to drive higher voltages outside of the DAC range? I'll re-check. I've disconnected the MAX519 for now while I attempt to get all 3 PWMs on the 14m2 going, but easy enough to re-breadboard
 

nbw

Senior Member
#12
Give the following a try. It's a subset of some code I wrote to control an RGB light strip with an IR remote control (you'll need to edit the pin definitions to match your project):

Code:
#picaxe 14m2
#no_data
#terminal 4800

setfreq m4
symbol GreenPWM = C.0
symbol RedPWM = B.2
symbol BluePWM = B.4

symbol red = b2
symbol green = b3
symbol blue = b4

symbol curColor = b8
symbol baseColorSet = b9

init:
    red = 0
    blue = 0
    green = 0
    curColor = 0
    ;read from EEPROM stored RGB values
    'read 0,red,green,blue
  
    pwmout RedPWM,63,red
    pwmout GreenPWM,63,green
    pwmout BluePWM,63,blue
  
main:
    gosub cycleColors     
    goto main

white: ;nice white color
    pwmduty RedPWM,254 ;50
    pwmduty BluePWM,254 ;60
    pwmduty GreenPWM,254 ;180
    return
  
black:
    pwmduty RedPWM,0
    pwmduty BluePWM,0
    pwmduty GreenPWM,0
    return

cycleColors:
    gosub white
    pause 3000
    ;cycle red first
    gosub cycleRed
    pause 1000
    gosub cycleBlue
    pause 1000
    gosub cycleGreen
    pause 3000
    return
  
cycleRed:
    for red = 254 to 0 step -1
        pwmduty RedPWM,red
        ;pause 250
    next red
    for red = 0 to 254
        pwmduty RedPWM,red
        ;pause 250
    next red
    return
  
cycleGreen:
    for green = 254 to 0 step -1
        pwmduty GreenPWM,green
        ;pause 250
    next green
    for green = 0 to 254
        pwmduty GreenPWM,green
        ;pause 250
    next green
    return
  
cycleBlue:
    for blue = 254 to 0 step -1
        pwmduty BluePWM,blue
        ;pause 250
    next blue
    for blue = 0 to 254
        pwmduty BluePWM,blue
        ;pause 250
    next blue
    return
Thanks for that - I'll give it a go. I'm out on a date tonight so short of time - I hope she appreciates she's using valuable PICAXE testing time haha!
 

nbw

Senior Member
#13
Thanks for that - I'll give it a go. I'm out on a date tonight so short of time - I hope she appreciates she's using valuable PICAXE testing time haha!
Just gave it a quick try - I had a few minutes - the C.0 port still does the weird flashing thing.

I slowed it down to 4000Hz - it still flashes but I noticed something a bit different. The red LED (flashing one) has an off state. Then quickly increases a little bit in brightness over a fraction of a section, then off (a slightly shorter off this time), then another mini fade-on, then a shorter off time etc.

I've got a few mins so will re-order as Hippy suggests (nope, it still does the flashing thing). Tried swapping the LEDs around too, it's definitely the c.0 port misbehaving.
 

nbw

Senior Member
#14
What do you have leg 13 (REF1) and leg 15 (REF0) connected to ?
Hmm. When one, ahem, re-reads the datasheet... it does in fact 'suggest' on the 519 that pins 13 and 15 are references for the DAC 0 and 1 inputs. Tomorrow night I'll re-try the MAX519, seeing as the 3-PWM-from-14m2 could be a dead end. Third option.... maybe use an old 18X (with a PWM) as a slave off the 14m2.
 

Hemi345

Senior Member
#15
So both C.2 and C.0 have the wierd flashing issue when PWM is used? Makes me think something is resetting port C, like an outpinsC or pinsC call.
 

premelec

Senior Member
#16
@nbw did you try Hemi's code at Post #9? Did you try looking for reset? [you can just turn on one LED long time on start before program is running main]...
 

nbw

Senior Member
#17
OK... latest update [it will make you laugh]

I tried Hemi's code again, still got the flashing. The red LED flashing was kind of in time to the other two LEDs slowly PWMing up. Made me wonder what if.

Using a current limiting resistor, I connected the red LED direct to 5V. It flashed.

It's a red LED with a bloody little onboard flashing circuit!!!! So here I am PWMing it and it's trying to work, then pauses then gets a stronger PWM burst. On, off, struggle, on, off, struggle, test over.

Hilarious.

With a regular red LED (of the non-flashing variety), all 3 PWMs appear to work well.

Thanks for your thoughts everyone. Let's put it down to "user error". I'd still be interested to know why I couldn't quite get the MAX519 going - maybe tying the reference pins to +5V decoupled might help. But at least, I don't need to learn how to slave an old 18X to the 14m2 to use the former's pwm!!

Barney
 

hippy

Technical Support
Staff member
#19
I'd still be interested to know why I couldn't quite get the MAX519 going - maybe tying the reference pins to +5V decoupled might help.
I think that's essential, and without a reference it won't work. PWM is probably better though because I couldn't see any output drive current rating in the datasheet, so into a LED+R might not have work.

Glad you found what the problem was.
 
Top