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

📄 spi.c

📁 使用C8051F040的硬件SPI口控制FLASH芯片AT45DB321,P0的0,1,2配置为3线SPI口,P6的0,1,2,3软件控制AT45DB321的状态引脚
💻 C
字号:
/*
    file AT45DB321C.C
    SOME FUNCTIONS RUNS IN AT45DB321C
//  SPI
*/

#include <C8051F040.H>	// Register definition file.
#include <intrins.h>
#include "YC8051F040.H"
#include "AT45DB321C.h"

#define  SYSCLK 22114800
#define SPICLK 1000000



unsigned char FLASHReadStatReg()
{
	unsigned char m;
	SFRPAGE = CONFIG_PAGE;

	 _FlashCS  =  0;

	SFRPAGE = SPI0_PAGE;
//Send command code
	m = 0xD7;
	SPI0DAT = m;
	while(!SPIF);
	SPIF = 0;
//Read the value returned
	SPI0DAT = 0; 
	while (!SPIF);                      // Wait for the value to be read
	SPIF = 0;
	delay1us();
	m = SPI0DAT;
	SFRPAGE = CONFIG_PAGE;
    _FlashCS  =  1;
    return m;
}

void FlASHWriteViaBuffer(	bit BufferNumber,
							unsigned int PageAddress,
							unsigned int BufferAddress,
							unsigned int num,
							unsigned char *pBuffer)
{
	unsigned char *pIndex;
    unsigned int unCount;	
	unsigned char BufferAddress0,BufferAddress1,BufferAddress2;
	//change the PA&BFA to 3 bytes form
	BufferAddress2 = 0x7F & (PageAddress>>6);
	BufferAddress1 = 0x003F & PageAddress;
	BufferAddress1 = BufferAddress1 << 2;
	BufferAddress1 = ((BufferAddress >> 8)&0x0003)|BufferAddress1;
	BufferAddress0 = BufferAddress&0x00FF;
    pIndex = pBuffer;
    unCount = 0;

	SFRPAGE = CONFIG_PAGE;

	_FlashCS  = 1;
	_FlashCS  = 0;

	SFRPAGE = SPI0_PAGE;
	

	if(!BufferNumber)//buffer1
		SPI0DAT = 0x82;
	else//buffer2
//write op code
		SPI0DAT = 0x85;

	while(!SPIF);
	SPIF = 0;
	
	SPI0DAT = BufferAddress2;
	while(!SPIF);
	SPIF = 0;
	SPI0DAT = BufferAddress1;
	while(!SPIF);
	SPIF = 0;
	SPI0DAT = BufferAddress0;
	while(!SPIF);
	SPIF = 0;			//write buffer address	 
   
    while( unCount < num )
    {								//write data to buffer
        SPI0DAT = *pIndex;
		while(!SPIF);
		SPIF = 0;
        pIndex++;
        unCount++;
	}
	SFRPAGE = CONFIG_PAGE;
	_FlashCS  = 1;	//start to write Flash via buffer
	
//	while(!_FlashBusy)_FlashBusy = 1;	//break when op complete
//	delay1us();	
	_FlashBusy = 1;
	while(!_FlashBusy)_FlashBusy = 1;

}

