基于C51的M25P16驱动程序

基于C51的M25P16驱动程序

已有 203 次阅读2015-5-30 11:06 | M25P16C51

转载至:http://bbs.elecfans.com/blog-423957-366079.html


//-------------------------------------------------------------------------------

#ifndef _FLASH_HAL_H
#define _FLASH_HAL_H

#include <reg51.h>
#include "public.h"

sbit FLASH_SPI_CS   = P3^3;
sbit FLASH_SPI_CLK  = P3^4;
sbit FLASH_SPI_MOSI = P3^5;
sbit FLASH_SPI_MISO = P3^6;

void flash_init(void);
uint8 flash_read_byte(uint8 addr);
void flash_write_byte(uint8 addr,uint8 wd);

#endif

//-----------------------------------------------------------------------

#include <reg51.h>
#include "flash_hal.h"


uint8 spi_read_byte(void)
{
 uint8 i,rxdata;
 rxdata = 0x00;

 for (i = 0;i < 8;i++)
 {
  rxdata = rxdata<<1;
  FLASH_SPI_CLK = 1;
  if (FLASH_SPI_MISO)
        { 
   rxdata |= 0x01;
  }
  else
  {
   rxdata &= ~0x01;
  }

  FLASH_SPI_CLK = 0; 
  }
  return rxdata;
}
void spi_write_byte(uint8 txdata)
{
 uint8 i;

 for (i = 0;i < 8;i++)
 {
  if (txdata&0x80) 
  {
   FLASH_SPI_MOSI = 1;
  }
  else
  {
   FLASH_SPI_MOSI = 0;
  }
  FLASH_SPI_CLK = 1;
  txdata = txdata<<1;
  FLASH_SPI_CLK = 0; 
 }
}
void flash_init(void)
{
 uint8 read_state = 0x00;
 
 FLASH_SPI_CS = 0;
 spi_write_byte(0x06);
 FLASH_SPI_CS = 1;
 
 while(!(read_state & 0x02))
 {
  FLASH_SPI_CS = 0;
  spi_write_byte(0x05);
  read_state = spi_read_byte();
  FLASH_SPI_CS = 1;
 }
 read_state = 0x00;
 
 FLASH_SPI_CS = 0;
 spi_write_byte(0x01);
 spi_write_byte(0x00);
 FLASH_SPI_CS = 1;
 
 FLASH_SPI_CS = 0;
 spi_write_byte(0x06);
 FLASH_SPI_CS = 1;
 
 while(!(read_state & 0x02))
 {
  FLASH_SPI_CS = 0;
  spi_write_byte(0x05);
  read_state = spi_read_byte();
  FLASH_SPI_CS = 1;
 }
 read_state = 0x01;
 
 FLASH_SPI_CS = 0;
 spi_write_byte(0xD8);
 spi_write_byte(0x01);
 spi_write_byte(0x00);
 spi_write_byte(0x00);
 FLASH_SPI_CS = 1;
 
 while((read_state & 0x01))
 {
  FLASH_SPI_CS = 0;
  spi_write_byte(0x05);
  read_state = spi_read_byte();
  FLASH_SPI_CS = 1;
 }
 read_state = 0x00;

void flash_write_byte(uint8 addr,uint8 wd)
{
 uint8 read_state = 0x00;
 
 FLASH_SPI_CS = 0;
 spi_write_byte(0x06);
 FLASH_SPI_CS = 1;
 
 
 while(!(read_state & 0x02))
 {
  FLASH_SPI_CS = 0;
  spi_write_byte(0x05);
  read_state = spi_read_byte();
  FLASH_SPI_CS = 1;
 }
 
 
 FLASH_SPI_CS = 0;
 spi_write_byte(0x02);
 spi_write_byte(0x01);
 spi_write_byte(0x00);
 spi_write_byte(addr);
 spi_write_byte(wd);
 FLASH_SPI_CS = 1;
}
uint8 flash_read_byte(uint8 addr)
{
 uint8 rx_data;
 
 FLASH_SPI_CS = 0;
 spi_write_byte(0x03);
 spi_write_byte(0x01);
 spi_write_byte(0x00);
 spi_write_byte(addr);
 rx_data = spi_read_byte();
 FLASH_SPI_CS = 1;
 
 return rx_data;
}

//----------------------------------------------------------------

#include <reg51.h>
#include "flash_hal.h"

void main(void)
{
 uint8 i = 0x00;
 
 flash_init();
 
 while ( 1 )
 {
  flash_write_byte(0x03,0x30);
  i = flash_read_byte(0x03);
  
  i = 0x01;
 }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值