Search code examples
cpointersmsp430iarlcd

MSP430 i var randomly resets at value 0 in middle of loop


So I got this very strange problem that happens every time. I am trying to interface and LCD with the MSP430 module. But in this function at the middle of the loop, the variable i resets itself to 0 for no apparent reason at all, somethimes it even crashes.
This is the structure of the lcd_t struct:

    struct lcd_t
{   
    uint8_t rs, rw, e;
    uint8_t pin[8];
};

This is the function where i is defined and "reseted":

    void
lcd_dat(const struct lcd_t* lcd, uint8_t dat)
{
  static uint8_t i;
  
  // Setting RS.
  //
  lcd_change_pin_state(lcd->rs, 1);
  
  // Setting each data pin to match the dat.
  //
  for(i = 0; i < 8; i++) // TODO: Four bit mode
  {
    
    if(dat & (1 << i))
    {
      lcd_change_pin_state(lcd->pin[i], 1);
    }
    
    else
    {
      lcd_change_pin_state(lcd->pin[i], 0); <-- This is where i resets
    }
    
  }
  
  // Setting E.
  //
  lcd_change_pin_state(lcd->e, 1);
  
  __delay_cycles(2*1000);
  
  // Clearing E.
  //
  lcd_change_pin_state(lcd->e, 0);
  
}

This is the function where i resets:

static volatile void
lcd_change_pin_state(uint8_t pin, uint8_t newstate)
{
  
  if(newstate == 0)
  {
    
    if(pin/10 == 1)
    {
      P1OUT &= ~(1 << (pin % 10));
    }
    
    else
    {
      P2OUT &= ~(1 << (pin % 10));
    }
    
  }
  
  else
  {
    
    if(pin/10 == 1)
    {
      P1OUT |= (1 << (pin % 10));
    }
    
    else
    {
      P2OUT |= (1 << (pin % 10));
    }
    
  }
  
}

Plese tell me what other information do you need! Thanks!


Solution

  • WDTCTL = WDTPW + WDTHOLD; // stop watchdog timer
    

    This instruction was missing...
    I can't even think how much time was lost because of this...