void twi_init(void)
/* 
short:			Init AVR TWI interface and set bitrate
inputs:			
outputs:	
notes:			TWI clock is set to 100 kHz
Version :    	DMK, Initial code
*******************************************************************/
{
	TWSR = 0;
	TWBR = 32;	 // TWI clock set to 100kHz, prescaler = 0
	// Init HT16K22. Page 32 datasheet
	twi_start();
	twi_tx(0xE0);	// Display I2C addres + R/W bit
	twi_tx(0x21);	// Internal osc on (page 10 HT16K33)
	twi_stop();
	
	twi_start();
	twi_tx(0xE0);	// Display I2C address + R/W bit
	twi_tx(0xA0);	// HT16K33 pins all output
	twi_stop();
	
	twi_start();
	twi_tx(0xE0);	// Display I2C address + R/W bit
	twi_tx(0xE3);	// Display Dimming 4/16 duty cycle
	twi_stop();
	
	twi_start();
	twi_tx(0xE0);	// Display I2C address + R/W bit
	twi_tx(0x81);	// Display OFF - Blink On
	twi_stop();
	
}
Example #2
0
int main( void )
 
{
	
	twi_init();		// Init TWI interface

	// Init HT16K22. Page 32 datasheet
	twi_start();
	twi_tx(0xE0);	// Display I2C addres + R/W bit
	twi_tx(0x21);	// Internal osc on (page 10 HT16K33)
	twi_stop();

	twi_start();
	twi_tx(0xE0);	// Display I2C address + R/W bit
	twi_tx(0xA0);	// HT16K33 pins all output
	twi_stop();

	twi_start();
	twi_tx(0xE0);	// Display I2C address + R/W bit
	twi_tx(0xE3);	// Display Dimming 4/16 duty cycle
	twi_stop();

	twi_start();
	twi_tx(0xE0);	// Display I2C address + R/W bit
	twi_tx(0x81);	// Display OFF - Blink On
	twi_stop();

	twi_clear();

		play_fill_animation();
		clear_rows();
	
	return 1;
	}
Example #3
0
uint16_t twi_read_2byte(uint8_t addr)
{
	uint32_t tmp;
	twi_start();
	twi_write(addr << 1);	/* slave addr */
	twi_write(0x00);	/* register addr */
	twi_stop();
	twi_start();
	twi_write((addr << 1) | 0x1);/* slave addr + read */
	tmp = twi_read();
	tmp = (tmp << 8) | twi_read();
	twi_stop();
	return (tmp & 0xffff);
}
Example #4
0
void write_data(int row, int column){
	twi_start();
	twi_tx(0xE0);		// Display I2C address + R/W bit
	twi_tx(row);	// Address
	twi_tx(column);  //column
	twi_stop();
}
Example #5
0
SSD_RESULT ssd_update()
{
    int8_t i, j;
    SSD_RESULT ssd_result;
    TWRESULT tw_result;

    ssd_result = ssd_set_column(0x00, SSD1306_MAXCOLUMN);
    if (ssd_result != SSD_OK)
        return ssd_result;

    ssd_result = ssd_set_page(0x00, SSD1306_MAXPAGE);
    if (ssd_result != SSD_OK)
        return ssd_result;

    for (i = 0; i < BUF_BYTES/16; ++i)
    {
        tw_result = twi_master_send_byte(SSD1306_I2C_ADDRESS, 0x40, KEEP_ALIVE);
        if (tw_result != TWST_OK)
            return SSD_TWI_ERROR;

        for (j = 0; j < 16; ++j)
        {
            tw_result = twi_master_send_byte(SSD1306_I2C_ADDRESS, _buffer[i*16 + j], KEEP_ALIVE);
            if (tw_result != TWST_OK)
                return SSD_TWI_ERROR;
        }

        twi_stop(CLOSE);
    }

    return SSD_OK;
}
Example #6
0
/** \brief This function acquires data from the LSM303DLHC accelerometer module.
*/
void imu::mag_position(void)
{
	uint8_t x_l = 0;
	uint8_t x_h = 0;
	uint8_t z_l = 0;
	uint8_t z_h = 0;
	uint8_t y_l = 0;
	uint8_t y_h = 0;
		
	twi_start(); // Start Condition
	twi_write(0b00111100); // SLA + W = 0x29 + w(0)
	twi_write(0x03 | 0x80); //Set Address Pointer to data register location. Setting the MSB makes sure the data autoincrements

	twi_start();
	twi_write(0b00111101); //SLA + R = 0x29 + w(1)
	
	x_l = twi_read(1);
	x_h = twi_read(1);
	
	y_l = twi_read(1);
	y_h = twi_read(1);
	
	z_l = twi_read(1);
	z_h = twi_read(0);
	
	twi_stop();

	x_mag = (x_h<<8)|(x_l) ;
	y_mag = (y_h<<8)|(y_l) ;
	z_mag = (z_h<<8)|(z_l) ;
}
Example #7
0
static DS1307 read_ds1307() 
{
	if (twi_mt_start(DS1307_CTRL_ID) != TWST_OK)
		return;

	twi_write(0x00); 						// Set pointer address to seconds

	if (twi_mr_start(DS1307_CTRL_ID) != TWST_OK)
		return;

	DS1307 time; 
	//Reading values
	time.second = bcd2dec(twi_read() & 0x7f); 
	time.minute = bcd2dec(twi_read());
	time.hour 	= bcd2dec(twi_read() & 0x3f);

	twi_read(); 							// week number not needed

	time.day 	= bcd2dec(twi_read());
	time.month 	= bcd2dec(twi_read());
	time.year 	= bcd2dec(twi_read());

	twi_stop(); 

    return time;
}
Example #8
0
/** \brief This function acquires data from the L3GD20 Gyro module, via i2c communication.
*/
void imu::gyro_position(void)
{
	uint8_t x_l = 0;
	uint8_t x_h = 0;
	uint8_t y_l = 0;
	uint8_t y_h = 0;
	uint8_t z_l = 0;
	uint8_t z_h = 0;
	
	twi_start(); // Start Condition
	twi_write(0b11010110); // SLA + W = 0x29 + w(0)
	twi_write(0x28 | 0x80); //Set Address Pointer to data register location. Setting the MSB makes sure the data autoincrements
	
	twi_start();
	twi_write(0b11010111); //SLA + R = 0x29 + w(1)
	
	x_l = twi_read(1);
	x_h = twi_read(1);
	y_l = twi_read(1);
	y_h = twi_read(1);
	z_l = twi_read(1);
	z_h = twi_read(0);
	
	twi_stop();
	
	x_gyro = ( (uint16_t) x_h<<8)|(x_l) ; // HUGE PROBLEM HERE. DOUBLE CHECK
	y_gyro = ( (uint16_t) y_h<<8)|(y_l) ;
	z_gyro = ( (uint16_t) z_h<<8)|(z_l) ;
}
Example #9
0
/**
 * \brief Select a protocol for a FLEXCOM device
 *
 *
 *  \param flexcom  Pointer to FLEXCOM peripheral to configure.
 *  \param protocol  Protocol to use.
 */
