📄 m25p16.c
字号:
/*****************************************************************************************************
* 华南理工大学
* 机械工程学院 焊接研究所
* South China University of Technology
* (c) Copyright 2006, Leon Lee
* All Rights Reserved
*
* 项目名称: ARM_STUD_CONTROLLER.UV2
*
* 文件名称: M25P16.c
*
* 作 者: Happy Xu
*
* 描 述: 基于PHILIPS LPC2136/38控制的逆变电源控制器焊接过程控制程序
*
* 备 注:
*
* 版 本:
* 版本号 | 日 期 | 签 名 | 描述
* ------------+----------+----------+----------------------------------------------
* V1.0 |2007/11/28| Happy Xu | 原始版本
* ------------+----------+----------+----------------------------------------------
*
*****************************************************************************************************/
#include "Arm-stud_Controller.h"
#define WRITE_EN 0x06 // 写使能
#define WRITE_DISABLE 0x04 // 写禁止
#define READ_ID 0x9f // 读器件标识
#define READ_STATUS_REG 0x05 // 读状态寄存器
#define WRITE_STATUS_REG 0x01 // 写状态寄存器
#define READ_DATA 0x03 // 读内存数据
#define FAST_READ_DATA 0x0b // 快速读内存数据------推荐使用
#define PAGE_PRO 0x02 // 页写入-------256 bytes
#define SECTOR_ERASE 0xd8 // 段擦除
#define BULK_ERASE 0xc7 // 内存全部擦除
#define M25P16_RES 0xab // 存储器重启
#define POWER_DOWN 0xb9 // 进入调电模式
/***************************************************************************************************
** 函数名称: WriteEn
** 功能描述: 写使能或禁能
** 输入参数: op:操作方式
**
** 输出参数: 无
**
** 全局变量: 无
** 调用模块: SendData()
**--------------------------------------------------------------------------------------------------
** 作 者: Happy Xu
** 日 期: 2007/11/28
***************************************************************************************************/
void WriteEn (BOOLEAN op)
{
SSEL0_OP(LOW);
if (op == HIGH)
{
SendByte(WRITE_EN);
}
else
{
SendByte(WRITE_DISABLE);
}
SSEL0_OP(HIGH);
}
/***************************************************************************************************
** 函数名称: M25p16Reset
** 功能描述: 存储器重启
** 输入参数: 无
**
** 输出参数: old style 8-bit Electronic Signature of the device
**
** 全局变量: 无
** 调用模块: SendByte();SSEL0_OP();RcvByte();
**--------------------------------------------------------------------------------------------------
** 作 者: Happy Xu
** 日 期: 2007/11/28
***************************************************************************************************/
U8 M25p16Reset(void)
{
U8 data;
SSEL0_OP(LOW);
SendByte(M25P16_RES);
SendByte(0);
SendByte(0);
SendByte(0);
data = RcvByte();
SSEL0_OP(HIGH);
return(data);
}
/***************************************************************************************************
** 函数名称: M25p16PowerOff
** 功能描述: 进入调电模式
** 输入参数: 无
**
** 输出参数:
**
** 全局变量: 无
** 调用模块: SendByte();SSEL0_OP();RcvByte();
**--------------------------------------------------------------------------------------------------
** 作 者: Happy Xu
** 日 期: 2007/11/28
***************************************************************************************************/
void M25p16PowerOff(void)
{
SSEL0_OP(LOW);
SendByte(POWER_DOWN);
SSEL0_OP(HIGH);
}
/***************************************************************************************************
** 函数名称: ReadByte
** 功能描述: 读一字节数据
** 输入参数: 内存地址
**
** 输出参数: 对应地址的一字节数据
**
** 全局变量: 无
** 调用模块: SendByte();SSEL0_OP();RcvByte();
**--------------------------------------------------------------------------------------------------
** 作 者: Happy Xu
** 日 期: 2007/11/28
***************************************************************************************************/
U8 ReadByte(U32 address)
{
U8 data = 0;
SSEL0_OP(LOW);
SendByte(READ_DATA);
SendByte((address & 0x00ff0000)>>16);
SendByte((address & 0x0000ff00)>>8);
SendByte(address & 0x000000ff);
data = RcvByte();
SSEL0_OP(HIGH);
return(data);
}
/***************************************************************************************************
** 函数名称: WriteByte
** 功能描述: 写一字节数据
** 输入参数: 内存地址;写入的数据
**
** 输出参数: 无
**
** 全局变量: 无
** 调用模块: SendByte();SSEL0_OP();RcvByte()
**--------------------------------------------------------------------------------------------------
** 作 者: Happy Xu
** 日 期: 2007/11/28
***************************************************************************************************/
void WriteByte(U32 address, U8 data)
{
U8 IsOver = 0;
WriteEn(HIGH);
SSEL0_OP(LOW);
SendByte(PAGE_PRO);
SendByte((address & 0x00ff0000)>>16);
SendByte((address & 0x0000ff00)>>8);
SendByte(address & 0x000000ff);
SendByte(data);
SSEL0_OP(HIGH);
do
{
SSEL0_OP(LOW);
SendByte(READ_STATUS_REG);
IsOver = RcvByte();
SSEL0_OP(HIGH);
}
while (IsOver & 0x01);
}
/***************************************************************************************************
** 函数名称: M25p16Erase
** 功能描述: 片擦除:占用时间大约10s
** 输入参数: 无
**
** 输出参数: 无
**
** 全局变量: 无
** 调用模块: SendByte();SSEL0_OP();RcvByte();
**--------------------------------------------------------------------------------------------------
** 作 者: Happy Xu
** 日 期: 2007/11/28
***************************************************************************************************/
void M25p16Erase(void)
{
U8 IsOver = 0;
WriteEn(HIGH);
SSEL0_OP(LOW);
SendByte(BULK_ERASE);
SSEL0_OP(HIGH);
do
{
SSEL0_OP(LOW);
SendByte(READ_STATUS_REG);
IsOver = RcvByte();
SSEL0_OP(HIGH);
}
while (IsOver & 0x01);
}
/***************************************************************************************************
** 函数名称: SectorErase
** 功能描述: 块擦除
** 输入参数: 块擦除的地址
**
** 输出参数: 无
**
** 全局变量: 无
** 调用模块: SendByte();SSEL0_OP();RcvByte();
**--------------------------------------------------------------------------------------------------
** 作 者: Happy Xu
** 日 期: 2007/11/28
***************************************************************************************************/
void SectorErase(U32 address)
{
U8 IsOver = 0;
WriteEn(HIGH);
SSEL0_OP(LOW);
SendByte(SECTOR_ERASE);
SendByte((address & 0x00ff0000)>>16);
SendByte((address & 0x0000ff00)>>8);
SendByte(address & 0x000000ff);
SSEL0_OP(HIGH);
do
{
SSEL0_OP(LOW);
SendByte(READ_STATUS_REG);
IsOver = RcvByte();
SSEL0_OP(HIGH);
}
while (IsOver & 0x01);
}
/***************************************************************************************************
** 函数名称: ReadStaReg
** 功能描述: 读状态寄存器
** 输入参数: 无
**
** 输出参数: 状态寄存器状态数据一字节
**
** 全局变量: 无
** 调用模块: SendByte();SSEL0_OP();RcvByte();
**--------------------------------------------------------------------------------------------------
** 作 者: Happy Xu
** 日 期: 2007/11/28
***************************************************************************************************/
U8 ReadStaReg(void)
{
U8 data = 0;
SSEL0_OP(LOW);
SendByte(READ_STATUS_REG);
data = RcvByte();
SSEL0_OP(HIGH);
return(data);
}
/***************************************************************************************************
** 函数名称: WrStaReg
** 功能描述: 写状态寄存器
** 输入参数: 写入状态寄存器的数据
**
** 输出参数: 无
**
** 全局变量: 无
** 调用模块: SendByte();SSEL0_OP();
**--------------------------------------------------------------------------------------------------
** 作 者: Happy Xu
** 日 期: 2007/11/28
***************************************************************************************************/
void WrStaReg(U8 data)
{
WriteEn(HIGH);
SSEL0_OP(LOW);
SendByte(WRITE_STATUS_REG);
SendByte(data);
SSEL0_OP(HIGH);
}
/***************************************************************************************************
** 函数名称: ReadBytes
** 功能描述: 从内存读数据
** 输入参数: 读出的起始地址,读出数据个数
**
** 输出参数: 内存数据数组 *block
**
** 全局变量: 无
** 调用模块: SendByte();SSEL0_OP();RcvByte();
**--------------------------------------------------------------------------------------------------
** 作 者: Happy Xu
** 日 期: 2007/11/28
***************************************************************************************************/
void ReadBytes(U32 address, U8 *block, U8 count)
{
SSEL0_OP(LOW);
SendByte(FAST_READ_DATA);
SendByte((address & 0x00ff0000)>>16);
SendByte((address & 0x0000ff00)>>8);
SendByte(address & 0x000000ff);
SendByte(0x00); //dummy byte
while (count)
{
*block = RcvByte();
block++;
count--;
}
SSEL0_OP(HIGH);
}
/***************************************************************************************************
** 函数名称: WrBytes
** 功能描述: 将数据连续写入存储器
** 输入参数: 写入的起始地址,写入数据指针,写入数据数
**
** 输出参数: 无
**
** 全局变量: 无
** 调用模块: SendByte();SSEL0_OP();RcvByte();
**--------------------------------------------------------------------------------------------------
** 作 者: Happy Xu
** 日 期: 2007/11/28
***************************************************************************************************/
U8 WrBytes (U32 address, U8 *block, U8 count)
{
U8 IsOver = 0;
if ((U16) (address & 0xff + count - 1) <= 0xff) // 页写个数在范围之内
{
WriteEn(HIGH);
SSEL0_OP(LOW);
SendByte(PAGE_PRO);
SendByte((address & 0x00ff0000)>>16);
SendByte((address & 0x0000ff00)>>8);
SendByte(address & 0x000000ff);
SendByte(*block);
count--;
block++;
address++;
while (count > 0 && (address & 0xff != 0))
{
SendByte(*block);
count--;
address++;
block++;
}
SSEL0_OP(HIGH);
do
{
SSEL0_OP(LOW);
SendByte(READ_STATUS_REG);
IsOver = RcvByte();
SSEL0_OP(HIGH);
}
while (IsOver & 0x01);
return(TRUE);
}
else
{
return(FALSE);
}
}
/***************************************************************************************************
** 函数名称: Read_ID()
** 功能描述: 无
**
** 输出参数: 无
**
** 全局变量: 无
** 调用模块: SendByte();SSEL0_OP();RcvByte();
**--------------------------------------------------------------------------------------------------
** 作 者: Happy Xu
** 日 期: 2007/11/28
***************************************************************************************************/
void ReadID(U8 *data)
{
U16 j;
SSEL0_OP(LOW);
for (j = 0;j < 50000; j++);
SendByte(READ_ID);
*data = RcvByte();
data++;
*data = RcvByte();
data++;
*data = RcvByte();
for (j = 0;j < 3000; j++);
SSEL0_OP(HIGH);
}
/****** End of File (M25P16.c) ******/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -