program antarmuka mmc dengan atmega 128


UNTUK SETINGAN DI PROTEUSNYA SEPERTI GAMBAR DIBAWAH INI

filemmc

PADA CARD IMAGE FILE KAMU BIKIN FILE DENGAN EKSTENSI .MMC, MISALNYA PUNYA SAYA DATA.MMC

BERIKUT TAMPILAN SIMULASINYA

filemmc1

DATA YANG DITULISKAN YAITU KARAKTER ASCII DARI 0-255. NAH KALO INGIN LIHAT DATA YANG DITULIS KE MMC SUDAH BENAR PAKAI WINHEX. MMC MASUKIN KE KOMPUTER TERUS LIHAT PAKAI PROGRAM WINHEX

BERIKKUT TAMPILAN MMC KETIKA DIBUKA DENGAN PROGRAM WINHEX.

filemmc2

 

UNTUK MEMBUKA MMC DENGAN WINHEX, MASUK TOOLS–>OPENDISK, TERUS FILIH MMC YANG DIPAKAI.

FILEMMC3

 

INI SOURCE NYA MEMAKAI AVR STUDIO4 +AVR GCC

//============================================
//atmega 128 crystal 11059200
//============================================
#define F_CPU 11059200UL
#include “util/delay.h”
#include “stdlib.h”
#include “avr/io.h”
#include “avr/iom128.h”
#include “avr/pgmspace.h”
#include “avr/interrupt.h”
//===========================================
#define UART_BAUD_RATE 9600
#define UART_BAUD_CALC(UART_BAUD_RATE,F_CPU) ((F_CPU)/((UART_BAUD_RATE)*16l)-1)
//===========================================
#define SPIDI    3    // Port B bit 6 (pin7): data in (data from MMC)
#define SPIDO    2    // Port B bit 5 (pin6): data out (data to MMC)
#define SPICLK    1    // Port B bit 7 (pin8): clock
#define SPICS    0    // Port B bit 4 (pin5: chip select for MMC

#define enable_mmc    PORTB&=~(1<<SPICS)
#define disable_mmc   PORTB |=(1<<SPICS)
//=====================================================mmc
#define GO_IDLE_STATE            0+0×40
#define SEND_OP_COND             1+0×40
#define READ_SINGLE_BLOCK        17+0×40
#define WRITE_SINGLE_BLOCK       24+0×40

#define idmaksimal 250

unsigned long startmmc;
unsigned int jumlah_idamr;
unsigned char temp1,temp2;
unsigned int immc,index1word,index2word,indexserial,indextime,indextime1;
unsigned char status_bacammc,status_tulismmc;
unsigned char ulang_bacammc,ulang_tulismmc;
unsigned char buffer_mmc[512];
//===================================================================
void init_serial1()
{
UBRR1H=(uint8_t) (UART_BAUD_CALC(UART_BAUD_RATE,F_CPU)>>8);
UBRR1L=(uint8_t) UART_BAUD_CALC(UART_BAUD_RATE,F_CPU);
UCSR1B |= (1<<RXEN1)|(1<<TXEN1);
UCSR1C |= (1 << UCSZ10) | (1 << UCSZ11);
}

void uart_putc1(unsigned char c)
{
while(!(UCSR1A & (1 << UDRE1)));
UDR1 = c;
}

void uart_puts1(char *s) {
while (*s) {
uart_putc1(*s);
s++;
}
}

void uart_puts_p1(const char *progmem_s)
{
register char c;
while ((c = pgm_read_byte(progmem_s++))) {
uart_putc1(c);
}
}

void uart_puts_pp1(const char *progmem_s)
{
register char c;
for(;;)
{
c = pgm_read_byte(progmem_s++);
if(c==0) break;
uart_putc1(c);
}
}

void enter()
{
uart_putc1(13);
uart_putc1(10);
}

void uartbyte(unsigned char datana)
{
unsigned char tempbyte;
unsigned char buff[3];
tempbyte=datana;
buff[2]=(tempbyte/100)+48;
buff[1]=((tempbyte%=100)/10)+48;
buff[0]=(tempbyte%10)+48;
if (datana <10){uart_putc1(buff[0]);}
else if(datana <100) {uart_putc1(buff[1]);uart_putc1(buff[0]);}
else {uart_putc1(buff[2]);uart_putc1(buff[1]);uart_putc1(buff[0]);}
}

