Other Parts Discussed in Thread: MSP430G2553
hai,
I want to read and write some data permenently during operation. So I choose flash write and read.I found lot of TI discussion and from that I modified one code. Iam using msp430g2553 and CCS v5
. Iam trying first write a character and comment that line and again upload read code. How can I check my code working or not? my code is pasted below
#include <msp430g2553.h>
char value_wrt=0x52;//character to write
char value_read;
void erase_flash(unsigned short address);
void write_flash (char value,unsigned short address);
unsigned short read_flash (unsigned short address);
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P1DIR =0xff;
P1OUT =0xff;
///////////////////////////////////////////////////////////comment when reading
erase_flash(0x1040); //erase
write_flash (value_wrt,0x1040); ////write flash value
/////////////////////////////////////////////////////////////////comment when writing
// value_read=read_flash (0x1040); ////read flash value
// if(value_read==value_wrt)
// {
// P1OUT =0x00; // if value read ok , led will off
// }
//////////////////////////////////////////////////////////////
return 0;
}
void erase_flash(unsigned short address)
{
__disable_interrupt(); // Disable interrupts. This is important, otherwise,
// a flash operation in progress while interrupt may
// crash the system.
while(BUSY & FCTL3); // Check if Flash being used
FCTL2 = FWKEY + FSSEL_1 + FN3; // Clk = SMCLK/4
char *Flash_ptr;
Flash_ptr = (char*) address;
FCTL1 = FWKEY + ERASE;
FCTL3 = FWKEY;
*Flash_ptr = 0;
while(BUSY & FCTL3); // Check if Flash being used
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCK;
__enable_interrupt();
}
void write_flash (char value,unsigned short address)
{
__disable_interrupt();
char *Flash_ptr;
Flash_ptr = (char*) address;
FCTL2 = FWKEY + FSSEL_1 + FN0; // Clk = SMCLK/4
FCTL3 = FWKEY; // Clear Lock bit
FCTL1 = FWKEY + WRT; // Set WRT bit for write operation
*Flash_ptr = value;
FCTL1 = FWKEY;
FCTL3 = FWKEY + LOCK;
while(BUSY & FCTL3);
__enable_interrupt();
}
unsigned short read_flash (unsigned short address)
{
__disable_interrupt();
FCTL2 = FWKEY + FSSEL_1 + FN0; // Clk = SMCLK/4
FCTL3 = FWKEY; // Clear Lock bit
char value;
char *Flash_ptr;
Flash_ptr = (char*) address;
value = *Flash_ptr;
return (value);
}