25045操作标准子程序集41.C
/*
;程 序 最 后 修 改 时 间 0-4-3 23:43
;软 件 标 题:25045操作标准子程序集41
;软 件 说 明:25045 I2C 串行EEPROM 驱动
;_________________________________________
;原作者: 庞波
;程序修改人:
;版本号:
;_________________________________________
*/
/*到现在为止所有的问题都已经解决,此版本已经较为完善*/
# include <stdio.h>
# include <reg52.h>
# define uchar unsigned char
# define uint unsigned int
sbit SO=P1^;/*25045输出*/
sbit SI=P1^;/*25045输入*/
sbit SCK=P1^;/*25045时钟*/
sbit CS=P1^;/*25045片选*/
uchar code WREN_INST=0X06;
/* Write enable latch instruction (WREN)*/
uchar code WRDI_INST=0X04;
/* Write disable latch instruction (WRDI)*/
uchar code WRSR_INST=0X01;
/* Write status register instruction (WRSR)*/
uchar code RDSR_INST=0X05;
/* Read status register instruction (RDSR)*/
uchar code WRITE_INST=0X02;
/* Write memory instruction (WRITE)*/
/*写入25045的先导字,应当为0000A010,其中的A为写入25045的高位地址
将此WRITE_INST和写入高位地址相或后即为正确的写先导字*/
uchar code READ_INST=0X03;
/* Read memory instruction (READ)*/
/*读出25045的先导字,应当为0000A011,其中的A为读出25045的高位地址
将此READ_INST和读出高位地址相或后即为正确的读先导字*/
uint code BYTE_ADDR=0X55;
/* Memory address for byte mode operations*/
uchar code BYTE_DATA=0X11;
/*Data byte for byte write operation*/
uint code PAGE_ADDR=0X1F;
/* Memory address for page mode operations*/
/*页面写入的其始地址*/
uchar code PAGE_DATA1=0X22;
/* 1st data byte for page write operation*/
uchar code PAGE_DATA2=0X33;
/* 2nd data byte for page write operation*/
uchar code PAGE_DATA3=0X44;
/* 3rd data byte for page write operation*/
uchar code STATUS_REG=0X20;
/* Status register,设置DOG时间设置为200毫秒,无写保护*/
/*这是状态寄存器的值,他的意义在于第5,第4位为WDI1,WDI0代表DOG的时间,00为1.4秒,01为600毫秒,10为200毫秒,00为disabled
第3位和第2位为BL1,BL0,是写保护设置位,00为无保护,01为保护180-1FF,10为保护100-1FF,11为保护000-1FF.第1位为WEL,
当他为1时代表已经"写使能"设置了,现在可以写了,只读位.第0位为WIP,当他为1时代表正在进行写操作,是只读*/
uchar code MAX_POLL=0x99;
/* Maximum number of polls*/
/*最大写过程时间,确定25045的最大的写入过程的时间*/
uchar code INIT_STATE=0x09;
/* Initialization value for control ports*/
uint code SLIC=0x30;
/* Address location of SLIC*/
void wren_cmd(void);/*写使能子程序*/
void wrdi_cmd(void);/*写使能复位*/
void wrsr_cmd(void);/*复位时间位和数据保护位写入状态寄存器*/
uchar rdsr_cmd(void);/*读状态寄存器*/
void byte_write(uchar aa,uint dd);/*字节写入,aa为写入的数据,dd为写入的地址*/
uchar byte_read(uint dd);/*字节读出,dd为读出的地址,返回读出的数据*/
void page_write(uchar aa1,uchar aa2,uchar aa3,uchar aa4,uint dd);/*页写入*/
void sequ_read(void);/*连续读出*/
void rst_wdog(void);/*DOG复位*/
void outbyt(uchar aa);/*输出一个字节到25045中,不包括先导字等*/
uchar inputbyt();/*由25045输入一个字节,不包括先导字等额外的东西*/
void wip_poll(void);/*检查写入过程是否结束*/
/*25045操作子程序集*/
/*;*******************************************************************************************
*
;* Name: WREN_CMD
;* Description: Set write enable latch
;* Function: This routine sends the command to enable writes to the EEPROM memory array or
;* status register
;* Calls: outbyt
;* Input: None
;* Outputs: None
;* Register Usage: A
;*******************************************************************************************
*/
/*写使能子程序*/
void wren_cmd(void)
{
uchar aa;
SCK=;/* Bring SCK low */
CS=;/* Bring /CS low */
aa=WREN_INST;
outbyt(aa);/* Send WREN instruction */
SCK=;/* Bring SCK low */
CS=;/* Bring /CS high */
}
/*;*******************************************************************************************
*
;* Name: WRDI_CMD
;* Description: Reset write enable latch
;* Function: This routine sends the command to disable writes to the EEPROM memory array or
;* status register
;* Calls: outbyt
;* Input: None
;* Outputs: None
;* Register Usage: A
;*******************************************************************************************
*/
/*写使能复位子程序*/
void wrdi_cmd(void)
{
uchar aa;
SCK=;/* Bring SCK low */
CS=;/* Bring /CS low */
aa=WRDI_INST;
outbyt(aa);/* Send WRDI instruction */
SCK=;/* Bring SCK low */
CS=;/* Bring /CS high */
}
/*;*******************************************************************************************
*
;* Name: WRSR_CMD
;* Description: Write Status Register
;* Function: This routine sends the command to write the WD0, WD1, BP0 and BP0 EEPROM
;* bits in the status register
;* Calls: outbyt, wip_poll
;* Input: None
;* Outputs: None
;* Register Usage: A
;*******************************************************************************************
*/
/*写状态寄存器子程序*/
void wrsr_cmd(void)
{
uchar aa;
SCK=;/* Bring SCK low */
CS=;/* Bring /CS low */
aa=WRSR_INST;
outbyt(aa) ;/* Send WRSR instruction */
aa=STATUS_REG;
outbyt(aa);/* Send status register */
SCK=;/* Bring SCK low */
CS=;/* Bring /CS high */
wip_poll();/* Poll for completion of write cycle */
}
/*;*******************************************************************************************
*
;* Name: RDSR_CMD
;* Description: Read Status Register
;* Function: This routine sends the command to read the status register
;* Calls: outbyt, inputbyt
;* Input: None
;* Outputs: A = status registerXicor Application Note AN21
;* Register Usage: A
;*******************************************************************************************
*/
/*读状态寄存器,读出的数据放入到aa中*/
uchar rdsr_cmd (void)
{
uchar aa;
SCK=;
CS=;
aa=RDSR_INST;
outbyt(aa);
aa=inputbyt();
SCK=;
CS=;
return aa;
}
/*;*******************************************************************************************
*
;* Name: BYTE_WRITE
;* Description: Single Byte Write
;* Function: This routine sends the command to write a single byte to the EEPROM memory
array
;* Calls: outbyt, wip_poll
;* Input: None
;* Outputs: None
;* Register Usage: A, B
;*******************************************************************************************
*/
/*字节写入,aa为写入的数据,dd为写入的地址,对于25045而言为000-1FF*/
void byte_write(aa,dd)
uchar aa;
uint dd;
{
SCK=;
CS=;
outbyt((((uchar)(dd-)|WRITE_INST);/* Send WRITE instruction including MSB of address */
/*将高位地址左移3位与写入先导字相或,得到正确的先导字写入25045*/
outbyt((uchar)(dd));
/*输出低位地址到25045*/
outbyt(aa);
/*写入数据到25045的对应单元*/
SCK=;
CS=;
wip_poll();
/*检测是否写完*/
}
/*;*******************************************************************************************
*
;* Name: BYTE_READ
;* Description: Single Byte Read
;* Function: This routine sends the command to read a single byte from the EEPROM memory
array
;* Calls: outbyt, inputbyt
;* Input: None
;* Outputs: A = read byte
;* Register Usage: A, BXicor Application Note AN21
;*******************************************************************************************
*/
/*字节读出,其中dd为读出的地址,返回的值为读出的数据*/
uchar byte_read(dd)
uint dd;
{
uchar cc;
SCK=;
CS=;
outbyt((((uchar)(dd-)|READ_INST);/* Send READ_INST instruction including MSB of address */
/*将高位地址左移3位与读出先导字相或,得到正确的先导字写入25045*/
outbyt((uchar)(dd));
/*输出低位地址到25045*/
cc=inputbyt();/*得到读出的数据*/
SCK=;
CS=;
return cc;
}
/*;*******************************************************************************************
*
;* Name: PAGE_WRITE
;* Description: Page Write
;* Function: This routine sends the command to write three consecutive bytes to the EEPROM
;* memory array using page mode
;* Calls: outbyt, wip_poll
;* Input: None
;* Outputs: None
;* Register Usage: A, B
;*******************************************************************************************
*/
/*页面写入,其中aa1,aa2,aa3,aa4为需要写入的4个数据(最大也就只能一次写入4个字,dd为写入的首地址*/
void page_write(aa1,aa2,aa3,aa4,dd)
uchar aa1,aa2,aa3,aa4;
uint dd;
{
SCK=;
CS=;
outbyt((((uchar)(dd-)|WRITE_INST);/* Send WRITE instruction including MSB of address */
/*将高位地址左移3位与写入先导字相或,得到正确的先导字写入25045*/
outbyt((uchar)(dd));
/*写入低位地址到25045*/
outbyt(aa1);
/*写入数据1到25045的对应单元*/
outbyt(aa2);
/*写入数据2到25045的对应单元*/
outbyt(aa3);
/*写入数据3到25045的对应单元*/
outbyt(aa4);
/*写入数据4到25045的对应单元*/
SCK=;
CS=;
wip_poll();
}
/*;*******************************************************************************************
*
;* Name: SEQU_READ
;* Description: Sequential Read
;* Function: This routine sends the command to read three consecutive bytes from the EEPROM
;* memory array using sequential mode
;* Calls: outbyt, inputbyt
;* Input: None
;* Outputs: A = last byte read
;* Register Usage: A, B
;*******************************************************************************************
*/
/*连续读出,由于函数的返回值只能为1个,对于连续读出的数据只能使用指针作为函数的返回值才能做到返回一系列的数组*/
/*sequ_read:*/
unsigned int *page_read(n,dd)
uchar n;/*n是希望读出的数据的个数,n<=11*/
unsigned int dd;/*dd是读出数据的首地址*/
{
uchar i;
uchar pp[];
unsigned int *pt=pp;
SCK=;
CS=;
outbyt((((uchar)(dd-)|READ_INST);
;i<n;i++)
{
pp[i]=inputbyt();
}
return (pt);
}
/*调用的方法如下*/
/*unsigned int *p;*/
/*p=page_read(4,100);*/
/*a=*(p)*/
/*b=*(p+1)*/
/*c=*(p+2)*/
/*d=*(p+3)*/
/*abcd中存放25045中由100地址开始的4个数据*/
/* Send WRITE */
/*mov DPTR, #PAGE_ADDR ; Set address of 1st byte to be read
clr sck ; Bring SCK low
clr cs ; Bring /CS low
mov A, #READ_INST
mov B, DPH
mov C, B.0
mov ACC.3, C
lcall outbyt ; Send READ instruction with MSB of address
mov A, DPL
lcall outbyt ; Send low order address byte
lcall inputbyt ; Read 1st data byte
lcall inputbyt ; Read 2nd data byte
lcall inputbyt ; Read 3rd data byte
clr sck ; Bring SCK low
setb cs ; Bring /CS high
ret*/
/*;*******************************************************************************************
*
;* Name: RST_WDOG
;* Description: Reset Watchdog Timer
;* Function: This routine resets the watchdog timer without sending a command
;* Calls: None
;* Input: None
;* Outputs: None
;* Register Usage: None
;*******************************************************************************************
*/
/*复位DOG*/
void rst_wdog (void)
{
CS=;
CS=;
}
/*;*******************************************************************************************
*
;* Name: WIP_POLL
;* Description: Write-In-Progress Polling
;* Function: This routine polls for completion of a nonvolatile write cycle by examining the
;* WIP bit of the status register
;* Calls: rdsr_cmdXicor Application Note AN21
;* Input: None
;* Outputs: None
;* Register Usage: R1, A
;*******************************************************************************************
*/
/*检测写入的过程是否结束*/
void wip_poll(void)
{
uchar aa;
uchar idata my_flag;
;aa>MAX_POLL;aa++)
{
my_flag=rdsr_cmd();
) {aa=MAX_POLL;}/*判断是否WIP=0,即判断是否写入过程已经结束,若结束就跳出,否则继续等待直到达到最大记数值*/
}
}
/*;*******************************************************************************************
*
;* Name: OUTBYT
;* Description: Sends byte to EEPROM
;* Function: This routine shifts out a byte, starting with the MSB, to the EEPROM
;* Calls: None
;* Input: A = byte to be sent
;* Outputs: None
;* Register Usage: R0, A
;*******************************************************************************************
*/
/*输出一个数据到25045,此数据可能为地址,先导字,写入的数据等*/
void outbyt(aa)
uchar aa;
{
uchar my_flag1,i;
;i>;i++)
{
my_flag1=aa;
SCK=;
SI=(my_flag1>>i);
SCK=;
}
SI=;/*使SI处于确定的状态*/
}
/*;*******************************************************************************************
*
;* Name: INPUTBYT
;* Description: Recieves byte from EEPROM
;* Function: This routine recieves a byte, MSB first, from the EEPROM
;* Calls: None
;* Input: None
;* Outputs: A = recieved byte
;* Register Usage: R0, A
;*******************************************************************************************
*/
/*得到一个数据,此数据可能为状态寄存器数据,读出的单元数据等*/
uchar inputbyt(void)
{
uchar aa,my_flag;
char i;
;i<;i--)
{
SCK=;
my_flag=(uchar)(SO);
SCK=;
aa=(aa||(my_flag<<i));
my_flag=0x00;
}
return aa;
}
25045操作标准子程序集41.C的更多相关文章
- 解析大型.NET ERP系统核心组件 查询设计器 报表设计器 窗体设计器 工作流设计器 任务计划设计器
企业管理软件包含一些公共的组件,这些基础的组件在每个新项目立项阶段就必须考虑.核心的稳定不变功能,方便系统开发与维护,也为系统二次开发提供了诸多便利.比如通用权限管理系统,通用附件管理,通用查询等组件 ...
- Xml语言
一.XML是什么?作用是什么? l XML ( eXtensible Markup Language )语言是一种可扩展的标记语言.其中的可扩展是相对HTML来说的.因为XML标签没有被预定义,需要 ...
- SMG12232A2标准图形点阵型液晶显示模块的演示程序[C51编程语言]
//SMG12232A2标准图形点阵型液晶显示模块的演示程序[C51编程语言][MCS51总线接口方式] //应用产品: SMG12232A2标准图形点阵型液晶显示模块 // 本演示程序适用于SMG1 ...
- JavaScript基础知识(二)
一.JavaScript事件详解 1.事件流:描述的是在页面中结束事件的顺序 事件传递有两种方式:冒泡与捕获. 事件传递定义了元素事件触发的顺序. 如果你将 <p> 元素插入到 <d ...
- shell入门笔记2:字符串、数组、echo与printf
说明: 本文是关于http://c.biancheng.net/cpp/shell/的相关笔记 shell字符串 字符串可以用单引号,也可以用双引号,也可以不用引号. 1 #!/bin/bash 2 ...
- 『性能』ServiceStack.Redis 和 StackExchange.Redis 性能比较
背景 近来,需要用到 Redis 这类缓存技术 —— MongoDB 和 Redis 没有进行过比较. 我也懒得在这些细节上 纠结那么多 —— 按照网友给出的文章,听从网友建议,选择 Redis. R ...
- jQuery实现select级联
使用Html5的数据属性(data-*)Map级联关系,代码如下: <!DOCTYPE html> <html> <head> <title>Selec ...
- elasticsearch 基础 语法总结
1. es 使用 restful 风格的 api 备注: es 的 api 格式 基本是这个样 请求方式 /索引名/文档名/id?参数 ,但是 还有 很多不是这样的 请求,比如 ...
- 《高性能CUDA应用设计与开发》--笔记
第一章 1.2 CUDA支持C与C++两种编程语言,该书中的实例采取的是Thrust数据并行API,.cu作为CUDA源代码文件,其中编译器为ncvv. 1.3 CUDA提供多种API: 数据并行 ...
随机推荐
- Android String 转 MD5
/** * 将字符串转成16 位MD5值 * * @param string * @return */ public static String MD5(String string) { byte[ ...
- OpenJTAG+Eclipse 3.5+GDB+Mini2440图文教程
OpenJTAG+Eclipse 3.5+GDB+Mini2440图文教程 OpenJTAG与JLink的区别比较: 相同点:都同时具备USB转JTAG.USB转串口功能 差别: 1. 操作系统: O ...
- Delphi 调用系统中的计算器、记事本、画图软件方法
1.直接调用 前面uses加 ShellAPI ShellExecute(Handle, 'open', PChar('calc.exe'), nil, nil, SW_SHOW); 2.直接调 ...
- 构建一个基于 Spring 的 RESTful Web Service
本文详细介绍了基于Spring创建一个“hello world” RESTful web service工程的步骤. 目标 构建一个service,接收如下HTTP GET请求: http://loc ...
- Ext中图片上传预览的问题,困扰了好几天终于解决了,记录下
{ columnWidth:.50, xtype:'textfield', style:"padding-top:5px", name:'goodsMainPhoto', id:' ...
- Fiddler 抓取eclipse中的请求
Fiddler 抓取eclipse中的请求 代码中添加 System.setProperty("http.proxySet", "true"); System. ...
- OC 图片圆角实现
self.imageTouX.layer.masksToBounds=YES; self.imageTouX.layer.cornerRadius=/2.0f; //设置为图片宽度的一半出来为圆形 s ...
- js为鼠标添加右击事件
<script language="javascript"> /*document.oncontextmenu=Youji;*/ //为当前文档添加鼠标右击事件,防 ...
- UIView的setNeedsLayout, layoutIfNeeded 和 layoutSubviews 方法之间的关系解释
转自:http://blog.csdn.net/meegomeego/article/details/39890385 layoutSubviews总结 ios layout机制相关方法 - (CGS ...
- C#遍历Object各个属性含List泛型嵌套。
同事遇到一个问题:在做手机app接口时,返回JSON格式,json里面的数据属性均是string类型,但不能出现NULL(手机端那边说处理很麻烦,哎).Model已经创建好了,而且model的每个属性 ...