void uartword(unsigned int datana) //(0-9999999)
{    unsigned int tempword;
unsigned char buff[5];
tempword=datana;
buff[4]=(tempword/10000)+48;
buff[3]=((tempword%=10000)/1000)+48;
buff[2]=((tempword%=1000)/100)+48;
buff[1]=((tempword%=100)/10)+48;
buff[0]=(tempword%=10)+48;
if (datana <10){uart_putc1(buff[0]);}
else if(datana <100) {uart_putc1(buff[1]);uart_putc1(buff[0]);}
else if(datana <1000) {uart_putc1(buff[2]);uart_putc1(buff[1]);uart_putc1(buff[0]);}
else if(datana <10000)
{uart_putc1(buff[3]);uart_putc1(buff[2]);uart_putc1(buff[1]);uart_putc1(buff[0]);}
else if(datana <65536)
{uart_putc1(buff[4]);uart_putc1(buff[3]);uart_putc1(buff[2]);uart_putc1(buff[1]);uart_putc1(buff[0]);}
}
//=======================================================
//================================================================
//spi
//================================================================
void init_spi()
{
DDRB &= ~(1 << SPIDI);    // set port B SPI data input to input
DDRB |= (1 << SPICLK);    // set port B SPI clock to output
DDRB |= (1 << SPIDO);    // set port B SPI data out to output
DDRB |= (1 << SPICS);    // set port B SPI chip select to output
SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR1) | (1 << SPR0);
//PORTB &= ~(1 << SPICS);    // set chip select to low (MMC is selected)
enable_mmc;
//SPE SPI ENABLE;
//MASTER
//64
}

char spi(char d) //kirim terima 1 byte
{
char received = 0;
SPDR = d;
while(!(SPSR & (1<<SPIF)));
received = SPDR;
return (received);
}
//=================================================================
//mmc
//=================================================================
char send_command(char command1, unsigned long adr, char command2 )
{
unsigned int retry=0;
unsigned char response;
spi(0xFF);
spi(command1);
spi(adr>>24);
spi(adr>>16);
spi(adr>>8);
spi(adr);
spi(command2);
//spi(0xFF);

//return spi(0xFF);    // return the last received character

retry=0;
do
{
response=spi(0xFF);
retry++;
//if(retry>0xfe) return 0; //time out
if(retry>2000) return 0; //time out
}
while(response == 0xff);
return response;
}

int init_mmc(void)
{
unsigned char i,response;
unsigned int retry=0
;
disable_mmc;
for(i=0;i<10;i++) spi(0xFF); //10*8=80 clock pulses
enable_mmc;

do
{
response = send_command(GO_IDLE_STATE, 0,0×95);//send ‘reset & go idle’ command
retry++;
if(retry>2000) return 0; //time out
//if(retry>0xfe) return 0; //time out
} while(response != 0×01);

retry = 0;

do
{
response =send_command(SEND_OP_COND, 0,0xff); //activate card’s initialization process
retry++;
if(retry>2000) return 0; //time out
//if(retry>0xfe) return 0; //time out
}while(response); // tunggu sampai 0
SPCR &= ~((1<<SPR0) | (1<<SPR1));
SPSR = SPSR|(1<<SPI2X);
return 1;
}

void inisialisasi_mmc() // 1 oke, 0 gagal inisialisasi mmc
{
unsigned char statusmmc;
for(;;)
{
statusmmc=init_mmc();
if(statusmmc==1)break;
}
}

//=========================================================
//write single block mmc
//=========================================================
unsigned char mmc_writesingleblock(unsigned long startBlock)
{
unsigned char response;
unsigned int i,retry=0;

do
{
response = send_command(WRITE_SINGLE_BLOCK, startBlock,0xff);//send ‘reset & go idle’ command
retry++;
//if(retry>0xfe) return 0; //time out
if(retry>2000) return 0; //time out
} while(response != 0×00);

spi(0xFF);
spi(0xFF);
spi(0xFE);
for (i=0;i<512;i++)
{
spi(buffer_mmc[i]);
}
spi(0xFF);
spi(0xFF);

response =spi(0xFF);
response&= 0x1F;     // 0x1F = 0b.0001.1111;
if (response!= 0×05)
{ // 0×05 = 0b.0000.0101
//uart_puts(“MMC: write error 2 “);//debug
return 0;
}

retry=0;
do
{
response=spi(0xFF);
retry++;
//if(retry>0xfe) return 0; //time out
if(retry>2000) return 0; //time out
}
while(response != 0xff);
return 1;
}

//=========================================================
//read single block mmc
//=========================================================
unsigned char mmc_readsingleblock(unsigned long startBlock)
{
unsigned char response;
unsigned int i=0;
unsigned int retry=0;
response = send_command(READ_SINGLE_BLOCK, startBlock,0xff);
if(response != 0×00)
{
return 0;
}

do
{
response = spi(0xff);
retry++;
//if(retry>0xfe) return 0; //time out
if(retry>2000) return 0; //time out
} while(response != 0xfe);

for (i=0;i<512;i++)
{
buffer_mmc[i]=spi(0XFF);
//uartword(i);                //debug
//uart_puts(“—>”);            //debug
//uart_putc(buffer_mmc[i]);// tampil uart
//enter();
}

spi(0xFF); // actually this returns the CRC/checksum byte
spi(0xFF);
return 1;
}

//======================================================================
void hitung_startblock()
{
unsigned long dataku;
dataku=index1word;
startmmc=dataku*512;
}
//===========================================
//program utama
//===========================================
int main()
{
_delay_ms(100);
init_spi();
init_serial1();
inisialisasi_mmc();
uart_puts1(“inisialiasi mmc oke”);        //indikator
enter();
for(;;)
{
for(index1word=0;index1word<512;index1word++)
{
buffer_mmc[ index1word]=index1word;
}

ulang_tulismmc=10;
for(immc=0;immc<ulang_tulismmc;immc++)
{
status_tulismmc=mmc_writesingleblock(0);
if(status_tulismmc==1)break;
_delay_ms(50);
}

ulang_bacammc=10;
for(immc=0;immc<ulang_bacammc;immc++)
{
status_bacammc=mmc_readsingleblock(0);
if(status_bacammc==1)break;
_delay_ms(50);
}

for(index1word=0;index1word<512;index1word++)
{
uart_putc1(buffer_mmc[index1word]);
if(index1word%50==0)enter();
}
enter();
uart_puts1(“selesai”);
enter();
for(;;){;}
}
}
//===========================================

About these ads

8 Balasan ke program antarmuka mmc dengan atmega 128

  1. indra mengatakan:

    terimakasih sudah membahas hal ini. klo pake atmega16 coding nya gimana(codevision)?

  2. friza mengatakan:

    kalau pgen manggil data dr mmc gmn y?bwt atmega8.

  3. friza mengatakan:

    kalau ada modul and program text to speech converter di upload donk….(pk atmega8)

  4. Rizky mengatakan:

    mas, mau tanya
    simulasinya pake proteus kan?
    kalo pake MMC bukannya perlu R dan zener untuk menurunkan tegangan sy tegangan masuk MMC menjadi 3.3V? (seperti posting rangkaian MMC sebelumnya)

    ataukah kalau simulasi pake virtual terminal nggak perlu rangakian R dan zener pd MMC?

  5. dhani mengatakan:

    mas ini cuma simulasi, ngga perlu pakai juga jalan.

    tapi nanti kalo pada alat sebenernya harus 3,3v. misal pakai pembagi tegangan, atau bisa juga dibatasi dengan zener. di artikel sebelumnya saya juga sudah bahas

  6. franki mengatakan:

    wah, bahanya bagus nih.. tapi ada ga contoh program yang pake BASCOM AVR buat baca dan tulis data di sd atau MMC? terimaksih

  7. dhan_t46 mengatakan:

    oh kalo pae bascom lebih gampang, sudah ada librarynya. nanti insya alloh saya share

  8. Iqbal mengatakan:

    terima kasih tutorialnya,tapi diatas ada sebagain gambar ketutup bisa tidak yah secara lengkapnya???atau bisa kirim keemail saya. Dan bagaimana kalo dikoneksikan dengan PMB-648 GPS SiRF Internal Antenna dan ditampilkan di LCD 128×64,dalam proteusnya tsb??

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Logout / Ubah )

Twitter picture

You are commenting using your Twitter account. Logout / Ubah )

Facebook photo

You are commenting using your Facebook account. Logout / Ubah )

Google+ photo

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

Ikuti

Get every new post delivered to your Inbox.

Bergabunglah dengan 41 pengikut lainnya.

%d bloggers like this: