Skip to content

Instantly share code, notes, and snippets.

@rat
Created April 2, 2018 01:12
Show Gist options
  • Save rat/b4e3c40b3affd466b474a062bfa79146 to your computer and use it in GitHub Desktop.
Save rat/b4e3c40b3affd466b474a062bfa79146 to your computer and use it in GitHub Desktop.
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "lib/twi.h"
#include "lib/millis.h"
#include "lib/uart.h"
uint8_t twi_scanner(uint8_t address);
int main(void)
{
//while(1);
uart_init(UART_BAUD_SELECT(9600, F_CPU));
millis_init();
sei();
millis_t lastChange = 0;
millis_t lastChange2 = 0;
while(1) {
millis_t now = millis();
/*
if(now - lastChange >= 3000) {
int active_addresses[5] = {0}, index = 0;
for (int i = 0; i <= 128; i++) {
if (twi_scanner(i)) {
active_addresses[index] = i;
index++;
} // end if
} // end for
lastChange = now;
} // end if millis */
if(now - lastChange2 >= 3000) {
unsigned char i = 0;
twi_init();
twi_start();
twi_write_address( (0x8<<1) | TW_WRITE);
twi_write_data('R');
twi_write_data('E');
twi_write_data(24);
twi_stop();
_delay_ms(100);
twi_init();
twi_start();
twi_read_address((0x8<<1) | TW_READ);
twi_read_data();
twi_read_data();
twi_read_data();
twi_read_data();
twi_stop();
// uart_puts(i);
while (1);
lastChange2 = now;
} // end if millis2
}
}
uint8_t twi_scanner(uint8_t address)
{
twi_init();
twi_start();
if ((TWSR & TW_STATUS_MASK ) != TW_START) {
twi_stop();
return 0;
}
twi_write( (address<<1) | TW_WRITE);
if ((TWSR & TW_STATUS_MASK ) != TW_MT_SLA_ACK) {
twi_stop();
return 0;
}
twi_stop();
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment