Hello, I program a code to write and read the flash memory in my MCU (C8051F020) in C ? The writing is ok but not the reading. After reading, i send the value by UART. This function isn't ok. can i help me ?
//----------------------------------------------------------------------------- // Includes //----------------------------------------------------------------------------- #include "C8051F020.h" // SFR declarations #include "stdio.h" //----------------------------------------------------------------------------- // 16-bit SFR Definitions for 'F02x //----------------------------------------------------------------------------- sfr16 DP = 0x82; // data pointer sfr16 TMR3RL = 0x92; // Timer3 reload value sfr16 TMR3 = 0x94; // Timer3 counter sfr16 ADC0 = 0xbe; // ADC0 data sfr16 ADC0GT = 0xc4; // ADC0 greater than window sfr16 ADC0LT = 0xc6; // ADC0 less than window sfr16 RCAP2 = 0xca; // Timer2 capture/reload sfr16 T2 = 0xcc; // Timer2 sfr16 RCAP4 = 0xe4; // Timer4 capture/reload sfr16 T4 = 0xf4; // Timer4 sfr16 DAC0 = 0xd2; // DAC0 data sfr16 DAC1 = 0xd5; // DAC1 data //----------------------------------------------------------------------------- // Global CONSTANTS //----------------------------------------------------------------------------- #define SYSCLK 22118400 // SYSCLK frequency in Hz #define BAUDRATE 115200 // Baud rate of UART in bps sbit LED = P1^6; // LED='1' means ON //----------------------------------------------------------------------------- // Function PROTOTYPES //----------------------------------------------------------------------------- void SYSCLK_Init (void); void PORT_Init (void); void UART0_Init (void); void Timer0_ms (unsigned ms); //unsigned char EE_Read (unsigned Addr); //void EE_Write (unsigned Addr, unsigned char value); //----------------------------------------------------------------------------- // Global VARIABLES //----------------------------------------------------------------------------- char eedata [6]; unsigned int ii; //----------------------------------------------------------------------------- // MAIN Routine //----------------------------------------------------------------------------- void main (void) { unsigned char xdata * idata pwrite; // pointer to FLASH used for writes // NOTE: this pointer must be located // in <data> or <idata> space! unsigned char code *pread; // pointer to FLASH used for reads char EA_save; // saves the current state of the // interrupt enable bit. // test string stored in FLASH unsigned char code test_string[] = "Howdy!"; // disable watchdog timer WDTCN = 0xde; WDTCN = 0xad; SYSCLK_Init (); // initialize oscillator PORT_Init (); // initialize crossbar and GPIO UART0_Init (); // initialize UART0 // PART A -- erase the FLASH page at 0x1000 // initialize write/erase pointer pwrite = (unsigned char xdata *) 0x1000; EA_save = EA; // save interrupt status EA = 0; // disable interrupts (precautionary) FLSCL |= 0x01; // enable FLASH writes/erases from // user software PSCTL = 0x03; // MOVX writes erase FLASH page *pwrite = 0; // initiate page erase PSCTL = 0; // MOVX writes target XRAM FLSCL &= ~0x01; // disable FLASH writes/erases from // user software EA = EA_save; // re-enable interrupts // PART B -- copy test string to FLASH memory at address 0x1000 // initialize FLASH read pointer pread = (unsigned char code *) test_string; EA_save = EA; // save interrupt status EA = 0; // disable interrupts (precautionary) pwrite = 0x1000; // initialize FLASH write pointer FLSCL |= 0x01; // enable FLASH writes/erases from // user software PSCTL = 0x01; // MOVX writes target FLASH memory while (*pread != '\0') { // copy until NULL is detected *pwrite = *pread; // copy byte pread++; // advance pointers pwrite++; } *pwrite = '\0'; PSCTL = 0x00; // MOVX writes target XRAM FLSCL &= ~0x01; // disable FLASH writes/erases from // user software EA = EA_save; // re-enable interrupts // PART C -- read test string to FLASH memory at address 0x1000 and copy in tab eedata EA_save = EA; EA = 0; pwrite = 0x1000; FLSCL |= 0x40; PSCTL = 0x00; while (*pread !='\0') { *eedata = *pread; // copy byte pread++; // advance pointers pwrite++; } PSCTL = 0x00; // MOVX writes target XRAM FLSCL &= ~0x40; // disable FLASH writes/erases from // user software EA = EA_save; // re-enable interrupts while (1) // spin forever { for (ii=0;ii<6; ii++) { LED = 1; printf("%c",eedata[ii]); printf("@13"); } printf("\x0d"); printf("\x0a"); LED = 0; } }
thanks
Regards