void flexcom_select(Flexcom * flexcom, uint32_t protocol)
{
	assert(flexcom);
	uint32_t current_protocol = flexcom->FLEX_MR;

	usart_set_receiver_enabled(&flexcom->usart, 0u);

	/* Shutdown previous protocol */
	switch (current_protocol) {
	case FLEX_MR_OPMODE_USART:
		usart_set_receiver_enabled(&flexcom->usart, 0u);
		usart_set_transmitter_enabled(&flexcom->usart, 0u);
		break;
	case FLEX_MR_OPMODE_SPI:
		spi_disable(&flexcom->spi);
		break;
	case FLEX_MR_OPMODE_TWI:
		twi_stop(&flexcom->twi);
	default:
		break;
	}

	assert(protocol & FLEX_MR_OPMODE_NO_COM ||
	       protocol & FLEX_MR_OPMODE_USART ||
	       FLEX_MR_OPMODE_SPI || FLEX_MR_OPMODE_TWI);

	/* Activate the new mode () */
	flexcom->FLEX_MR = protocol;
}
Example #10
0
/* Read num bytes starting from regAddrStart. Each subsequent byte will be from the register above (newRegPtr = oldRegPtr + 1).
 * 
 * Input:	regAddrStart	The register address to start reading bytes from
 *			data			Location to store read data
 *			num				The number of registers to be read
 * Return:	0				Succeeded
 *			>0				Failed, see handle_error() for relavant error codes
 */
uint8_t twi_read_bytes(uint8_t regAddrStart, uint8_t *data, uint8_t num) {
	uint8_t i, error;
	
	/* Transmit start condition. */
	if((error = twi_start())) return error;
	
	/* Transmit slave address and write bit. */
	if((error = twi_sla_w(IMU_ADDR))) return error;
	
	/* Transmit to slave the register to be read. */
	if((error = twi_write(regAddrStart))) return error;
	
	/* Transmit repeated start condition. */
	if((error = twi_start_r())) return error;
	
	/* Transmit slave address and read bit. */
	if((error = twi_sla_r(IMU_ADDR))) return error;
	
	/* Read data from slave. */
	for(i = 0; i < num; i++) {
		if(i == num - 1) {
			/* Last byte, return NACK. */
			if((error = twi_read_nack(&data[i]))) return error;
		} else {
			if((error = twi_read_ack(&data[i]))) return error;
		}
	}
	
	/* Transmit stop condition. */
	twi_stop();
	
	return 0;
}
Example #11
0
int main()
{
	DDRB = 0x20;
	PORTC = 1 << 4 | 1 << 5;
	stdout = &mystdout;
	stdin = &mystdin;

	uart_init();
	twi_master_init();

	puts("Master Receive!");
	while (twi_mr_start(0x08) != TWST_OK)
	{
		printf("Failed: %x\n", TW_STATUS);
		twi_stop();
		puts("No ACK (Enter to continue)");
		getchar();
	}

	while (1)
	{
		char c = twi_read();

		printf("Char: %c, Status: %x\n", c, TW_STATUS);		
		if (TW_STATUS != TW_MR_DATA_ACK)
			break;

		PINB = 0x20;
	}

	puts("Disconnected");
	return 0;
}
Example #12
0
/**
 * rtc_read_register() - read a register of realt time clock
 *
 * @address:	address of realt time clock register
 * @byte:		pointer to address where the read byte will be stored in
 *
 * Return: TWI_OK on success, TWI_ERR on error
 */
