AVR MCU I2C bus program

Publisher:JoyfulJourneyLatest update time:2019-10-15 Source: eefocusKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include "iom16v.h"


/*I2C bus master mode error handling*/

void error(unsigned char type) {


        switch (type & 0xF8) {

                case 0x20: /*Address write failed*/

                        /*stop*/

                        TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);

                        break;

                case 0x30: /*Data write failed*/

                        /*stop*/

                        TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);

                        break;

                case 0x38: /*Arbitration failed*/

                        break;

                case 0x48: /*Address read failed*/

                        /*stop*/

                        TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);

                        break;

                }

}


/*I2C bus single byte write*/

unsigned char twi_write(unsigned char addr, unsigned char dd) {


        TWBR = 2;


        /*start*/

        TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);

        while (!(TWCR & (1 << TWINT)));

        if ((TWSR & 0xF8) != 0x08) {

                error(TWSR);

                return 0;

                }


        /*SLA_W chip address*/

        TWDR = 0xA0;

        TWCR = (1 << TWINT) | (1 << TWEN);

        while (!(TWCR & (1 << TWINT)));

        if ((TWSR & 0xF8) != 0x18) {

                error(TWSR);

                return 0;

                }


        /*addr operation address*/

        TWDR = addr;

        TWCR = (1 << TWINT) | (1 << TWEN);

        while (!(TWCR & (1 << TWINT)));

        if ((TWSR & 0xF8) != 0x28) {

                error(TWSR);

                return 0;

                }

        

        /*dd write data*/

        TWDR = dd;

        TWCR = (1 << TWINT) | (1 << TWEN);

        while (!(TWCR & (1 << TWINT)));

        if ((TWSR & 0xF8) != 0x28) {

                error(TWSR);

                return 0;

                }

        

        /*stop*/

        TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);

        return 1;

}


/*I2C bus single byte read*/

unsigned char twi_read(unsigned char addr, unsigned char *dd) {


        TWBR = 2;


        /*start*/

        TWCR=(1<        while(!(TWCR&(1<        if ((TWSR & 0xF8) != 0x08) {

                error(TWSR);

                return 0;

                }


        /*SLA_W chip address*/

        TWDR = 0xA0;

        TWCR = (1 << TWINT) | (1 << TWEN);

        while (!(TWCR & (1 << TWINT)));

        if ((TWSR & 0xF8) != 0x18) {

                error(TWSR);

                return 0;

                }


        /*addr operation address*/

        TWDR = addr;

        TWCR = (1 << TWINT) | (1 << TWEN);

        while (!(TWCR & (1 << TWINT)));

        if ((TWSR & 0xF8) != 0x28) {

                error(TWSR);

                return 0;

                }

                

        /*start*/

        TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);

        while (!(TWCR & (1 << TWINT)));

        if ((TWSR & 0xF8) != 0x10) {

                error(TWSR);

                return 0;

                }

        

        /*SLA_R chip address*/

        TWDR = 0xA1;

        TWCR = (1 << TWINT) | (1 << TWEN);

        while (!(TWCR & (1 << TWINT)));

        if ((TWSR & 0xF8) != 0x40) {

                error(TWSR);

                return 0;

                }


        /*Read data*/

        TWCR = (1 << TWINT) | (1 << TWEN);

        while (!(TWCR & (1 << TWINT)));

        if ((TWSR & 0xF8) != 0x58) {

                error(TWSR);

                return 0;

                }

        *dd = TWDR;

                

        /*stop*/

        TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN);

        return 1;

}


Keywords:AVR Reference address:AVR MCU I2C bus program

Previous article:Atmega16 based voltage meter production program + schematic diagram
Next article:4-bit independent keyboard control program of Atmega128a

Recommended ReadingLatest update time:2024-11-16 13:28

Bascom-avr program that can display temperature and time (and set alarm time)
$regfile = "m16def.dat" $crystal = 8000000 Config Lcdpin = Pin , Db4 = Portc.4 , Db5 = Portc.5 , Db6 = Portc.6 , Db7 = Portc.7 , E = Portc.2 , Rs = Portc.0 Config Lcd = 16 * 2 Config 1wire = Porta.0 '************************************************* Dim Id(8) As Byte Dim I As Byte Dim C(2) As Byte Dim T As Word      
[Microcontroller]
Manually implement 51 MCU function switching
1. Introduction Why do we need to study the process of MCU function switching? In fact, I wrote a simple real-time operating system for 51 MCU in the summer of 2020, which has a simple preemptive kernel scheduling function. Although it is very simple, I still want to share the implementation process. This artic
[Microcontroller]
Manually implement 51 MCU function switching
Design Method of Stable Operation of 51 Single Chip Microcomputer in Microcomputer Automatic Switching System
In power line carrier communication, the microcomputer automatic disk has many functions and strong logic. The real-time performance of the MCS-51 single-chip microcomputer when processing tasks in this system is particularly prominent. Since the main service object of the system configuration is power dispatching, and
[Industrial Control]
Design Method of Stable Operation of 51 Single Chip Microcomputer in Microcomputer Automatic Switching System
51 MCU (VI) - GPIO control of multiple LEDs
In this article, we control multiple LEDs. The hardware circuit is shown in the figure below. This circuit has been introduced in detail in "51 Single Chip Microcomputer (V) - GPIO Lights Up an LED", so I will not go into details here. In this experiment, we use two methods to control multiple LEDs. The experiment
[Microcontroller]
51 MCU (VI) - GPIO control of multiple LEDs
51 single chip microcomputer (AT89C52) one key multi-function (short press, long press)
Short press (press for less than 3s) once, the digital tube display will increase by 1, and the running light count will increase by 1 at the same time. When long pressing (pressing duration is greater than 3s), the digital tube display increases by 1 every 500ms, and the running light counts by 1 every 500ms. #in
[Microcontroller]
STC89C52 MCU running water lamp
/////////////////////////////////////////////////////////////////////////   Function: Let the eight lights D1-D8 on the experimental board run in a cycle, first light up D1, and the other lights are off.           After an interval of 0.3 seconds, light up D2, and the other lights are off. Repeat this cycle until  
[Microcontroller]
Method for determining pitch and beat in single chip microcomputer music
Methods for determining pitch and beat in single-chip music: Key signature - In music, it refers to the symbol used to determine the pitch of the main note of the music. Obviously, there are 12 semitones in an octave. A, B, C, D, E, F, G. After the study of acousticians, these letters are used all over the world to r
[Microcontroller]
Introduction of charger circuit based on MCU AT89C2051
Charger circuit controlled by single chip microcomputer AT89C2051
[Microcontroller]
Introduction of charger circuit based on MCU AT89C2051
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号