void FLASHRead(bit ReadType,//Array Or Page
               unsigned int PageAddress,	//0~8191;0~0x2000;
			   unsigned int BufferAddress, //0~527;0~0x210;
               unsigned int num,
               unsigned char *pBuffer )
{
	unsigned char *pIndex;
    unsigned int unCount;	
	unsigned char BufferAddress0,BufferAddress1,BufferAddress2;
	//change the PA&BFA to 3 bytes form
	BufferAddress2 = 0x7F & (PageAddress>>6);
	BufferAddress1 = 0x003F & PageAddress;
	BufferAddress1 = BufferAddress1 << 2;
	BufferAddress1 = ((BufferAddress >> 8)&0x0003)|BufferAddress1;
	BufferAddress0 = BufferAddress&0x00FF;
    
	pIndex = pBuffer;
    unCount = 0;

	SFRPAGE = CONFIG_PAGE;

	_FlashCS  = 1;
	_FlashCS  = 0;
	SFRPAGE = SPI0_PAGE;
	if( !ReadType )//ArrayRead:0;PageRead:1
	SPI0DAT = 0xE8;
	else
	SPI0DAT = 0xD2;
	while(!SPIF);
	SPIF = 0;	
	
	BufferAddress2  &= 0x7F; 
//It is recommended that “r” be a logical “0” for densities of 32M bits or smaller

	SPI0DAT = BufferAddress2;
	while(!SPIF);
	SPIF = 0;

	SPI0DAT = BufferAddress1;
	while(!SPIF);
	SPIF = 0;
	
	for(unCount = 0; unCount<5; unCount++)
	{
		SPI0DAT = BufferAddress0;
		while(!SPIF);
		SPIF = 0;
	}//64 clock
	
	unCount = 0;
	while(unCount < num)
	{
		SPI0DAT = 0; 
		while (!SPIF);                      // Wait for the value to be read
		SPIF = 0;
		delay1us();
		*pIndex = SPI0DAT;	
		pIndex++;
		unCount++;
	}
	
	SFRPAGE = CONFIG_PAGE;
	_FlashCS  = 1;//complete the read operation
}

void EnableSectorProtection()
{
	SFRPAGE = CONFIG_PAGE;
	_FlashCS  = 1;
	_FlashCS  = 0;
	SFRPAGE = SPI0_PAGE;

	SPI0DAT = 0x3D;
	while(!SPIF);
	SPIF = 0;

	SPI0DAT = 0x2A;
	while(!SPIF);
	SPIF = 0;

	SPI0DAT = 0x7F;
	while(!SPIF);
	SPIF = 0;

	SPI0DAT = 0xA9;
	while(!SPIF);
	SPIF = 0;

	SFRPAGE = CONFIG_PAGE;
	_FlashCS  = 1;
}


void DisableSectorProtection()
{
	SFRPAGE = CONFIG_PAGE;
	_FlashCS  = 1;
	_FlashCS  = 0;
	SFRPAGE = SPI0_PAGE;

	SPI0DAT = 0x3D;
	while(!SPIF);
	SPIF = 0;

	SPI0DAT = 0x2A;
	while(!SPIF);
	SPIF = 0;

	SPI0DAT = 0x7F;
	while(!SPIF);
	SPIF = 0;

	SPI0DAT = 0x9A;
	while(!SPIF);
	SPIF = 0;

	SFRPAGE = CONFIG_PAGE;
	_FlashCS  = 1;

}



void os_init()
{
    int i = 0;
    SFRPAGE   = CONFIG_PAGE;
    OSCXCN    = 0x67;									//外部22.1184M晶振
    for (i = 0; i < 3000; i++);                         //等待外部晶振稳定 
    while ((OSCXCN & 0x80) == 0);
    CLKSEL    = 0x01;									//系统时钟不分频
}


void port_init()
{
    SFRPAGE = CONFIG_PAGE;
   	
	XBR0    = 0x06;		//配置SPI端口
	XBR2    = 0x40;

//	P2MDOUT = 0x0e;
//	P1MDOUT = 0x7d;
	P0MDOUT = 0x35;
	P6MDOUT = 0x77;	
	P6=0xFF;
}

void spi_init()
{
	SFRPAGE = SPI0_PAGE;
	SPI0CFG = 0x70;				//开启SPI0总线
	SPI0CN  = 0x01;
//    SPI0CKR = 0x6d;				//波特率90.91KHz
//	SPI0CKR = ((SYSCLK / SPICLK) >> 1) - 1;
	SPI0CKR = 0x08;
}


void delay1us(void)
{
	unsigned char i;
	for(i=20;i>0;i--);
} 

⌨️ 快捷键说明

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