1

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!

  • 1
    Should you have updated the source of the `dat` argument to reflect the change in status? – Weather Vane May 30 '21 at 12:27
  • 2
    This usually occurs when an error elsewhere overwrite the variable value because in that other place, for example, you access an array out of his bounds, or you use a pointer not pointing where you think it does. By the way, why do you use a local static variable for that loop? – fpiette May 30 '21 at 12:28
  • It is also possible that you use lcd_dat from an interrupt routine and since the variable `i` is static, the same variable is used in the interrupt routine and in the other instance running. This would also happens with multi-threading. – fpiette May 30 '21 at 12:32
  • @fpiette Sorry, I made 'i' static in my hopeless attempt to fix the problem when i ran out of ideas. Making it back to non-static does not solve the problem. The lcd_t lcd whoose pointer i am sending is also static for stack economy reasons. – Paul Schuldesz May 30 '21 at 13:25
  • As an update. In the Proteus 8 Simulator, it works no problem – Paul Schuldesz May 30 '21 at 13:25
  • @WeatherVane No, the dat argumment contains a letter (ex 'A') and it's content should not be modified, it could as well be a constant. I just need to have it printed on the lcd. – Paul Schuldesz May 30 '21 at 13:30
  • This is one reason to post the [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) that shows exactly what is happening. – Weather Vane May 30 '21 at 13:33
  • @WeatherVane Sorry, I am a beginner when it coms to explaining the problem as good as possible. I mill make sure my future posts will take the Minimal Reproducible Example into account – Paul Schuldesz May 30 '21 at 13:38
  • 1
    You can start right now. ;-) Cut down your program until it is minimal and still shows the issue. – the busybee May 30 '21 at 13:48
  • Try making `i` `volatile` – Chimera May 30 '21 at 16:31
  • @Chimera Does not solve the problem, sorry... – Paul Schuldesz Jun 01 '21 at 19:05

1 Answers1

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

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

0___________
  • 60,014
  • 4
  • 34
  • 74