独闷闷网

 找回密码
 立即注册
搜索
查看: 2279|回复: 0
打印 上一主题 下一主题
收起左侧

[原创] 把已经调试好的flash存储芯片W25X40的C语言驱动代码分享给大家。

[复制链接]
跳转到指定楼层
楼主
发表于 2015-6-15 22:53:38 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
本帖最后由 jianhong_wu 于 2015-7-22 10:19 编辑

★坚鸿-深圳:
今天在一个项目上要用到flash存储芯片W25X40,现在把已经调试好的C语言驱动代码分享给大家参考:


  1. unsigned char Read_Status_Register();
  2. unsigned char Get_Byte();
  3. void Send_Byte(unsigned char dd);
  4. unsigned char Read25(unsigned long byte_address);  //从指定地址中读取一个字节数据
  5. void Byte_Program(unsigned long byte_address, unsigned char tempdata) ;//写入一个字节数据到指定存储地址里。
  6. void Chip_Erase();  //整片擦除。
  7. void busy_check();





  8. unsigned char Read_Status_Register()
  9. {
  10. unsigned char outdata= 0;
  11. TRISB5=0;

  12. CE=0;
  13. asm("nop");asm("nop");
  14. Send_Byte(0x05);
  15. outdata=Get_Byte();
  16. asm("nop");asm("nop");
  17. CE=1;
  18. asm("nop");asm("nop");

  19. return outdata;


  20. }





  21. unsigned char Get_Byte()
  22. {
  23.         unsigned char outdata,tempdata;
  24.    
  25.         outdata=0;
  26.    TRISB7=0;TRISB6=1;TRISB5=0;TRISB4=0;
  27.    RB6=1;
  28.         asm("nop");asm("nop");
  29.    CE=0;
  30.         for(tempdata=0;tempdata<8;tempdata++)
  31.         { CLRWDT();
  32.                 outdata<<=1;
  33.             SCK=1;        
  34.                 asm("nop");asm("nop");
  35.                 if(RB6==1)outdata++;
  36.               SCK=0;
  37.                 asm("nop");asm("nop");



  38.         }



  39.     return(outdata);
  40.    
  41. }







  42. void Send_Byte(unsigned char dd)
  43. {

  44.         unsigned char tempdata=0;

  45.    TRISB7=0;TRISB6=1;TRISB5=0;TRISB4=0;
  46.    CE=0;
  47.         for(tempdata=0;tempdata<8;tempdata++)
  48.         { CLRWDT();
  49.                 if(dd>=0x80)SI=1;
  50.                 else SI=0;
  51.                 dd<<=1;
  52.                 SCK=1;
  53.                 asm("nop");asm("nop");
  54.                 SCK=0;
  55.                 asm("nop");asm("nop");
  56.         }



  57. }


  58. unsigned char Read25(unsigned long byte_address)
  59. {
  60.     unsigned char tempdata = 0;
  61.     unsigned long temp_address;
  62.     DS1302_CE=0; //CE引脚为低,数据传送中止

  63.     TRISB5=0;
  64.     CE=0;

  65.     Send_Byte(0x03);

  66.     temp_address=byte_address;
  67.     Send_Byte((temp_address&0x00ff0000)>>16); /* send 3 address     bytes */

  68.     temp_address=byte_address;
  69.     Send_Byte((temp_address&0x0000ff00)>>8);

  70.     temp_address=byte_address;
  71.     Send_Byte((temp_address&0x000000ff));
  72.     tempdata= Get_Byte();

  73.     CE=1;
  74.     return tempdata;


  75. }



  76. void Byte_Program(unsigned long byte_address, unsigned char tempdata)
  77. {
  78.     unsigned long temp_address;
  79.     DS1302_CE=0; //CE引脚为低,数据传送中止

  80.     TRISB5=0;


  81.     CE=0;
  82.     Send_Byte(0x06);
  83.     CE=1;
  84.     busy_check();

  85.     CE=0;

  86.     Send_Byte(0x02);
  87.     temp_address=byte_address;
  88.     Send_Byte((temp_address&0x00ff0000)>>16); /* send 3 address     bytes */

  89.     temp_address=byte_address;
  90.     Send_Byte((temp_address&0x0000ff00)>>8);

  91.     temp_address=byte_address;
  92.     Send_Byte((temp_address&0x000000ff));
  93.     Send_Byte(tempdata);



  94.     CE=1;

  95.     Delay1(1);


  96. }


  97. void Chip_Erase()
  98. {

  99.     DS1302_CE=0; //CE引脚为低,数据传送中止
  100.     TRISB5=0;


  101.     CE=0;
  102.     Send_Byte(0x06);
  103.     CE=1;

  104.     CE=0;
  105.     Send_Byte(0x60);
  106.     CE=1;
  107.     busy_check();
  108.     Delay1(300);  



  109.      check25=Read25(520190);
  110.      if(check25!=0x55)
  111.      {
  112.          Byte_Program(520190,0x55);
  113.      }



  114. }




  115. void busy_check()
  116. {
  117. unsigned char data_temp;
  118.     TRISB5=0;
  119. while(data_temp== 0x03)
  120.   {
  121.      CLRWDT();
  122.      data_temp=Read_Status_Register();
  123.   }

  124. }
复制代码

上述代码用到的Delay1延时函数如下:
  1. void Delay1(unsigned int MS)
  2. {
  3.      unsigned char us,usn;
  4.      while(MS!=0)            //for 12M
  5.            {
  6.                CLRWDT();
  7.         usn = 2;
  8.                  while(usn!=0)
  9.                        {       CLRWDT();
  10.                              us=0xf5;
  11.                              while (us!=0){us--;};
  12.                              usn--;
  13.                        }
  14.                  MS--;
  15.            }
  16. }
复制代码



乐于分享,勇于质疑!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|独闷闷网 ( 粤ICP备12007667号-2 )

GMT+8, 2024-4-25 18:47 , Processed in 0.158991 second(s), 17 queries .

快速回复 返回顶部 返回列表