Koneksi MMC dengan Mikrokontroler AVR Menggunakan SPI Mode

Berikut contoh schematic rangkaiannya.

Berikut beberapa command yang digunakan.

Command
Index

Argument

Response

Data

Abbreviation

Description

CMD0 None(0) R1 No GO_IDLE_STATE Software reset.
CMD1 None(0) R1 No SEND_OP_COND Initiate initialization process.
ACMD41(*1) *2 R1 No APP_SEND_OP_COND For only SDC. Initiate initialization process.
CMD8 *3 R7 No SEND_IF_COND For only SDC V2. Check voltage range.
CMD9 None(0) R1 Yes SEND_CSD Read CSD register.
CMD10 None(0) R1 Yes SEND_CID Read CID register.
CMD12 None(0) R1b No STOP_TRANSMISSION Stop to read data.
CMD16 Block
length[31:0]
R1 No SET_BLOCKLEN Change R/W block size.
CMD17 Address[31:0] R1 Yes READ_SINGLE_BLOCK Read a block.
CMD18 Address[31:0] R1 Yes READ_MULTIPLE_BLOCK Read multiple blocks.
CMD23 Number of
blocks[15:0]
R1 No SET_BLOCK_COUNT For only MMC. Define number of blocks to transfer
with next multi-block read/write command.
ACMD23(*1) Number of
blocks[22:0]
R1 No SET_WR_BLOCK_ERASE_COUNT For only SDC. Define number of blocks to pre-erase
with next multi-block write command.
CMD24 Address[31:0] R1 Yes WRITE_BLOCK Write a block.
CMD25 Address[31:0] R1 Yes WRITE_MULTIPLE_BLOCK Write multiple blocks.
CMD55(*1) None(0) R1 No APP_CMD Leading command of ACMD<n> command.
CMD58 None(0) R3 No READ_OCR Read OCR.
*1:ACMD<n> means a command sequense of CMD55-CMD<n>.
*2: Rsv(0)[31], HCS[30], Rsv(0)[29:0]
*3: Rsv(0)[31:12], Supply Voltage(1)[11:8], Check Pattern(0xAA)[7:0]

Berikut adalah timming diagram pada pengiriman command.

Berikut adalah format data ketika write/read data.

Lengkapnya sebagai berikut.



Berikut adalah timming diagram dari simulasi dari file http://rapidshare.com/files/71131573/SD_MMC.rar dari web http://www.sonsivri.to/forum/index.php?topic=6658.0

Dummy Clock

CMD0

CMD1

CMD16

CMD17

Timming diagram keseluruhan.

Timming diagram CMD17 sebelum menerima data.

Timming diagram CMD17 saat menerima data.

Timming diagram CMD17 saat menerima token dan data awal.

 

Berikut adalah program yang saya buat untuk koneksi dengan MMC via SPI yang kemudian hasilnya saya kirim lewat UART yang kemudian masuk Terminal pada PC. Compiler yang saya gunakan adalah CodevisionAVR. Yang saya lakukan adalah mencoba berapa command; -membuat inisialisasi MMC agar bisa digunakan pada mode SPI,-menulis data lalu membacanya lagi. Konfigurasi PIN yang saya gunakan adalah MOSI ke DI, MISO ke DO, SCK ke CLK, SS ke CS.

/*****************************************************
Chip type           : ATmega128
Program type        : Application
Clock frequency     : 11.059200 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 1024
*****************************************************/

#include <mega128.h>  
#include <delay.h>
#include <io.h>  
#ifndef SPIF
#define SPIF 7
#endif

// SPI functions
#include <spi.h> 

// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x12 ;PORTD
#endasm
#include <lcd.h> 

#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)

// USART0 Receiver buffer
#define RX_BUFFER_SIZE0 8
char rx_buffer0[RX_BUFFER_SIZE0];

#if RX_BUFFER_SIZE0<256
unsigned char rx_wr_index0,rx_rd_index0,rx_counter0;
#else
unsigned int rx_wr_index0,rx_rd_index0,rx_counter0;
#endif

