This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Changing certain bits in password protected register

Hello everyone,

I don't know how to set/clear only one bit into a password protected register such as WDTCTL. So here is the issue:

If I write: WDTCTL = WDTPW + WDTCNTCL; I will clear the WDT counter register, but I will also clear bits as WDTIS, WDTSSEL, which is undesired. I want to reset the counter and all the other bits to remain unchanged. Is there any technique to work around this problem?

 

  • In general, you need to read the existing value of password protected register, modify the bit/bits you desired, and write it back with the password.

    But in the special case of clearing the count in WDT, you could use: WDTCTL ^= 0x3308;
  • old_cow_yellow said:
    you could use: WDTCTL ^= 0x3308

    Nice creative solution, can see the sea waves in it!

    But I prefer to create a definition with the WatchDog content and load at boot and reload this before expiration and so saving a little bit of code space.

    #define	WatchDog	(WDTPW | WDTCNTCL | WDTSSEL)
    
    	WDTCTL = WatchDog;
    

  • #define FXKEY (0x3300u) /* for use with XOR instruction */

    Password write protected registers reads a different value back than the 0x5A you wrote, it reads 0x69
    So if you XOR it with 0x33 the read-and-modify instruction will still work and will not cause a reset due to password error.
    The above define belongs to the FlashWrite group that also uses password, but can be used with WDT too

    There is 16 pre-defined WDT in IAR already, example1:
    #define WDT_MDLY_32         (WDTPW+WDTTMSEL+WDTCNTCL)                         /* 32ms interval (default) */

  • Leo,

    Be aware that the OP says: "I want to reset the counter and all the other bits to remain unchanged.".

    --OCY

  • old_cow_yellow said:
    Be aware that the OP says: "I want to reset the counter and all the other bits to remain unchanged.".

    OCY,

    I think I read Radoslav problem more carefully;

    Radoslav Marinov said:
    If I write: WDTCTL = WDTPW + WDTCNTCL; I will clear the WDT counter register, but I will also clear bits as WDTIS, WDTSSEL, which is undesired. I want to reset the counter and all the other bits to remain unchanged. Is there any technique to work around this problem?

    Solution to this is not only write ‘WDTCNTCL’ but also write all other bits again.

    And on the other hand, initializing the WatchDog and re-writing the WatchDog in a loop with the same value results in a “Counter clear and all other bits remain Unchanged”.

    Leo.

  • >not only write ‘WDTCNTCL’ but also write all other bits again.
    If one part of the software is changing SEL and one other part is CNTCL independent of what the SEL settings are.
    Using a global variable that stores it so you can andn/or on that variable and final stage use = (e.g mov.w) is one solution.

    int WDT_Settings = WDTPW;
    WDT_Settings |= WDTCNTCL;
    WDTCTL = WDT_Settings;

  • Leo is okay, Tony is okay, and I am okay too.

    Leo is okay because he knows what bits are set in the WDTCTL..

    Tony is okay because he keeps track of what bits are set in the WDTCTL.

    I am okay because I do not change any bit in the WDTCTL.

**Attention** This is a public forum