problems with for/next loop

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

problems with for/next loop

2,081 Views
stevec
Contributor III
I don't know if this is the correct forum for this but here goes. I am using the MC9S08RE16 processor with Code Warrior. I am reading the contents of an EEPROM and sending it out the serial port (both of which work o/k). I am using a for() loop to increment the EEPROM read address and this works ok until the address gets to more than 8 bits long. When the for loop is set to terminate at any value over 255 it does not terminate but just keeps going. I am fairly new to this processor and Code Warrior. I have included the routine with the for() loop in and also the routine that this calls. The loop calls a read routine and when this call is commented out the loop terminates ok. If it is left in it doesn't. It looks like the high byte of the count variable (an int) is being corrupted.

//############################################################
//unsigned char random_read(unsigned int)
//reads a byte from EEPROM at address passed in
//returns byte read
//###########################################################
unsigned char random_read(unsigned int eeprom_address)
{
    unsigned char eeprom_byte;
       
    i2c_start();
    i2c_write(0xa0);

    i2c_write(eeprom_address >> 8);
    i2c_write(eeprom_address % 256);
    i2c_start();

    i2c_write(0xa1);
    eeprom_byte = i2c_read();
    send_i2c_ack(1);
    i2c_stop();
    return(eeprom_byte);
}
//==========================================================



//###############################################################
//void verify_EEPROM(void)
//
//
//##############################################################
void verify_EEPROM(void)
{
    unsigned int i;
    unsigned char  val;

    val = 0x31;

    for(i = 0; i < 0x200; i++)
    {
    //    val = random_read(i);     //**************
        send_serial_char(val);    //send to serial port
    }
}
//==============================================================

With the call to read the EEPROM (marked with ********) commented out the loop terminates. If it is put back in it doesnt so something in the called routine must be corrupting the count. Can anyone shed some light on this?

Steve

Labels (1)
0 Kudos
3 Replies

224 Views
CompilerGuru
NXP Employee
NXP Employee
The code looks ok, but I wonder if it could be the COP? It does not seem to be handled in this loop.
Also as you do allocate >256 bytes on the stack on another thread, did you increase the stack size enough?

Daniel
0 Kudos

224 Views
stevec
Contributor III
thanks for that, I'll check.
I've done some more detailed testing. The fall over point is when the for loop is set
    for(i = 0; i < 0x1fa; i++)

If set at 0x1f9...it's ok...0x1fa it falls over.

I recon you're probably right on the COP. I'll check it out.

Steve

0 Kudos

224 Views
stevec
Contributor III
Daniel,
Yep, you were right about the COP. Works fine now. The COP is a new thing for me so I'll have to stick a notice on my monitor to "remember to feed the 'dog". Thanks again

Steve
0 Kudos