Tach auch!
Ich taste mich langsam an das Thema I2C ran.
Aktuell versuche ich den 12bit DAC über I2C anzusteuern. Leider konnte
ich keine Erfolge verbuchen. Ich weiß nicht genau wie ich da was
debuggen soll, aber ich denke stark, dass der Fehler im Code liegt.
Vielleicht fällt da jemandem ein Fehler auf?
Ich wäre euch jedenfalls sehr dankbar :)
(Übrigends habe ich die Library von Peter Fleury benutzt)
MCP4725.h
1 | #define MCP4726_CMD_WRITEDAC (0x40) // Writes data to the DAC
|
2 | #define MCP4726_CMD_WRITEDACEEPROM (0x60) // Writes data to the DAC and the EEPROM (persisting the assigned value after reset)
|
3 |
|
4 | void MCP4725_start(uint8_t address);
|
5 |
|
6 | void MCP4725_setVoltage(uint16_t output, uint8_t writeEEPROM);
|
MCP4725.c
1 | #include <avr/io.h>
|
2 | #include "i2cmaster.h"
|
3 | #include "MCP4725.h"
|
4 |
|
5 | #define F_CPU 8000000UL
|
6 |
|
7 | uint8_t addr;
|
8 |
|
9 | void MCP4725_start(uint8_t address)
|
10 | {
|
11 | i2c_init();
|
12 | addr = address;
|
13 | }
|
14 |
|
15 | void MCP4725_setVoltage(uint16_t output, uint8_t writeEEPROM)
|
16 | {
|
17 | uint8_t twbrback = TWBR;
|
18 | TWBR = ((F_CPU / 400000L) - 16) / 2;
|
19 | i2c_start(addr + I2C_WRITE);
|
20 |
|
21 | if (writeEEPROM) {
|
22 | i2c_write(MCP4726_CMD_WRITEDACEEPROM);
|
23 | }
|
24 | else {
|
25 | i2c_write(MCP4726_CMD_WRITEDAC);
|
26 | }
|
27 |
|
28 | i2c_write(output / 16);
|
29 | i2c_write((output % 16) << 4);
|
30 | i2c_stop();
|
31 | TWBR = twbrback;
|
32 | }
|
Beste Grüße.