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

📄 nvm.c

📁 abov公司的单片机器MC80F7208的演示程序,C代码,包含LCD,I2C,KEY,ADC,NVM,TIME等内容,适合初学者熟悉.
💻 C
字号:
//===============================================================
//  	File name : Nvm.C 
//			Non Volatile Memory interface 
//=============================================================== 

//------------------------------------------------------------------
//	Include file and variable 
//------------------------------------------------------------------

#include <hms800.h>				//
#include "MC80C7208.h"				// SFR define 
#define  USER_EXTERN extern			// extern variable define            
#include "Variable.h"				//

//------------------------------------------------------------------
//	external prototype function	
//------------------------------------------------------------------

#define	NVM_PAGE1		0xA0		//
#define	NVM_PAGE2		0xA2		//

#define	NVM_Option		0xE2		// 	

#define	NVM_Virgin_ID	0xFE		//
#define	Old_NVM		0x39		//


//=============================================================== 
/*
;     task  : I2C bus function  
;     input : 
;             rI2C_Mad  	Main(Slave)address
;             rI2C_Sad  	Sub register address  
;             *mI2C_post	Data Pointer of MCU 
;             rI2C_byte 	How many Data byte accessed ? 
;             rI2C_Mode 	Data Transfer format 
;                 x--- ----   1=read, 0=write
;                 -xx- ----   how many subaddress (max.3 bytes)
;                 ---x xxxx   how many data byte accessed ?(max.31 bytes)
;     output: 
;             f_NoACK (0=ok, 1=fail)
;
*/
//==================================================================  
//------------------------------------------------------------------
//	task	: I2C delay
//------------------------------------------------------------------ 

void Delay_5us();

