赛元单片机SC92F732x系列eeprom毒蝎操作[以7321为例]

目标:在0x0E区域写入0x20,在0x2E区域写入0x21,在0x4E区域写入0x22
先放效果图:
断电后,将写入程序删掉,再上电:
赛元单片机SC92F732x系列eeprom毒蝎操作[以7321为例]
代码部分:
首先本代码依据赛元官方DEMO代码所作修改,官方地址:赛元芯片官方DEMO

main函数部分

#include "H/Function_Init.H"
#include "stdio.h"
/**************************************************************
说明:
1、Options for Target‘Target1’:BL51 Locate->Code Range:0x100,烧录选项请选择DISRST,复位脚作为普通IO使用;
2、改变TEST的定义,可以分别测试对应的功能;
3、注意:先在Function.H里面选择测试型号(SC92F7320无LCD/LED和PWM功能)
***************************************************************/
#define Test  5    //BTM:0 EXTI:1 Timer:2 LCD:3 PWM:4 Uart0:5 ADC:7 IAP:8 
extern uchar IapReadData;   //存放读取的IAP数据
void main(void)
{
	IO_Init();
	WDTCON |= 0x10;		    //清看门狗
	Uart0_Init();
	printf("Write Data:\r\n0x0E->0x20\r\n0x2E->0x21\r\n0x4E->0x22\r\n");
	IAPWrite(0x0E,0x20,IapEPPROM);
	IAPWrite(0x2E,0x21,IapEPPROM);
	IAPWrite(0x4E,0x22,IapEPPROM);
	printf("waiting...\r\n");
	IapReadData = IAPRead(0x0E,IapEPPROM);
	IAPADE = 0x00;       //IAP指针指回ROM区
	printf("Read 0x0E Data:%x\r\n",IapReadData);
	IapReadData = IAPRead(0x2E,IapEPPROM);
	IAPADE = 0x00;       //IAP指针指回ROM区
	printf("Read 0x2E Data:%x\r\n",IapReadData);
	IapReadData = IAPRead(0x4E,IapEPPROM);
	IAPADE = 0x00;       //IAP指针指回ROM区
	printf("Read 0x4E Data:%x\r\n",IapReadData);
	switch(Test)
	{
		case 0: BTM_Test();
		break;
		case 1: EXTI_Test();
		break;
		case 2: Timer_Test();
		break;
		case 3: LCD_Test();
		break;
		case 4: PWM_Test();
		break;
		case 5: //Uart0_Test();
		break;
		case 7: ADC_Test();
		break;
		case 8: //IAP_Test(0x1fff,IapROM);   //操作ROM,对地址0x1fff进行读写测试
		        //IAP_Test(0x7f,IapEPPROM);   //操作EPPROM,对地址0x7f进行读写测试
		break;
		default:
		break;
	}
}

IAP_INIT部分:

#include "H/Function_Init.H"

//#define IapROM  0x00	  //0x00:选择ROM区操作  0x02:选择EEPROM区操作
//#define IapEPPROM  0x02	  //0x00:选择ROM区操作  0x02:选择EEPROM区操作
unsigned char code *IapAddr = 0x00;

void IAPWrite(uint Addr,uchar Value,uchar IAPArea);
uchar IAPRead(uint Addr,uchar IAPArea);
uchar IapReadData = 0x00;   //存放读取的IAP数据

/*****************************************************
*函数名称:void IAP_Test(uchar Address,uchar IAPArea)
*函数功能:IAP测试
*入口参数:Address,IAPArea
*出口参数:void
*****************************************************/
void IAP_Test(uint Address,uchar IAPArea)
{
	IAPWrite(Address,0xff,IAPArea);
	IapReadData = IAPRead(Address,IAPArea);
	IAPADE = 0x00;       //IAP指针指回ROM区
	while(1)
	{
		if(IapReadData == 0xff)
		{
			P20 = ~P20;
		}
		else
		{
			P21 = ~P21;
		}
	}
}

/*****************************************************
*函数名称:void IAPWrite(uchar Addr,uchar Value,uchar IAPArea)
*函数功能:IAP写初始化
*入口参数:Addr,Value,IAPArea
*出口参数:void
*****************************************************/
//IAP写操作
void IAPWrite(uint Addr,uchar Value,uchar IAPArea)
{	
	EA = 0;
	IAPDAT = Value;      //写入数据Data
	IAPADH = ((Addr>>8)&0X7F);   //写入地址的高5位
	IAPADL = Addr;               //写入地址的低8位
	
	IAPADE = IAPArea;  //IAPArea=0x00:选择ROM区操作  IAPArea=0x02:选择EEPROM区操作
	IAPKEY = 240;		   //IAP开启保护控制时间
	IAPCTL = 0x06;     //CPU hold time为2ms,写入操作命令
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	IAPADE = 0x00;      //MOVC指向ROM
	EA = 1;
}
/*****************************************************
*函数名称:uchar IAPRead(uchar Addr,uchar IAPArea)
*函数功能:IAP读初始化
*入口参数:Addr,IAPArea
*出口参数:ReadValue
*****************************************************/
uchar IAPRead(uint Addr,uchar IAPArea)
{
	uchar ReadValue = 0x00;
	EA = 0;
	IAPADE = IAPArea;
	ReadValue = *(IapAddr+Addr); //读取的数据
	IAPADE = 0x00;               //MOVC指向ROM
	EA = 1;
	return ReadValue;
}

function_INIT部分:

#ifndef _Function_Init_H_
#define _Function_Init_H_

#include "SC92F732x_C.H"
#include "intrins.H"
#define uchar unsigned char
#define uint unsigned int

#define IapROM  0x00	  //0x00:选择ROM区操作  0x02:选择EEPROM区操作
#define IapEPPROM  0x02	  //0x00:选择ROM区操作  0x02:选择EEPROM区操作

extern void IO_Init(void);
extern void BTM_Test(void);
extern void EXTI_Test(void);
extern void Timer_Test(void);
extern void LCD_Test(void);
extern void PWM_Test(void);
extern void ADC_Test(void);
extern void IAP_Test(uint Address,uchar IAPArea);

void Uart0_Init(void);
void UartSendBit(unsigned char c);

void IAPWrite(uint Addr,uchar Value,uchar IAPArea);
uchar IAPRead(uint Addr,uchar IAPArea);

/*****************************************
说明:选择测试IC
*****************************************/
#define SC92F7323  0
#define SC92F7322  1
#define SC92F7321  2
#define SC92F7320  3

#define IC_MODEL  SC92F7321

#endif
上一篇:JS-Web-API —— DOM增删改查


下一篇:【基于Arduino开发环境ESP8266 oled1306绘图(程序是修改至CSDN的Willliam_william博主)】