|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2011-11-22 08:20:03
|
显示全部楼层
代码是这样的 也是在论坛找的稍微改了一下
#define SPI_SCK_DDR DDRB
#define SPI_SCK_PORT PORTB
#define SPI_SCK_PIN 7
#define SPI_MISO_DDR DDRB
#define SPI_MISO_PORT PINB
#define SPI_MISO_PIN 6
#define SPI_MOSI_DDR DDRB
#define SPI_MOSI_PORT PORTB
#define SPI_MOSI_PIN 5
#define SPI_CS_DDR DDRB
#define SPI_CS_PORT PORTB
#define SPI_CS_PIN 4
#define SPI_SCK_HIGH() SPI_SCK_PORT |= _BV(SPI_SCK_PIN) //SCK置高电平
#define SPI_SCK_LOW() SPI_SCK_PORT &= ~_BV(SPI_SCK_PIN) //SCK置低电平
#define SPI_MOSI_HIGH() SPI_MOSI_PORT |= _BV(SPI_MOSI_PIN) //MOSI置高电平
#define SPI_MOSI_LOW() SPI_MOSI_PORT &= ~_BV(SPI_MOSI_PIN) //MOSI置低电平
#define SPI_CS_HIGH() SPI_CS_PORT |= _BV(SPI_CS_PIN) //CS置高电平
#define SPI_CS_LOW() SPI_CS_PORT &= ~_BV(SPI_CS_PIN) //CS置低电平
#define RDSR 0x05
#define READ 0x03
#define RDID 0x9f
#define WREN 0x06
#define FASTREAD 0x0b
void InitMx25l(void)
{
SPI_SCK_DDR |= _BV(SPI_SCK_PIN);
SPI_MOSI_DDR |= _BV(SPI_MOSI_PIN);
SPI_CS_DDR |= _BV(SPI_CS_PIN);
SPI_MISO_DDR &= ~_BV(SPI_MISO_PIN);
SPI_CS_HIGH();
}
/*********************************************************************************
*函数原型:SPI_WriteByte(BYTE dat)
*函数功能: SCK上升沿向芯片输入数据有效
*函数参数:
*返回值:
*说明:入口是,sck为高,出口是sck仍为高
*********************************************************************************/
void SPI_WriteByte(BYTE dat)
{
BYTE count;
SPI_SCK_HIGH();
for(count=0;count<8;count++)
{
SPI_SCK_LOW();
if (dat&0x80)
{
SPI_MOSI_HIGH();
}
else
{
SPI_MOSI_LOW();
}
dat = dat<<1;
SPI_SCK_HIGH();
}
}
/*********************************************************************************
*函数原型:BYTE SPI_ReadByte()
*函数功能:SCK下降沿从芯片读出数据有效
*函数参数:
*返回值:
*说明:入口是,sck为高,出口是sck仍为高
*********************************************************************************/
BYTE SPI_ReadByte()
{
BYTE count,dat;
dat=0;
SPI_SCK_HIGH();
for(count=0;count<8;count++)
{
SPI_SCK_LOW();
dat <<= 1;
if(PINB&_BV(6))
{
dat++;
}
SPI_SCK_HIGH();
}
return(dat);
}
/*********************************************************************************
*函数原型:void Flash_WaitBusy(void)
*函数功能:
*函数参数:
*返回值:
*说明:
*********************************************************************************/
void Flash_WaitBusy(void)
{
BYTE state_reg = 0x00;
SPI_CS_LOW();
SPI_WriteByte(RDSR); //read status register
delay_us(10);
do
{
state_reg = SPI_ReadByte();
delay_us(10);
}
while(state_reg&0x01); // ==0 not in write operation--no busy
SPI_CS_HIGH();
}
/*********************************************************************************
*函数原型:BYTE Flash_ReadID(void)
*函数功能:
*函数参数:
*返回值:
*说明:
*********************************************************************************/
BYTE Flash_ReadID(void) //ID读取
{
BYTE ChipID;
///Flash_WaitBusy();
SPI_CS_LOW();
delay_us(10);
SPI_WriteByte(RDID); //ID = C2 //read identification
//delay_us(10);
ChipID = SPI_ReadByte();
SPI_ReadByte();
SPI_ReadByte();
//str[1] = SPI_ReadByte(); //mem type id 20h
//str[2] = SPI_ReadByte(); //mx25l8005 id 14h
SPI_CS_HIGH();
//ChipID =0xc2;
//Flash_WaitBusy();
return ChipID;
} |
|