asm(" 
	.sect	.text
	.globl _Delay_5us
	.type	_Delay_5us,@function

_Delay_5us:						; 8 
		ret					; 5	= 6.5us 
   ");

//------------------------------------------------------------------
//	task	: I2C Restart
//------------------------------------------------------------------ 

void I2C_restart();

asm("
	.sect	.text
	.globl _I2C_restart
	.type	_I2C_restart,@function

SDA	equ	4,0C0h				; R04
SCL	equ	6,0C0h				; R06 

_I2C_restart:
;	ldm	0C0h, #52h				; R0 
	ldm	0A5h, #70h				; R0PU
;	ldm	0C1h, #52h				; R0IO = 0101_0010

	ldm	0C0h, #72h				; R0 
	ldm	0C1h, #72h				; R0IO

	call	!_Delay_5us				;
	clr1	SDA					; SDA = low
	call	!_Delay_5us				;
	clr1	SCL					; SCL = low 	
	ret		
");

//------------------------------------------------------------------
//	task	: I2C stop generate
//------------------------------------------------------------------ 
void I2C_stop();

asm("
	.sect	.text
	.globl _I2C_stop
	.type	_I2C_stop,@function

_I2C_stop:
	ldm	0C1h, #52h				; SCL/SDA out 
	
	clr1	SCL					; SCL = low
	clr1	SDA					; SDA = high
	call	!_Delay_5us				;
	set1	SCL					; SCL = high
	call	!_Delay_5us				;
	set1	SDA					; SDA = high		
	ret						;
"); 

//------------------------------------------------------------------
//	task	: I2C clock pulse out
//------------------------------------------------------------------ 
void SCL_out();	

asm("	
	.sect	.text
	.globl _SCL_out
	.type	_SCL_out,@function

_SCL_out:
	set1	SCL					; SCL = high
	nop						;
	nop						;
	nop						;
	nop						;
	nop						;
	clr1	SCL					; SCL = low
	ret						;
");						


//------------------------------------------------------------------
//	task	: i = 1 Byte狼 Data write
//	input : i 
//	output: f_NoACK = 1 fail 
//------------------------------------------------------------------ 
void Write_byte(void);

asm(" 
	.sect	.text
	.globl _Write_byte
	.type	_Write_byte,@function

_Write_byte:
	ldm	0C1h, #70h				; -o-o ---- = SCL output, SDA output
;	ldm	0C1h, #52h				; -o-o ---- = SCL output, SDA output
	lda	_i					; 
	ldy	#8					;
Rpt_Write_byte:
	rol	A					;
	stc	SDA					; Data out 
	call	!_SCL_out				;
	dbne	Y,Rpt_Write_byte			;

	ldm	0C1h, #40h				; -o-i ---- = SCL output, SDA input
	nop						;
	nop						;
	nop						;
	set1	SCL					; SCL=high
	nop						;
	nop						;
	nop						;
	nop						;
	nop						;
	ldc	SDA					; ACK read 
	stc	_rComm_flag.0			; f_NoACK
	clr1	SCL					; SCL=low
	ret						;
  ");

//------------------------------------------------------------------
//	task	: 1 Byte狼 Data甫 Slave肺 何磐 佬澜
//	input	:
//	output: i = read data
//------------------------------------------------------------------ 

void Read_byte(void);

asm("
	.sect	.text
	.globl _Read_byte
	.type	_Read_byte,@function

_Read_byte:
	ldm	0C1h, #42h				; -o-i ---- = SCL output, SDA input
	ldy	#8					;
Rpt_Read_byte:
	set1	SCL					; SCL=high
	nop						;
	nop						;
	nop						;
	nop						;
	nop						;
	nop						;
	ldc	SDA					; data bit read 
	clr1	SCL					; SCL=low
	nop						;
	rol	A					;
	dbne	Y,Rpt_Read_byte			;
	sta	_i					;
	ret						;
 ");
 
//==================================================================
//	task	: n Byte Data iterface with NVM 
//	input	: mI2C_mode  R/W(7), Subaddress(6), Data byte(4~0) 
//		  mI2C_mad	 0xA0 or 0xA2 (24C04)
//		  mI2C_sad 	 0x00~0xFF
//		  mI2C_post  data post in MCU
//	output: f_NoACK (1=NAK, 0=ACK) 
//================================================================== 

void I2C_func(void)
{
	uchar m,k;
	
	I2C_restart();					// start 
	rI2C_byte = rI2C_mode & 0x1F;			// data byte 荐 ?  31 bytes max     	

	i = rI2C_mad;					//
	Write_byte(); 					//
	if (f_NoACK)					// Slave address out, no Acknowledge	 
	{	I2C_stop();					// write fail 
		return;					// no Acknowledge 
	}
	i = rI2C_sad;					//
	Write_byte();					//
	if (f_NoACK)					// Sub address out, no Acknowledge	 
	{	I2C_stop();					// write fail 
		return;					// no Acknowledge 
	}
	if (rI2C_mode & 0x80)				// read/write ? 
	{	
		I2C_restart();				// restart for read cammand 	 

		i = rI2C_mad | 0x01;
		Write_byte(); 				// read command out 
		if (f_NoACK)				// no Acknowledge
		{	I2C_stop();				// write fail no Acknowledge
			return;				//   
		}

		for(k=0;k<rI2C_byte;k++)
		{	Read_byte();			// Read byte  
			*rI2C_post = i;			//
			(uchar *)rI2C_post++;		//
			
			R0IO	= 0x52;			// sda(R04) output 
			if (k == (rI2C_byte -1))	// Last byte ? 
				p_SDA = ON;			// No Acknowledge out		 
			else	p_SDA	= OFF;		// Acknowledge out	 
			SCL_out();
		}
	}
	else
	{	
		for(k=0;k<rI2C_byte;k++)		//
		{	i = *rI2C_post;			//
			Write_byte();			// Write Byte 
			(uchar *)rI2C_post++;		//
			if (f_NoACK) 			//
			{	I2C_stop();			// write fail 
				return;			// no Acknowledge 
			}
		}
	} 

	I2C_stop();			
}	

//==================================================================
//	task	: NVM Data Read/Write 
//==================================================================

//------------------------------------------------------------------
//	task	: Read / Write Option data  
//------------------------------------------------------------------

void RW_Option(void)
{
	rI2C_mad		= NVM_PAGE1;		// page1 = 0xA0  
	rI2C_sad		= NVM_Option;		//   
	(uchar *)rI2C_post= &rOption;			//      
	I2C_func();						// 
}

void Rd_Option(void)
{	
	rI2C_mode	= 0xC1;				// 1100_0001b 
	RW_Option();
}

void Wr_Option(void)
{	
	R01	= OFF;					// write protect off  
	rI2C_mode	= 0x41;				// 0100_0001b 
	RW_Option();
	R01	= ON;						// write protect on  
}

//==================================================================
//	task	: NVM Initialize  
//================================================================== 

void NVM_Initialize(void)
{
	R01	= OFF;					// write protect disable 

	rOption = 0x00;					//
	Wr_Option();					//

	R01	= ON;						// write protect enable
}

//==================================================================
//	task	: Check NVM Virgin 
//	1. ACK啊 沥惑利捞绊 2雀 捞惑 Virgin ID啊 促甫 锭 檬扁拳. 
//	2. 2雀 捞惑 NoAck捞搁 FeRAM阑 焊龋窍扁 困秦 眠啊 I2C 烹脚阑
//	   窍瘤 臼绰促. 
//	3. 贸澜 Virgin_ID啊 促福绊 滴锅掳 NoAck捞搁 促矫 Check茄促. 
//================================================================== 

void Rd_NVM_Virgin(void)
{
	rI2C_mad		= NVM_PAGE1;		// page 2 = 0xA2  
	rI2C_sad		= NVM_Virgin_ID;		// 0xFE 
	rI2C_mode		= 0xC1;			// 1100_0001b = 1 byte read 
	(uchar *)rI2C_post= &rTemp[0];		// read virgin ID 

	I2C_func();						//
}


void Check_NVM_Initial(void)
{
	Rd_NVM_Virgin();
	if(rTemp[0] != Old_NVM)
	{	Rd_NVM_Virgin();
		if(rTemp[0] != Old_NVM)
			NVM_Initialize();
	}
}

//================================================================ 
//     the end of Nvm.C					    			 

⌨️ 快捷键说明

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