Problem On SPIC

Go To Last Post
22 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Hello all,

I want to write 128byte to AT24C512 on Page0

but after writing about 30 byte the program stop and this is repeating  constantly.

I guess probably one Interrupt is cause of the problem

can every body help me?

my compiler is CodeVision 14.3

void twic_init(void)
{
// General TWIC initialization
// External Driver Interface: Off
// SDA Hold: Off
twi_init(&TWIC,false,TWI_SDAHOLD_OFF_gc);

// TWIC Master initialization
// Master interrupt: High Level
// Peripheral Clock frequency: 2000000 Hz
// SCL Rate: 100000 bps
// Real SCL Rate: 100000 bps, Error: 0.0 %
twi_master_init(&twic_master,&TWIC,TWI_MASTER_INTLVL_HI_gc,
    TWI_BAUD_REG(2000000,200000));

// TWIC Slave is disabled
TWIC.SLAVE.CTRLA=0;
}

// TWIC Master interrupt service routine
#pragma optsize- // optimize for speed
interrupt [TWIC_TWIM_vect] void twic_master_isr(void)
{
twi_master_int_handler(&twic_master);
 //PORTB_OUTTGL=0b000000010;
 //delay_ms(20);
}
#pragma optsize_default
#define EEPROM_TWI_BUS_ADDRESS (0b01010010 >> 0)
//#define EEPROM_TWI_BUS_ADDRESS (0b1101000 >> 0)
unsigned char eeprom_rd_data,page_number=0,temp_page=0,i=0;
struct
     {
     struct
          {
          unsigned char msb;
          unsigned char lsb;
          } addr;
     unsigned char data[128];
     } twi_eeprom;
     