// This flag is set on USART0 Receiver buffer overflow
bit rx_buffer_overflow0;

// USART0 Receiver interrupt service routine
interrupt [USART0_RXC] void usart0_rx_isr(void)
{
char status,data;
status=UCSR0A;
data=UDR0;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
   {
   rx_buffer0[rx_wr_index0]=data;
   if (++rx_wr_index0 == RX_BUFFER_SIZE0) rx_wr_index0=0;
   if (++rx_counter0 == RX_BUFFER_SIZE0)
      {
      rx_counter0=0;
      rx_buffer_overflow0=1;
      };
   };
}

#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART0 Receiver buffer
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter0==0);
data=rx_buffer0[rx_rd_index0];
if (++rx_rd_index0 == RX_BUFFER_SIZE0) rx_rd_index0=0;
#asm("cli")
--rx_counter0;
#asm("sei")
return data;
}
#pragma used-
#endif

// USART0 Transmitter buffer
#define TX_BUFFER_SIZE0 8
char tx_buffer0[TX_BUFFER_SIZE0];

#if TX_BUFFER_SIZE0<256
unsigned char tx_wr_index0,tx_rd_index0,tx_counter0;
#else
unsigned int tx_wr_index0,tx_rd_index0,tx_counter0;
#endif

// USART0 Transmitter interrupt service routine
interrupt [USART0_TXC] void usart0_tx_isr(void)
{
if (tx_counter0)
   {
   --tx_counter0;
   UDR0=tx_buffer0[tx_rd_index0];
   if (++tx_rd_index0 == TX_BUFFER_SIZE0) tx_rd_index0=0;
   };
}

#ifndef _DEBUG_TERMINAL_IO_
// Write a character to the USART0 Transmitter buffer
#define _ALTERNATE_PUTCHAR_
#pragma used+
void putchar(char c)
{
while (tx_counter0 == TX_BUFFER_SIZE0);
#asm("cli")
if (tx_counter0 || ((UCSR0A & DATA_REGISTER_EMPTY)==0))
   {
   tx_buffer0[tx_wr_index0]=c;
   if (++tx_wr_index0 == TX_BUFFER_SIZE0) tx_wr_index0=0;
   ++tx_counter0;
   }
else
   UDR0=c;
#asm("sei")
}
#pragma used-
#endif

// Standard Input/Output functions
#include <stdio.h>

#define ss_ PORTB.0
// Declare your global variables here
unsigned char   index
                ,argument[4]
                ,CRC
                ,data[4]
                ,kata[16];
unsigned int i;
void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=Out Func1=Out Func0=Out
// State7=T State6=T State5=T State4=T State3=T State2=0 State1=0 State0=0
PORTB=0x00;
DDRB=0x07;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Port E initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTE=0x00;
DDRE=0x00;

// Port F initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTF=0x00;
DDRF=0x00;

// Port G initialization
// Func4=In Func3=In Func2=In Func1=In Func0=In
// State4=T State3=T State2=T State1=T State0=T
PORTG=0x00;
DDRG=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
ASSR=0x00;
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// OC1C output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
OCR1CH=0x00;
OCR1CL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// Timer/Counter 3 initialization
// Clock source: System Clock
// Clock value: Timer 3 Stopped
// Mode: Normal top=FFFFh
// Noise Canceler: Off
// Input Capture on Falling Edge
// OC3A output: Discon.
// OC3B output: Discon.
// OC3C output: Discon.
// Timer 3 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off
TCCR3A=0x00;
TCCR3B=0x00;
TCNT3H=0x00;
TCNT3L=0x00;
ICR3H=0x00;
ICR3L=0x00;
OCR3AH=0x00;
OCR3AL=0x00;
OCR3BH=0x00;
OCR3BL=0x00;
OCR3CH=0x00;
OCR3CL=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
// INT3: Off
// INT4: Off
// INT5: Off
// INT6: Off
// INT7: Off
EICRA=0x00;
EICRB=0x00;
EIMSK=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
ETIMSK=0x00;    

