初级会员
- 积分
- 159
- 金钱
- 159
- 注册时间
- 2016-11-27
- 在线时间
- 15 小时
|
#include <msp430.h>
char readdata=0;
char busy_data=0;
void write_byte(char data_byte);
void send_command(char address1,char address2, char address3,char data);// command and address and data
char recive_data(char read_address1, char read_address2,char read_address3);
void delay();
void erase(char command,char erase_address1,char erase_address2,char erase_address3);
void read_busy(void);
int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
P2DIR|=BIT5+BIT4+BIT0; //flsah sck hold cs out
P4DIR|=BIT2+BIT3; //flash wp si output
P4REN|=BIT1; // so pull up
P4OUT|=BIT1; //flash so pullup selected p4.1
P4OUT|=BIT2; //wp=1
P6DIR=BIT4;
P6OUT=BIT4; //led
P2OUT|=BIT0 ; //CS=1;
P2OUT|=BIT5; //sck=1;
//P2OUT|=BIT4; // hold =1
_NOP();
_NOP();
P6OUT^=BIT4;
erase(0x20,0x00,0x00,0x00);
read_busy();
send_command(0x00,0x01,0x01,0x00); //xie,mingling
read_busy();
readdata=recive_data(0x00,0x01,0x01);
while(1)
{
// send_command(0x00,0x00,0x00,0x0f); //xie,mingling
// if(readdata==0x00)
P6OUT^=BIT4;
delay();
// readdata=recive_data(0x00,0x01,0x01);
}
}
void write_byte(char data_byte)
{
char num=0;
for(num=0;num<=7;num++)
{
if(data_byte&0x80)
P4OUT|=BIT3 ; //si=1;
else
P4OUT&=~BIT3; //si=0;
P2OUT&=~BIT5; //sck=0;
_NOP();
_NOP();
_NOP();
_NOP();
data_byte=data_byte<<1; //数据从高位传输
P2OUT|=BIT5; //sck=1;
}
}
void send_command(char address1, char address2 ,char address3 ,char data)
{
P2OUT|=BIT0 ; //CS=1;
P2OUT|=BIT5; //sck=1;
_NOP();
P2OUT&=~BIT0 ; //CS=0;
write_byte(0x06); // write command
write_byte(address1);
write_byte(address2);
write_byte(address3);
write_byte(data);
P2OUT|=BIT0 ; //CS=1;
_NOP();
_NOP();
_NOP();
}
char recive_data(char read_address1,char read_address2, char read_address3)
{
char read_data=0;
char num=0;
P2OUT|=BIT0 ; //CS=1;
P2OUT|=BIT5; //sck=1;
_NOP();
P2OUT&=~BIT0 ; //CS=0;
write_byte(0x03);
write_byte(read_address1);
write_byte(read_address2);
write_byte(read_address3);
for(num=0;num<8;num++)
{
P2OUT|=BIT5; //sck=1;
read_data<<=1;
_NOP();
_NOP();
_NOP();
_NOP();
P2OUT&=~BIT5; //sck=0;
if(P4IN&0x02)
read_data|=0x01;
else
read_data&=0xfe;
_NOP();
_NOP();
}
P2OUT|=BIT0 ; //CS=1;
_NOP();
_NOP();
_NOP();
return read_data;
}
void erase(char command,char erase_address1,char erase_address2,char erase_address3)
{
P2OUT|=BIT0 ; //CS=1;
P2OUT|=BIT5; //sck=1;
_NOP();
P2OUT&=~BIT0 ; //CS=0;
write_byte(0x06);
write_byte(command);
write_byte(erase_address1);
write_byte(erase_address2);
write_byte(erase_address3);
P2OUT|=BIT0 ; //CS=1;
_NOP();
_NOP();
_NOP();
}
void delay()
{
unsigned int i=0;
unsigned int j=0;
for(i=0;i<=500;i++)
for(j=0;j<=250;j++)
;
}
void read_busy()
{
char read_data=0;
char num=0;
P2OUT|=BIT0 ; //CS=1;
P2OUT|=BIT5; //sck=1;
_NOP();
P2OUT&=~BIT0 ; //CS=0;
write_byte(0x05); // read the SR1
do{
for(num=0;num<8;num++)
{
P2OUT|=BIT5; //sck=1;
_NOP();
_NOP();
_NOP();
_NOP();
P2OUT&=~BIT5; //sck=0;
if(P4IN&0x02)
read_data|=0x01;
else
read_data&=0xfe;
read_data<<=1;
}
_NOP();
_NOP();
_NOP();
busy_data=read_data;
}
while((busy_data&0x03)!=0x02);
P2OUT|=BIT0 ; //CS=1;
}
用的单片机是msp430f5438A,模拟spi实现对flash程序的读写,现在读出的数据始终是FF,即应该没有写进数据,那位大神开源指点一二!
|
|