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

📄 main.c

📁 用飞思卡尔的单片机读取FM25256的源代码。调试通过。
💻 C
字号:
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */

#define uchar unsigned char
#define uint unsigned int
#define NOP _nop_()

//********************************************************************************
#define WREN_INST 0X06
 /* Write enable latch instruction (WREN)*/
#define WRDI_INST 0X04
 /* Write disable latch instruction (WRDI)*/
#define WRSR_INST 0X01
 /* Write status register instruction (WRSR)*/
#define RDSR_INST 0X05
 /* Read status register instruction (RDSR)*/
#define WRITE_INST 0X02
 /* Write memory instruction (WRITE)*/
#define READ_INST 0X03
 /* Read memory instruction (READ)*/
#define STATUS_REG 0X00
 /* Status register,BP1 and BP0 are all zero,no menory write protection*/
 
 
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//***********************************************************************************
//0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55
//0XAA,0XAA,0XAA,0XAA,0XAA,0XAA,0XAA,0XAA,0XAA,0XAA,0XAA,0XAA,0XAA,0XAA,0XAA,0XAA,
// 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8
uchar temp[16];
uchar tempw[16]={0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55};
uchar tempr[16];
//**************************************************************************************
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
uchar tempa;
uint number;
uchar numtime;

uchar d0,d1,d2,d3,d4,d5,d6,d7;
uchar bbyte;


//********************************************************************************

#define DIN PTED_PTED6	//define SI-fm25256
#define DOUT PTED_PTED5	//define SO-fm25256
#define CLK PTED_PTED7	//define CLK
#define CSX PTCD_PTCD3	//define /CS

/*;********************************************************************************
*
;* Name: OUTBYT
;* Description: Sends byte to FM25640
;* Function: This routine shifts out a byte, starting with the MSB, to the FM25040
;* Calls: None
;* Input: outbyte= byte to be sent
;* Outputs: None
;* Notes: The pin is sampled on the rising edge of SCK and is ignored at other times.
;*********************************************************************************
*/
void outbyt(uchar outbyte)
{
	bbyte=outbyte;
	d0=0; d1=0; d2=0; d3=0; d4=0; d5=0; d6=0; d7=0;
	if(outbyte&0x80){d7=1;}
	DIN=d7;CLK=0;CLK=1;
	if(outbyte&0x40){d6=1;}
	DIN=d6;CLK=0;CLK=1;
	if(outbyte&0x20){d5=1;}
	DIN=d5;CLK=0;CLK=1;
	if(outbyte&0x10){d4=1;}
	DIN=d4;CLK=0;CLK=1;
	if(outbyte&0x08){d3=1;}
	DIN=d3;CLK=0;CLK=1;
	if(outbyte&0x04){d2=1;}
	DIN=d2;CLK=0;CLK=1;
	if(outbyte&0x02){d1=1;}
	DIN=d1;CLK=0;CLK=1;
	if(outbyte&0x01){d0=1;}
	DIN=d0;CLK=0;CLK=1;
	//CLK=0;
}   
/*;*******************************************************************************
*
;* Name: INPUTBYT
;* Description: Recieves byte from FM25640
;* Function: This routine recieves a byte, MSB first, from the FM25040
;* Calls: None
;* Input: None
;* Outputs: bbyte= recieved byte
;* Notes: Data transitions are driven on the falling edge of the serial clock.
;*********************************************************************************
*/
uchar inputbyt(void)
{
	
	DOUT=1;
	bbyte=0;
	CLK=0;CLK=1;d7=DOUT;
	if(d7) bbyte|=0x80;
	CLK=0;CLK=1;d6=DOUT;
	if(d6) bbyte|=0x40;
	CLK=0;CLK=1;d5=DOUT;
	if(d5) bbyte|=0x20;
	CLK=0;CLK=1;d4=DOUT;
	if(d4) bbyte|=0x10;
	CLK=0;CLK=1;d3=DOUT;
	if(d3) bbyte|=0x08;
	CLK=0;CLK=1;d2=DOUT;
	if(d2) bbyte|=0x04;
	CLK=0;CLK=1;d1=DOUT;
	if(d1) bbyte|=0x02;
	CLK=0;CLK=1;d0=DOUT;
	if(d0) bbyte|=0x01;
	return bbyte;
}

/*;*******************************************************************************
*
;* Name: WREN_CMD
;* Description: Set write enable latch
;* Function: This routine sends the command to enable writes to the FRAM memory array or
;* status register
;* Calls: outbyt
;* Input: None
;* Outputs: None
;********************************************************************************
*/
void wren_cmd(void)
{
 	CLK=0;CSX=0;/* Bring /CSX low */
 	outbyt(WREN_INST);/* Send WREN instruction */
 	CLK=0;CSX=1;
}

/*;*******************************************************************************
*
;* Name: WRDI_CMD
;* Description: Reset write enable latch
;* Function: This routine sends the command to disable writes to the FRAM memory array or
;* status register
;* Calls: outbyt
;* Input: None
;* Outputs: None
;********************************************************************************
*/
void wrdi_cmd(void)
{
  	CLK=0;CSX=0;/* Bring /CSX low */
 	outbyt(WRDI_INST);/* Send WRDI instruction */
 	CLK=0;/* Bring CLK low */
 	CSX=1;/* Bring /CSX high */
}
/*;********************************************************************************
*
;* Name: RDSR_CMD
;* Description: Read Status Register
;* Function: This routine sends the command to read the status register
;* Calls: outbyt, inputbyt
;* Input: None
;* Outputs: aa= status register
;***********************************************************************************
*/
uchar rdsr_cmd (void)
{
 	uchar aa;
 	CLK=0;CSX=0;
 	outbyt(RDSR_INST);
 	aa=inputbyt();
 	CLK=0;CSX=1;
 	return aa;
}

/*;*******************************************************************************
*
;* Name: WRSR_CMD
;* Description: Write Status Register
;* Function: This routine sends the command to write the BP0 and BP0 FRAM
;* bits in the status register
;* Calls: outbyt
;* Input: None
;* Outputs: None
;*******************************************************************************
*/
void wrsr_cmd(uchar status)
{
	wren_cmd();
 	CLK=0;/* Bring CLK low */
 	CSX=0;/* Bring /CSX low */
 	outbyt(WRSR_INST) ;/* Send WRSR instruction */
 	outbyt(status);/* Send status register */
 	CLK=0;/* Bring CLK low */
 	CSX=1;/* Bring /CSX high */
}


/*;***************************************************************************
*
;* Name: BYTE_WRITE
;* Description: Single Byte Write
;* Function: This routine sends the command to write a single byte to the FRAM memory array
;* Calls: outbyt
;* Input: aa=The data want to be writen,dd=The address of the menory want to be writen
;* Outputs: None
;******************************************************************************
*/
void byte_write(uint dd,uchar aa)
{
	wren_cmd(); /*write enable*/
	CLK=0;CSX=0;
 	outbyt(WRITE_INST);/* Send WRITE instruction including MSB of address */
 	outbyt((uchar)(dd>>8));
 	outbyt((uchar)(dd));
	outbyt(aa);
 	CLK=0;CSX=1;
}
//======================================
/*;***************************************************************************
*
;* Name: BYTE_SEQ_WRITE
;* Description: Subsequent Byte Write
;* Function: This routine sends the command to write Subsequent byte to the FRAM memory array
;* Calls: outbyt
;* Input: PTR is the data pointer want to be writen,dd=The address of the menory want to be writen
	  NUM is the byte number want to be writen to the FRAM menory
;* Outputs: None
;******************************************************************************
*/
void write_seq(uchar *ptr,uint dd,uchar num)
{
	uchar i;
	wren_cmd(); /*write enable*/
	CLK=0;CSX=0;
 	outbyt(WRITE_INST);/* Send WRITE instruction including MSB of address */
 	outbyt((uchar)(dd>>8));
 	outbyt((uchar)(dd));
 	for(i=0;i<num;i++)
 	{outbyt(*(ptr+i));}
 	CLK=0;CSX=1;
}