// USART0 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART0 Receiver: On
// USART0 Transmitter: On
// USART0 Mode: Asynchronous
// USART0 Baud rate: 9600
UCSR0A=0x00;
UCSR0B=0xD8;
UCSR0C=0x06;
UBRR0H=0x00;
UBRR0L=0x47;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// SPI initialization
// SPI Type: Master
// SPI Clock Rate: 2764.800 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
SPCR=0x50;
SPSR=0x00; 

// Global enable interrupts
#asm("sei")
lcd_init(16);                     

//_________________beri clock awal_______________
delay_ms(2);
ss_ =1; //cs=1  
for(i=0;i<10;i++)spi(0xFF); //74 cycle 
delay_us(10);
ss_ =0; //cs=0;   
printf("dummy clock \n\r");   //kirim UART ke Terminal PC
delay_ms(2);   
//_________________CMD0_______________
for(i=0;i<16;i++)spi(0xFF); //8x16 cycle 
//'40 00 00 00 00 95'
spi(0x40);
spi(0x00);
spi(0x00);
spi(0x00);
spi(0x00);
spi(0x95);      
spi(0xFF);  //8clock cycle     
data[0]=spi(0xFF); //get data    
delay_us(5);

printf("CMD0:%d \n\r",data[0]);  //kirim UART ke Terminal PC          
//_________________CMD1_______________
for(i=0;i<16;i++)spi(0xFF); //8x16 cycle
//'41 00 00 00 00 95'
spi(0x41);
spi(0x00);
spi(0x00);
spi(0x00);
spi(0x00);
spi(0x95);      
spi(0xFF);  //8clock cycle     
data[0]=spi(0xFF); //get data    
delay_us(5);

printf("CMD1:%d \n\r",data[0]);   //kirim UART ke Terminal PC

//_________________CMD16_______________
for(i=0;i<16;i++)spi(0xFF); //8x16 cycle
//'50 00 00 00 00 95'
spi(0x50);
spi(0x00);
spi(0x00);
spi(0x00);
spi(0x20);  //fat32
spi(0x95);      
spi(0xFF);  //8clock cycle     
data[0]=spi(0xFF); //get data    
delay_us(5);

printf("CMD16:%d \n\r",data[0]);    //kirim UART ke Terminal PC

//_________________CMD10_______________
for(i=0;i<16;i++)spi(0xFF); //8x16 cycle
//'50 00 00 00 00 95'
spi(0x4A);
spi(0x00);
spi(0x00);
spi(0x00);
spi(0x00);
spi(0x95);      
spi(0xFF);  //8clock cycle     
data[0]=spi(0xFF); //get data    
delay_us(5);

printf("CMD10/CID READ:%d \n\r",data[0]);    //kirim UART ke Terminal PC

while(spi(0xFF)!=0xFE);
for(i=0;i<16;i++)printf("%c \n\r",spi(0xFF));  //kirim data ke UART ke Terminal PC
spi(0xFF);  //8clock cycle  
spi(0xFF);  //8clock cycle           

//_________________CMD9_______________
for(i=0;i<16;i++)spi(0xFF); //8x16 cycle
//'50 00 00 00 00 95'
spi(0x49);
spi(0x00);
spi(0x00);
spi(0x00);
spi(0x00);
spi(0x95);      
spi(0xFF);  //8clock cycle     
data[0]=spi(0xFF); //get data    
delay_us(5);

printf("CMD9/CSD READ:%d \n\r",data[0]);    //kirim UART ke Terminal PC

while(spi(0xFF)!=0xFE);
for(i=0;i<16;i++)printf("%d \n\r",spi(0xFF));
spi(0xFF);  //8clock cycle  
spi(0xFF);  //8clock cycle     

