⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flash.h

📁 基于51单片机的心电监护仪程序
💻 H
字号:
#ifndef __serial_com_H__
#define __serial_com_H__
#ifndef __delay_us__
#define __delay_us__

void delay_us(unsigned int time)   //软件延时time us
{  
    unsigned long ii;
	ii=time*11;
	while(ii>0)
    {
		ii--;
	}
}
#endif

//*****flash
//***************************************************************************************************

/***************************************************
*函数原型:at_send_byte(byte d)    2008.5.21       *
*功    能:向AT45D041发送一个字节	               *
*入口参数:所发送字节                              *
****************************************************/
void at_send_byte(byte d){
	byte i, mask=0x80, t;
	
	at_sck=1;
	for (i=0;i<8;i++){
		at_sck=0;
		t = d&mask;								//extract bit from MSB to LSB
		at_si=(t==0)? 0:1;	    //send 1 bit
		at_sck=1;
		mask>>=1;
		}
	}
	
	
/****************************************************
*函数原型:at_receive_byte()    2008.5.21     		*
*功    能:从AT45D041读一个字节	                    *
*返回值:  所读字节                                 *
****************************************************/
byte at_receive_byte(){
	byte i, mask=0x80, rb;
	
	at_sck=1;
	for (i=0;i<8;i++){
		at_sck=0;
		delay_us(0);
		at_sck=1;
		rb=(at_so==1)? (rb|mask):(rb&(~mask));	//receive one bit
		mask>>=1;
		}
	return (rb);
	}
	
/****************************************************
*函数原型:at_send_dcarebyte(byte n)    2008.5.21    *
*功    能:向AT45D041发送n字节无关数,用于命令发送	*
*入口参数:所发送无关数的字节数                     *
****************************************************/
void at_send_dcarebyte(byte n){
	byte j;
	
	for (j=0;j<n;j++){
		at_send_byte(0);
		}
	}
	
	
/****************************************************
*函数原型:at_send_adress(ulong addr)    2008.5.21  *
*功    能:将用户使用的长整型地址转为flash支持的页码*
*					 数和页内地址发送给AT45D041 						  *
*入口参数:用户使用的长整型地址                     *
*注意事项:入口参数须小于0x80000										*
****************************************************/
void at_send_addr(ulong addr){
	byte byte1, byte2, byte3;
	uint temp1;
	ulong temp2;
	
	temp1 = addr/264;   //计算页码数
	byte1 = temp1 >> 7;  //the first byte
	
	byte2 = temp1 << 1; 
	temp2 = (uint)(addr/264)*264;				
	temp1 = addr - temp2;	  //calculate the address within a page
	byte2 = (temp1 & 0x100) ? (byte2 | 0x01) : (byte2 & 0xFE);  //the second byte
	
	byte3 = temp1;         //the third byte
	
	at_send_byte(byte1);
	at_send_byte(byte2);
	at_send_byte(byte3);
	
	baddr = temp1;         //record buffer address
	}	
	
	
/****************************************************
*函数原型:at_waiting ()    		 2008.5.21 		*
*功    能:读取AT45D041的闲忙状态,直到ready为止 	*
*																										*
****************************************************/
void at_waiting (){
	byte cmd = 0xD7; 		//status register read mode
	bit ss;

	at_cs = 0;					//chip select
	at_send_byte(cmd);  //send command
	
	at_sck = 0; 
	delay_us(1);
	at_sck = 1;

    ss = !at_so;

    while (ss);	//waiting for "ready" of bit7 of the status register

    at_cs = 1;
	
	}


/*******************************************************
*函数原型:at_read_dyte (ulong addr)     2008.5.21     *
*功    能:从AT45D041读取1字节数据					   *
*入口参数:读取数据的地址addr						   *
*返回值:  返回所读数据at_data					       *
*注意事项:入口参数须小于0x80000					   *
*******************************************************/
byte at_read_byte (ulong addr){
	byte cmd = 0xE8;    //continuous array read mode 
	byte at_data;

	at_waiting();		//waiting until the flash is ready	

	at_sck = 1;         //SPI mode 3
	at_cs = 0;					 //chip select
	at_send_byte(cmd);   //send command
	at_send_addr(addr);  //send address
	at_send_dcarebyte(4); //send 4 don't care bytes 
	
	at_data = at_receive_byte();	 //read data
	
	at_cs = 1;
	return (at_data);
}	

 
/********************************************************
*函数原型:at_write_init (ulong addr)     2008.5.21 		*
*功    能:准备写入AT45d041									    				*
*入口参数:写入的首地址addr															*
*注意事项:入口参数addr须小于0x80000										*
********************************************************/
void at_read_init(ulong addr){
		byte cmd = 0xE8;    //continuous array read mode 

	at_waiting();				 //waiting until the flash is ready	

  at_sck = 1;         //SPI mode 3
	at_cs = 0;					 //chip select
	at_send_byte(cmd);   //send command
	at_send_addr(addr);  //send address
	at_send_dcarebyte(4); //send 4 don't care bytes 

	}
	
	
	
/****************************************************************
*函数原型:at_memtobuf(uint page_n)					2008.5.21						*
*功    能:将flash中某一页的数据复制到buffer中   	  						*
*入口参数:要复制的页码page_n																		*
*注意事项:入口参数须小于2048																		*
****************************************************************/
void at_memtobuf(uint page_n){
	byte t , cmd;
	cmd = (buf_n == 1)? 0x53 : 0x55;    //choose which buffer to write
	
	at_cs = 0;
	at_send_byte (cmd);
	
	//send the following bits
	t = page_n >> 7;  //the first byte
	at_send_byte(t);
	t = page_n << 1;  //the second byte
	at_send_byte(t);
	at_send_byte(0);  //the third byte
	
	at_cs = 1;
	}

/********************************************************
*函数原型:at_write_init (ulong addr)     2008.5.21 		*
*功    能:准备写入AT45d041									    				*
*入口参数:写入的首地址addr															*
*注意事项:入口参数addr须小于0x80000										*
********************************************************/
void at_write_init (ulong addr){
	uint temp1;
	ulong temp2;
    byte cmd;
//    uint page_n;

	buf_n = 3 - buf_n;     		//change buffer
	
	temp1 = addr/264;   //计算页码数
	temp2 = (uint)(addr/264)*264;			
	baddr = addr - temp2;	  //calculate buffer address

  /*
	if (baddr != 0){   		  	//if not write from the beginning of the buffer
		page_n = addr/264;   //calculate the page number
		at_memtobuf (page_n);			//recover the page last written to the buffer
		at_waiting();  						//waiting until the recover is over
		}    */
		
	cmd = (buf_n == 1)? 0x82 : 0x85;    //choose which buffer to write
	at_cs = 0;
	at_send_byte (cmd); 						//send command
	at_send_addr (addr);		//send 3 bytes address
	
	}
	
/****************************************************************
*函数原型:at_write_data(byte dd, ulong addr)			2008.5.21			*
*功    能:初始化后写入一字节数据						    	  						*
*入口参数:写入的数据dd,以及写入地址addr												*
*注意事项:入口参数addr须小于0x80000														*
****************************************************************/
void at_write_data (byte dd, ulong addr){
	at_send_byte (dd);					//write the data
	
	baddr++;										//record address
	
	if (baddr == 264){
		at_cs = 1;
		baddr = 0;
		at_write_init (addr+1);    //change buffer and start next page
		}
	}



//数据存储
void store_to_flash()
{
    //at_write_data(0X0F,flash_add);
   // flash_add++;
    // sys_flag=!sys_flag;
    if(store_or_not==0)
    {

        
        at_write_data(ECG_data_temp[i],flash_add);
    	flash_add++;
        if(flash_add>=((num_of_abnormity+1)*2640+264))
        {  //sys_flag=!sys_flag;
           flash_add=flash_add-2640;
           at_cs = 1;
           at_write_init(flash_add);   
        }	
    	//flash_add=(flash_add>=(num_of_abnormity+2)*2000)? flash_add-2000:flash_add;
        
    }
    else
    {
    	
        at_write_data(ECG_data_temp[i],flash_add);
    	flash_add++;	
    	flash_add=(flash_add>=((num_of_abnormity+1)*2640+264))? flash_add-2640:flash_add;
    	store_num++;
    	if(store_num>=1320)
    	{
    		store_over=1;
    		store_or_not=0;
    		store_num=0;
    		flash_add=(num_of_abnormity+1)*2640+264;
            at_cs = 1;
            at_write_init(flash_add);
            SCLK=!SCLK;	
    	}	
    } 
}
#endif

//***************************************************************************************************
//*****flash

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -