Tool/software: Code Composer Studio
Hello, I'm trying to save switch staus in flash memory. So that the next time I turn on the device, I will have previous switch setting. following is the code I'm unable to so, please help me.
#include <msp430.h>
volatile int Rdata;
volatile int Sw1=0;
volatile int Sw2=0;
// Function prototypes
void read(void);
void write(void);
void switching(unsigned int);
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //Disable the Watchdog timer for convenience.
P1DIR |= BIT0; //Set pin 4.1 to the output direction.
P1DIR |= BIT1;
// P4SEL |= BIT1; //Select pin 4.1 as our PWM output
//configuring the switch
P2REN |= (BIT7 + BIT6);
P2OUT |= (BIT7 + BIT6);
P2IE |= (BIT7 + BIT6);
P2IES |= (BIT7 + BIT6);
P2IFG &= ~(BIT7 + BIT6);
__enable_interrupt();
while (1)
{
read();
if (Sw1 == 0)
{
switching(Sw2); //turn the led according to the value of counter SW2
}
else if (Sw1 == 1)
{
switching(Rdata); // turn the led according to value of counter SW2 written on flash memory when SW1=1;
write();
}
}
}
void switching(unsigned int a)
{
if (a == 0)
{
P1OUT = BIT0;
}
else if (a == 1)
{
P1OUT = BIT1;
}
}
void write(void)
{
__disable_interrupt(); // Disable interrupts
int *Flash_ptr = (int *) 0x0E000;
while (BUSY & FCTL3)
; // Check if Flash being used
FCTL1 = FWKEY + ERASE; // Set Erase bit
FCTL3 = FWKEY; // Clear Lock bit
*Flash_ptr = 0; // Dummy write to erase Flash segment
while (BUSY & FCTL3)
; // Check if Flash being used
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCK; // Set LOCK bit
FCTL1 = FWKEY + WRT; // Set WRT bit for write operation
while ((FCTL3 & BUSY) != 0)
;
*Flash_ptr= Sw2; // Write value to flash
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCK; // Set LOCK bit
__enable_interrupt();
}
void read(void)
{
int *Flash_ptr = (int *) 0x0E000;
while ((FCTL3 & BUSY) != 0)
;
Rdata = *Flash_ptr;
}
#pragma vector=PORT2_VECTOR
__interrupt
void Port_2(void)
{
switch (__even_in_range(P2IV, 0x10))
{
case 0x00:
break; //None
case 0x02:
break; //Pin0
case 0x04:
break; //Pin1
case 0x06:
break; //pin2
case 0x08:
break; //pin3
case 0x0A:
break; //pin4
case 0x0C:
break; //pin5
case 0x0E:
{
if (Sw1 < 1)
{
Sw1++;
}
else
{
Sw1 = 0;
}
P2IFG &= ~BIT6;
break;
}
case 0x10:
{
//pin7
if (Sw2 < 1)
{
Sw2++;
}
else
{
Sw2 = 0;
}
P2IFG &= ~BIT7;
break;
}
}
}