//_________________CMD24_______________
for(i=0;i<16;i++)spi(0xFF); //8x16 cycle
//'50 00 00 00 00 95'
spi(0x58);
spi(0x00);
spi(0x00);
spi(0x00);
spi(0x00);
spi(0x95);      
spi(0xFF);  //8clock cycle     
data[0]=spi(0xFF); //get data    
delay_us(5);

printf("CMD24:%d \n\r",data[0]);    //kirim UART ke Terminal PC

for(i=0;i<18;i++)spi(0xFF); //8x18 cycle
spi(0xFE);   //token
for(i=0;i<32;i++)spi(49+i); //kirim data angka 1 dalam ASCII dan 32 kode dst
spi(0xFF);  //CRC
spi(0xFF);  //CRC

while(spi(0xFF)!=0xFF)              

//_________________CMD17_______________
for(i=0;i<16;i++)spi(0xFF); //8x16 cycle
//'50 00 00 00 00 95'
spi(0x51);
spi(0x00);
spi(0x00);
spi(0x00);
spi(0x00);
spi(0x95);      
spi(0xFF);  //8clock cycle     
data[0]=spi(0xFF); //get data    
delay_us(5);

printf("CMD17:%d \n\r",data[0]);   

while(spi(0xFF)!=0xFE);
//baca data lalu kirim UART ke Terminal PC
for(i=0;i<32;i++)printf("READ-%d:%c \n\r",i,spi(0xFF));
spi(0xFF);  //8clock cycle  
spi(0xFF);  //8clock cycle         
//__________________________________________  

while (1)
      {
      // Place your code here

      };
}

Berikut adalah program yang lainnya yang sudah saya buat fungsinya.

/*****************************************************
Chip type           : ATmega128
Program type        : Application
Clock frequency     : 11.059200 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 1024
*****************************************************/

#include <mega128.h>  
#include <delay.h>
#include <io.h>  
#ifndef SPIF
#define SPIF 7
#endif

// SPI functions
#include <spi.h> 

// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x12 ;PORTD
#endasm
#include <lcd.h> 

#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)

// USART0 Receiver buffer
#define RX_BUFFER_SIZE0 8
char rx_buffer0[RX_BUFFER_SIZE0];

#if RX_BUFFER_SIZE0<256
unsigned char rx_wr_index0,rx_rd_index0,rx_counter0;
#else
unsigned int rx_wr_index0,rx_rd_index0,rx_counter0;
#endif

// This flag is set on USART0 Receiver buffer overflow
bit rx_buffer_overflow0;

// USART0 Receiver interrupt service routine
interrupt [USART0_RXC] void usart0_rx_isr(void)
{
char status,data;
status=UCSR0A;
data=UDR0;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
   {
   rx_buffer0[rx_wr_index0]=data;
   if (++rx_wr_index0 == RX_BUFFER_SIZE0) rx_wr_index0=0;
   if (++rx_counter0 == RX_BUFFER_SIZE0)
      {
      rx_counter0=0;
      rx_buffer_overflow0=1;
      };
   };
}

#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART0 Receiver buffer
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter0==0);
data=rx_buffer0[rx_rd_index0];
if (++rx_rd_index0 == RX_BUFFER_SIZE0) rx_rd_index0=0;
#asm("cli")
--rx_counter0;
#asm("sei")
return data;
}
#pragma used-
#endif

// USART0 Transmitter buffer
#define TX_BUFFER_SIZE0 8
char tx_buffer0[TX_BUFFER_SIZE0];

#if TX_BUFFER_SIZE0<256
unsigned char tx_wr_index0,tx_rd_index0,tx_counter0;
#else
unsigned int tx_wr_index0,tx_rd_index0,tx_counter0;
#endif

// USART0 Transmitter interrupt service routine
interrupt [USART0_TXC] void usart0_tx_isr(void)
{
if (tx_counter0)
   {
   --tx_counter0;
   UDR0=tx_buffer0[tx_rd_index0];
   if (++tx_rd_index0 == TX_BUFFER_SIZE0) tx_rd_index0=0;
   };
}