/*;****************************************************************************
*
;* Name: BYTE_READ
;* Description: Single Byte Read
;* Function: This routine sends the command to read a single byte from the FRAM memory array
;* Calls: outbyt, inputbyt
;* Input: dd=The address of the menory want to be readed
;* Outputs: temp= read byte
;*****************************************************************************
*/
uchar byte_read(uint dd)
{
 	uchar temp;
 	uchar temph,templ;
 	temph=(uchar)(dd>>8);
 	templ=(uchar)(dd);
	CLK=0;CSX=0;
 	outbyt(READ_INST);/* Send READ_INST instruction including MSB of address */
	outbyt((uchar)(dd>>8));
	outbyt((uchar)(dd));
 	temp=inputbyt();
 	CLK=0;CSX=1;
	return(temp);
}
//===============================
/*;****************************************************************************
*
;* Name: BYTE_SEQ_READ
;* Description: Subsequent Byte Read
;* Function: This routine sends the command to read Subsequent byte from the FRAM memory array
;* Calls: outbyt, inputbyt
;* Input: PTR is the first address of the saved buffer
          dd=The address of the menory want to be readed
          NUM is the byte number want to be read from the FRAM menory
;* Outputs: temp= read byte
;*****************************************************************************
*/
void read_seq(uchar *ptr,uint dd,uchar num)
{
	uchar i;
	uchar temp;
	temp=(uchar)dd;
 	CLK=0;CSX=0;
 	outbyt(READ_INST);/* Send READ_INST instruction including MSB of address */
	outbyt((uchar)(dd>>8));
	outbyt((uchar)dd);
	for(i=0;i<num;i++)
	{*(ptr+i)=inputbyt();}
 	CLK=0;CSX=1;
}
void dkcsh(void) 
{
  PTCDD=0x28;         //PTC3,C5开出         
  PTCPE=0x28;                 
  PTCDS=0x28;                
  PTCSE=0x28;  
  PTCD=0x28;          //PTC3 C5 CS0 C1
  
  PTEDD=0xc0;         //PTE6,7开出 5入         
  PTEPE=0xc0;                 
  PTEDS=0xc0;                
  PTESE=0xc0;  
  PTED=0xc0;          //PTC3 C5 CS0 C1
}

//==========================================================================
void main(void)
{
	uint i=0;
	SOPT=0x53+0x80;       //打开看门狗,时间(1/40M)*262144=6.5536mS
                                
  SPMSC1=0x1c;          //LVDF  LVDACK  LVDIE   LVDRE  LVDSE LVDE -  BGBE            
  SPMSC2=0x00;
//时钟部分4MHZ-20MHZ        
  ICGC1=0x7a;           
  ICGC2=0x30;
                        // M = 10, R = 1 (RFD = 0), P = 1
                        // ICGOUT = fext * P * N / R = 4MHz * 1 * 10 / 1 = 40 MHz
                        // fbus = 20 MHz             
  SRS=0x00;               
  while(ICGS1_LOCK==0){SRS=0x00;} 
	dkcsh();
	
	wrsr_cmd(0x00);//init the protect bit ,no protect
	wren_cmd();
	number=0;
	numtime=0;
	//注意:请将number,temp,tempr加入变量观察窗口,如果内容一致,则存储器正确
	//请将对应IO口修改为目标硬件对应的IO口,同时将SLK,SI,SO加上4K7的上拉电阻
	while(1)
	{
		numtime=rdsr_cmd ();
		//以下代码测试单字节读写,从地址0开始,连续16个字节
		//number是将写入的数据,temp数组中存储读出的值
		if(number++>200)
		{number=0;}
		for(i=0;i<0x10;i++)        //0x7fff
		{
			byte_write(i,number);
			__RESET_WATCHDOG();   // feeds the dog
		}
		for(i=0;i<0x10;i++)         //0x7fff
		{
			numtime=byte_read(i);
			__RESET_WATCHDOG();   // feeds the dog
			if(numtime!=number)
			{
				while(1){;}
			}
		}
		//以下代码测试连读连写,从地址16开始,连续16个字节
		//number是将写入的数据,tempr数组中存储读出的值
		/*for(i=0;i<16;i++)
		{
			tempw[i]=number;
		}
		write_seq(tempw,16,16);//参考分别对应缓冲区首地址,写入内存首地址,写入长度
		read_seq(tempr,16,16);*///参考分别对应缓冲区首地址,写入内存首地址,写入长度
		i=0;
	}
	
}

⌨️ 快捷键说明

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