1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
-
-
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <stdint.h>
-
- uint8_t data[7] = { 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42 };
- uint8_t dataSent = 0;
-
- ISR(TWI_vect) {
- static uint8_t dataPos = 0;
- switch (TWSR & ~(7)) {
- case 0xB0: case 0xA8:
- dataPos = 1;
- TWDR = data[0];
- TWCR |= (1 << TWEA);
- dataSent = 0;
- break;
- case 0xB8:
- if (dataPos >= 6) {
-
-
-
- dataPos = 0;
- TWDR = data[6];
- TWCR &= ~(1 << TWEA);
- dataSent = 1;
- } else {
- TWDR = data[dataPos++];
- TWCR |= (1 << TWEA);
- dataSent = 0;
- }
- break;
- case 0xC0: case 0xC8:
- TWCR |= (1 << TWEA);
- dataSent = 1;
- break;
- }
-
- TWCR |= (1 << TWINT);
- }
-
- void twiInit(uint8_t address) {
- if (address == 0) {
- TWAR = 1;
- } else {
- TWAR = address & ~(1);
- }
- TWCR = (1 << TWEN) | (1 << TWEA) | (1 << TWIE);
-
- sei();
- }
-
- void twiSetDataToSend(uint8_t *d) {
-
-
-
- uint8_t i;
- for (i = 0; i < 7; i++) {
- data[i] = d[i];
- }
- }
-
- uint8_t twiDataWasSent(void) {
- if (dataSent != 0) {
- dataSent = 0;
- return 1;
- } else {
- return 0;
- }
- }
|