![]() |
|
Tài trợ cho PIC Vietnam |
Giao tiếp USB, CAN, I2C, SPI, USART... Những giao tiếp được tích hợp trên PIC |
|
Ðiều Chỉnh | Xếp Bài |
![]() |
#34 |
Nhập môn đệ tử
Tham gia ngày: Aug 2010
Bài gửi: 6
: |
Chào các bác !!!
Em cũng đang ngâm cứu về SDCard Nhưng hiện tại em mới chỉ khởi tạo cho thẻ mà không được Reset thì thẻ vẫn trả về được Reponse R1 = 0x01 Đây là code và shematic Mong các bác xem và giúp đỡ em Code:
//Chuong trinh giao tiep voi SD card -------------------------- // ------------ main.c ------------------------------ #include <18F452.h> #fuses hs,nowdt,nolvp #use delay (clock = 12000000) #include <LCD_HYknight.C> #include <def_18f452.c> #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,ERRORS) #include <SDCard.c> void main() { SETUP_ADC_PORTS(NO_ANALOGS); SETUP_SPI (SPI_MASTER | SPI_SS_DISABLED |SPI_H_TO_L| SPI_CLK_DIV_4 | SPI_XMIT_L_TO_H); TRISA = 0X00 ; TRISC = 0X93; TRISB = 0XF0; TRISD = 0X0F; TRISE = 0X00; CS=1; LCD_INIT(); PRINTF("\r\n**** Doc ghi SD card - HYknight - CFOC **** "); PRINTF("\r\n"); DELAY_MS(2000); PRINTF("\r\ Khoi tao Card..... "); PRINTF("\r\n"); DELAY_MS(2000); if(mmc_init()== 0) { printf("> Khoi tao card thanh cong...\r\n>"); DELAY_MS(2000); } else { printf("> Khong khoi tao duoc, xin moi khoi tao lai...!\r\n"); LCD_PUTCHAR("Khong kt duoc"); } } Code:
// --------------- SDCard.c ---------------------- // ----------------------------------------------- #DEFINE CS rc2 // chip select for MMC #DEFINE SDO rc5 #DEFINE CLK rc3 #DEFINE SDI rc4 void send_6byte(int8 cmd, int16 argh, int16 argl, int8 crc7); int mmc_init(); char str[600]; CHAR CH; int8 HIGH(long data) { int8 modi; modi = data>>8; return(modi); } int8 LOW(long data) { int8 modi; modi = data; return(modi); } void send_6byte(int8 cmd, int16 argh, int16 argl, int8 crc7) { spi_write(cmd); spi_write(make8(argh,1)); spi_write(make8(argh,0)); spi_write(make8(argl,1)); spi_write(make8(argh,0)); spi_write(crc7); } int mmc_response(unsigned char response) { unsigned long count = 0xFFFF; // 16bit repeat, it may be possible to shrink this to 8 bit but there is not much point while(SPI_READ(0xFF) != response && --count > 0); if(count==0) return 1; // loop was exited due to timeout else return 0; // loop was exited before timeout } int mmc_init() { int i; *0x94 |= 0x40; // set CKE = 1 - clock idle low *0x14 &= 0xEF; // set CKP = 0 - data valid on rising edge CS =1; // set cs = 1 (off) SPI_WRITE(0xFF); for(i=0;i<16;i++) // initialise the MMC card into SPI mode by sending clks on { SPI_WRITE(0xFF); } CS =0; // set cs = 0 (on) tells card to go to spi mode when it receives reset send_6byte(0x40,0,0,0x95); for(i= 0; i< 100; i++) { if(spi_read(0x00)== 0x01) { break; } } if(i>= 100) // Check for Time-out Error { return(1); } printf("CMD40 OK (R1= 0x01)\n\r"); DELAY_MS(2000); // ----------> Den day van duoc send_6byte(0x55, 0, 0, 0xff); send_6byte(0x41, 0, 0, 0xff); for(i= 0; i<255; i++) { if(spi_read(0x00)== 0x00) { break; } } if(i>=255) { return(1); } printf("CMD41 OK (R1= 0x00)\n\r"); DELAY_MS(2000); return (0); } |
![]() |
![]() |
Ðiều Chỉnh | |
Xếp Bài | |
|
|