ATMEGA16 read and write iic (24c02) C language program test passed

Publisher:平安心境Latest update time:2015-03-11 Source: 51heiKeywords:ATMEGA16 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include

#include "I2C.h"
#include "1602.h"
#include "delay.h"

/*Write data to IIC through AVR, and read and display the data through the serial port*/
//=======================================================================
void uart_init(void) //UART initialization
{ DDRD = 0x02;
     PORTD = 0x00;

UCSRA = 0x02; /*No speed*/
     UCSRB = 0x18; /*Allow receiving and sending*/
     UCSRC = 0x06; /*8-bit data, 1-bit stop bit, no parity*/
     UBRRH = 0x00;
     UBRRL = 12; /*9600*/

}


//====================================================================
void USART_TXD(float data) //Sending in query mode
{
while( !(UCSRA & BIT(UDRE)) );
UDR=data;
while( !(UCSRA & BIT(TXC )) );
UCSRA|=BIT(TXC);
}


void main(void)

{
unsigned char i;
//LCD_init();

uart_init(); //TART initialization
SEI(); //Global interrupt enable

   while(1)
   {/*
I2C_Write('n',0x00);
I2C_Write('c',0x01);
I2C_Write('e',0x02);
I2C_Write('p',0x03);
I2C_Write('u',0x04 );
*/
i=I2C_Read(0x00);
//LCD_write_char(0,0,i);
   USART_TXD(i);
i=I2C_Read(0x01);
//LCD_write_data(i);
   USART_TXD(i);
i=I2C_Read( 0x02);
//LCD_write_data(i);
   USART_TXD(i);
i=I2C_Read(0x03);
//LCD_write_data(i);
   USART_TXD(i);
   i=I2C_Read(0x04);
//LCD_write_data(i);
   USART_TXD( i);
  
   }

}

/*The main function above */

#include
#include "delay.h"

//I2C status definition
//MT master mode transmission MR master mode acceptance
#define START 0x08
#define RE_START 0x10
#define MT_SLA_ACK 0x18
#define MT_SLA_NOACK 0x20
#define MT_DATA_ACK 0x28 #define
MT_DATA_NOACK 0x30
#define MR_SLA_ACK 0x40
#define MR_SLA_NOACK 0x48 #define
MR_DATA_ACK 0x50
#define MR_DATA_NOACK 0x58

#define RD_DEVICE_ADDR 0xA1 //The first 4 bits are fixed, the last 3 bits depend on the connection, and the last bit is the read/write instruction bit
#define WD_DEVICE_ADDR 0xA0

//Common TWI operations (master mode write and read)
#define Start() (TWCR=(1< #define Stop() (TWCR=(1< #define Wait() {while(!(TWCR&(1< #define TestAck() (TWSR&0xf8) //Observe the return status
#define SetAck (TWCR|=(1< #define SetNoAck (TWCR&=~(1< #define Twi() (TWCR=(1< #define Write8Bit(x) {TWDR=(x);TWCR=(1<

unsigned char I2C_Write(unsigned char Wdata,unsigned char RegAddress);
unsigned char I2C_Read(unsigned RegAddress);

/************************************************
I2C bus writes a byte
Return 0: write successful
Return 1: write failed
**********************************************/
unsigned char I2C_Write(unsigned char Wdata,unsigned char RegAddress)
{

   Start(); //I2C starts
   Wait();
   if(TestAck()!=START)
   return 1; //ACK
  
   Write8Bit(WD_DEVICE_ADDR); //Write I2C slave device address and write mode
   Wait();
   if(TestAck()!=MT_SLA_ACK)
   return 1; //ACK
  
   Write8Bit(RegAddress); //Write device corresponding register address
   Wait();
   if(TestAck()!=MT_DATA_ACK)
   return 1; //ACK
  
   Write8Bit(Wdata); //Write data to device corresponding register
   Wait();
   if(TestAck()!=MT_DATA_ACK)
   return 1; //ACK
  
   Stop(); //I2C stops
    delay_nms(10); //Delay
   return 0;
}[page]

/************************************************
I2C bus reads a byte
Returns 0: Read successful
Returns 1: Read failed
**********************************************/

unsigned char I2C_Read(unsigned RegAddress)

