?? flash.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 char 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 + -