返回列表 发帖

c51写的X25045的读写源代码

  1. /***************************************************************
  2. * X25043/45 application ; Procedures
  3. * absolute one address access
  4. ***************************************************************
  5. WARNING: The function with '_' ahead ,user may not use it.as it
  6. used internal
  7. */
  8. //使用函数:write_status(status) 写状态,一般写0
  9. //clr_wchdog(void) 清看门狗
  10. //unsigned char read_byte(address) //读一个字节
  11. //void write_byte(address,Data) //写一个字节
  12.  

  13. #define ALONE_COMPILE
  14. #ifdef ALONE_COMPILE
  15. #include "INTRINS.H"
  16. #endif

  17. #ifndef nop2()
  18. #define nop2() _nop_();_nop_();_nop_()
  19. #endif

  20. #define WREN 0x06
  21. #define WRDI 0x04
  22. #define RDSR 0x05
  23. #define WRSR 0x01
  24. #define READ 0x03
  25. #define WRITE 0x02

  26. //#define PORT P1
  27. sfr PORT=0x90; //25045的4根io脚接在同一端口,本例为p1
  28. //请根据实际电路更改引脚定义
  29. #define _SI 0x80 //si接在p1.3,0x80=00001000b
  30. #define _SCK 0x40 //sck接在p1.2,0x80=00000100b
  31. #define _SO 0x20 //so接在p1.1
  32. #define _CS 0x10 //cs接在p1.0

  33. //----------------------------------------------------------------
  34. #ifndef dword
  35. #define dword unsigned long
  36. #define word unsigned int
  37. #define byte unsigned char
  38. typedef union{
  39. word w;
  40. byte bh;
  41. byte bl;
  42. }WordType;

  43. typedef union{
  44. dword dw;
  45. word w[2];
  46. byte b[4];
  47. }DwordType;

  48. #endif

  49. //----------------------------------------------------------------
  50. void _w_byte(Data)
  51. char Data;
  52. {
  53. char i;

  54. PORT &= (_SCK^0xff);
  55. for(i=0;i<8;i++)
  56. {
  57. nop2();nop2();/////////////
  58. if(Data & 0x80) PORT |= _SI;
  59. else PORT &= (_SI^0xff);
  60. nop2();nop2();/////////////
  61. PORT |=_SCK;
  62. nop2();nop2();/////////////
  63. Data=Data<<1;
  64. nop2();nop2();/////////////
  65. PORT &= (_SCK^0xff);
  66. nop2();nop2();/////////////
  67. }
  68. }
  69. //----------------------------------------------------------------
  70. char _r_byte(void)
  71. {
  72. char i;
  73. char result;

  74. result=0;
  75. for(i=0;i<8;i++)
  76. {
  77. nop2();nop2();/////////////
  78. PORT |=_SCK;
  79. result=result<<1;
  80. nop2();nop2();/////////////
  81. if((PORT & _SO) != 0)
  82. result |= 0x01;
  83. nop2();nop2();/////////////
  84. PORT &=(_SCK^0xff);
  85. nop2();nop2();/////////////
  86. }
  87. return(result);
  88. }
  89. //----------------------------------------------------------------
  90. void write_status(status)
  91. char status;
  92. {
  93. PORT &=(_CS^0xff);
  94. nop2();nop2();/////////////
  95. _w_byte(status);
  96. PORT |=_CS;
  97. nop2();nop2();/////////////
  98. return;
  99. }
  100. //----------------------------------------------------------------
  101. void clr_wchdog(void)
  102. {
  103. PORT &= (_CS^0xff);
  104. PORT |=_CS;
  105. }
  106. //----------------------------------------------------------------
  107. void wait_free(void)
  108. {
  109. unsigned int t;

  110. t=3000;
  111. while(--t);
  112. }
  113. //----------------------------------------------------------------
  114. void write_reg(_code)
  115. char _code;
  116. {
  117. write_status(WREN);
  118. PORT &= (_CS^0xff);
  119. nop2();nop2();/////////////
  120. _w_byte(WRSR);
  121. _w_byte(_code);
  122. nop2();nop2();/////////////
  123. PORT |=_CS;
  124. wait_free();
  125. }
  126. //----------------------------------------------------------------

  127. unsigned char read_byte(address)
  128. unsigned int address;
  129. {
  130. char result;

  131. PORT &=(_CS^0xff); // Chip select
  132. nop2();nop2();/////////////
  133. _w_byte((char)(address>255 ? (0x08|READ): READ));
  134. _w_byte((char)(address & 0x00ff));
  135. result=_r_byte();
  136. nop2();nop2();/////////////
  137. PORT |=_CS;
  138. // Chip unselect
  139. return(result);
  140. }
  141. //----------------------------------------------------------------
  142. void write_byte(address,Data)
  143. unsigned int address;
  144. char Data;
  145. {
  146. write_status(WREN);
  147. nop2();nop2();/////////////
  148. PORT &= (_CS^0xff);
  149. nop2();nop2();/////////////
  150. _w_byte((unsigned char)(address>255 ? (0x08|WRITE): WRITE));
  151. _w_byte((unsigned char)(address & 0x00ff));
  152. _w_byte(Data);
  153. nop2();nop2();/////////////
  154. PORT |=_CS;
  155. wait_free();
  156. return;
  157. }
  158. /*
  159. //----------------------------------------------------------------
  160. unsigned long read_data(format,address)
  161. unsigned char format;
  162. unsigned int address;
  163. {
  164. DwordType result;

  165. switch(format&0xdf)
  166. {
  167. case 'L':
  168. result.b[0]=read_byte(address);
  169. result.b[1]=read_byte(address+1);
  170. result.b[2]=read_byte(address+2);
  171. result.b[3]=read_byte(address+3);
  172. break;
  173. case 'D':
  174. result.b[2]=read_byte(address);
  175. result.b[3]=read_byte(address+1);
  176. break;
  177. case 'C':
  178. result.b[3]=read_byte(address);
  179. break;
  180. }
  181. return(result.dw);
  182. }
  183. //----------------------------------------------------------------
  184. void write_data(format,address,Data)
  185. unsigned char format;
  186. unsigned int address;
  187. DwordType data* Data;
  188. {
  189. switch(format&0xdf)
  190. {
  191. case 'L':
  192. write_byte(address , Data->b[0]);
  193. write_byte(address+1, Data->b[1]);
  194. write_byte(address+2, Data->b[2]);
  195. write_byte(address+3, Data->b[3]);
  196. break;
  197. case 'D':
  198. write_byte(address , Data->b[2]);
  199. write_byte(address+1, Data->b[3]);
  200. break;
  201. case 'C':
  202. write_byte(address , Data->b[3]);
  203. break;
  204. }

  205. }
  206. //----------------------------------------------------------------
  207. */
复制代码

返回列表

最新关注 关闭


关于论坛注册,最新修改,请网友们注意

由于最近大量垃圾信息出现在我们的论坛,为了营造一个良好的氛围,目前论坛只开发邀请注册,你可以点击以下链接自动邀请注册(如果有人使用了点击下一个试一试) ...


查看
珩源工控论坛热诚欢迎您联系我们进行合作!

Powered by Discuz! 7.2© 2001-2009 Comsenz Inc.

珩源工控论坛 ( 桂ICP备19004328号) |论坛统计|WAP| 客服中心-www.hymcu.com
  

GMT+8, 2024-11-24 18:45, Processed in 1.044886 second(s), 5 queries, Gzip enabled.