在MSP430的闪存中写入增量值(Cold start Power on Count)

Write a incremental value in flash memory of MSP430 (Cold start Power on Count)

我正在尝试实现开机计数。 每当产品通电时它都会计数。 所以我把这个写在MSP430F249的闪存上; 我想为计数的写入值制作 2bytes 但是,我无法写入值 像 00 01、00 02、00 03、00 04,....

    uint16_t *Flash_ptr;                          // Flash pointer

    Flash_ptr = (char *)0x1040;               // Initialize Flash pointer
    FCTL3 = FWKEY;                            // Clear Lock bit
    FCTL1 = FWKEY + ERASE;                    // Set Erase bit
    *Flash_ptr = 0;                           // Dummy write to erase Flash seg

    FCTL1 = FWKEY + WRT;                      // Set WRT bit for write operation

   
    Flash_ptr = 0x0001;                   // Write value to flash
    

    FCTL1 = FWKEY;                            // Clear WRT bit
    FCTL3 = FWKEY + LOCK;                     // Set LOCK bit

闪存初始化有问题

         // Initialize Flash memory
        uint16_t *Flash_ptr;                          // Flash pointer
        uint16_t power_on_count;
        uint16_t endian_swap;

        Flash_ptr = (uint16_t *)0x1040;               // Initialize Flash pointer

        // read power on count from flash memory and add 1
        while ((FCTL3 & BUSY) != 0);
        power_on_count = *Flash_ptr;
        power_on_count = power_on_count + 0x0001;
        endian_swap = power_on_count >> 8 | power_on_count <<8 ;
        g_stCold_start_cnt.cold_start_cnt = (uint16_t)endian_swap;

        // write an increased power on count on the flash memory
        FCTL3 = FWKEY;                            // Clear Lock bit
        FCTL1 = FWKEY + ERASE;                    // Set Erase bit
        *Flash_ptr = 0;                           // Dummy write to erase Flash seg

        FCTL1 = FWKEY + WRT;                      // Set WRT bit for write operation

        while ((FCTL3 & BUSY) != 0);

        if(power_on_count == 0xFF)
        {
            power_on_count = 0;
        }

         *Flash_ptr = power_on_count;                   // Write value to flash

        FCTL1 = FWKEY;                            // Clear WRT bit
        FCTL3 = FWKEY + LOCK;                     // Set LOCK bit