?? 1.c
字號:
#include<reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
///////////引腳定義
sbit SCLK =P2^7;
sbit MISO =P2^6;
sbit MISI =P2^5;
sbit CS =P2^4;
sbit lcden=P0^2;
sbit lcdrs=P0^0;
sbit rw=P0^1;
uchar dat[10]={1,2,3,4,5,6,7,8,9,1};
uchar add[3]={1,0,1};
uchar R_dat[10];
uchar code table0[]="成都信息工程學院";
uint num;
void Flash_init() ; /* 程序功能: 用來將時鐘線初始化為低. 必須在將設備設置為模式0之前調用*/
void Flash_WRSR(uchar byte) ;//寫狀態寄存器
uchar Flash_Read_Status_Register(void); //讀狀態寄存器的值
void Flash_WREN_Check(void) ; /* 功能: 檢查擦寫操作前WEL位是否為1 */
void Flash_WREN(void) ; //寫使能命令
void Flash_Sector_Erase(ulong Dst) ; /* 功能: Sector Erases the Chip. 4K */
void Flash_Wait_Busy(void) ;//等待 FLASH空閑
void Flash_Chip_Erase(void) ; /* 功能: 擦除整個芯片 */
void Flash_Block_Erase_32K(ulong Dst) ; /* 功能: Block Erases 32 KByte of the Chip. */
void Flash_Block_Erase_64K(ulong Dst) ;
void Flash_EWSR(void) ; //;使能改寫狀態寄存器
void Flash_WRDI(void) ;// 禁止寫使能
void Flash_Send_Byte(uchar dat) ;//發送一個數據char 型 的 從51到FLASH
uchar Flash_Get_Byte(void);// 接收一個char型的數據 51接收
uchar Flash_Read(ulong Dst);//讀取一個措定地址內的一個數據 作為函數返回值
void Flash_Write(ulong Dst,uchar dat); //向flash 內的一個地址內寫入一個數據
void Flash_Write_data(uchar add[],uchar dat[],uchar size); //向flash 內的一個基地址 add[3],add[0]放地址高八位 內寫入size個數據。
//向從flash 內的一個基地址 add[3],add[0]放地址高八位 內讀出size個數據。 放到dat[]中
void Flash_Read_data(uchar add[],uchar R_dat[],uchar size);
//向從flash 內的一個基地址 add, 內讀出size個數據。 放到dat[]中
void Flash_Read_data_add(ulong Dst,uchar R_dat[],uchar size);
void Flash_Write_data_add(ulong Dst,uchar dat[],uchar size);//向flash 內的一個基地址 add
delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
lcdrs=0;
P1=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar date)
{
lcdrs=1;
P1=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init()
{
lcden=0;
rw=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x80);
}
/*------------------顯示字符串--------------------------*/
void write(unsigned char *s)
{ while(*s>0)
{
write_data(*s);
s++;
delay(30);
}
}
////////////////////////////////////SPI讀寫驅動////////////////////////////////////////////////////////////////////////////////
void Flash_Send_Byte(uchar dat) //發送一個數據char 型 的 從51到FLASH
{
uchar i;
SCLK=0;
CS=0;
for(i=0;i<8;i++)
{
if(dat & 0x80)
{
MISI=1;
}
else
MISI=0;
dat<<=1;
SCLK=1; //信號拉高讀進數據
_nop_();
SCLK=0; //將信號拉低
_nop_();
}
SCLK=0;
}
uchar Flash_Get_Byte(void) //接收一個char型的數據
{
uchar i;
uchar one_data=0;
CS=0;
// SCLK=1;
for(i=0;i<8;i++)
{
one_data=one_data<<1;
SCLK=0;
_nop_();
_nop_();
if(MISO==1)
{
one_data=one_data | 0x01;
}
SCLK=1;
_nop_();
}
SCLK=0;
//CS=1;
return one_data;
}
/************************************************************************/
/* 名稱: Read */
/* 功能: 讀取一個地址內一個字節的數據.返回讀取的數據*/
/* 輸入: Dst: Destination Address 000000H - 1FFFFFH */
/* 返回: byte */
/************************************************************************/
uchar Flash_Read(ulong Dst)
{
uchar byte = 0;
SCLK=1;
CS =0;
/* enable device */
Flash_Send_Byte(0x03); /* read command */
Flash_Send_Byte(((Dst & 0xFFFFFF) >> 16)); /* send 3 address bytes */
Flash_Send_Byte(((Dst & 0xFFFF) >> 8));
Flash_Send_Byte(Dst & 0xFF);
byte = Flash_Get_Byte();
CS =1; /* disable device */
return byte; /* return one byte read */
}
////////////////////////////////////////////////////////////////////////////////////////////////
void Flash_Write(ulong Dst,uchar dat) //向flash 內的一個地址內寫入一個數據。
{
CS=0;
Flash_Send_Byte(0x02); /* read command */
Flash_Send_Byte(((Dst & 0xFFFFFF) >> 16)); /* send 3 address bytes */
Flash_Send_Byte(((Dst & 0xFFFF) >> 8));
Flash_Send_Byte(Dst & 0xFF);
Flash_Send_Byte(dat);///secd data to flash
CS=1;
}
void Flash_Write_data(uchar add[],uchar dat[],uchar size) //向flash 內的一個基地址 add[3],add[0]放地址高八位 內寫入size個數據。
{
uchar i;
ulong Dst=0;
for(i=0;i<2;i++)
{
Dst|=add[i];
Dst=Dst<<8;
}
Dst|=add[2];
for(i=0;i<size;i++)
{
Flash_WREN();
CS=0 ;
Flash_Send_Byte(0x02); /* read command */
Flash_Send_Byte(((Dst & 0xFFFFFF) >> 16)); /* send 3 address bytes */
Flash_Send_Byte(((Dst & 0xFFFF) >> 8));
Flash_Send_Byte(Dst & 0xFF);
Flash_Send_Byte(dat[i]);///secd data to flash
Dst++;
CS=1;
Flash_WRDI();
}
//CS=1;
}
//向從flash 內的一個基地址 add[3],add[0]放地址高八位 內讀出size個數據。 放到dat[]中
void Flash_Read_data(uchar add[],uchar R_dat[],uchar size)
{
uchar i;
CS=0;
Flash_Send_Byte(0x03); /* read command */
for(i=0;i<3;i++)
{
Flash_Send_Byte(add[i]);
}
for(i=0;i<size;i++)
{
R_dat[i]=Flash_Get_Byte(); ///secd data to flash
}
CS=1;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Flash_init() /* 程序功能: 用來將時鐘線初始化為低. 必須在將設備設置為模式0之前調用*/
{
uchar i;
// P7 |= 0x7f; /* 設置SCK為低電平初始化狀態 */
SCLK=0;
for(i=255;i>0;i--);
// P7 &= 0x80;
SCLK=1;
}
void Flash_WREN_Check(void) /* 功能: 檢查擦寫操作前WEL位是否為1 */
{
uchar byte;
byte = Flash_Read_Status_Register(); /* 讀取狀態register */
if ((byte&0x02) != 0x02) /* 檢查WEL位置位 */
{
Flash_WREN();
//如果未置1進行相應處理,如對其進行寫使得操作
}
}
void Flash_Chip_Erase(void) /* 功能: 擦除整個芯片 */
{
Flash_EWSR();
Flash_WRSR(0);//(Read_Status_Register())&0x83 );
Flash_WREN_Check();
CS =0;
Flash_Send_Byte(0xc7); /* 發送 Chip Erase命令 (60h or C7h) */
CS =1;
Flash_Wait_Busy();
//CE =1;
}
void Flash_Sector_Erase(ulong Dst) /* 功能: Sector Erases the Chip. 4K */
{
uchar i;
Flash_WREN_Check();
Flash_init();
CS =0;
Flash_Send_Byte(0x20); /* 發送Sector Erase 命令 */
Flash_Send_Byte(((Dst & 0xFFFFFF) >> 16)); /* 發送地址 */
Flash_Send_Byte(((Dst & 0xFFFF) >> 8));
Flash_Send_Byte(Dst & 0xFF);
for(i=100;i>0;i--);
CS =1;
Flash_Wait_Busy();
// CS=1;
}
/*void Flash_Block_Erase_32K(ulong Dst) /* 功能: Block Erases 32 KByte of the Chip. */
/*{
Flash_WREN_Check();
CS =0;
Flash_Send_Byte(0x52); /* 發送32 KByte Block Erase命令*/
/*Flash_Send_Byte(((Dst & 0xFFFFFF) >> 16)); /* 發送3bytes地址*/
/*Flash_Send_Byte(((Dst & 0xFFFF) >> 8));
Flash_Send_Byte(Dst & 0xFF);
CS =1;
Flash_Wait_Busy();
} */
/************************************************************************/
/* 名稱: Block_Erase_64K */
/* 功能: Block Erases 64 KByte */
/* 輸入: Dst: 目標地址000000H - 1FFFFFH */
/* 返回: Nothing */
/************************************************************************/
/*void Flash_Block_Erase_64K(ulong Dst)
{
Flash_WREN_Check();
CS =0;
Flash_Send_Byte(0xD8); /* 發送64KByte Block Erase 命令 */
/*Flash_Send_Byte(((Dst & 0xFFFFFF) >> 16)); /* 發送3 bytes地址 */
/*Flash_Send_Byte(((Dst & 0xFFFF) >> 8));
Flash_Send_Byte(Dst & 0xFF);
CS =1;
Flash_Wait_Busy();
} */
uchar Flash_Read_Status_Register(void) //讀狀態寄存器的值
{
uchar byte = 0;
CS =0; /* 使能設備 */
Flash_Send_Byte(0x05); /* 發送讀狀態寄存器的命令 */
byte = Flash_Get_Byte(); /* 讀取狀態寄存器 */
CS =1; /* 禁止設備 */
return byte;
}
void Flash_WREN(void) //寫使能命令
{
CS =0;
Flash_Send_Byte(0x06); /* 發送WREN命令 */
CS =1;
}
void Flash_WRSR(uchar byte) //寫狀態寄存器
{
CS =0; /* 使能設備 */
Flash_Send_Byte(0x01); /* 發送寫狀態寄存器 */
Flash_Send_Byte(byte); /* 改變寄存器里BPx或者BPL (只有2,3,4,5,7位可以改寫) */
CS =1; /* 禁止設備 */
}
void Flash_WRDI(void) // 禁止寫使能
{
CS =0;
Flash_Send_Byte(0x04); /* 發送WRDI命令*/
CS =1;
}
void Flash_Wait_Busy(void) //等待 FLASH空閑
{
while ((Flash_Read_Status_Register())&0x01 == 0x01) /* waste time until not busy */
Flash_Read_Status_Register();
}
void Flash_EWSR(void) //使能改寫狀態寄存器
{
CS =0; /* 使能設備*/
Flash_Send_Byte(0x50); /* 發送使能寫寄存器的命令 */
CS =1; /* 禁止設備*/
}
/*void Flash_Write_data_add(ulong Dst,uchar dat[],uchar size) //向flash 內的一個基地址 add內寫入size個數據。
{
uchar i;
for(i=0;i<size;i++)
{
Flash_WREN();
CS=0 ;
Flash_Send_Byte(0x02); /* read command */
/* Flash_Send_Byte(((Dst & 0xFFFFFF) >> 16)); /* send 3 address bytes */
/*Flash_Send_Byte(((Dst & 0xFFFF) >> 8));
Flash_Send_Byte(Dst & 0xFF);
Flash_Send_Byte(dat[i]);///secd data to flash
Dst++;
CS=1;
Flash_WRDI();
}
CS=1;
}*/
//向從flash 內的一個基地址 add, 內讀出size個數據。 放到dat[]中
/*void Flash_Read_data_add(ulong Dst,uchar R_dat[],uchar size)
{
uchar i;
CS=0;
Flash_Send_Byte(0x03); /* read command */
/*Flash_Send_Byte(((Dst & 0xFFFFFF) >> 16)); /* send 3 address bytes */
/*Flash_Send_Byte(((Dst & 0xFFFF) >> 8));
Flash_Send_Byte(Dst & 0xFF);
for(i=0;i<size;i++)
{
R_dat[i]=Flash_Get_Byte(); ///secd data to flash
}
CS=1;
} */
void main()
{
uchar a;
init();
Flash_Chip_Erase(); //擦除整個芯片。
// Flash_Write(0x01,7);
Flash_Write_data(add,dat,10); //把dat 中的10個數寫到以add為基地址的falsh中
// a=Flash_Read(1);
Flash_Read_data(add,R_dat,10); //把以add為基地址的falsh中 的 10個數讀到dat
write_com(0x80);
write_data(0x30+R_dat[0]);
write_data(0x30+R_dat[1]);
write_data(0x30+R_dat[2]);
write_data(0x30+R_dat[3]);
write_data(0x30+R_dat[4]);
write_data(0x30+R_dat[5]);
write_data(0x30+R_dat[6]);
write_data(0x30+R_dat[7]);
write_data(0x30+R_dat[8]);
write_data(0x30+R_dat[9]);
write_com(0x90);
write(table0);
while(1);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -