Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
11/04/08 13:02
Read: times


 
#159624 - flash memeory reading
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;
   }

}
//-----------------------------------------------------------------------------
// Subroutines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// This routine initializes the system clock to use an 22.1184 MHz crystal
// as its clock source.
//
void SYSCLK_Init (void)
{
   int i;                              // delay counter

   OSCXCN = 0x67;                      // start external oscillator with
                                       // 22.1184 MHz crystal

   for (i=0; i < 256; i++) ;           // Wait for osc. to start up

   while (!(OSCXCN & 0x80)) ;          // Wait for crystal osc. to settle

   OSCICN = 0x88;                      // select external oscillator as SYSCLK
                                       // source and enable missing clock
                                       // detector
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports
//
void PORT_Init (void)
{
   XBR0   |= 0x06;                     // Enable SPI0 and UART0
   XBR1    = 0x00;
   XBR2    = 0x40;                     // Enable crossbar and weak pull-ups
   P0MDOUT |= 0x15;                    // enable P0.0 (TX), P0.2 (SCK), and 
                                       // P0.4 (MOSI) as push-pull outputs 
   P1MDOUT |= 0x40;                    // enable P1.6 (LED) as push-pull outputs
                                      
   //P74OUT |= 0x01;                     //pins 4.0 and 4.1 as push-pull outputs
}
//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Configure the UART0 using Timer1, for <baudrate> and 8-N-1.
//
void UART0_Init (void)
{
   SCON0  = 0x50;                      // SCON0: mode 1, 8-bit UART, enable RX
   TMOD   = 0x20;                      // TMOD: timer 1, mode 2, 8-bit reload
   TH1    = -(SYSCLK/BAUDRATE/16);     // set Timer1 reload value for baudrate
   TR1    = 1;                         // start Timer1
   CKCON |= 0x10;                      // Timer1 uses SYSCLK as time base
   PCON  |= 0x80;                      // SMOD00 = 1 (disable baud rate 
                                       // divide-by-two)
   TI0    = 1;                         // Indicate TX0 ready
}


//-----------------------------------------------------------------------------
// Timer0_ms
//-----------------------------------------------------------------------------
//
// Configure Timer0 to delay <ms> milliseconds before returning.
//
void Timer0_ms (unsigned ms)
{
   unsigned i;                         // millisecond counter

   TCON  &= ~0x30;                     // STOP Timer0 and clear overflow flag
   TMOD  &= ~0x0f;                     // configure Timer0 to 16-bit mode
   TMOD  |=  0x01;
   CKCON |=  0x08;                     // Timer0 counts SYSCLKs

   for (i = 0; i < ms; i++) {          // count milliseconds
      TR0 = 0;                         // STOP Timer0
      TH0 = (-SYSCLK/1000) >> 8;       // set Timer0 to overflow in 1ms
      TL0 = -SYSCLK/1000;
      TR0 = 1;                         // START Timer0
      while (TF0 == 0);                // wait for overflow
      TF0 = 0;                         // clear overflow indicator
   }
}

thanks Regards

List of 31 messages in thread
TopicAuthorDate
flash memeory reading            01/01/70 00:00      
   How do you know            01/01/70 00:00      
      flash memory reading            01/01/70 00:00      
   Cross-posted on Keil            01/01/70 00:00      
   pread and pwrite...            01/01/70 00:00      
      my problem            01/01/70 00:00      
         No, you ignored my notes in my previous post            01/01/70 00:00      
            answers            01/01/70 00:00      
               Still no answers            01/01/70 00:00      
   first            01/01/70 00:00      
      a quick glance seems to revceal ...            01/01/70 00:00      
         /            01/01/70 00:00      
         MOVX During FLASH Write            01/01/70 00:00      
   and now he crossed it on the SILabs forum            01/01/70 00:00      
   my problem            01/01/70 00:00      
      RTFM            01/01/70 00:00      
         not xdata            01/01/70 00:00      
            reding flash            01/01/70 00:00      
               So fix the loop then            01/01/70 00:00      
            Not ok            01/01/70 00:00      
         reading flash problem            01/01/70 00:00      
            Yet one more post with broken read loop            01/01/70 00:00      
               correcting my code            01/01/70 00:00      
                  Then specify exactly what you don't understand            01/01/70 00:00      
                     i correct my code            01/01/70 00:00      
                        it's working            01/01/70 00:00      
                           Report feedback            01/01/70 00:00      
                              write cycle time            01/01/70 00:00      
                                 New Question, New Thread.            01/01/70 00:00      
                           So what fixed it?            01/01/70 00:00      
   correct registers ?            01/01/70 00:00      

Back to Subject List