找回密码
 注册会员
搜索附件  

x25045CN.pdf

 

25045操作标准子程序集,25045中英文PDF,25045中英文资料:

  1. /*
  2. ;程 序 最 后 修 改 时 间  0-4-3 23:43
  3. ;软 件 标 题:25045操作标准子程序集41
  4. ;软 件 说 明:25045 I2C 串行EEPROM 驱动
  5. ;_________________________________________
  6. ;原作者: 庞波     
  7. ;程序修改人:
  8. ;版本号:
  9. ;_________________________________________
  10. */

  11. /*到现在为止所有的问题都已经解决,此版本已经较为完善*/
  12. # include <stdio.h>
  13. # include <reg52.h>
  14. # define uchar unsigned char
  15. # define uint unsigned int
  16. sbit SO=P1^1;/*25045输出*/
  17. sbit SI=P1^2;/*25045输入*/
  18. sbit SCK=P1^3;/*25045时钟*/
  19. sbit CS=P1^4;/*25045片选*/
  20. uchar code WREN_INST=0X06;
  21. /* Write enable latch instruction (WREN)*/
  22. uchar code WRDI_INST=0X04;
  23. /* Write disable latch instruction (WRDI)*/
  24. uchar code WRSR_INST=0X01;
  25. /* Write status register instruction (WRSR)*/
  26. uchar code RDSR_INST=0X05;
  27. /* Read status register instruction (RDSR)*/
  28. uchar code WRITE_INST=0X02;
  29. /* Write memory instruction (WRITE)*/
  30. /*写入25045的先导字,应当为0000A010,其中的A为写入25045的高位地址
  31. 将此WRITE_INST和写入高位地址相或后即为正确的写先导字*/
  32. uchar code READ_INST=0X03;
  33. /* Read memory instruction (READ)*/
  34. /*读出25045的先导字,应当为0000A011,其中的A为读出25045的高位地址
  35. 将此READ_INST和读出高位地址相或后即为正确的读先导字*/
  36. uint code BYTE_ADDR=0X55;
  37. /* Memory address for byte mode operations*/
  38. uchar code BYTE_DATA=0X11;
  39. /*Data byte for byte write operation*/
  40. uint  code PAGE_ADDR=0X1F;
  41. /* Memory address for page mode operations*/
  42. /*页面写入的其始地址*/
  43. uchar code PAGE_DATA1=0X22;
  44. /* 1st data byte for page write operation*/
  45. uchar code PAGE_DATA2=0X33;
  46. /* 2nd data byte for page write operation*/
  47. uchar code PAGE_DATA3=0X44;
  48. /* 3rd data byte for page write operation*/
  49. uchar code STATUS_REG=0X20;
  50. /* Status register,设置DOG时间设置为200毫秒,无写保护*/
  51. /*这是状态寄存器的值,他的意义在于第5,第4位为WDI1,WDI0代表DOG的时间,00为1.4秒,01为600毫秒,10为200毫秒,00为disabled
  52. 第3位和第2位为BL1,BL0,是写保护设置位,00为无保护,01为保护180-1FF,10为保护100-1FF,11为保护000-1FF.第1位为WEL,
  53. 当他为1时代表已经"写使能"设置了,现在可以写了,只读位.第0位为WIP,当他为1时代表正在进行写操作,是只读*/
  54. uchar code  MAX_POLL=0x99;
  55. /* Maximum number of polls*/
  56. /*最大写过程时间,确定25045的最大的写入过程的时间*/
  57. uchar code INIT_STATE=0x09;
  58. /* Initialization value for control ports*/
  59. uint code SLIC=0x30;
  60. /* Address location of SLIC*/
  61. void wren_cmd(void);/*写使能子程序*/
  62. void wrdi_cmd(void);/*写使能复位*/
  63. void wrsr_cmd(void);/*复位时间位和数据保护位写入状态寄存器*/
  64. uchar rdsr_cmd(void);/*读状态寄存器*/
  65. void byte_write(uchar aa,uint dd);/*字节写入,aa为写入的数据,dd为写入的地址*/
  66. uchar byte_read(uint dd);/*字节读出,dd为读出的地址,返回读出的数据*/
  67. void page_write(uchar aa1,uchar aa2,uchar aa3,uchar aa4,uint dd);/*页写入*/
  68. void sequ_read(void);/*连续读出*/
  69. void rst_wdog(void);/*DOG复位*/
  70. void outbyt(uchar aa);/*输出一个字节到25045中,不包括先导字等*/
  71. uchar inputbyt();/*由25045输入一个字节,不包括先导字等额外的东西*/
  72. void wip_poll(void);/*检查写入过程是否结束*/


  73. /*25045操作子程序集*/
  74. /*;*******************************************************************************************
  75. *
  76. ;* Name: WREN_CMD
  77. ;* Description: Set write enable latch
  78. ;* Function: This routine sends the command to enable writes to the EEPROM memory array or
  79. ;* status register
  80. ;* Calls: outbyt
  81. ;* Input: None
  82. ;* Outputs: None
  83. ;* Register Usage: A
  84. ;*******************************************************************************************
  85. */
  86. /*写使能子程序*/
  87. void wren_cmd(void)
  88. {
  89. uchar aa;
  90. SCK=0;/* Bring SCK low */
  91. CS=0;/* Bring /CS low */
  92. aa=WREN_INST;
  93. outbyt(aa);/* Send WREN instruction */
  94. SCK=0;/* Bring SCK low */
  95. CS=1;/* Bring /CS high */
  96. }
  97. /*;*******************************************************************************************
  98. *
  99. ;* Name: WRDI_CMD
  100. ;* Description: Reset write enable latch
  101. ;* Function: This routine sends the command to disable writes to the EEPROM memory array or
  102. ;* status register
  103. ;* Calls: outbyt
  104. ;* Input: None
  105. ;* Outputs: None
  106. ;* Register Usage: A
  107. ;*******************************************************************************************
  108. */
  109. /*写使能复位子程序*/
  110. void wrdi_cmd(void)
  111. {
  112. uchar aa;
  113. SCK=0;/* Bring SCK low */
  114. CS=0;/* Bring /CS low */
  115. aa=WRDI_INST;
  116. outbyt(aa);/* Send WRDI instruction */
  117. SCK=0;/* Bring SCK low */
  118. CS=1;/* Bring /CS high */
  119. }

  120. /*;*******************************************************************************************
  121. *
  122. ;* Name: WRSR_CMD
  123. ;* Description: Write Status Register
  124. ;* Function: This routine sends the command to write the WD0, WD1, BP0 and BP0 EEPROM
  125. ;* bits in the status register
  126. ;* Calls: outbyt, wip_poll
  127. ;* Input: None
  128. ;* Outputs: None
  129. ;* Register Usage: A
  130. ;*******************************************************************************************
  131. */
  132. /*写状态寄存器子程序*/
  133. void wrsr_cmd(void)
  134. {
  135. uchar aa;
  136. SCK=0;/* Bring SCK low */
  137. CS=0;/* Bring /CS low */
  138. aa=WRSR_INST;
  139. outbyt(aa) ;/* Send WRSR instruction */
  140. aa=STATUS_REG;
  141. outbyt(aa);/* Send status register */
  142. SCK=0;/* Bring SCK low */
  143. CS=1;/* Bring /CS high */
  144. wip_poll();/* Poll for completion of write cycle */
  145. }


  146. /*;*******************************************************************************************
  147. *
  148. ;* Name: RDSR_CMD
  149. ;* Description: Read Status Register
  150. ;* Function: This routine sends the command to read the status register
  151. ;* Calls: outbyt, inputbyt
  152. ;* Input: None
  153. ;* Outputs: A = status registerXicor Application Note AN21
  154. ;* Register Usage: A
  155. ;*******************************************************************************************
  156. */
  157. /*读状态寄存器,读出的数据放入到aa中*/
  158. uchar rdsr_cmd (void)
  159. {
  160. uchar aa;
  161. SCK=0;
  162. CS=0;
  163. aa=RDSR_INST;
  164. outbyt(aa);
  165. aa=inputbyt();
  166. SCK=0;
  167. CS=1;
  168. return aa;
  169. }



  170. /*;*******************************************************************************************
  171. *
  172. ;* Name: BYTE_WRITE
  173. ;* Description: Single Byte Write
  174. ;* Function: This routine sends the command to write a single byte to the EEPROM memory
  175. array
  176. ;* Calls: outbyt, wip_poll
  177. ;* Input: None
  178. ;* Outputs: None
  179. ;* Register Usage: A, B
  180. ;*******************************************************************************************
  181. */
  182. /*字节写入,aa为写入的数据,dd为写入的地址,对于25045而言为000-1FF*/
  183. void byte_write(aa,dd)
  184. uchar aa;
  185. uint dd;
  186. {
  187. SCK=0;
  188. CS=0;
  189. outbyt((((uchar)(dd-0XFF))<<3)|WRITE_INST);/* Send WRITE instruction including MSB of address */
  190. /*将高位地址左移3位与写入先导字相或,得到正确的先导字写入25045*/
  191. outbyt((uchar)(dd));
  192. /*输出低位地址到25045*/
  193. outbyt(aa);
  194. /*写入数据到25045的对应单元*/
  195. SCK=0;
  196. CS=1;
  197. wip_poll();
  198. /*检测是否写完*/
  199. }

  200. /*;*******************************************************************************************
  201. *
  202. ;* Name: BYTE_READ
  203. ;* Description: Single Byte Read
  204. ;* Function: This routine sends the command to read a single byte from the EEPROM memory
  205. array
  206. ;* Calls: outbyt, inputbyt
  207. ;* Input: None
  208. ;* Outputs: A = read byte
  209. ;* Register Usage: A, BXicor Application Note AN21
  210. ;*******************************************************************************************
  211. */
  212. /*字节读出,其中dd为读出的地址,返回的值为读出的数据*/
  213. uchar byte_read(dd)
  214. uint dd;
  215. {
  216. uchar cc;
  217. SCK=0;
  218. CS=0;
  219. outbyt((((uchar)(dd-0XFF))<<3)|READ_INST);/* Send READ_INST instruction including MSB of address */
  220. /*将高位地址左移3位与读出先导字相或,得到正确的先导字写入25045*/
  221. outbyt((uchar)(dd));
  222. /*输出低位地址到25045*/
  223. cc=inputbyt();/*得到读出的数据*/
  224. SCK=0;
  225. CS=1;
  226. return cc;
  227. }


  228. /*;*******************************************************************************************
  229. *
  230. ;* Name: PAGE_WRITE
  231. ;* Description: Page Write
  232. ;* Function: This routine sends the command to write three consecutive bytes to the EEPROM
  233. ;* memory array using page mode
  234. ;* Calls: outbyt, wip_poll
  235. ;* Input: None
  236. ;* Outputs: None
  237. ;* Register Usage: A, B
  238. ;*******************************************************************************************
  239. */
  240. /*页面写入,其中aa1,aa2,aa3,aa4为需要写入的4个数据(最大也就只能一次写入4个字,dd为写入的首地址*/
  241. void page_write(aa1,aa2,aa3,aa4,dd)
  242. uchar aa1,aa2,aa3,aa4;
  243. uint dd;
  244. {
  245. SCK=0;
  246. CS=0;
  247. outbyt((((uchar)(dd-0XFF))<<3)|WRITE_INST);/* Send WRITE instruction including MSB of address */
  248. /*将高位地址左移3位与写入先导字相或,得到正确的先导字写入25045*/
  249. outbyt((uchar)(dd));
  250. /*写入低位地址到25045*/
  251. outbyt(aa1);
  252. /*写入数据1到25045的对应单元*/
  253. outbyt(aa2);
  254. /*写入数据2到25045的对应单元*/
  255. outbyt(aa3);
  256. /*写入数据3到25045的对应单元*/
  257. outbyt(aa4);
  258. /*写入数据4到25045的对应单元*/
  259. SCK=0;
  260. CS=1;
  261. wip_poll();
  262. }


  263. /*;*******************************************************************************************
  264. *
  265. ;* Name: SEQU_READ
  266. ;* Description: Sequential Read
  267. ;* Function: This routine sends the command to read three consecutive bytes from the EEPROM
  268. ;* memory array using sequential mode
  269. ;* Calls: outbyt, inputbyt
  270. ;* Input: None
  271. ;* Outputs: A = last byte read
  272. ;* Register Usage: A, B
  273. ;*******************************************************************************************
  274. */
  275. /*连续读出,由于函数的返回值只能为1个,对于连续读出的数据只能使用指针作为函数的返回值才能做到返回一系列的数组*/
  276. /*sequ_read:*/
  277. unsigned int *page_read(n,dd)
  278. uchar n;/*n是希望读出的数据的个数,n<=11*/
  279. unsigned int dd;/*dd是读出数据的首地址*/
  280. {
  281. uchar i;
  282. uchar pp[10];
  283. unsigned int *pt=pp;
  284. SCK=0;
  285. CS=0;
  286. outbyt((((uchar)(dd-0XFF))<<3)|READ_INST);
  287. for (i=0;i<n;i++)
  288. {
  289.    pp[i]=inputbyt();
  290. }
  291. return (pt);
  292. }
  293. /*调用的方法如下*/
  294. /*unsigned int *p;*/
  295. /*p=page_read(4,100);*/
  296. /*a=*(p)*/  
  297. /*b=*(p+1)*/
  298. /*c=*(p+2)*/
  299. /*d=*(p+3)*/
  300. /*abcd中存放25045中由100地址开始的4个数据*/
  301. /* Send WRITE */
  302. /*mov DPTR, #PAGE_ADDR ; Set address of 1st byte to be read
  303. clr sck ; Bring SCK low
  304. clr cs ; Bring /CS low
  305. mov A, #READ_INST
  306. mov B, DPH
  307. mov C, B.0
  308. mov ACC.3, C
  309. lcall outbyt ; Send READ instruction with MSB of address
  310. mov A, DPL
  311. lcall outbyt ; Send low order address byte
  312. lcall inputbyt ; Read 1st data byte
  313. lcall inputbyt ; Read 2nd data byte
  314. lcall inputbyt ; Read 3rd data byte
  315. clr sck ; Bring SCK low
  316. setb cs ; Bring /CS high
  317. ret*/


  318. /*;*******************************************************************************************
  319. *
  320. ;* Name: RST_WDOG
  321. ;* Description: Reset Watchdog Timer
  322. ;* Function: This routine resets the watchdog timer without sending a command
  323. ;* Calls: None
  324. ;* Input: None
  325. ;* Outputs: None
  326. ;* Register Usage: None
  327. ;*******************************************************************************************
  328. */
  329. /*复位DOG*/
  330. void rst_wdog (void)
  331. {
  332. CS=0;
  333. CS=1;
  334. }


  335. /*;*******************************************************************************************
  336. *
  337. ;* Name: WIP_POLL
  338. ;* Description: Write-In-Progress Polling
  339. ;* Function: This routine polls for completion of a nonvolatile write cycle by examining the
  340. ;* WIP bit of the status register
  341. ;* Calls: rdsr_cmdXicor Application Note AN21
  342. ;* Input: None
  343. ;* Outputs: None
  344. ;* Register Usage: R1, A
  345. ;*******************************************************************************************
  346. */
  347. /*检测写入的过程是否结束*/
  348. void wip_poll(void)
  349. {
  350. uchar aa;
  351. uchar idata my_flag;
  352. for (aa=1;aa>MAX_POLL;aa++)
  353. {
  354.   my_flag=rdsr_cmd();
  355.   if ((my_flag&&0x01)==0) {aa=MAX_POLL;}/*判断是否WIP=0,即判断是否写入过程已经结束,若结束就跳出,否则继续等待直到达到最大记数值*/
  356. }
  357. }


  358. /*;*******************************************************************************************
  359. *
  360. ;* Name: OUTBYT
  361. ;* Description: Sends byte to EEPROM
  362. ;* Function: This routine shifts out a byte, starting with the MSB, to the EEPROM
  363. ;* Calls: None
  364. ;* Input: A = byte to be sent
  365. ;* Outputs: None
  366. ;* Register Usage: R0, A
  367. ;*******************************************************************************************
  368. */
  369. /*输出一个数据到25045,此数据可能为地址,先导字,写入的数据等*/
  370. void outbyt(aa)
  371. uchar aa;
  372. {
  373. uchar my_flag1,i;
  374. for (i=0;i>7;i++)
  375. {
  376.    my_flag1=aa;
  377.    SCK=0;
  378.    SI=(my_flag1>>i);
  379.    SCK=1;
  380. }
  381. SI=0;/*使SI处于确定的状态*/
  382. }



  383. /*;*******************************************************************************************
  384. *
  385. ;* Name: INPUTBYT
  386. ;* Description: Recieves byte from EEPROM
  387. ;* Function: This routine recieves a byte, MSB first, from the EEPROM
  388. ;* Calls: None
  389. ;* Input: None
  390. ;* Outputs: A = recieved byte
  391. ;* Register Usage: R0, A
  392. ;*******************************************************************************************
  393. */
  394. /*得到一个数据,此数据可能为状态寄存器数据,读出的单元数据等*/
  395. uchar inputbyt(void)
  396. {
  397. uchar aa,my_flag;
  398. char i;
  399. for (i=7;i<0;i--)
  400. {
  401.    SCK=0;
  402.    my_flag=(uchar)(SO);
  403.    SCK=1;
  404.    aa=(aa||(my_flag<<i));
  405.    my_flag=0x00;
  406. }
  407. return aa;
  408. }

复制代码



QQ|手机版|MCU资讯论坛 ( 京ICP备18035221号-2 )|网站地图

GMT+8, 2024-12-23 18:46 , Processed in 0.038237 second(s), 8 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

返回顶部