Steve Church

problems with for/next loop

Discussion created by Steve Church on Jun 20, 2007
Latest reply on Jun 20, 2007 by Steve Church
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

Outcomes