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

Publisher:EtherealJourneyLatest update time:2020-03-05 Source: eefocusKeywords:ATmega16 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Introduction: This program has been tested on the experimental board ATMEGA16. If you see data on the SCL and SDA signal lines on the oscilloscope, you can transfer the data to your own circuit and use it with confidence. It can also be used 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.


#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 stop bit, no check*/

UBRRH = 0x00;

UBRRL = 12; /*9600*/


}



//===============================================================

void USART_TXD(float data) //Send using query method

{

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 reception

#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 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 the corresponding register address of the device

Wait();

if(TestAck()!=MT_DATA_ACK)

return 1; //ACK


Write8Bit(Wdata); //Write data to the corresponding register of the device

Wait();

if(TestAck()!=MT_DATA_ACK)

return 1; //ACK


Stop(); //I2C stop

delay_nms(10); //delay

return 0;

}


/****************************************************

I2C bus reads a byte

Return 0: Read successfully

Return 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 the corresponding register address of the device

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 the main 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;idelay_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;idelay_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:Design scheme of protection circuit based on lithium battery pack of electric vehicle
Next article:A design scheme for digitally controlled low voltage and high current pulse power supply

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号