|
/******************************************************************************/ /* 函数名称:void write_to_flash_byte(uchar) */ /* 功能描述:向AT45DB081B flas写入一个字节 */ /* 参数说明:cdata为待写入的数据 */ /* 参数返回:无 */ /******************************************************************************/ void Write_To_Flash_Byte(uchar cdata) { uchar byte; SPDR = cdata; while (!(SPSR & 0x80)) ; //等待写结束 byte = SPDR; }
/******************************************************************************/ /* 函数名称:uchar check_flash_busy(void) */ /* 功能描述:检查flash是否忙 */ /* 参数说明:无 */ /* 参数返回: 如果忙返回1,不忙返回0 */ /******************************************************************************/ uchar Check_Flash_Busy(void) { uchar busy_flag; PORTB &=0xef; //置cs低选中 Write_To_Flash_Byte(0xd7); Write_To_Flash_Byte(0xff); //写字节数据,使字节数据从SO移出 busy_flag = SPDR; PORTB |= 0x10; //置cs高不选中 if(busy_flag & 0x80) busy_flag = 0; else busy_flag = 1; return busy_flag; }
/******************************************************************************/ /* 函数名称:void write_buff(uchar,uint,uint) */ /* 功能描述:从datasave_buff数组向flash缓冲区buffer1或buffer2 */ /* 写入指定数量的数据 */ /* 参数说明:Write_com为命令字0x84写buffer1,0x87写buffer2, */ /* buffer_offset为写缓冲区起始地址,byte_count为 */ /* 待写入的字节数 */ /* 参数返回: 无 */ /******************************************************************************/ void Write_Buff(uchar Write_com,uint buffer_offset,uint byte_count) { uint i; PORTB &=0xef; //置cs低选中 Write_To_Flash_Byte(Write_com); //写buffer指令 Write_To_Flash_Byte(0); //写无关位 Write_To_Flash_Byte((uchar)(buffer_offset>>8)); Write_To_Flash_Byte((uchar)buffer_offset); for (i=0;i<byte_count;i++) //将byte_count个字节写入FLASH Buffer1 Write_To_Flash_Byte(Data_Buff[i]); PORTB |= 0x10; //置cs高不选中 }
/******************************************************************************/ /* 函数名称:void read_write_flash(uchar,uint) */ /* 功能描述: 从buffer1或buffer2向flash存贮区带擦除或不带擦除写一页 */ /* 或从flash存贮区向buffer1或buffer2写一页 */ /* 参数说明:Write_com为命令字0x83带擦除buffer1到主存,0x86带擦除 */ /* buffer2到主存,0x88不带擦除buffe1到主存,0x89为不带擦 */ /* 除buffer2主存,0x53为主存传送到buffer1,0x55为主存传 */ /* 送到buffer2,page_address为页地址 */ /* 参数返回: 无 */ /******************************************************************************/ void Read_Write_Flash(uchar Write_com,uint page_address) { PORTB &=0xef; //置cs低选中 Write_To_Flash_Byte(Write_com); page_address = page_address << 1; Write_To_Flash_Byte((uchar)(page_address>>8)); Write_To_Flash_Byte((uchar)page_address); Write_To_Flash_Byte(0); //写无关位 PORTB |= 0x10; //置cs高不选中 while(Check_Flash_Busy()); //等待器件写完结束 }
/******************************************************************************/ /* 函数名称:void read_buff(uchar,uint,uint) */ /* 功能描述:从buffer1或buffer2向datasave_buff数组读指定数量的数据 */ /* 参数说明:Write_com为命令字0xd4读buffer1,0xd6读buffer2, */ /* buffer_offset为读缓冲区起始地址,byte_count为 */ /* 欲读取的字节数 */ /* 参数返回: 无 */ /******************************************************************************/ void Read_Buff(uchar Readcmd,uint buffer_offset,uint count) { uint i; PORTB &= 0xef; //置cs低选中 Write_To_Flash_Byte(Readcmd); Write_To_Flash_Byte(0); Write_To_Flash_Byte((uchar)(buffer_offset>>8)); Write_To_Flash_Byte((uchar)buffer_offset); Write_To_Flash_Byte(0); for(i=0;i<count;i++) { Write_To_Flash_Byte(0xff); Data_Buff[i]=SPDR; } PORTB |= 0x10; //置cs高不选中 }
/******************************************************************************/ /* 函数名称:void read_flash(uchar,uint,uint,uint) */ /* 功能描述:从flash指定页指定页内起始地址读取指定数量的数据字节到 */ /* datasave_buff数组 */ /* 参数说明:Write_com为命令字0xd2读命令,page_offset为页内起始地址 */ /* page_address为页地址,count为欲读取的字节数 */ /* 参数返回: 无 */ /******************************************************************************/ void Read_Flash(uchar Readcmd,uint page_offset,uint page_address,uint count) { uint pa,i; uchar byte; pa=0; page_address <<=1; pa = page_offset>>8; page_address |= pa; PORTB &= 0xef; //置cs低选中 Write_To_Flash_Byte(Readcmd); Write_To_Flash_Byte((uchar)(page_address>>8)); Write_To_Flash_Byte((uchar)page_address); Write_To_Flash_Byte((uchar)page_offset); for(byte=0;byte<4;byte++) { Write_To_Flash_Byte(0); } for(i=0;i<count;i++) { Write_To_Flash_Byte(0xff); //写字节数据,使字节数据从SO移出 Data_Buff[i] = SPDR; } PORTB |= 0x10; //置cs高不选中 }
SPI控制字为:SPCR = 0x5f;
|