1 | /*
|
2 | * MCP4725_DAC.c
|
3 | *
|
4 | * Created: 28.02.2019 09:11:19
|
5 | * Author : USER
|
6 | *
|
7 | * hier soll ein Wert mit I2C in das Register des DAC MCP4725 geschrieben werden, dieser soll dann eine Spannung
|
8 | * an Vout ausgeben. Ich Stelle mir das in der Whileschleife so vor
|
9 | *
|
10 | * 1. Startbedingung Senden
|
11 | * 2. Übertragung der Adresse des MCP4725 (hier 11000000) + Write + ACK Adresse A0 ist auf GND gelegt
|
12 | * 3. Übertragen des Wertes der in das Register geschrieben werden soll mit MT_DATA_ACK 0x00
|
13 | * 4. Stopbedingung Senden jetzt soll der DAC soll 0V ausgeben
|
14 | *
|
15 | *
|
16 | * Ein möglicher Übertragungsfehler (ERROR) wird durch eine LED an PB1 angezeigt
|
17 | *
|
18 | * CPU: ATmega88, FCPU = 16MHz
|
19 | * die nachfolgende Rechnung mit 62,5kHz wurde mit 8Mhz berechnet, das habe ich noch nicht verstanden wie dies funktioniert
|
20 | * I²C CLK Frequency = 62,5kHz, --> CLK Frequency = FCPU/(16 + 2*(TWBR)*Prescaler Value)
|
21 | */
|
22 | #define F_CPU 16000000UL
|
23 | #include <avr/io.h>
|
24 | #include <util/delay.h>
|
25 |
|
26 | #define START 0x08 // A START condition has been transmitted
|
27 | #define R_START 0x10 // A REPEATED START condition has been transmitted
|
28 | #define SLA_W 0xC0 // Slave Address & Write 1100 0000 A0 ist auf GND gelegt
|
29 | #define SLA_R 0xC1 // Slave Address & Read 1100 0001
|
30 | #define MT_SLA_ACK 0x18 // Master Transmit SLA + W has been transmitted & ACK has been received
|
31 | #define MT_DATA_ACK 0x00 // Master Transmit Data byte has been transmitted & ACK has been received
|
32 | #define MR_SLA_ACK 0x40 // Master Receive SLA + R has been transmitted & ACK has been received
|
33 | #define MR_SLA_NACK 0x48 // Master Receive SLA + R has been transmitted & ACK has been received
|
34 | #define MR_DATA_ACK 0x50 // Master Receive Data byte has been transmitted & ACK has been returned
|
35 | #define MR_DATA_NACK 0x58 // Master Receive Data byte has been transmitted & NACK has been returned
|
36 | #define BIT_RATE 56 // Set value for the bit rate register TWBR
|
37 |
|
38 | void ERROR(void); // Prototyping of function "ERROR"
|
39 |
|
40 | /*** Function to send a START Condition ***/
|
41 | void TWI_START(void)
|
42 | {
|
43 | // Send a START condition
|
44 | TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
|
45 | // Wait for TWINT Flag set. This indicates that the START condition has been transmitted
|
46 | while (!(TWCR & (1<<TWINT)));
|
47 | // Check value of TWI statusregister. Mask prescaler bits. If status different from START go to ERROR if ((TWSR & 0xF8) != START)
|
48 | ERROR();
|
49 | }
|
50 |
|
51 | /*** Function to send the Slave Address with ACK in Master Transmit Mode ***/
|
52 | void TWI_MT_SLA_ACK(void)
|
53 | {
|
54 | // Load Slave Address + Write into TWDR Register
|
55 | TWDR = SLA_W;
|
56 | // Clear TWINT bit in TWCR to start transmission
|
57 | TWCR = (1<<TWINT) | (1<<TWEN);
|
58 | // Wait for TWINT Flag set. This indicates that the SLA+W has been transmitted, and ACK/NACK has been received.
|
59 | while (!(TWCR & (1<<TWINT)));
|
60 | // Check value of TWI status register. Mask prescaler bits. If status different from MT_SLA_ACK go to ERROR
|
61 | if ((TWSR & 0xF8) != MT_SLA_ACK)
|
62 | ERROR();
|
63 | }
|
64 |
|
65 | /*** Function to send 8Bit of data with ACK in Master Transmit Mode **/
|
66 | void TWI_MT_DATA_ACK(uint8_t data)
|
67 | {
|
68 | // Load DATA into TWDR register
|
69 | TWDR = data;
|
70 | // Clear TWINT bit in TWCR to start transmission
|
71 | TWCR = (1<<TWINT) | (1<<TWEN);
|
72 | // Wait for TWINT flag set. This indicates that the DATA has been transmitted, and ACK/NACK has been received.
|
73 | while (!(TWCR & (1<<TWINT)));
|
74 | // Check value of TWI status register. Mask prescaler bits. If status different from MT_DATA_ACK go to ERROR
|
75 | if ((TWSR & 0xF8) != MT_DATA_ACK)
|
76 | ERROR();
|
77 | }
|
78 |
|
79 | /*** Function to send a REPEATED START condition **/
|
80 | void TWI_R_START(void)
|
81 | {
|
82 | // Send a START condition
|
83 | TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
|
84 | // Wait for TWINT Flag set. This indicates that the START condition has been transmitted
|
85 | while (!(TWCR & (1<<TWINT)));
|
86 | // Check value of TWI statusregister. Mask prescaler bits. If status different from R_START go to ER-ROR
|
87 | if ((TWSR & 0xF8) != R_START)
|
88 | ERROR();
|
89 | }
|
90 |
|
91 | /*** Function to send the Slave Address in Master Receive Mode with Acknowledge **/
|
92 | void TWI_MR_SLA_ACK(void)
|
93 | {
|
94 | // Load Slave Address + Read into TWDR Register
|
95 | TWDR = SLA_R;
|
96 | // Clear TWINT bit in TWCR to start transmission
|
97 | TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN);
|
98 | // Wait for TWINT Flag set. This indicates that the SLA+W has been transmitted, and ACK/NACK has been received.
|
99 | while (!(TWCR & (1<<TWINT)));
|
100 | // Check value of TWI status register. Mask prescaler bits. If status different from MR_SLA_ACK go to ERROR
|
101 | if ((TWSR & 0xF8) != MR_SLA_ACK)
|
102 | ERROR();
|
103 | }
|
104 |
|
105 | /*** Function to send the Slave Address in Master Receive Mode without Acknowledge **/
|
106 | void TWI_MR_SLA_NACK(void)
|
107 | {
|
108 | // Load Slave Address + Read into TWDR Register
|
109 | TWDR = SLA_R;
|
110 | // Clear TWINT bit in TWCR to start transmission
|
111 | TWCR = (1<<TWINT) | (1<<TWEN);
|
112 | // Wait for TWINT Flag set. This indicates that the SLA+W has been transmitted, and ACK/NACK has been received.
|
113 | while (!(TWCR & (1<<TWINT)));
|
114 | // Check value of TWI status register. Mask prescaler bits. If status different from MR_SLA_NACK go to ERROR
|
115 | if ((TWSR & 0xF8) != MR_SLA_NACK)
|
116 | ERROR();
|
117 | }
|
118 |
|
119 | /*** Function to read one Databyte in Master Receive Mode and send NACK ***/
|
120 | uint8_t TWI_READ_DATABYTE_NACK(void)
|
121 | {
|
122 | // Clear TWINT bit in TWCR to start transmission with NACK
|
123 | TWCR = (1<<TWINT) | (1<<TWEN);
|
124 | // Wait for TWINT flag set. This indicates that the DATA has been received
|
125 | while (!(TWCR & (1<<TWINT)));
|
126 | // Check value of TWI status register. Mask prescaler bits. If status different from MR_SLA_NACK go to ERROR
|
127 | if ((TWSR & 0xF8) != MR_DATA_NACK)
|
128 | ERROR();
|
129 | return TWDR; // Return the value of data register
|
130 | }
|
131 |
|
132 | /*** Function to read one Databyte in Master Receive Mode and send ACK ***/
|
133 | uint8_t TWI_READ_DATABYTE_ACK(void)
|
134 | {
|
135 | // Clear TWINT bit in TWCR to start transmission with ACK
|
136 | TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN);
|
137 | // Wait for TWINT flag set. This indicates that the DATA has been received
|
138 | while (!(TWCR & (1<<TWINT)));
|
139 | // Check value of TWI status register. Mask prescaler bits. If status different from MR_SLA_ACK go to ERROR
|
140 | if ((TWSR & 0xF8) != MR_DATA_ACK)
|
141 | ERROR();
|
142 | return TWDR; // Return the value of data register
|
143 | }
|
144 |
|
145 | /*** function to send a STOP condition ***/
|
146 | void TWI_STOP(void)
|
147 | {
|
148 | TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); // Transmit STOP condition
|
149 | }
|
150 |
|
151 | /*** function to show a bus error ***/
|
152 | void ERROR(void)
|
153 | {
|
154 | PORTB |= (1<<PB1); // ERROR LED ON
|
155 | }
|
156 |
|
157 | uint8_t LSByte, MSByte;
|
158 | uint16_t data_16bit, lux_value, high_byte, low_byte;
|
159 |
|
160 | int main(void)
|
161 | {
|
162 | //DDRD = 0xFF; // PORTD = output
|
163 | //DDRC = 0x3F; // PC0 ... PC5 = output
|
164 | DDRB = 0x03; // PB0 and PB1 = output
|
165 | TWBR = BIT_RATE; // Set the TWI clock-frequency in bit rate register
|
166 | //TWI_START(); // Send START condition
|
167 | //TWI_MT_SLA_ACK(); // Master Transmit Slave Address with Acknowledge
|
168 | //TWI_MT_DATA_ACK(0x00); // Transmit 0x00 for Register Command with Acknowledge
|
169 | //TWI_STOP(); // Send STOP condition
|
170 | //_delay_ms(10); // Delaytime between STOP and next START contition
|
171 |
|
172 | while(1)
|
173 | {
|
174 | TWI_START(); // Send START condition
|
175 | TWI_MT_SLA_ACK(); // Master Transmit Slave Address with ACK
|
176 | TWI_MT_DATA_ACK(0x00); // Transmit 0x00 to start with Register 0x00 with ACK
|
177 | TWI_STOP(); // Send STOP condition
|
178 |
|
179 | _delay_ms(200); // wait 200ms
|
180 | }
|
181 | }
|