IT_etc/dk-128 (ATmega128)

[스크랩] 8-2. avr 직렬 양방향 통신

JJun ™ 2009. 8. 25. 15:58

< avr양방향_하이퍼 >

#include <avr/io.h>

 

#define CPU_CLOCK 16000000

#define BAUD_RATE 19200

#define BAUD_RATE_L (CPU_CLOCK /(16l*BAUD_RATE))-1

#define BAUD_RATE_H ((CPU_CLOCK /(16l*BAUD_RATE))-1)>>8

 

void USART_Transmit(unsigned char ch);

char USART_Receive(void);

 

int main(void)

{

         DDRF=0xFF;

         PORTF=0xFF;

 

         DDRE=0xFF;

         PORTE=0xFF;

 

         char ch;

 

         UBRR1L = (unsigned char)BAUD_RATE_L;

         UBRR1H = (unsigned char)BAUD_RATE_H;

 

         UCSR1C = (0<<UPM1) | (0<<UPM0) | (0<<USBS) | (1<<UCSZ1) | (1<<UCSZ0);

         UCSR1B = (1<<TXEN) | (1<<RXEN) | (0<<UCSZ2);

 

         while(1)

         {

                  ch=USART_Receive();

                  PORTE = ((ch-48)<<4)|(ch-48);

                  PORTF=(ch-48)^0xFF;

                  USART_Transmit(ch);

                  asm("NOP");

         }

         return 1;

}

 

void USART_Transmit(unsigned char ch)

{

         while(!(UCSR1A & (1 << UDRE)));

         UDR1 = ch;

}

 

char USART_Receive(void)

{

         while(!(UCSR1A & (1<<RXC)) );

          return UDR1;

}

===================================================================================

< avr양방향통신_콘솔 >

#include <windows.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

#pragma comment(lib, "User32.lib")

 

int main(void)

{

         charszPort[15];

         charreadbuf[10];

         charsendbuf[1];

         char ch;

         HANDLEm_handle = NULL;

         DCBdcb;

         OVERLAPPEDosWrite, osRead;

 

         wsprintf(szPort, "COM%d", 1);

 

         m_handle = CreateFile(szPort,

                                        GENERIC_READ | GENERIC_WRITE,

                                        0,

                                        NULL,

                                        OPEN_EXISTING,

                                        FILE_ATTRIBUTE_NORMAL,

                                        NULL);

 

         if(m_handle == INVALID_HANDLE_VALUE)

         {

                  printf("핸들값이 올바르지 않습니다.\n");

                  return 1;

         }

 

         dcb.DCBlength = sizeof(DCB);

 

         GetCommState(m_handle, &dcb);

 

         dcb.BaudRate =19200;

         dcb.ByteSize =8;

         dcb.Parity =0;

         dcb.StopBits =0;

 

         SetCommState(m_handle, &dcb);

 

         osWrite.Offset=osRead.Offset= 0;

         osWrite.OffsetHigh=osRead.OffsetHigh= 0;

         osWrite.hEvent=osRead.hEvent= CreateEvent(NULL, TRUE, FALSE, NULL);

 

         while(1)

         {

                  printf("숫자를 입력하세요 : ");

                  //memset(sendbuf, '\0', sizeof(sendbuf));

                  fgets(readbuf,sizeof(readbuf),stdin);

                  ch=readbuf[0];

 

                  WriteFile(m_handle, (CONST void far *)&ch, sizeof(ch), NULL, &osWrite);

                  printf("Send to DK128 : %c\n", ch);

 

                  //memset(readbuf, '\0', sizeof(readbuf));

                  ReadFile(m_handle, (void far *)&ch, sizeof(ch), NULL, &osRead);

                  printf("Recv from DK128 : %c\n", ch);

         }

         CloseHandle(m_handle);

         return 0;

}

출처 : 단 한번의 선택이라도 후회없이 하자!!
글쓴이 : 꼬리치는늑대 원글보기
메모 :