EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / i2c repeate start condition how to wrok

i2c repeate start condition how to wrok

|

Microcontroller › AVR › i2c repeate start condition how to wrok

  • This topic has 1 reply, 2 voices, and was last updated 6 years, 10 months ago by Saatwik.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • September 22, 2016 at 9:02 am #4558
    bhavin
    Participant
    #define F_CPU 16000000UL
    #include <avr/io.h>
    #include <util/delay.h>
     
     
     
     
    void TWI_init_master(void) // Function to initialize master
    {
    TWSR = 0x00;   //  TWPS = 00;
    TWBR = 0x48;  //   100 khz freq
    TWCR = 0x00;
    TWCR |= (1<<TWEN);
    }
    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
    if ((TWSR & 0xF8) == 0x10);
    }
    void TWI_REPEATED_START(void)
    {
    TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
    while(!(TWCR & (1<<TWINT)));
    }
     
     
    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_stop(void)
    {
    // Clear TWI interrupt flag, Put stop condition on SDA, Enable TWI
    TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
    _delay_ms(250);
    }
     
     
    int main(void)
    {
    while (1)
    {
    TWI_init_master();
    TWI_start();
     
     
     
     
    TWI_write_data(0x20);
    TWI_write_data(0x59);
    TWI_write_data(0x60);
    TWI_stop();
    _delay_ms(1000);
    }
    //while(1);
     
    March 1, 2019 at 11:33 am #15036
    Saatwik
    Participant

    these both link may help you https://www.engineersgarage.com/tutorials/twi-i2c-interface

    https://www.engineersgarage.com/embedded/avr-microcontroller-projects/atmega32-twi-two-wire-interface

  • Author
    Posts
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Sine wave distortion January 17, 2026
  • Micro mouse January 17, 2026
  • flexible copper cable January 16, 2026
  • Flip Flop for My Mirrors January 16, 2026
  • potenciometer attachment / screwdriver January 16, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2026 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise