Esempio n. 1
0
int32_t screenSpin::Savestr(char* Data)
{
  for(int i=0;i<strlen(Data);i++)
  {
    Writebyte((int)Data[i]);
  }
}
/*
Write n byte data to EEPROM
*/
unsigned char Write_EEPROM_Block(unsigned char DevAddr,unsigned int MemAddr,unsigned char *p,unsigned char num)
{
	unsigned char i;	
	/* Start TWI */
	Start();
	Wait();
	if(TestACK()!=START)
	{
	   return 0;
	}
	/* Write Device Address */
	Writebyte((DevAddr<<1)|(Write));
	Wait();
	if(TestACK()!=MT_SLA_ACK)
	{
	   return 0;
	}
	/* Write Memory Address High Byte) */
	Writebyte(MemAddr>>8);
	Wait();
	if(TestACK()!=MT_DATA_ACK)
	{
		return 0;
	}
	/* Write Memory Address Low Byte) */
	Writebyte(MemAddr&0xff);
	Wait();
	if(TestACK()!=MT_DATA_ACK)
	{
		return 0;
	}
	/* Write Data to EEPROM */
	for(i = 0;i < num;i++){
		Writebyte(*(p+i));
		Wait();
		if(TestACK()!=MT_DATA_ACK)
		{
	   		return 0;
		}
	}

	Stop();
	_delay_ms(10);
	
	return 1;
}
/*
Write 1 byte data to EEPROM
*/
unsigned char WriteEEPROM(unsigned char DevAddr,unsigned int MemAddr,unsigned char data)
{
	/* Start TWI */
	Start();
	Wait();
	if(TestACK()!=START)
	{
	   return 0;
	}
	/* Write Device Address */
	Writebyte((DevAddr<<1)|(Write));
	Wait();
	if(TestACK()!=MT_SLA_ACK)
	{
	   return 0;
	}
	/* Write Memory Address High Byte) */
	Writebyte(MemAddr>>8);
	Wait();
	if(TestACK()!=MT_DATA_ACK)
	{
		return 0;
	}
	/* Write Memory Address Low Byte) */
	Writebyte(MemAddr&0xff);
	Wait();
	if(TestACK()!=MT_DATA_ACK)
	{
		return 0;
	}
	/* Write Data to EEPROM */
	Writebyte(data);
	Wait();
	if(TestACK()!=MT_DATA_ACK)
	{
	   return 0;
	}
	Stop();
	_delay_ms(10);
	
	return 1;
}
/*
Write n byte data to EEPROM
*/
unsigned char Read_EEPROM_Block(unsigned char DevAddr,unsigned int MemAddr,unsigned char *p,unsigned char num)
{
	//unsigned char data;
	unsigned char i = 0;
	
	DevAddr = (DevAddr<<1)|(Write);//????????????????
	/* Start TWI */
	Start();
	Wait();
	if(TestACK()!=START)
	{
	   return 0;
	}
	/* Write Device Address */
	Writebyte(DevAddr);//SLA+W
	Wait();
	if(TestACK()!=MT_SLA_ACK)
	{
	   return 0;
	}
	/* Write Memory Address High Byte) */
	Writebyte(MemAddr>>8);
	Wait();
	if(TestACK()!=MT_DATA_ACK)
	{
		return 0;
	}
	/* Write Memory Address Low Byte) */
	Writebyte(MemAddr&0xff);
	Wait();
	if(TestACK()!=MT_DATA_ACK)
	{
		return 0;
	}
	/* Restart */	
	Start();
	Wait();
	if(TestACK()!=ReStart)
	{
	   return 0;
	}
	/* Write Device Address(read format) */
	Writebyte(DevAddr + 1);//SLW+R
	Wait();
	if(TestACK()!=MR_SLA_ACK)//
	{
	   return 0;
	}
	/* Read MultiByte From EEPROM */
	for(i = 0;i < (num-1);i++){
		/* Receive Data(1 byte only) */
		SetACK();
		Wait();
		if(TestACK()!=MR_DATA_ACK)//
		{
	   		return 0;
		}
		*(p+i) = TWDR;
		//to do (ACK)????	
	}
	ResetACK();
	Wait();
	if(TestACK()!=MR_DATA_NACK)//
	{
	   	return 0;
	}
	*(p+num-1) = TWDR;

	Stop();

	_delay_ms(5);
	
	return 1;

}
/*
Read 1 byte data from EEPROM
*/
unsigned char ReadEEPROM(unsigned char DevAddr,unsigned int MemAddr)
{
	unsigned char data;
	/* Start TWI */
	Start();
	Wait();
	if(TestACK()!=START)
	{
		return 0;
	}	
	/* Write Device Address */
	Writebyte((DevAddr<<1)|(Write));//SLA+W
	Wait();
	if(TestACK()!=MT_SLA_ACK)
	{
		return 0;
	}
	/* Write Memory Address High Byte) */
	Writebyte(MemAddr>>8);
	Wait();
	if(TestACK()!=MT_DATA_ACK)
	{
		return 0;
	}
	/* Write Memory Address Low Byte) */
	Writebyte(MemAddr&0xff);
	Wait();
	if(TestACK()!=MT_DATA_ACK)
	{
		return 0;
	}
	/* Restart */	
	Start();
	Wait();
	if(TestACK()!=ReStart)
	{
		return 0;
	}
	/* Write Device Address(read format) */
	Writebyte((DevAddr<<1)|(Read));//SLW+R
	Wait();
	if(TestACK()!=MR_SLA_ACK)//
	{
		return 0;
	}

	/* added for certian purpose */
	TWCR=(1<<TWINT)|(1<<TWEN);
	Wait();

	/* Receive Data(1 byte only) */

	data = TWDR;

	/* Stop TWI */

	Stop();

	_delay_ms(5);

	return data;
}