#ifndef _DEBUG_TERMINAL_IO_
// Write a character to the USART0 Transmitter buffer
#define _ALTERNATE_PUTCHAR_
#pragma used+
void putchar(char c)
{
while (tx_counter0 == TX_BUFFER_SIZE0);
#asm("cli")
if (tx_counter0 || ((UCSR0A & DATA_REGISTER_EMPTY)==0))
   {
   tx_buffer0[tx_wr_index0]=c;
   if (++tx_wr_index0 == TX_BUFFER_SIZE0) tx_wr_index0=0;
   ++tx_counter0;
   }
else
   UDR0=c;
#asm("sei")
}
#pragma used-
#endif

// Standard Input/Output functions
#include <stdio.h>

#define ss_ PORTB.0
// Declare your global variables here
unsigned char   count;

void dummy_clock()
{
unsigned char i;
        ss_ =1; //cs=1  
        for(i=0;i<10;i++)spi(0xFF); //74 cycle 
        delay_us(10);
        ss_ =0; //cs=0;   
}                     

unsigned char CMD_R1response(unsigned char CMD,unsigned char ARG1,unsigned char ARG2,unsigned char ARG3,unsigned char ARG4)
{   
unsigned char   r1_respone
                ,i;
        for(i=0;i<16;i++)spi(0xFF); //8x16 cycle 
        spi(CMD+64);
        spi(ARG1);
        spi(ARG2);
        spi(ARG3);
        spi(ARG4);
        spi(0x95);      
        do r1_respone=spi(0xFF); //get data    
        while(r1_respone==0xFF);
        delay_us(5);
        return(r1_respone);
} 

unsigned char data_[512];
void read_data(unsigned char jum_byte)
{
unsigned int i;
        while(spi(0xFF)!=0xFE);  //token
        for(i=0;i<jum_byte;i++)data_[i]=spi(0xFF);
        spi(0xFF);  //8clock cycle  
        spi(0xFF);  //8clock cycle 
}    

void clear_data_()
{
 unsigned int i; 
        for(i=0;i<512;i++)data_[i]=0;
}

void write_data(unsigned char jum_byte)
{
 unsigned int i;                                

        for(i=0;i<18;i++)spi(0xFF); //8x18 cycle
        spi(0xFE);  //token
        for(i=0;i<jum_byte;i++)spi(data_[i]);
        spi(0xFF);  //8clock cycle  
        spi(0xFF);  //8clock cycle  
}

void write_data_str(unsigned char jum_byte,unsigned char size_text , char flash *str)
{
 unsigned int i;                                

        for(i=0;i<18;i++)spi(0xFF); //8x18 cycle
        spi(0xFE);  //token
        for(i=0;i<size_text;i++)spi(str[i]);
        for(i=size_text-1;i<jum_byte;i++)spi(0);
        spi(0xFF);  //8clock cycle  
        spi(0xFF);  //8clock cycle 
}

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=Out Func1=Out Func0=Out
// State7=T State6=T State5=T State4=T State3=T State2=0 State1=0 State0=0
PORTB=0x00;
DDRB=0x07;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Port E initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTE=0x00;
DDRE=0x00;

// Port F initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTF=0x00;
DDRF=0x00;

// Port G initialization
// Func4=In Func3=In Func2=In Func1=In Func0=In
// State4=T State3=T State2=T State1=T State0=T
PORTG=0x00;
DDRG=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
ASSR=0x00;
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// OC1C output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
OCR1CH=0x00;
OCR1CL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// Timer/Counter 3 initialization
// Clock source: System Clock
// Clock value: Timer 3 Stopped
// Mode: Normal top=FFFFh
// Noise Canceler: Off
// Input Capture on Falling Edge
// OC3A output: Discon.
// OC3B output: Discon.
// OC3C output: Discon.
// Timer 3 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off
TCCR3A=0x00;
TCCR3B=0x00;
TCNT3H=0x00;
TCNT3L=0x00;
ICR3H=0x00;
ICR3L=0x00;
OCR3AH=0x00;
OCR3AL=0x00;
OCR3BH=0x00;
OCR3BL=0x00;
OCR3CH=0x00;
OCR3CL=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
// INT3: Off
// INT4: Off
// INT5: Off
// INT6: Off
// INT7: Off
EICRA=0x00;
EICRB=0x00;
EIMSK=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
ETIMSK=0x00;    

