c语言定义eeprom变量,spi eeprom读写c语言实现(spi eeprom读写c语言实现).doc

spi eeprom读写c语言实现(spi eeprom读写c语言实现)

spi eeprom读写c语言实现(spi eeprom读写c语言实现)

/ / spi接口eeprom的驱动.

# include "2407.h"

# include "eeprom.h"

unsigned int sleep _ data spirddata, spistatus, spibsyloop; / / spibsyloop决定忙状态的循环判断

unsigned int spirdflag, spiwrflag, spiwrenflag, spibsyflag, spiendflag, spitxcnt, spirxcnt;

/ / spi要靠自己判别发送和接受 spi发送的字节数 接受的字节数

/ / 设置了写使能、写数据、读数据、忙状态等标志

/ / 根据标志决定中断中的操作, 自己等计划中的中断

unsigned int spierrflag; / / 忙标志长期有效后的报错标志

unsigned int spitxdata [1000], spirxdata [1000]; / / 建立的写数组和读数组

unsigned int spitxnum, spirxnum; / / 批量读写计数标志

unsigned int * spiwrptr; / / 写数据指针指向将写的源数组

unsigned int * spirdptr; / / 读数据指针指向将读目的数组

unsigned int e2promerr;

spiinit void (void)

{

mcrb | = 0x1c; / / spi相应i / o口基本功能, ste使用io口控制

mcrb & = 0xffdf;

pcdatdir | = 0x2020; / / ste设置为输出且置位

spiccr = 0x07; / / 8位传送方式

spictl = 0x1f; / / 使能覆盖中断

spibrr = 0x004f; / / 波特率500k

spipri = 0x10; / / 高优先级中断spi、不受仿真影响; 0x40低优先级 \ 一旦进入仿真spi停止

spiccr = 0x87; / / 处于准备状态

spirdflag = 0;

spiwrflag = 0;

spiwrenflag = 0;

spibsyflag = 0;

spierrflag = 0;

spiendflag = 0;

spitxcnt = 0;

spirxcnt = 0;

e2promerr = 0;

}

void spiwordwr (unsigned int wraddress, unsigned int wrdata)

{/ / 单字的写入 写入地址 写入数据

/ / 无论读写都是把spi当成2048个16位存储空间, 因为不能跨16个字 (32字节) 写

/ / spi内部是4096 * 8的空间, 32字节一页, 最多一页的写入, 不能跨页写, 可以跨页读

unsigned int i = 0;

unsigned int j;

spibsy ();

spiwrenflag = 1;

pcdatdir | = 0x2020; / / 置iopc5为输出高

pcdatdir & = 0xffdf; / / 置ste为输出低

/ / for (j = 0, j < 5; j + +); / / 延时500ns

/ / a (spiendflag = = 0);

spitxbuf = 0x0600;

while (spiendflag = = 0); / / & & (spierrflag = = 0)); / / 写使能

spiendflag = 0;

/ / for (j = 0, j < 5; j + +); / / 延时500ns

pcdatdir | = 0x2020; / / 置iopc5为输出高

for (j = 0, j < 5; j + +); / / 延时500ns

pcdatdir & = 0xffdf; / / 置ste为输出低

/ / for (j = 0, j < 1; j + +); / / 延时500ns

spiwrflag = 1;

spitxcnt = 5;

spitxbuf = 0x0200;

while (spiendflag = = 0); / / & & (spierrflag = = 0));

spiendflag = 0;

spitxbuf = (wraddress < < 1) & 0xff00;

/ / (((spists & 0x20) = = 0x20);

while (spiendflag = = 0); / / & & (spierrflag = = 0));

spiendflag = 0;

s

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值