×
单片机 > 单片机程序设计 > 详情

AVR(M48)的i2c程序,读写at24c02

发布时间:2020-06-17 发布时间:
|

//ICC-AVR application builder : 2007-12-5 17:34:01

// Target : m48

// Crystal: 1.0000Mhz


#include

#include

#include

//#include


#define START 0x08

#define ReSTART 0x10

#define MT_SLA_ACK 0x18

#define MT_DATA_ACK 0x28


#define MR_SLA_ACK 0x40

#define MR_DATA_ACK 0x50

#define MR_DATA_NACK 0x58


#define SLA_W 0xA0

#define SLA_R 0xA1

#define ADDRESS 0x00

#define DATA 0x55


void port_init(void)

{

//set LED PIN output

DDRC |= (1<

DDRD |= (1<

//set LED off

PORTD = 0xF0;PORTC = 0x0F;

}


void Error()

{

USART_Transmit(TWSR & 0xF8);

while(1) 

     LedBlk();

}


//****TWI_ByteW()****//

void TWI_ByteW(unsigned char add,unsigned data)

{

TWCR = (1<

while(!(TWCR & (1<

     ;

if((TWSR & 0xF8) != START)                //check

     Error();


TWDR = SLA_W;                             //send SLA_W

TWCR = (1<

while(!(TWCR & (1<

     ;

if((TWSR & 0xF8) != MT_SLA_ACK)           //check

     Error();


TWDR = add;                               //send address

TWCR = (1<

while(!(TWCR & (1<

     ;

if((TWSR & 0xF8) != MT_DATA_ACK)          //check

     Error();

   

TWDR = data;                              //send data

TWCR = (1<

while(!(TWCR & (1<

     ;

if((TWSR & 0xF8) != MT_DATA_ACK)          //check

     Error();


TWCR = (1<


Delayus(150);

}


//****TWI_CByteR()****//

unsigned char TWI_CByteR()

{

TWCR = (1<

while(!(TWCR & (1<

     ;

if((TWSR & 0xF8) != START)                //check

     Error();


TWDR = SLA_R;                             //send SLA_R

TWCR = (1<

while(!(TWCR & (1<

     ;

if((TWSR & 0xF8) != MR_SLA_ACK)           //check

     Error();


TWCR = (1<

while(!(TWCR & (1<

     ;

if((TWSR & 0xF8) != MR_DATA_NACK)         //check

     Error();


TWCR = (1<


return TWDR;

}

//****TWI_ByteR()****//

unsigned char TWI_ByteR(unsigned char add)

{

TWCR = (1<

while(!(TWCR & (1<

     ;

if((TWSR & 0xF8) != START)                //check

     Error();


TWDR = SLA_W;                             //send SLA_W

TWCR = (1<

while(!(TWCR & (1<

     ;

if((TWSR & 0xF8) != MT_SLA_ACK)           //check

     Error();


TWDR = add;                               //send address

TWCR = (1<

while(!(TWCR & (1<

     ;

if((TWSR & 0xF8) != MT_DATA_ACK)          //check

     Error();


TWCR = (1<

while(!(TWCR & (1<

     ;

if((TWSR & 0xF8) != ReSTART)              //check

     Error();


TWDR = SLA_R;                             //send SLA_R

TWCR = (1<

while(!(TWCR & (1<

     ;

if((TWSR & 0xF8) != MR_SLA_ACK)           //check

     Error();


TWCR = (1<

while(!(TWCR & (1<

     ;

if((TWSR & 0xF8) != MR_DATA_NACK)         //check

     Error();


TWCR = (1<


return TWDR;

}

void main(void)

{

unsigned char data;


CLI();

port_init();

USART_Init();

SEI();


TWI_ByteW(0x00,0xA0);

TWI_ByteW(0x01,0xA1);

TWI_ByteW(0x02,0xA2);

TWI_ByteW(0x03,0xA3);

TWI_ByteW(0x04,0xA4);

TWI_ByteW(0x05,0xA5);

TWI_ByteW(0x06,0xA6);

TWI_ByteW(0x07,0xA7);

TWI_ByteW(0x08,0xA8);

TWI_ByteW(0x09,0xA9);

USART_Transmit(TWI_ByteR(0x00));

USART_Transmit(TWI_ByteR(0x01));

USART_Transmit(TWI_ByteR(0x02));

USART_Transmit(TWI_ByteR(0x03));

USART_Transmit(TWI_ByteR(0x04));

USART_Transmit(TWI_ByteR(0x05));

USART_Transmit(TWI_ByteR(0x06));

USART_Transmit(TWI_ByteR(0x07));

USART_Transmit(TWI_ByteR(0x08));

USART_Transmit(TWI_ByteR(0x09));

}


关键字:AVR  M48  i2c程序  读写at24c02 


『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
浅谈AVR中定时器几种工作模式