📄 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 + -