⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 copy of example10.c

📁 source code for I square c (I2c) AVR mega16
💻 C
字号:
//EXMPALE10.C
// I square c
#define W 		0
#define R 		1
#define OWN_ADR 	60
#define SUCCESS 	0xFF

#define START  	       0x08
#define	REP_START      0x10

#define	MTX_ADR_ACK	            0x18
#define	MTX_ADR_NACK	        0x20
#define	MTX_DATA_ACK	        0x28

#define	MRX_ADR_ACK	            0x40
#define	MRX_DATA_ACK	        0x50
#define	MRX_DATA_NACK	        0x58

unsigned char Send_start(void);
void Wait_TWI_int(void);
void Send_stop(void);
unsigned char Send_byte(unsigned char);
unsigned char Get_byte(unsigned char*);
unsigned char Send_adr(unsigned char);
void Reset_TWI (void);
char Init_TWI(void);

#define TWINT 7
#define TWEA 6
#define TWSTA 5
#define TWSTO 4
#define TWEN 2

unsigned char AT24C64_WriteByte(unsigned int address, unsigned char tdata);
unsigned char AT24C64_ReadByte(unsigned int address, unsigned char *tdata);
#define EEPROM_ID 0xA0

void setup_AVR();

#include <mega16.h>
#include<delay.h>
#include<stdio.h>

void main(void)
{
unsigned char data_array[100];
unsigned char ans,dummy;
int i;
unsigned char state = SUCCESS;
unsigned char data1 = 0;
unsigned char data2 = 0;
unsigned int address = 0;

setup_AVR();

Init_TWI();

while (1)
    {
	        printf("\rRead or Write? r/w \r");
            ans = getchar();
            if(ans == 'w')
            {
              printf("\rEnter the Address followed by Data: \r");
              if(scanf("%d %d", &address, &data1)== 2)// be ma ennou ana 3am ba3mil %d%d hatraje3 2
                state =  AT24C64_WriteByte(address, data1);
            }
            if(ans == 'r')
            {
              printf("\rEnter the Address: \r");
	      if(scanf("%d", &address) == 1)
                 state =  AT24C64_ReadByte(address, &data2);
	      printf("\rData at %d is %d \r",address, data2);
	    }
    }
}


void setup_AVR()
{
int i;
PORTA=0xFF;
DDRA=0xFF;

UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x2F;

}


char Init_TWI(void)
{
  	TWAR = OWN_ADR;
	TWBR = 12;
	TWCR = (1<<TWEN);
    return 1;
}


unsigned char	Send_start(void)
{
	TWCR = ((1<<TWINT)+(1<<TWSTA)+(1<<TWEN));
	Wait_TWI_int();
        if(((TWSR & 0xF8) != START)&&((TWSR & 0xF8) != REP_START))
		return TWSR;

	return SUCCESS;
}

void Wait_TWI_int(void)
{
	while (!(TWCR & (1<<TWINT)));// talama innha manna equal 1 khallik bi nagess al sater
}

void Send_stop(void)
{
	TWCR = ((1<<TWEN)+(1<<TWINT)+(1<<TWSTO));
}


unsigned char Send_byte(unsigned char data)
{
	Wait_TWI_int();

	TWDR = data;
 	TWCR = ((1<<TWINT)+(1<<TWEN));

	Wait_TWI_int();

	if((TWSR & 0xF8) != MTX_DATA_ACK)
		return TWSR;
	return SUCCESS;
}


unsigned char Send_adr(unsigned char adr)
{
	Wait_TWI_int();

	TWDR = adr;
	TWCR = ((1<<TWINT)+(1<<TWEN));

	Wait_TWI_int();

	if(( (TWSR & 0xF8) != MTX_ADR_ACK)&&((TWSR & 0xF8) != MRX_ADR_ACK))
		return TWSR;
	return SUCCESS;
}

unsigned char Get_byte(unsigned char *rx_ptr)by reference llilli waraha deghre ll bein haydoul ()
{
	Wait_TWI_int();

     		TWCR = ((1<<TWINT)+(1<<TWEN));
	Wait_TWI_int();

	*rx_ptr = TWDR;// 3ala shan traje3 tnein [note  c lang ma btrage3 gheir ]


 	if((((TWSR & 0xF8) == MRX_DATA_NACK))||((TWSR & 0xF8) == MRX_DATA_ACK))
		return SUCCESS;
	return TWSR;
}

unsigned char AT24C64_WriteByte(unsigned int address, unsigned char tdata)
{
unsigned char state;
unsigned char MSByte;
unsigned char LSByte;
state = SUCCESS;
state = Send_start();
   if (state == SUCCESS)//ma kafit al amer al tani illa ma 3milit check la l tahet 
    state = Send_adr(EEPROM_ID|W);

MSByte = (address >> 8) & 0x00FF;
LSByte = address & 0x00FF;
state = Send_byte(MSByte);
state = Send_byte(LSByte);
state = Send_byte(tdata);
Send_stop();
return (state);
}

unsigned char AT24C64_ReadByte(unsigned int address, unsigned char *tdata)
{
unsigned char state;
unsigned char MSByte;
unsigned char LSByte;
unsigned char data_temp;
state = SUCCESS;
state = Send_start();
   if (state == SUCCESS)
     state = Send_adr(EEPROM_ID|W);
MSByte = (address >> 8) & 0x00FF;
LSByte = address & 0x00FF;
state = Send_byte(MSByte);
state = Send_byte(LSByte);
state = Send_start();
   if (state == SUCCESS)
    state = Send_adr(EEPROM_ID|R);

state = Get_byte(&data_temp);

*tdata = data_temp;

Send_stop();
return (state);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -