contoh program serial pakai avr gcc

6


#define F_CPU 11059200UL
#include “util/delay.h”
#include “stdlib.h”
#include “avr/io.h”
#include “avr/pgmspace.h”
#include “avr/interrupt.h”
#include “avr/wdt.h”
//============================================================
#define     WDTO_2S   7

#define UART_BAUD_RATE 9600
#define UART_BAUD_CALC(UART_BAUD_RATE,F_CPU) ((F_CPU)/((UART_BAUD_RATE)*16l)-1)

//================================================================SERIAL
void initserial()
{
UBRRH=(uint8_t) (UART_BAUD_CALC(UART_BAUD_RATE,F_CPU)>>8);
UBRRL=(uint8_t) UART_BAUD_CALC(UART_BAUD_RATE,F_CPU);
UCSRB = (1<<RXEN)|(1<<TXEN);
UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);
}

void uart_putc(unsigned char c)
{
while(!(UCSRA & (1 << UDRE)));
UDR = c;
}

void uart_puts (char *s) {
while (*s) {
uart_putc(*s);
s++;
}
}
//=================================================
int main()
{
unsigned char tempserial;
initserial();
DDRB=255;
uart_puts(“hello serial”);
uart_putc(13);
uart_putc(10);

for(;;)
{
if (bit_is_set(UCSRA,RXC))
{
tempserial=UDR;
PORTB=tempserial;
}

}
}

crysta yang dipakai 11.0592MHz, baud rate 9600, data yang diterima mikro dari hyperterminal, akan dikeluarkan ke led pada PORTB.

jika anda membutuhkan kursus mikrokontroler, hubungi 085228292473

3 thoughts on “contoh program serial pakai avr gcc”

Tinggalkan Balasan

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

Logo WordPress.com

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

Gambar Twitter

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

Foto Facebook

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

Foto Google+

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

Connecting to %s