'AVR'에 해당되는 글 1건

  1. 2006.10.18 AVR(ATmega128) 에서 간단한 UART 통신 예제
2가지 방법
- 인터럽트로 받기
- 그냥 받기 -_-;

#include <iom128.h>
#include <ina90.h>
#include <sig-avr.h>
#include <interrupt.h>

#define SYSTEM_CLOCK 8000000 // CLOCK (X-tal frequency) (128L : 8Mhz)

typedef unsigned char BYTE;

volatile BYTE RS0_Char=0x00;
volatile BYTE RS1_Char=0x00;

void SendByte0(BYTE data)
{
while (!(inp(UCSR0A) & (1<<UDRE)));
outp(data, UDR0);
}

void SendByte1(BYTE data)
{
while (!(inp(UCSR1A) & (1<<UDRE)));
outp(data, UDR1);
}


SIGNAL(SIG_UART0_RECV) // UART0 수신 인터럽트
{
RS0_Char=inp(UDR0);
SendByte0(RS0_Char);
}

SIGNAL(SIG_UART1_RECV) // UART1 수신 인터럽트
{
RS1_Char=inp(UDR1);
SendByte0(RS1_Char);
}



void UART_Init(unsigned long BaudRate)
{
// Not Double mode, Not multi_communication
outp(0x00, UCSR0A);
// 0b 1001 1000 RXCIE,TXCIE,UDRIE,RxEN,TxEN,xxx
outp(0x98, UCSR0B);
outp(0x06, UCSR0C);
// Setting BaudRate
outp(0x00, UBRR0H);
outp((SYSTEM_CLOCK/BaudRate/16 - 1),UBRR0L);

// Not Double mode, Not multi_communication
outp(0x00, UCSR1A);
// 0b 1001 1000 RXCIE,TXCIE,UDRIE,RxEN,TxEN,xxx
outp(0x98, UCSR1B);
outp(0x06, UCSR1C);
// Setting BaudRate
outp(0x00, UBRR1H);
outp((SYSTEM_CLOCK/BaudRate/16 - 1),UBRR1L);
}

int main(void)
{
// External Memory Disable
outp(0x00, MCUCR);
UART_Init(9600);
sei();

while(1);

return 0;
}


uart0이나 uart1이 수신인터럽트가 발생할때마다 다시 각각 uart0/1에 같은 내용을 바이트단위로 전송해준다.

지금 사용중인 보드의 제조사 http://myavr.co.kr 의 자료를 이해하느라 아주 조금 수정했다 ;
Posted by freezn