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

📄 flash.c

📁 该设备是一款基于AVR mega16L单片机的电子书阅读器
💻 C
字号:
#include <mega16.h>
#include <flashic.h>
//PD4是RDY/BUSY
void Initi_Flash( void )
{
   SPCR=0x5C ;  // 0101 1100   SCK在闲置时是高电平,SPI是模式3     
                            //移出数据是下降沿,锁存数据是上升沿
   DDRB=0xB0;  // all outputs except PB6= MISO 1011 1111
   DDRD&=0xef;
   PORTD&=0xef;                   //MISO是输入,其他都是输出
   PORTB=0x00;                   
   return;
}
/******************************************************************************
 * MasterOut()
 * Sends data to the flash memory. Finish = 1 if this is the last byte of the
 * transfer, 0 otherwise. Command = 1 if this is a command to send, 0 if
 * to recieve data.
******************************************************************************/
char MasterOut( char data, char finish, char isCommand )
{
   if( isCommand )   
     PORTB&=~0x10;  //将PB4清零sbi是置位即使~CS清零,处低位  
   SPDR=data ;   //将要发送数据写入SPDR,将启动或准备数据传送   
   // wait for transfer to complete, indicated by the interrupt flag 
   while( !( SPSR&0x80 ) );//当SPSR中,串行传送完成时,SPIF位置1
   if( finish )//如果这是最后一个字节,FINISH就是1
   {
    PORTB|=0x10; //~CS置位,由低电平变成高电平,实现命令
   }
   return (  SPDR );
}
/******************************************************************************
 * WriteBufferToMemory()
 * Writes the given buffer to memory. The page address is 10 bits.
 * pageAddrHigh should be xxxxx987 
 * pageAddr  should be 6543210x
 * where x is a 0 and a number is the bit of the page address.
******************************************************************************/
//将字符数组内容写到闪存上
//参数为 数组地址,数组长度,写到的闪存地址
void WriteBufferToMemory( unsigned char *buffer, unsigned char bufferLength,
                          unsigned int pageAddr )
{
   unsigned char  i        = 0;
   unsigned char  j        = 0;
   for( i = 0; i < bufferLength; i++ )
   {    // write each byte to the internal SRAM buffer of the flash memory 
      for( j = 0; j < 255; j++ );
      while(!PIND&0x10);        //每写一个字节看来就要写下字节地址,无效位,内容
      MasterOut( 0x84, 0, 1 );   // select buffer 1 
      MasterOut( 0x00, 0, 1 );   // don't care 
      MasterOut( 0x00, 0, 1 );   // don't care + 1 addr 
      MasterOut( i, 0, 1 );      // 8 address bits for address in buffer 
                                 //从BUFFER1的第一个地址开始写,它能存贮264个字节                              
      MasterOut( buffer[i], 1, 1 );
   } 
   while(!PIND&0x10);        //每写一个字节看来就要写下字节地址,无效位,内容
   // once the given buffer has been written to the SRAM buffer,
   // write it to the main memory array  
   MasterOut( 0x83, 0, 1 );   // copy buffer 1 to main memory w/ builtin erase
   MasterOut( pageAddr>>8, 0, 1 );   // pageAddrHigh
   MasterOut( pageAddr, 0, 1 );
   MasterOut( 0x00, 1, 1 );      // don't cares 
   PORTB|=0x10;
   delay_ms(150); //建议延时
   return;
}
/******************************************************************************
 * ReadMemoryToBuffer()
 * Reads data from memory. The page address is 10 bits.
 * pageAddrHigh should be xxxxx987 
 * pageAddr  should be 6543210x
 * where x is a 0 and a number is the bit of the page address.
******************************************************************************/
void ReadMemoryToBuffer( unsigned char *buffer, unsigned char bufferLength,
                         unsigned int pageAddr )
{
   unsigned char  i        = 0;
   unsigned char  j        = 0; 
       
   for( j = 0; j < 255; j++ );
   while(!PIND&0x10);   
   MasterOut( 0x52, 0, 1 );
   MasterOut( pageAddr>>8, 0, 1 );    // pageAddrHigh
   MasterOut( pageAddr, 0, 1 );
   MasterOut( 0x00, 0, 1 );   // byte offset w/in page 
   MasterOut( 0x00, 0, 1 );   // 32 don't care bits 
   MasterOut( 0x00, 0, 1 );
   MasterOut( 0x00, 0, 1 );
   MasterOut( 0x00, 0, 1 );
   for( i = 0; i < bufferLength; i ++ )
   {
      buffer[i] = MasterOut( 0x00, 0, 0 );
     // if( ( buffer[i] < ' ' ) || ( buffer[i] > '~' ) )
     // {
     //    buffer[i] = ' ';
    //  }
   }
     PORTB|=0x10;
     delay_ms(150); //建议延时
     
   return;
}

⌨️ 快捷键说明

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