写到RAM里很简单,定义一个指针直接读写就好了:
- int *a = (int *)0x0300H;
- *a = 345;
写到Flash是个相对复杂的问题,要允许Flash的写入考虑到Flash的写入时间,同时还要考虑到Flash是否已经擦除等等,下面的代码来自TI写Flash的示例,楼主可以参考一下
- /***********************************************************************/
- /* flash_idle.c 2000-06-20 */
- /* */
- /* Flash erase and program functions */
- /* */
- /* Below functions using the direct Flash programming algorithm. */
- /* After starting a flash write or erase cycle, the CPU will wait */
- /* until the flash is read-accessable again, so no program must be */
- /* copied into RAM. However, during flash programming, the CPU is */
- /* in ”idle” mode. */
- /* */
- /* Note: Since all interrupt vectors are unavailable during flash */
- /* programming, all interrupts must be disabled. */
- /* */
- /* Anton Muehlhofer Texas Instruments Incorporated */
- /***********************************************************************/
- #define _CPU_ 5 /* 5=MSP430F1121, 6=MSP430F149 device */
- #include <std_def.h> /* ports */
- #include ”flash_prog.h” /* function prototypes */
- /***********************************************************************/
- /* Flash_wb */
- /* programs 1 byte (8 bit) into the flash memory */
- /***********************************************************************/
- void Flash_wb( char *Data_ptr, char byte )
- {
- FCTL3 = 0x0A500; /* Lock = 0 */
- FCTL1 = 0x0A540; /* WRT = 1 */
- *Data_ptr=byte; /* program Flash word */
- FCTL1 = 0x0A500; /* WRT = 0 */
- FCTL3 = 0x0A510; /* Lock = 1 */
- }
- /***********************************************************************/
- /* Flash_ww */
- /* programs 1 word (16 bits) into the flash memory */
- /***********************************************************************/
- void Flash_ww( int *Data_ptr, int word )
- {
- FCTL3 = 0x0A500; /* Lock = 0 */
- FCTL1 = 0x0A540; /* WRT = 1 */
- *Data_ptr=word; /* program Flash word */
- FCTL1 = 0x0A500; /* WRT = 0 */
- FCTL3 = 0x0A510; /* Lock = 1 */
- }
- /***********************************************************************/
- /* Flash_clr */
- /* erases 1 Segment of flash memory */
- /***********************************************************************/
- void Flash_clr( int *Data_ptr )
- {
- FCTL3 = 0x0A500; /* Lock = 0 */
- FCTL1 = 0x0A502; /* ERASE = 1 */
- *Data_ptr=0; /* erase Flash segment */
- FCTL1 = 0x0A500; /* ERASE = 0 */
- FCTL3 = 0x0A510; /* Lock = 1 */
- }
|