Skip to content

Thrusters Communication

kshitijgoel007 edited this page Jul 25, 2014 · 6 revisions

Overview

  • The thrusters we have, need 3 bytes of data as a command.
  • First Byte is the address of the thruster. Address tells which thruster (out of 6) you want to access.
  • Second and Third byte are Speed and Maximum speed respectively.
  • We need I2C Two Wire Interface for interfacing and controlling 6 thrusters (Seabotix thrusters, they only support I2C Communication).
  • The advantage of I2C is it needs only 2 wires for interfacing every I2C device and 127 devices can be interfaced simultaneously on the bus.

How it Works?

  • Consists of a “BUS” (A “bus”, in electronics, is a sort of channel which can carry stuff. Here bytes and clock pulses) which is responsible for data transmission and its control.
  • “Control” of data transmission means “when to send data?” “When to stop all the transmission?” etc.
  • Data is exchanged in form of bits. Normally 8-bit data register is responsible for all the transmission. However, we also have a 10-bit version available.
  • Lines through which data is exchanged are SDA and SCL.
  • SDA means Serial Data Line and SCL means Serial Clock Line. SDA is used for data transmission and SCL is used to control data transmission.

Tutorial in I2C Communication

I2C Tutorial

Datasheet of the Thruster

Seabotix Thruster Datasheet

Sample Code for I2C Communication protocol using ATMega16 Microcontroller

#include<avr/io.h>
#include<util/delay.h>
#include<inttypes.h>

void TWI_start(void);
void TWI_repeated_start(void);
void TWI_init_master(void);
void TWI_write_address(unsigned char);
void TWI_read_address(unsigned char);
void TWI_write_data(unsigned char);
void TWI_read_data(void);
void TWI_stop(void);

unsigned char address=0x20, read=1, write=0;
unsigned char write_data=0x01, recv_data;

int main(void)
{
	_delay_ms(2000);
	DDRB=0xff;
	TWI_init_master();  // Function to initialize TWI
	while(1)
	{
		if(write_data==0x00)
		write_data=1;
		
		TWI_start(); // Function to send start condition
		TWI_write_address(address+write); // Function to write address and data direction bit(write) on SDA
		TWI_write_data(write_data);      // Function to write data in slave
		TWI_stop(); // Function to send stop condition
		
		
		_delay_ms(10); // Delay of 10 mili second
		
		TWI_start();
		TWI_read_address(address+read); // Function to write address and data direction bit(read) on SDA
		TWI_read_data(); // Function to read data from slave
		TWI_stop();
		
		_delay_ms(1000);
		
		write_data = write_data * 2;
	}
	
	
}

void TWI_init_master(void) // Function to initialize master
{
	TWBR=0x01; // Bit rate
	TWSR=(0<<TWPS1)|(0<<TWPS0); // Setting prescalar bits
	// SCL freq= F_CPU/(16+2(TWBR).4^TWPS)
}

void TWI_start(void)
{
	// Clear TWI interrupt flag, Put start condition on SDA, Enable TWI
	TWCR= (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
	while(!(TWCR & (1<<TWINT))); // Wait till start condition is transmitted
	while((TWSR & 0xF8)!= 0x08); // Check for the acknowledgement
}

void TWI_repeated_start(void)
{
	// Clear TWI interrupt flag, Put start condition on SDA, Enable TWI
	TWCR= (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
	while(!(TWCR & (1<<TWINT))); // wait till restart condition is transmitted
	while((TWSR & 0xF8)!= 0x10); // Check for the acknoledgement
}

void TWI_write_address(unsigned char data)
{
	TWDR=data; // Address and write instruction
	TWCR=(1<<TWINT)|(1<<TWEN);    // Clear TWI interrupt flag,Enable TWI
	while (!(TWCR & (1<<TWINT))); // Wait till complete TWDR byte transmitted
	while((TWSR & 0xF8)!= 0x18);  // Check for the acknoledgement
}

void TWI_read_address(unsigned char data)
{
	TWDR=data; // Address and read instruction
	TWCR=(1<<TWINT)|(1<<TWEN);    // Clear TWI interrupt flag,Enable TWI
	while (!(TWCR & (1<<TWINT))); // Wait till complete TWDR byte received
	while((TWSR & 0xF8)!= 0x40);  // Check for the acknoledgement
}

void TWI_write_data(unsigned char data)
{
	TWDR=data; // put data in TWDR
	TWCR=(1<<TWINT)|(1<<TWEN);    // Clear TWI interrupt flag,Enable TWI
	while (!(TWCR & (1<<TWINT))); // Wait till complete TWDR byte transmitted
	while((TWSR & 0xF8) != 0x28); // Check for the acknoledgement
}

void TWI_read_data(void)
{
	TWCR=(1<<TWINT)|(1<<TWEN);    // Clear TWI interrupt flag,Enable TWI
	while (!(TWCR & (1<<TWINT))); // Wait till complete TWDR byte transmitted
	while((TWSR & 0xF8) != 0x58); // Check for the acknoledgement
	recv_data=TWDR;
	PORTB=recv_data;
}

void TWI_stop(void)
{
	// Clear TWI interrupt flag, Put stop condition on SDA, Enable TWI
	TWCR= (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
	while(!(TWCR & (1<<TWSTO)));  // Wait till stop condition is transmitted
}