void main(void){
{
// Declare your local variables here
unsigned char n;

// Interrupt system initialization
// Optimize for speed
#pragma optsize- 
// Make sure the interrupts are disabled
#asm("cli")
// Low level interrupt: Off
// Round-robin scheduling for low level interrupt: Off
// Medium level interrupt: Off
// High level interrupt: On
// The interrupt vectors will be placed at the start of the Application FLASH section
n=(PMIC.CTRL & (~(PMIC_RREN_bm | PMIC_IVSEL_bm | PMIC_HILVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_LOLVLEN_bm))) |
    PMIC_HILVLEN_bm;
CCP=CCP_IOREG_gc;
PMIC.CTRL=n;
// Set the default priority for round-robin scheduling
PMIC.INTPRI=0x00;
// Restore optimization for size if needed
#pragma optsize_default

// System clocks initialization
system_clocks_init();

// Ports initialization
ports_init();

// Virtual Ports initialization
vports_init();

// TWIC initialization
twic_init();

// Globally enable interrupts
#asm("sei")
}

        for(i=0;i<128;i++){
        twi_eeprom.data[i]=i;
        PORTB_OUTTGL=0b00000100;
        delay_ms(20);
        }
        
        PORTA_OUT=0b00100000;
        PORTR_OUTSET=0b00000010;

while (1)
      {
        PORTA_OUTTGL=0b00000001;
        delay_ms(20);
        temp_page=page_number<<6;               //page start address
        twi_eeprom.addr.msb=(temp_page>>8);
        twi_eeprom.addr.lsb=(temp_page & 0x00FF); 
        twi_master_trans(&twic_master,EEPROM_TWI_BUS_ADDRESS,(unsigned char *) &twi_eeprom,2+128,0,0);
        

      }

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For the benefit of others the readable version of that with the indentation restored is:

void twic_init(void)
{
	// General TWIC initialization
	// External Driver Interface: Off
	// SDA Hold: Off
	twi_init(&TWIC,false,TWI_SDAHOLD_OFF_gc);

	// TWIC Master initialization
	// Master interrupt: High Level
	// Peripheral Clock frequency: 2000000 Hz
	// SCL Rate: 100000 bps
	// Real SCL Rate: 100000 bps, Error: 0.0 %
	twi_master_init(&twic_master,&TWIC,TWI_MASTER_INTLVL_HI_gc,
	TWI_BAUD_REG(2000000,200000));

	// TWIC Slave is disabled
	TWIC.SLAVE.CTRLA=0;
}

// TWIC Master interrupt service routine
#pragma optsize- // optimize for speed
interrupt [TWIC_TWIM_vect] void twic_master_isr(void)
{
	twi_master_int_handler(&twic_master);
	//PORTB_OUTTGL=0b000000010;
	//delay_ms(20);
}
#pragma optsize_default
#define EEPROM_TWI_BUS_ADDRESS (0b01010010 >> 0)
//#define EEPROM_TWI_BUS_ADDRESS (0b1101000 >> 0)
unsigned char eeprom_rd_data,page_number=0,temp_page=0,i=0;
struct
{
	struct
	{
		unsigned char msb;
		unsigned char lsb;
	} addr;
	unsigned char data[128];
} twi_eeprom;


void main(void){
	{
		// Declare your local variables here
		unsigned char n;

		// Interrupt system initialization
		// Optimize for speed
		#pragma optsize-
		// Make sure the interrupts are disabled
		#asm("cli")
		// Low level interrupt: Off
		// Round-robin scheduling for low level interrupt: Off
		// Medium level interrupt: Off
		// High level interrupt: On
		// The interrupt vectors will be placed at the start of the Application FLASH section
		n=(PMIC.CTRL & (~(PMIC_RREN_bm | PMIC_IVSEL_bm | PMIC_HILVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_LOLVLEN_bm))) |
		PMIC_HILVLEN_bm;
		CCP=CCP_IOREG_gc;
		PMIC.CTRL=n;
		// Set the default priority for round-robin scheduling
		PMIC.INTPRI=0x00;
		// Restore optimization for size if needed
		#pragma optsize_default

		// System clocks initialization
		system_clocks_init();

		// Ports initialization
		ports_init();

		// Virtual Ports initialization
		vports_init();

		// TWIC initialization
		twic_init();

		// Globally enable interrupts
		#asm("sei")
	}

	for(i=0;i<128;i++){
		twi_eeprom.data[i]=i;
		PORTB_OUTTGL=0b00000100;
		delay_ms(20);
	}
	
	PORTA_OUT=0b00100000;
	PORTR_OUTSET=0b00000010;

	while (1)
	{
		PORTA_OUTTGL=0b00000001;
		delay_ms(20);
		temp_page=page_number<<6;               //page start address
		twi_eeprom.addr.msb=(temp_page>>8);
		twi_eeprom.addr.lsb=(temp_page & 0x00FF);
		twi_master_trans(&twic_master,EEPROM_TWI_BUS_ADDRESS,(unsigned char *) &twi_eeprom,2+128,0,0);
		

	}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The AT24C512 has got a 128 byte page.

 

	while (1)
	{
		PORTA_OUTTGL=0b00000001;
		delay_ms(20);
		temp_page=page_number<<6;               //page start address
		twi_eeprom.addr.msb=(temp_page>>8);
		twi_eeprom.addr.lsb=(temp_page & 0x00FF);
		twi_master_trans(&twic_master,EEPROM_TWI_BUS_ADDRESS,(unsigned char *) &twi_eeprom,2+128,0,0);
	}

So your loop is selecting the SAME page in every loop i.e. Page#0

It should write to the 24C512 just fine.  The Page Write takes about 3ms on this chip.  You allow 20ms which is plenty enough time.

 

The chip is rated for a million Page Writes (I think).

So after 20000s (56 hours) I would expect the chip might exhibit write errors.

 

David.

 

Last Edited: Thu. Nov 14, 2019 - 12:32 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:

The AT24C512 has got a 128 byte page.

 

	while (1)
	{
		PORTA_OUTTGL=0b00000001;
		delay_ms(20);
		temp_page=page_number<<6;               //page start address
		twi_eeprom.addr.msb=(temp_page>>8);
		twi_eeprom.addr.lsb=(temp_page & 0x00FF);
		twi_master_trans(&twic_master,EEPROM_TWI_BUS_ADDRESS,(unsigned char *) &twi_eeprom,2+128,0,0);
	}

So your loop is selecting the SAME page in every loop i.e. Page#0

It should write to the 24C512 just fine.  The Page Write takes about 3ms on this chip.  You allow 20ms which is plenty enough time.

 

The chip is rated for a million Page Writes (I think).

So after 20000s (56 hours) I would expect the chip might exhibit write errors.

 

David.

 

 

thank you david.prentice ,

but in 1 period I have 1 page writing ,

for example page0

but It's bytes (128 byte) not will be complet,

because AT24C512 will be Nack On Byte 30 or 31 or 29...

I am using this device only 1 hour ...

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is it a real at24c512 chip? It could be a counterfeit chip.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
struct
{
	struct
	{
		unsigned char msb;
		unsigned char lsb;
	} addr;
	unsigned char data[128];
} twi_eeprom;

Your struct looks big enough.

I will try this for myself.

As far as I know,  CV uses the buffers that you give it.   Admittedly,

bool twi_master_trans(

            TWI_MASTER_INFO_t *twi,

            unsigned char slave_addr,

            unsigned char *tx_data,

            unsigned char tx_count,

            unsigned char *rx_data,

            unsigned char rx_count)

 

            performs a TWI transaction using the master module.

 

means that your tx_count must be 0-255.  But you are using 130

 

I have CV v3.37 on my PC.   I am not aware of the twi_master_trans() having had any historic issues.

 

As always.   If a library function gives a return value,   USE IT.

 

David.

Last Edited: Thu. Nov 14, 2019 - 12:53 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
Is it a real at24c512 chip? It could be a counterfeit chip.

 

Yes I have been changed it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:

struct
{
	struct
	{
		unsigned char msb;
		unsigned char lsb;
	} addr;
	unsigned char data[128];
} twi_eeprom;

Your struct looks big enough.

I will try this for myself.

As far as I know,  CV uses the buffers that you give it.   Admittedly,

bool twi_master_trans(

            TWI_MASTER_INFO_t *twi,

            unsigned char slave_addr,

            unsigned char *tx_data,

            unsigned char tx_count,

            unsigned char *rx_data,

            unsigned char rx_count)

 

            performs a TWI transaction using the master module.

 

means that your tx_count must be 0-255.  But you are using 130

 

I have CV v3.37 on my PC.   I am not aware of the twi_master_trans() having had any historic issues.

 

As always.   If a library function gives a return value,   USE IT.

 

David.

 

Yes,But The EE2prom first sent a Nack then TWI will be Stop Process.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What is the full part number written on the chip?

What CV version are you using?

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:

What is the full part number written on the chip?

What CV version are you using?

 

David.

 

CV is 3.14

Chip is:

ATMLH905

2FCD CN

+19089..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The Important Note is that:

When I change TWI baud Rate The Total time that At24C512 react ACK is about 3.7ms

It means after this time It will be NACK Status.

by increase Baud rate this time is constant and data that can send will be increase to 3.7ms.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I ran your program with an 24C1025 and it worked fine.

 

Note that the EEPROM will not ACK while it is busy with a Page-Write.

 

It is always wise to attach your complete project.   You don't have clock init code.  I assume default 2MHz.

 

I am not sure about your part number.   Atmel datasheets suggest different numbers.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:

I ran your program with an 24C1025 and it worked fine.

 

Note that the EEPROM will not ACK while it is busy with a Page-Write.

 

It is always wise to attach your complete project.   You don't have clock init code.  I assume default 2MHz.

 

I am not sure about your part number.   Atmel datasheets suggest different numbers.

 

David.

 

 

thank you

I found the problem,

When I connect Oscilloscope or Logic Analyzer to SCL Pin this case will be Issue.(At24C512 can not receive more than 33 byte)

I guest this is because the Oscilloscope or logic analyzer has capacitor on the their input. but I can not analyze how this capacitor can destroy SCL line.

Can every body describe this problem?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thank you

I found the problem,

When I connect Oscilloscope or Logic Analyzer to SCL Pin this case will be Issue.(At24C512 can not receive more than 33 byte)

I guest this is because the Oscilloscope or logic analyzer has capacitor on the their input. but I can not analyze how this capacitor can destroy SCL line.

Can every body describe this problem?

 

 

and by touching SCL pin the program will be stop.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You should always use x10 probe with an oscilloscope.

The Logic Analyser probably has a lower load capacitance.

 

Neither should matter on a proper I2C bus.   e.g. with 2k2 external pullup resistors to 3.3V

 

I still recommend that you attach your project instead of pasting 90%.

 

Sometimes pasting a snippet is appropriate.   Other times it is worth attaching the complete project.   e.g. ZIP up the specific AS7.0 project directory.

 

David.

 

Oops.  Looking at the AT24C512C datasheet:

ATMLH905    SOIC-8 package. Year 2019   Week 05

2FCD CN     24C512 min 2.5V China

+19089..    Wafer Lot Number

 

Last Edited: Thu. Nov 14, 2019 - 05:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:

You should always use x10 probe with an oscilloscope.

The Logic Analyser probably has a lower load capacitance.

 

Neither should matter on a proper I2C bus.   e.g. with 2k2 external pullup resistors to 3.3V

 

I still recommend that you attach your project instead of pasting 90%.

 

Sometimes pasting a snippet is appropriate.   Other times it is worth attaching the complete project.   e.g. ZIP up the specific AS7.0 project directory.

 

David.

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When I aconnect Oscilloscope or touch SCL pin

the program will be drop out from while

why?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for posting the C file.

 

I can not remember CV v3.14

Did it create separate port_init.c file ?

Did it have a "Format source code" in the Edit Menu?

 

If you do have "Format code" I strongly advise you to use it.

 

I have already dismantled Xmega, ATMEL-ICE, Logic Analyser.

Have you got proper pullup resistors?

I am sure the program will be fine.

 

David.

Last Edited: Thu. Nov 14, 2019 - 05:25 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hasanzadeh99 wrote:
I guest this is because the Oscilloscope or logic analyzer has capacitor on the their input.
and inductance (ground lead)

hasanzadeh99 wrote:
but I can not analyze how this capacitor can destroy SCL line.
Excessive ground bounce will EOS the XMEGA's ESD suppressors.

XMEGA have impressive drive (a 1K ohm resistor into a 50 ohm scope input, logic analyzers are 100K ohm typical input)

 


OSCILLOSCOPE PROBE ACCESSORIES | Tyrosys Online Shop | Tyrosys Corp | Apple Authorized Service Provider

probe tip spring into a nearby plated via to ground that's un-tented, or, BNC to probe tip adapter to further minimize inductance.

 

edit :

DC to 1GHz Probe by Douglas C. Smith

 

"Dare to be naïve." - Buckminster Fuller

Last Edited: Thu. Nov 14, 2019 - 05:38 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:

Thanks for posting the C file.

 

I can not remember CV v3.14

Did it create separate port_init.c file ?

Did it have a "Format source code" in the Edit Menu?

 

If you do have "Format code" I strongly advise you to use it.

 

I have already dismantled Xmega, ATMEL-ICE, Logic Analyser.

Have you got proper pullup resistors?

I am sure the program will be fine.

 

David.

 

thank you David

What is the "Format source code" ??

following pic is my CV edit Menu.

and David I change resistor from47k to 8k and this problem has been solved.

but when I touch SCL pin the program will be pause

it means toggling LED will be discard.

why?

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I2C pullups should be 2k2. 8k will work but I have never heard of an 8k resistor. 47k is NOT wise.
.
Formatting code makes it easy to read. Indenting blocks of code. Putting spaces around operators e.g. =
You can do it by hand. Or with an IDE or external program.
.
David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:

I2C pullups should be 2k2. 8k will work but I have never heard of an 8k resistor. 47k is NOT wise.
.
Formatting code makes it easy to read. Indenting blocks of code. Putting spaces around operators e.g. =
You can do it by hand. Or with an IDE or external program.
.
David.

 

thank you david,