{
    unsigned char temp;
    Start();//I2C start
    Wait();
    if (TestAck()!=START)
       return 1; //ACK   
   
    Write8Bit(WD_DEVICE_ADDR); //Write I2C slave device address and write mode
    Wait();
    if (TestAck()!=MT_SLA_ACK)
       return 1; //ACK
   
    Write8Bit(RegAddress); //Write device corresponding register address
    Wait();
    if (TestAck()!=MT_DATA_ACK)
       return 1;
   
    Start(); //I2C restart
    Wait();
    if (TestAck()!=RE_START)
       return 1;
   
    Write8Bit(RD_DEVICE_ADDR); //Write I2C slave device address and read mode
    Wait();
    if(TestAck()!=MR_SLA_ACK)
       return 1; //ACK
   
    Twi(); //Start master I2C read mode
    Wait();
    if(TestAck()!=MR_DATA_NOACK)
    return 1; //ACK
   
    temp=TWDR; //Read I2C receive data
       Stop(); //I2C stops
    return temp;
      }

/*The above is the IIC.h header file part, you need to study it carefully according to the technical documentation*/

*-----------------------------------------------------------------------
Delay function
Compiler: ICC-AVR v6.31A Date: 2005-11-24 20:29:57
Target chip: M16
Clock: 8.0000Mhz
Author: archeng504
-----------------------------------------------------------------------*/
#ifndef __delay_h
#define __delay_h
void delay_nus(unsigned int n);
void delay_nms(unsigned int n);
void delay_1us(void);
void delay_1ms(void);

void delay_1us(void) //1us delay function
{
   asm ("nop");
}

void delay_nus(unsigned int n) //N us delay function
{
   unsigned int i=0;
   for (i=0;i    delay_1us();
}

void delay_1ms(void) //1ms delay function
{
   unsigned int i;
   for (i=0;i<1140;i++);
}

void delay_nms(unsigned int n) //N ms delay function
{
   unsigned int i=0;
   for (i=0;i    delay_1ms();
}

#endif
/*The above is the delay.h part, plus the iom16v.h and macros.h that come with IIC, it can be compiled*/

/*Note: This program has been tested on the experimental board ATMEGA16. If you use the oscilloscope to see the data on the SCL and SDA signal lines, you can transfer them to your own circuit and use them with confidence. You can also use them on ATMEGA32. A2, A1, and A0 of my 24C02 are all grounded. If the address is different, just change it in the corresponding position of the program. This is the basis for debugging the microcontroller on the serial port, so you must know how to use it*/

/*The debugging software environment for this program is ICC6.31*/

Keywords:ATMEGA16 Reference address:ATMEGA16 read and write iic (24c02) C language program test passed

Previous article:Homemade AVR ATmega16 JTAG
Next article:Summary of UART communication of AVR microcontroller ATMega16

Recommended ReadingLatest update time:2024-11-16 17:46

Design of Micro Weather Detection System Based on ATmega16 MCU
  0 Introduction   Climate change has made natural disasters such as droughts and floods in my country more serious. Improving my country's meteorological detection capabilities will help enhance my country's disaster warning and weather modification capabilities. Therefore, the development and research of meteorolo
[Microcontroller]
Design of Micro Weather Detection System Based on ATmega16 MCU
51 MCU drives I2C (24C02) assembly language program
;Program description: Write the contents of 55h~58h inside the MPU to 24c02 via the I2C bus from external interrupt 0, and then read the written contents to  ;MPU internal data memory 60H~~63H    SCL EQU P1.1   SDA EQU P1.0   org 0000h  jmp begin  org 0003H  ljmp write  org 0013H  ljmp read   
[Microcontroller]
stm32F103 simulates I2C read and write 24c02
/*********File name: i2c_ee.h**********/ /* Define to prevent recursive inclusion ------------------------------------ */ #ifndef __I2C_EE_H #define __I2C_EE_H /* Includes ------------------------------------------------------------------*/ #include "stm32f10x.h" /* Exported macro -------------------------------
[Microcontroller]
Example of ATmega16 driving spi interface 128x64 screen written by MikroC
The ATmega16 driver spi interface 128x64 screen example written by MikroC comes with simulation. It comes with a driver library, and the tool for converting images into arrays is also integrated in the software. It can be downloaded from the official website without registration. The microcontroller source program i
[Microcontroller]
AVR-atmega16 BOOTLoader Program
This program is written with reference to Ma Chao's mega128. It can support 485 or 232 interfaces. Variant software ICCAVR, host computer software application hyperterminal or avrubd, etc., Xmodem, 9600, 8, 1, n #include iom16v.h #include macros.h #include eeprom.h #define SPM_PAGESIZE 128 //One Flash page of M16 i
[Microcontroller]
Design of electro-hydraulic servo valve feedback controller based on ATmega16
Electro-hydraulic servo valves are widely used in engineering systems. Although hydraulic systems have disadvantages such as difficult maintenance, leakage, and high noise, for high-power automatic control systems, hydraulic control is irreplaceable by other control forms. With the expansion of the application field o
[Power Management]
Design of electro-hydraulic servo valve feedback controller based on ATmega16
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号