This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

c51: Writing to flash

When we interface flash with 8051,
we send Commands to read and write.
For write operation, after sending Write Command, we send data,
Should there be any change in the way the data (data byte which is to be written) is sent ? For instance,
we send the 8-bit word bit by bit, clock being set to 1 & 0, manually for each bit.
I believe the Commands are Read and are processed by the Logic circuitry.
When data is to be written, the Write cycles may take practically more time to Write in the flash,
so could somebody say what is the right way to send the data,

The code is given below:
quite long,
but probably going through only the necessary functions would reduce time for you,

#include<reg51.h>
#include<stdio.h>

        sbit si = P1^1;
        sbit SCK = P1^3;
        sbit SO = P1^5;
        sbit ce = P1^0;

unsigned char Get_Byte();                               // To receive 8 bit word bit by bit
void Send_Byte(unsigned char b_out);    // To send 8 bit word bit by bit thru SI
void E_WRSR();                                                  // Enable & write to status reg.
// unsigned char Read_Status_Register();
void WREN();                                                    // Write Enable before Write operation
void Byte_Program();                                    // Write single byte to location
void Delay();                                                   // To wait for completion of write operation
unsigned char Read_Func();
// unsigned char Read_ID();
// void Read_Cont(unsigned long addr, unsigned long no_bytes);

        unsigned char i, SR_byte, r_byte;


void main(void){
        ce = 1;         // Chip Enable
        SCK = 0;        // Clock

                TMOD = 0x20;
                TH1 = -3;                               // 9600 baud
                SCON = 0x50;
                TR1 = 1;
                TI = 1;

                ce = 0;
        Byte_Program();
                ce = 1;
                ce = 0;
        Read_Func();
                ce = 1;

}

unsigned char Get_Byte(){       // Receive dataByte from Flash through SO bit by bit
        unsigned char i=0, in=0, temp=0;
        for(i=0;i<8;i++){
                in = (in << 1);
                temp = SO;
                SCK = 1;                        // S clock, manually set to 1 & 0
                if (temp == 1){
                        in = in | 0x01;
                }
                SCK = 0;
        }
        return in;
}
void Send_Byte(unsigned char b_out){    // To Send Data Byte to Flash, bit by bit through SI
        for(i=0;i<8;i++){
                if((b_out & 0x80) == 0x80)
                        si = 1;
                else
                        si = 0;
                        SCK = 1;
                        b_out = (b_out << 1);
                        SCK = 0;
        }
}

void E_WRSR(){
        //ce = 0;
        Send_Byte(0x50);                // enable Status Reg
//      ce = 1;
        Delay();
//      ce = 0;
        Send_Byte(0x01);                // select write to status register
        Send_Byte(0x00);                // (BPL = 0, mode 00 (bp1 & 2), NONE)
//      ce = 1;
}
void WREN(){
//      ce = 0;
        Send_Byte(0x06);        // Sending Write Enable Command
//      ce = 1;
}

unsigned char Read_Func(){  // Read single byte from Flash
//      ce = 0;
        Send_Byte(0x03);                                        // Sending Read CMD
        Send_Byte(0x00);                                        // Sending address bytes-24 bit. (0-7)
        Send_Byte(0x01);                                        // Bits (8-15)
        Send_Byte(0x23);                                        // Bits (16-23)

        Delay();
        r_byte = Get_Byte();
        SBUF = r_byte;
        P1 = r_byte;                                            // transmit to led port1
        while(TI){
        }
        TI = 0;

//      ce = 1;
        return r_byte;
}

void Byte_Program(){// Write Operation (takes longer time to complete Compared to read)

                ce = 0;
        E_WRSR();
                ce = 1;
                ce = 0;
        WREN();
                ce = 1;

//      ce = 0;
        Send_Byte(0x02);        // Byte Program CMD

        Send_Byte(0x00);        // sending 3 address bytes (0-7)
        Send_Byte(0x01);        // (8-15)
        Send_Byte(0x23);        // (16-23)
        Send_Byte(0x29);        // send byte to be programmed (data)

        Delay();
//      ce = 1;
}

void Delay(){           // 14 - 20us delay, the time it takes for Write operation
        unsigned int j = 77;
        for(i=0;i<100;i++){
                j = j--;
        }
}

/*unsigned char Read_Status_Register(){
        ce = 0;
        Send_Byte(0x05);                // send RDSR command
        SR_byte = Get_Byte();           // receive byte

        Delay();
        P1 = SR_byte;
/*      SBUF = SR_byte;
        while(TI){
        }
        TI = 0;
    // TI = 1;

        ce = 1;
        return SR_byte;
}


/* unsigned char Read_ID(){
        ce = 0;
        Rd_ID = 0x00;
        Send_Byte(0x90);                // send read ID command (90h or ABh)
        Send_Byte(0x00);                // sending address in 3 bytes
        Send_Byte(0x00);
        Send_Byte(0x00);                // send address - either 00H or 01H
        Rd_ID = Get_Byte();             // receive byte

//      SBUF = Rd_ID;
//      P1 = Rd_ID;
//      while(TI){
//      }
//      TI = 0;

        ce = 1;
        return Rd_ID;
}
*/

0