我又改了一下。还是不行,哪里出了问题,用的是SD卡模块插座
#include "reg51.h"
#include "delay.h"
#define uchar unsigned char
#define uint unsigned int
#include "lcd12864.h"
#define reset_ERROR 0x01
#define init_ERROR 0x02
sbit MISO=P2^0;
sbit SCK=P2^1;
sbit MOSI=P2^2;
sbit CS=P2^3;
void SPI_write(uchar dat)
{
uchar a;
for(a=0;a<8;a++)
{
SCK=0;
MOSI=dat&0x80;
SCK=1;
delayus(5);
dat<<=1;
}
}
uchar SPI_read()
{
uchar a,dat;
for (a=0;a<8;a++)
{
SCK=0;
delayus(5);
SCK=1;
dat<<=1;
dat|=MISO;
}
return dat;
}
uchar SD_write_cmd(uchar *s)
{
uchar a,status,time=0;
for(a=0;a<6;a++) SPI_write(s[a]);
do
{
status=SPI_read();
time++;
}
while(time<100&&status!=0x01);
return status;
}
uchar reset_SD()
{
uchar a,status,time;
uchar restcmd[]={0x40,0x00,0x00,0x00,0x00,0x95};//初始化指令
CS=1;
for(a=0;a<100;a++)
{
SCK=0;
ys(5);
SCK=1;
ys(5);
}
CS=0;
for(time=0;time<200;time++)
{ for(a=0;a<6;a++)SPI_write(restcmd[a]);
for(a=0;a<100;a++)
{
status=SPI_read();
if(status==0x01)break;
}
if(status==0x01)break;
else if(time==199)return reset_ERROR;
}
CS=1;
return 0;
}
void main()
{
uchar a;
initial_lcd(); //初始化液晶屏
clear_screen(); //清屏函数
display_text("reseting...",0,0,2,0);
a=reset_SD();
if(a==0)display_text("OK! ",0,0,2,0);
if(a==reset_ERROR)display_text("reset_ERROR",0,0,2,0);
while(1);
{
}
}
|