uint8_t rtc_read_register(uint8_t address, uint8_t* byte)
{
	rtc_disable_avr_interrupt();
	uint8_t ret = 0;
	ret |= twi_start();
	if (ret != TWI_OK)
		goto rtc_read_register_exit;
	ret |= twi_send_slave_address(TWI_WRITE, RTC_ADDRESS);
	if (ret != TWI_OK)
		goto rtc_read_register_exit;
	ret |= twi_send_byte(address);
	if (ret != TWI_OK)
		goto rtc_read_register_exit;
	ret |= twi_start();
	if (ret != TWI_OK)
		goto rtc_read_register_exit;
	ret |= twi_send_slave_address(TWI_READ, RTC_ADDRESS);
	if (ret != TWI_OK)
		goto rtc_read_register_exit;
	ret |= twi_read_byte(TWI_NACK, byte);
	twi_stop();
rtc_read_register_exit:
	if (during_bitmask == FALSE)	{
		rtc_enable_avr_interrupt();
	}
	return ret;
}
Example #13
0
/** \brief This function initializes the LSM303DLHC magnetometer module, via i2c communication.
*/
void imu::mag_init(void)
{
	_delay_us(1);
	twi_start();
	twi_write(0b00111100); //Write SLA + W.
	twi_write(0x00); 
	twi_write(0x14); 
	twi_stop();
	
	_delay_us(1);
	twi_start();
	twi_write(0b00111100); //Write SLA + W.
	twi_write(0x01); 
	twi_write(0x40); 
	twi_stop();
}
Example #14
0
uint8_t twi_read_byte(void)
{
    //uint8_t databyte;
    twi_start();

    if (twi_get_status() != 0x08)
        return ERROR;

    //select devise and send A2 A1 A0 address bits
    twi_write(0xD0);
    if (twi_get_status() != 0x18)
        return ERROR;

    twi_write(0x01);
    if (twi_get_status() != 0x18)
        return ERROR;
	twi_read_ack();

    twi_write(0xD1);
    if (twi_get_status() != 0x18)
        return ERROR;

	twi_start();

    twi_read_nack();
    if (twi_get_status() != 0x58)
        return ERROR;
    twi_stop();

    return SUCCESS;
}
Example #15
0
/** \brief Initialize the L3GD20 Gyro module, via i2c communication protocol.
 *  \details This function initializes the L3GD20 Gyro to acquire data at 95Hz, over
 *  a range of (+-)500 degrees per second.
*/
void imu::gyro_init(void)
{
	_delay_us(1);
	twi_start();
	twi_write(0b11010110); //Write SLA + W.
	twi_write(0x20); // Write SUB address to CTRL_REG1.
	twi_write(0x0F); // Set data rate to 95Hz.
	twi_stop();

	_delay_ms(1); //_delay_us(1);
	
	twi_start();
	twi_write(0b11010110); //Write SLA + W.
	twi_write(0x23); // Write SUB address to CTRL_REG4.
	twi_write(0x10); // Set range to 500degrees/second
	twi_stop();
}
Example #16
0
void l3gd20h_write(char reg, char data)
{
	twi_start();
	twi_send_address(L3GD20H::ADDRW); //write
	twi_send_data(reg);
	twi_send_data(data);
	twi_stop();
}
void write_data(unsigned char adress,unsigned char data)
{
	twi_start();
	twi_tx(0xE0);	// Display I2C addres + R/W bit
	twi_tx(adress);	// Address
	twi_tx(data);	// data
	twi_stop();
}
Example #18
0
void lsm303_write(char reg, char data)
{
	twi_start();
	twi_send_address(LSM303::ADDRW); //write
	twi_send_data(reg);
	twi_send_data(data);
	twi_stop();
}
Example #19
0
/** \brief This function initializes the LSM303DLHC accelerometer module, via i2c communication.
*/
void imu::accl_init(void)
{
	_delay_us(1);
	twi_start();
	twi_write(0b00110010); //Write SLA + W.
	twi_write(0x20); // Write SUB address to CTRL_REG1.
	twi_write(0x47); // Set data rate to 95Hz.
	twi_stop();
}
Example #20
0
File: i2c.c Project: sannyas/avrlib
uint8_t	i2c_writeByte( uint8_t dev_addr, uint8_t data_addr, uint8_t data ){
	twi_start( );
	twi_send( I2C_ADDR_WRITE(dev_addr) );
	twi_send( data_addr );
	twi_send( data );
	twi_stop( );

	return 0;
}
Example #21
0
void twi_write_2byte(uint16_t buf, uint8_t addr)
{
	twi_start();
	twi_write(addr << 1); /* slave addr */
	twi_write(0x00);	/* register addr */
	twi_write(buf);
	twi_write(buf >> 8);
	twi_stop();
}
Example #22
0
void lps25h_init()
{
	// 0xB0 = 0b10110000
	// PD = 1 (active mode);  ODR = 011 (12.5 Hz pressure & temperature output data rate)
	twi_start();
	twi_send_address(LPS25H::ADDRW); //write
	twi_send_data(LPS25H::CTRL_REG1);
	twi_send_data(0xB0);
	twi_stop();
}
Example #23
0
void twi_clear(void){
	int i = 0x00;
	for(; i <=0x0E; i += 0x02){
		twi_start();
		twi_tx(0xE0);
		twi_tx(i);
		twi_tx(0x00);
		twi_stop();
	}
}
Example #24
0
void twi_data_inverse(PATTERN_STRUCT pattern[]) //geeft pattern weer (inverted) nog niet getest!
{
	twi_start();
	twi_tx(0xE0);
	for (int i = 0; i < 8; i++)
	{
		twi_tx(pattern[i].address);
		twi_tx(pattern[i].data |~(0xFF));
	}
	twi_stop();
}
Example #25
0
void twi_data(PATTERN_STRUCT pattern[]) //geeft een simpele pattern weer
{
	twi_start();
	twi_tx(0xE0);
	for (int i = 0; i < 8; i++)
	{
		twi_tx(pattern[i].address);
		twi_tx(pattern[i].data);
	}
	twi_stop();
}
Example #26
0
File: i2c.c Project: sannyas/avrlib
uint8_t	i2c_writeBytes( uint8_t dev_addr, uint8_t data_addr, uint8_t *data, uint8_t data_len ){
	uint8_t i;
	twi_start( );
	twi_send( I2C_ADDR_WRITE(dev_addr) );
	twi_send( data_addr );
	for( i=0; i<data_len; ++i ){
		twi_send( data[i] );
	}
	twi_stop();

	return 0;
}
Example #27
0
char lsm303_whoami()
{
	char a;
	twi_start();
	twi_send_address(LSM303::ADDRW); //write
	twi_send_data(LSM303::WHO_AM_I); //WHOAMI
	twi_start();
	twi_send_address(LSM303::ADDRR); //read
	twi_read_data(&a, 0);
	twi_stop();
	return a;
}
Example #28
0
uint8_t SeeedOLED_sendCommand(unsigned char command)
{
  uint8_t ret = 0;
  twi_start();
  ret = twi_send((SeeedOLED_Address<<1)|0); // begin I2C communication & write
  if(ret) goto out;
  ret = twi_send(SeeedOLED_Command_Mode);        // Set OLED Command mode
  if(ret) goto out;
  ret = twi_send(command); 
 out:  
  twi_stop(); // End I2C communication
  return ret;
}
Example #29
0
File: i2c.c Project: sannyas/avrlib
uint8_t	i2c_readByte( uint8_t dev_addr, uint8_t data_addr ){
	uint8_t data;

	twi_start( );
	twi_send( I2C_ADDR_WRITE(dev_addr) );
	twi_send( data_addr );
	twi_start( );
	twi_send( I2C_ADDR_READ(dev_addr) );
	data = twi_recv( 0 );
	twi_stop();

	return data;
}
Example #30
0
void twi_adc_query(void) {
    /* keep sampling adc data */
    twi_start(TWI_ADC_ADDR<<1);
    /* start at AIN0, autoincrement through channels */
    twi_write(1<<2 | (0 & 0x03));
    twi_stop();
    twi_start((TWI_ADC_ADDR<<1) | 1);
    twi_read(1); /* discard result code of precious cycle */
    for (uint8_t adc = 0; adc < TWI_ADC_CHANNELS; adc++) {
        twi_adc_raw[adc] = twi_read(adc < TWI_ADC_CHANNELS-1);
        /* scale to 10 bit values */
        twi_adc_values[adc] = twi_adc_raw[adc]<<2;
    }
}