// USART0 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART0 Receiver: On
// USART0 Transmitter: On
// USART0 Mode: Asynchronous
// USART0 Baud rate: 9600
UCSR0A=0x00;
UCSR0B=0xD8;
UCSR0C=0x06;
UBRR0H=0x00;
UBRR0L=0x47;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// SPI initialization
// SPI Type: Master
// SPI Clock Rate: 2764.800 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
SPCR=0x50;
SPSR=0x00; 

// Global enable interrupts
#asm("sei")
lcd_init(16);                     

dummy_clock();  
printf("dummy clock \n\r");

printf("CMD0:%d \n\r",CMD_R1response(0,0,0,0,0));

printf("CMD1:%d \n\r",CMD_R1response(1,0,0,0,0));

printf("CMD16:%d \n\r",CMD_R1response(16,0,0,0,32));

printf("CMD10:%d \n\r",CMD_R1response(10,0,0,0,0));     
read_data(16);                        
for(count=0;count<16;count++)printf("%d \n\r",data_[count]);
clear_data_(); 

printf("CMD9:%d \n\r",CMD_R1response(9,0,0,0,0));     
read_data(16);                        
for(count=0;count<16;count++)printf("%d \n\r",data_[count]);
clear_data_();

printf("CMD24:%d \n\r",CMD_R1response(24,0,0,1,0));                
write_data_str(32,21,"tes data kirim\n\rMMC\n\r");  
clear_data_();  

printf("CMD24:%d \n\r",CMD_R1response(24,0,0,0,0));   
data_[0] = 'U';           
write_data(32);  
clear_data_(); 

printf("CMD17:%d \n\r",CMD_R1response(17,0,0,0,0));     
read_data(32);                        
for(count=0;count<32;count++)printf("%c \n\r",data_[count]);   
clear_data_();          

printf("CMD17:%d \n\r",CMD_R1response(17,0,0,1,0));     
read_data(32);                        
for(count=0;count<32;count++)printf("%c",data_[count]);   
clear_data_(); 

printf("CMD17:%d \n\r",CMD_R1response(17,0,0,0,0));     
read_data(32);                        
for(count=0;count<32;count++)printf("%c \n\r",data_[count]);   
clear_data_();  

while (1)
      {
      // Place your code here

      };
}

Berikut project file simulasi dengan compiler CodevisionAVR dan Simulator Proteus 7.7

http://www.mediafire.com/file/m7go0c1pf457kib/MMC_myfile.rar

________________________________________

DAFTAR PUSTAKA

http://nawattlabs.com/index.php/corporate/blog/26-sd-mmc-programming

http://www.sonsivri.to/forum/index.php?topic=6658.0

http://elm-chan.org/docs/mmc/mmc_e.html

http://www.mikroe.com/download/eng/documents/compilers/mikrobasic/pro/avr/help/mmc_library.htm

http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus

http://elm-chan.org/docs/spi_e.html

http://www.ulrichradig.de/site/arm_projekts/mmc_sd/doc/MMCSDTimming.pdf

http://www.innovativeelectronics.com/innovative_electronics/pro_ems_smd_mmc_fram.htm

http://www.innovativeelectronics.com/innovative_electronics/download_files/artikel/AN_SD_CVAVR.pdf

http://www.innovativeelectronics.com/innovative_electronics/download_files/artikel/AN_SD_FAT.pdf

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s