1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| #include <p18f4550.h> //head file.
#include <spi.h>
#define cs LATCbits.LATC6 //define sd pin.
#define SSPIF PIR1bits.SSPIF
unsigned char r1=0;
void delay_100ms(void);
void spi_init(void); //spi init
void spi_low(void); //produce low band func(sd card init use.)
void spi_high(void); //produce high band func(sd card init use.)
unsigned char sd_reset(void); //sd card init func
unsigned char SD_SendCommand(char CMD,char ARG1,char ARG2,char ARG3,char ARG4,char CRC); //write sd card command func
unsigned char SPI_WriteByte(unsigned char val); //write one byte func.
void delay(void); //delay func.
/////////////////////////////////////////////////
//main
void main()
{
unsigned char loop,res;
//On désactive l'acquisition analogique
ADCON0=0x0f;
ADCON1=0x0f;
OpenSPI(SPI_FOSC_64, MODE_11, SMPEND);
while(1)
{
res= sd_reset(); //sd card init.
if(res) break; //if init is abnormal,exit and don't perform the next func.
printlcd('3');
}
}
////////////////////////////////////////////////
//spi init
void spi_init()
{
//TRISCbits.TRISC7=0; // SDO declarer en sortie
//TRISBbits.TRISB1=0; //SCK est declarer en sortie sur le maitre
//TRISBbits.TRISB0=1; //SCK est declarer en sortie sur le maitre
//TRISC=0xd0; //set SDI as input£¬other port c are output.
//TRISD=0X00; //set port d as output.
//TRISA=0X00; //set port a as output.
//ADCON1=0X07; //set port a as ordinary i/o.
SSPCON1=0x32; //high when idle.fosc/64
SSPSTAT=0x80; // rising edge,send data.
cs=1;
//SSPSTAT = SSPSTAT & 0b00111111; // registre port spi mode 11 osc/64
//SSPCON1 = 0b00110010; //clock middle
//LATCbits.LATC0=0;
//LATCbits.LATC1=0;
//PORTA=0XFF;
}
unsigned char SD_SendCommand(char CMD,char ARG1,char ARG2,char ARG3,char ARG4,char CRC)
{
unsigned char r1;
unsigned char retry1=0; //repeat count.
cs=0; //enable cs signal.
SPI_WriteByte(CMD); //write command.
SPI_WriteByte(ARG1);
SPI_WriteByte(ARG2);
SPI_WriteByte(ARG3);
SPI_WriteByte(ARG4);
SPI_WriteByte(CRC); //CRC check sum.
while((r1 = SPI_WriteByte(0xff)) == 0xff)//wait ack.
if(retry1++ > 100) break; //overtime exit.
cs=1; //clear cs.
return r1; //return the status value.
}
////////////////////////////////////////////////
//set low band.
void spi_low()
{
SSPCON1=0x32; //SPI clk use the system clk--fosc/64 0x32
}
////////////////////////////////////////////////
//set high band.
void spi_high()
{
SSPCON1=0x31; //SPI clk use the system clk--fosc/64 0x31
}
///////////////////////////////////////////////
//write one byte.
unsigned char SPI_WriteByte(unsigned char val)
{
SSPBUF = val; //load to send buffer
while(!SSPIF); //wait to finish sending.
SSPIF=0; //clear send finish flag.
return SSPBUF; //read the receive buffer(even the data is unvalid,it is still need cleared.)
}
////////////////////////////////////////////////
//SD reset.
unsigned char sd_reset()
{
unsigned char i,tmp;
unsigned char retry; //repeat times.
retry=0;
spi_low(); //use low band.
do
{
cs=1;
for(i=0;i<10;i++) SPI_WriteByte(0xff);
delay_100ms();
cs=0;
delay_100ms();
r1=SD_SendCommand(0x40,0,0,0,0,0x95);
retry++;
//if(retry>10) return 1; //overtime exit.
} while(r1 != 0x01); //wait IDLE command return.
retry = 0;
do
{
for(i=0;i<100;i++) SPI_WriteByte(0xff);
//r1 = SD_SendCommand(1, 0); //send Active command.
r1 = SD_SendCommand(0x41,0,0,0,0,0xff);
retry++;
if(retry>100) return 1; //overtime exit.
} while(r1); //wait Active command exit.
spi_high(); //use high band.
return 0; //return normal.
}
///////////////////////////////////////////////
//
void delay() //
{
int i; //
for(i=0x100;i--;); //
}
void delay_100ms()
{
int i;
for(i=0;i<=9;i++)
{
Delay10TCYx( 5);;
}
} |
Partager