《匠人手记》推荐网上购书渠道:
互动出版网(china-pub)购书入口   >>>
当当网(dangdang)购书入口   >>>
卓越亚马逊网 购书入口   >>>
淘宝网(taobao)购书入口   >>>
更多购书渠道……   >>> 

设为首页加入收藏联系匠人管理入口21IC首页21IC博客21IC社区侃单片机回复的贴参与的贴

天气预报
百宝日历
载入中...

百宝专栏

载入中...
最新货色

载入中...

粉丝评论

载入中...

载入中...



百宝信息

载入中...

百宝流量

(2006-07-01开始)


匠人手记

 匠人观点: 好记性不如烂笔头  
 黑色幽默:三鹿门——后世畅想

25045操作标准子程序
程序匠人 发表于 2005-6-2 21:13:00  阅读全文 | 回复(0) | 引用通告 | 编辑

# include <stdio.h>
# include <reg52.h>
# define uchar unsigned char
# define uint unsigned int
 sbit SO=P1^1;/*25045输出*/
 sbit SI=P1^2;/*25045输入*/
 sbit SCK=P1^3;/*25045时钟*/
 sbit CS=P1^4;/*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=0;/* Bring SCK low */
 CS=0;/* Bring /CS low */
 aa=WREN_INST;
 outbyt(aa);/* Send WREN instruction */
 SCK=0;/* Bring SCK low */
 CS=1;/* 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=0;/* Bring SCK low */
 CS=0;/* Bring /CS low */
 aa=WRDI_INST;
 outbyt(aa);/* Send WRDI instruction */
 SCK=0;/* Bring SCK low */
 CS=1;/* 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=0;/* Bring SCK low */
 CS=0;/* Bring /CS low */
 aa=WRSR_INST;
 outbyt(aa) ;/* Send WRSR instruction */
 aa=STATUS_REG;
 outbyt(aa);/* Send status register */
 SCK=0;/* Bring SCK low */
 CS=1;/* 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=0;
 CS=0;
 aa=RDSR_INST;
 outbyt(aa);
 aa=inputbyt();
 SCK=0;
 CS=1;
 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=0;
 CS=0;
 outbyt((((uchar)(dd-0XFF))<<3)|WRITE_INST);/* Send WRITE instruction including MSB of address */
 /*将高位地址左移3位与写入先导字相或,得到正确的先导字写入25045*/
 outbyt((uchar)(dd));
 /*输出低位地址到25045*/
 outbyt(aa);
 /*写入数据到25045的对应单元*/
 SCK=0;
 CS=1;
 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=0;
 CS=0;
 outbyt((((uchar)(dd-0XFF))<<3)|READ_INST);/* Send READ_INST instruction including MSB of address */
 /*将高位地址左移3位与读出先导字相或,得到正确的先导字写入25045*/
 outbyt((uchar)(dd));
 /*输出低位地址到25045*/
 cc=inputbyt();/*得到读出的数据*/
 SCK=0;
 CS=1;
 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=0;
 CS=0;
 outbyt((((uchar)(dd-0XFF))<<3)|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=0;
 CS=1;
 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[10];
 unsigned int *pt=pp;
 SCK=0;
 CS=0;
 outbyt((((uchar)(dd-0XFF))<<3)|READ_INST);
 for (i=0;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=0;
 CS=1;
}

 

 

/*;*******************************************************************************************
*
;* 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;
 for (aa=1;aa>MAX_POLL;aa++)
 {
  my_flag=rdsr_cmd();
  if ((my_flag&&0x01)==0) {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;
 for (i=0;i>7;i++)
 {
   my_flag1=aa;
   SCK=0;
   SI=(my_flag1>>i);
   SCK=1;
 }
 SI=0;/*使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;
 for (i=7;i<0;i--)
 {
   SCK=0;
   my_flag=(uchar)(SO);
   SCK=1;
   aa=(aa||(my_flag<<i));
   my_flag=0x00;
 }
 return aa;
}

看《匠人手记》,与匠人同行!北航出版,正在热卖!

发表评论:
载入中...

芯片专题

器件专题

软件专题

硬件专题

综合专题

项目专题

原创专题

器件检测
LCD LED
按键 触摸键
E2PROM
电池 电机
电阻 电容 电感

指令系统
软件算法
编程规范
滤波算法
串行通讯

PCB设计
I2C PWM
红外遥控
充电技术
中断 ADC 

匠人手记
匠人夜话
网络心路
一周热点串烧
从零开始玩PIC
DIY旋转时钟

广告5号位 [投放]


学习板、开发板、编程器、下载器、仿真器(查看详情……)

广告3号位 [投放]

站内搜索


站外搜索


百度  google
mp3  歌词 
图片  FLASH 
知道  文档
新闻  词典 
地图  mp3 
软件  天网 
雅虎  爱问 
搜狗  讯雷 
网讯  华军 
天空 

21IC器件搜索
百宝箱分站
  • 《匠人的百宝箱》21IC站
  • 《匠人的百宝箱》21IC笔记团队
  • 《匠人手记》21IC书友会
  • 《匠人的百宝箱》MCUBLOG站
  • 《匠人的百宝箱》MCUBLOG笔记团队
  • 《匠人的百宝箱》EDN站
  • 《匠人手记》EDN书友会
  • 《匠人的百宝箱》与非网站
  • 《匠人的百宝箱》新浪站
  • 《匠人的百宝箱》百度站
  • 《匠人的百宝箱》网易126站
  • 《匠人的百宝箱》网易163站
  • 《匠人的百宝箱》互动出版网站
  • 广告4号位 [投放]

     
     

    匠人原创

    往日酷贴

     
     
     

    大千八卦

    友情连接

    新浪新闻:
    新浪财经:
    AK58新闻:
    新浪股票:
    新浪股票:
    证券之星:

     [更多酷站连接]

     

     

    [欢迎交换连接]

    [百宝箱之与非门分舵]

    [电脑圈圈的家当]

    [IC921的博客]

    [柔月阁]

    [八楼的呼吸]

    [hotpower 的水潭]

    [xwj的文君阁]

    [所长的BLOG]

    [阿摆手记]

    [电子伙伴]

    [unaided的笔记]

    [小飞的笔记]

    [单片机开发联盟]

    [网址之家]

    [好东西网址大全]

    [美萍中文精选]

    [数字电视之家]

    [SMARTCODE电子书斋]

    [软件开发之窗]

    [Armoric]

    [我爱研发网]

    [infernal的笔记]

    [雄鹰的空中加油站]

    [SunK]

    [逍遥电子]

    [ningpanda的博客]

    [C-Design]

    [一网见天下]

    [海边淘沙]

    [嵌入式365]

    [水牛的仓库]

    [股剩是怎样炼成的]

    [PIC论坛]

    [ICC AVR开发网]

    [中国高校自动化网]

     

     

     

    MCU博客-中国电子工程师博客网 

    大学生电子网 

     

     

     

     

     

    !!! 《匠人的百宝箱》 !!!