PE WE EM (PWM MAINAN ANAK ANAK ROBOT

main main pe we em, jadi ingat waktu dulu, pasti selalu terpakai kalo bikin robot. Saya bikin program pe we em simple dengan avr gcc.

#include “avr/io.h”

void init_pwm()
{
DDRB|=(1<<1);DDRB|=(1<<2);
PORTB|=(1<<1);PORTB|=(1<<2);
TCCR1A=0b11110001;
TCCR1B=0b00001011;
}

void pwm(unsigned char nilai_kiri,unsigned char nilai_kanan)
{
OCR1AL=nilai_kiri;
OCR1BL=nilai_kanan;
}
//==================================
int main()
{
init_pwm();
for(;;)
{
pwm(200,60);
}

}

 

HASIL PRAKTIKUM

MENGHILANGKAN STRESS, WALAPUN IPK JEBLOG, TAPI AKU BISA LEBIH BAIK DARI YANG IPK NYA 3

IJAZAHKU BELUM TERPAKAI, YANG DILIHAT DARI DULU CV

antara usb to serial, atmega 8 atau max 232

Kemarin saya punya masalah dengan komunikasi serial, masalahnya disini PC bisa ngirim data ke MCU, tetapi disini permasalahannya yaitu data yang dikirim MCU ke PC kacau. nyari tahu masalahnya akhirnya ketahuan juga

osilator internal atmega 8 ternyata kalo semakin lama jadi ngaco, akhirnya saya pun menggantinya dengan eksternal dan komunikasi serial pun berjalan seperti sedia kala

UNEK UNEK MMC

Kemarin SD card (MMC) saya dipinjam oleh teman, munkin sama teman di format pakai FAT32, dan keesokan harinya MMC tersebut dikembalikan, kemudian saya pun langsung, memasukan MMC tersebut ke alat yang saya buat.

Tetapi ternyata setelah saya masukan MMC Ke alat saya, ternyata ada masalah, sebelumnya, menulis dan membaca ke MMC itu yang normal normal saja dan hampir dipastikan jarang sekali gagal, ini ternyata sering gagalnya, akhirnya saya pun, punya pemikiran untuk memformat MMC dengan komputer saya kembali , tetapi cuma dengan format FAT saja, akhirnya setelah saya format, MMC saya masukan kembali,dan akhirnya alat saya berjalan dengan normal kembali.

Saya kurang tahu kenapa di format FAT32 kurang stabil, Tetapi di format FAT saja sangat stabil!!!!!!!!!!!!!!

adc internal atmega 8

unsigned int getadc(unsigned char channeladc)
{
unsigned int adcVal;
ADMUX=channeladc|0x40;   //avcc
ADCSRA|=(1<<ADEN);
ADCSRA|=(1<<ADSC);
loop_until_bit_is_clear(ADCSRA,ADSC);
adcVal = ADCW;
ADCSRA&=~(1<<ADEN);
return adcVal;
}

mantap adc atmega keren, itu yang paling di suka di atmega. dulu kalo mau rubah data dari analog ke digital harus pakai adc 0804, atau 0809.

adc 0804 dengan mode free runing kerenlah, mantap, tapi hanya 1 chanel. kalo pengen banyak chanel harus ditambahkan ic 4051, jadi nambah lagi ic. ya zaman dulu belum ada atmega.

kalo pegalaman adc 0809, memang dulu saya pernah nyoba dan sudah bisa jalan, tapi saya kapok kalo harus bikin clock 500Khz. permaasalahannya cuma di clocknya aja.

kalo sekarang mah udah ada adc internal, jadi ngga perlu ribet lagi.  pokoknya hidup adc 0804.

tulis baca eeprom internal atmega128

void writeeeprom(unsigned int alamat, unsigned char datana)
{
loop_until_bit_is_clear(EECR,EEWE);
EEAR = alamat;
EEDR = datana;
EECR |= (1<<EEMWE);
EECR |= (1<<EEWE);
}

unsigned char readeeprom(unsigned int alamat)
{
unsigned char dataeeprom;
loop_until_bit_is_clear(EECR,EEWE);
EEAR = alamat;
EECR |= (1<<EERE);
dataeeprom=EEDR;
return dataeeprom;
}

kalo datanya penting penting banget kalo bisa pakai eeprom eksternal lah. apalagi kalo penulisan ke eeepromnya minimal 1 menit sekali, itu mah udah harus pakai eksternal. sebuah ic yang fungsinya banyak lebih jelek daripada ic yang fungsinya  khusus untuk 1 pekerjaan.

itu perkataan siapa ya dulu, tapi memang benar. sudah terbukti.

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+0x40
#define SEND_OP_COND             1+0x40
#define READ_SINGLE_BLOCK        17+0x40
#define WRITE_SINGLE_BLOCK       24+0x40

#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 != 0x01);

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 != 0x00);

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!= 0x05)
{ // 0x05 = 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 != 0x00)
{
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(;;){;}
}
}
//===========================================