Example #1
0
int main(void)
{
  unsigned char write_data=0x75, recv_data;
  int testn=0;
  USART_init();
  print_UART(" This is the test for TWI ");

  _delay_ms(1000);
  PORTD |= 0x80; ///turn led on
  _delay_ms(100);
  TWI_init_master();
  sei();
  print_UART(" Sending Start condition ");
  TWI_start();
  print_UART(" address= %x ", ((address<<1)|write));
  TWI_write_address((address<<1)|write);
  print_UART(" writing register = %x ", write_data);
  TWI_write_byte(write_data); //write register
  print_UART(" Repeat Start ");
  TWI_start(); //repeated start
  print_UART(" address read= %x ", ((address<<1)|read));
  TWI_write_address((address<<1)|read);
   print_UART(" read Data ");
  recv_data=TWI_read_byte(1); ///read byte and nack
  TWI_stop();
  print_UART(" This is the value of the register= %x  ", recv_data);
  PORTD &= 0x7F;
    
 
  return 0;
}
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(255);      // Function to write data in slave
		TWI_stop(); // Function to send stop condition
		
		
		_delay_ms(100); // Delay of 10 mili second
		
		PORTB=0xff;
		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(2000);
	}
	
	
}
Example #3
0
void TWI_read_buf(uint8_t SLA, uint8_t adr, uint8_t len, uint8_t *buf) {

	TWI_start();
	TWI_write(SLA);
	TWI_write(adr);
	TWI_start();
	TWI_write(SLA + 1);
	while (len--) *buf++ = TWI_read( len ? ACK : NACK );
	TWI_stop();
}
Example #4
0
File: sfp.c Project: lysy1033/sfp
void sfp_read_vendor_name(uint8_t *buf) {
	uint8_t len=15;
	TWI_start();
	TWI_write(0xa0);
	TWI_write(0x14);
	TWI_start();
	TWI_write(0xa1);
	while (len--) *buf++ = TWI_read( len ? ACK : NACK );
	TWI_stop();
}
Example #5
0
File: sfp.c Project: lysy1033/sfp
void sfp_read_to_buf(uint8_t SLA, uint8_t len, uint8_t *buf) {

	TWI_start();
	TWI_write(SLA);
	TWI_write(0xa0);
	TWI_start();
	TWI_write(SLA + 1);
	while (len--) *buf++ = TWI_read( len ? ACK : NACK );
	TWI_stop();
}
Example #6
0
uint8_t TWI_read_int(uint8_t SLA) {
	uint8_t data;

	TWI_start();
	TWI_write(SLA);
	TWI_start();
	TWI_write(SLA + 1);
	data = TWI_read(NACK);
	TWI_stop();

	return data;
}
Example #7
0
File: sfp.c Project: lysy1033/sfp
void EI2C_read_buf(uint8_t device, uint16_t subAddr, uint16_t len, uint8_t *buf) {

	while (len--) {
		TWI_start();
		TWI_write(device | ((subAddr>>8)<<1) );
		TWI_write(subAddr);
		TWI_start();
		TWI_write(device + 1);
		*buf++ = TWI_read( NACK );
		TWI_stop();
		subAddr++;
	}


}
Example #8
0
void TWI_ScrollFrame(void)
{
    uint8_t segindex=0,i,j;
    uint8_t str_Ptr[8];
    uint8_t activeStatus = 1;


    while((segindex<8) && (NewMsgReceived==0))
    {
        activeStatus = 1;
        NumberOfSegments = 1;
        for(i=0; (activeStatus) && (NewMsgReceived==0);i++)
        {


            for(j=0;j<MaxColPerSegment;j++)
            {

                str_Ptr[j] = FrameBuffer[(i*8)+j+segindex];
            }



            if(i==0)
            {
                for(j=0;j<MaxColPerSegment;j++)
                {
                    pattern[j] = str_Ptr[j];
                }
            }
            else
            {

                activeStatus = TWI_start(); // Function to send start condition

                if(activeStatus)
                {

                    activeStatus = TWI_write_address(i*2+0x30); // Function to write address and data direction bit(write) on SDA

                    if(activeStatus)
                    {

                        NumberOfSegments++;
                        for(j=0;j<8;j++)
                        {
                            TWI_write_data(str_Ptr[j]);      // Function to write data in slave
                        }
                    }
                }

                TWI_stop(); // Function to send stop condition
            }
        }

        for(i=NumberOfSegments;i<MAX_DEVICES;i++)
            DELAY_ms(SCROLL_DELAY_MS);
        segindex++;
    }
}
Example #9
0
File: TWI.c Project: aelg/plogen
uint8_t TWI_write(uint8_t addr, uint8_t *s, uint8_t len){
  // Save start local variable in case it is changed by an ISR
  uint8_t start = twrite_start;
	// Temporary end to handle circular buffer.
	uint8_t end;
	// Make sure end always is bigger than start.
	if(start > twrite_end){
	  end = twrite_end + TWI_BUFFER_SIZE;
	}
	else end = twrite_end;
	// Return false if the message doesn't fit in buffer.
	if(len + 1> TWI_BUFFER_SIZE - 1 -(end - start)){
		// TODO: signal buffer error.
		return 0;
	}
	twrite_buff[twrite_end] = addr;
	twrite_end = TWIca(twrite_end+1);
	// Copy message to buffer.
	for(int i = 0; i < len; ++i){
		twrite_buff[twrite_end] = s[i];
		twrite_end = TWIca(twrite_end+1);
	}
	TWI_start();
	return 1;
}
Example #10
0
void TWI_write_int( uint8_t SLA, uint8_t data) {

	TWI_start();
	TWI_write(SLA);
	TWI_write(data);
	TWI_stop();
}
Example #11
0
void DisplayString(char *ptr_msgPointer)
{
    int i,j;
    uint8_t activeStatus = 1;
    NumberOfSegments = 1;
    BuildFrame(ptr_msgPointer);

    for(i=0;activeStatus;i++)
    {
        if(i==0)
        {
            for(j=0;j<8;j++)
                pattern[j] = FrameBuffer[j];
        }
        else
        {
            TWI_start(); // Function to send start condition

            activeStatus = TWI_write_address(i*2+0x30); // Function to write address and data direction bit(write) on SDA
            if(activeStatus)
            {
                NumberOfSegments++;
                for(j=0;j<8;j++)
                {
                    TWI_write_data(FrameBuffer[i*8+j]);      // Function to write data in slave
                    //   UART_TxChar(' ');
                    // UART_TxHexNumber(i,2);      // Function to write data in slave
                }
            }
            TWI_stop(); // Function to send stop condition
        }
    }
}
Example #12
0
void TWI_write_buf( uint8_t SLA, uint8_t adr, uint8_t len, uint8_t *buf) {

	TWI_start();
	TWI_write(SLA);
	TWI_write(adr);
	while (len--) TWI_write(*buf++);
	TWI_stop();
}
Example #13
0
// funkcja do odczytu z EXPANDERA PCF8574 *********************
uint8_t PCF8574_read( uint8_t SLA ) {
	uint8_t res=0;
	TWI_start();
	TWI_write(SLA + 1);
	res = TWI_read( NACK );
	TWI_stop();
	return res;
}
Example #14
0
void TWI_wait(char slave)
{
	//send slave address until a slave ACKs it. Good for checking if the EEPROM
	//has finished a write operation. Use carefully! If the wrong slave address
	//is being waited for, this function will end in an infinite loop.
	do {
		TWI_start();
	} while(TWI_write_data(slave) != 0x18);
	TWI_stop();
}
Example #15
0
/////////////////Functions/////////////////////////////////
void TWI_init_master(void) // Function to initialize master
{
	TWBR=3;	// Bit rate
	TWSR=(0<<TWPS1)|(0<<TWPS0);	// Setting prescalar bits
	PORTD|=(1<<0)|(1<<1);
	// SCL freq= F_CPU/(16+2(TWBR).4^TWPS)
	TCCR2=0b00001011;
	 TIMSK|=1<<OCIE2;
		OCR2=200;
		TWI_start(); 
		TWI_write_address(0xD2); 
		TWI_write_data(0x20);	 
		TWI_write_data(0b11101111);
		TWI_stop(); 
		TWI_start(); 
		TWI_write_address(0xD2); 
		TWI_write_data(0x23);	 
		TWI_write_data(0b00010000);
		TWI_stop(); 

}
Example #16
0
void MPU_write_adress(unsigned char mpuAdress, unsigned char registerAdress, unsigned char value){
	TWI_start();
	
	TWI_check_ack(mpuAdress);
	
	TWI_write_data(registerAdress);
	
	TWI_write_data(value);

	TWI_stop();
	
}
Example #17
0
signed short int getdata(unsigned char add)
{
        signed short int DATA; 
        TWI_start(); 
		TWI_write_address(0xD2); 
		TWI_write_data(add);	
		
		TWI_repeated_start();	
		TWI_read_address(0xD3);	
		DATA=TWI_read_data();		
		TWI_stop();
		return DATA;

}
Example #18
0
void MPU_read_adress(unsigned char mpuAdress, unsigned char registerAdress, unsigned char* variable){
	TWI_start();
	
	TWI_check_ack(mpuAdress);
	
	TWI_write_data(registerAdress);
	
	TWI_repeated_start();
	
	TWI_read_address(mpuAdress|0x01);
	
	*variable = TWI_read_data();
	TWI_stop();
	
	}
Example #19
0
char EE_read_byte(const int address)
{
	char dummy;
	//we need this for the first if()
	dummy = TWI_start();
	//as in EE_write_byte, first send the page address and the word address
	if((dummy != 0x08) && (dummy != 0x10))
		return TWSR;
	if(TWI_write_data(EE_ADDR|((address>>7) & 0x000E)) != 0x18)
		return TWSR;
	if(TWI_write_data((char)(address)) != 0x28)
		return TWSR;
	//send a repeated start for entering master receiver mode
	if(TWI_start() != 0x10)
		return TWSR;
	//send slave address, now with the read bit set
	if(TWI_write_data((EE_ADDR|1)|((address>>7) & 0x000E)) != 0x40)
		return TWSR;
	//now get the data from the EEPROM, don't return ACK
	if(TWI_read_data(0) != 0x58)
		return TWSR;
	//if everything was OK, return zero
	return 0;
}
Example #20
0
void MPU_self_test(unsigned char mpuAdress){
		char buf[17]="                ";
		TWI_start();
		
		TWI_check_ack(mpuAdress);
		
		TWI_write_data(0x75);
		
		TWI_repeated_start();
		
		TWI_read_address(mpuAdress|0x01);
		
		char response = TWI_read_data();
		
		sprintf(buf,"Response:  %x",response);
		LCD_WriteText(buf);
		
		TWI_stop();
}
Example #21
0
char EE_write_byte(const int address, const char data)
{
	char dummy;
	//we need this for the first if()
	dummy = TWI_start();
	//if the start was successful, continue, otherwise return 1
	if((dummy != 0x08) && (dummy != 0x10))
		return TWSR;
	//now send the EEPROM slave address together with the address bits 8..10 for page select
	//address format:
	//|bit7	|bit6 |bit5 |bit4 |bit3 |bit2 |bit1 |bit0 |
	//|			EE_ADDR		  |  page select	| R/W |
	if(TWI_write_data(EE_ADDR|((address>>7) & 0x000E)) != 0x18)
		return TWSR;
	//now send the word address byte
	if(TWI_write_data((char)(address)) != 0x28)
		return TWSR;
	//now send the data byte
	if(TWI_write_data(data) != 0x28)
		return TWSR;
	//if everything was OK, return zero.
	return 0;
}