충북인력개발원 전자과 공학박사 강원찬 5. serial communication. up avr (atmega128) -...
TRANSCRIPT
충북인력개발원 전자과공학박사 강원찬
5. Serial Communication
uP AVR (Atmega128)
- 2 -
uP AVR (Atmega128)
- 3 -
PORTA=J30(S/W &LED)PORTC=J19(LCD)USB=(Serial)[SW49 DIP=6,8 ON]** USB 2.0 TO RS232 Cable 프로그램설치
uP AVR (Atmega128)
- 4 -
uP AVR (Atmega128)
- 5 -
PC-> Hyper Terminal 설치[ 시작 ]-[ 프로그램 ]-[ 보조프로그램 ]-[ 통신 ]-[ 하이퍼터미널 ]
uP AVR (Atmega128)
- 6 -
COM 5, 9600bps8bit, no parity하드웨어 흐름제어 사용않음
제어판에서 포트번호 확인
uP AVR (Atmega128)
- 7 -
새 프로젝트 생성 (File->New)
uP AVR (Atmega128)
- 8 -
uP AVR (Atmega128)
- 9 -
시리얼통신#include <mega128.h>#include <stdio.h> // 자동으로 추가된 부분void main(void){PORTA=0x00;DDRA=0x00; // USART0 initialization// Communication Parameters: 8 Data, 1 Stop, No Parity// USART0 Receiver: On// USART0 Transmitter: On// USART0 Mode: Asynchronous// USART0 Baud rate: 9600UCSR0A=0x00;UCSR0B=0x18;UCSR0C=0x06;UBRR0H=0x00;UBRR0L=0x2F;while (1) { };}
// USART0 initialization // Communication Parameters: 8 Data, 1 Stop, No Parity // USART0 Receiver: Off // USART0 Transmitter: On // USART0 Mode: Asynchronous // USART0 Baud rate: 19200 UCSR0A=0x00; UCSR0B=0x08; UCSR0C=0x06; UBRR0H=0x00; UBRR0L=0x33;
uP AVR (Atmega128)
- 10 -
예제 1#include <mega128.h>#include <stdio.h>void main(void){ int i=0;
PORTA=0x00;DDRA=0x00;
// Communication Parameters: 8 Data, 1 Stop, No Parity// USART0 Baud rate: 9600
UCSR0A=0x00;UCSR0B=0x18;UCSR0C=0x06;UBRR0H=0x00;UBRR0L=0x2F;
printf("Connect sucess!!\n\r");while (1) { if (PINA.0==0) // S/W1 을 누르면 PC 로 데이터 전송 { printf("Command[%3d]\n\r", i++) ; }
else { scanf("%c",&temp); printf("%c->ok",temp);
} };}
uP AVR (Atmega128)
- 11 -
#include <mega128.h>#include <stdio.h>char flash TITLE_LINE[] = "Chung-buk HRDI of KorCham";char print_back(void){ char cmd,c[5];
int ch,i;while((ch=getchar()) != '\r') /* 한문자를 입력받고 줄바꿈이 아니면 실행한다 .*/{ c[i++] = ch;
putchar(ch); } cmd = c[i-1];return(cmd);
} void main(void){ // USART0 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity// USART0 Baud rate: 9600UCSR0A=0x00;UCSR0B=0x18;UCSR0C=0x06;UBRR0H=0x00;UBRR0L=0x33; //67(16M)// Global enable interrupts#asm("sei")putsf(TITLE_LINE);printf("\n\rPress Keyboard\n\r");while (1)
{ char cmd;cmd = print_back(); }
}
uP AVR (Atmega128)
- 12 -
연습문제1) 다음과 같이 PC 화면을 나타내시오 .=========================
Wel come to My AVR WorldPress Any Key(a, s, w, z)
=========================
2) Switch 문을 사용하여 다음의 기능을 수행a: LED 출력 (0xfe), 처리결과 PC 에 전송s: LED 출력 (0xf0), 처리결과 PC 에 전송w: LED 출력 (0x0f), 처리결과 PC 에 전송z: LED 출력 (0xcc), 처리결과 PC 에 전송기타 : LED 출력 (0x00), 에러 PC 에 전송
uP AVR (Atmega128)
- 13 -
- 끝 -
uP AVR (Atmega128)
- 14 -
#include <mega128.h>#include <stdio.h> #include <lcd.h>char flash TITLE_LINE[] = "Chung-buk HRDI of KorCham"; #asm .equ __lcd_port=0x15 ;PORTC#endasm unsigned temp;void main(void){ int i=0; PORTA=0x00; DDRA=0x00;// Communication Parameters: 8 Data, 1 Stop, No Parity// USART0 Baud rate: 9600 UCSR0A=0x00; UCSR0B=0x18; UCSR0C=0x06; UBRR0H=0x00; UBRR0L=0x2F;
uP AVR (Atmega128)
- 15 -
lcd_init(16); lcd_gotoxy(0,0); lcd_putsf("hello serial\n"); printf("Connect sucess!!\n\r");while (1) { if (PINA.0==0) // S/W1 을 누르면 PC 로 데이터 전송 { printf("Command[%3d]\n\r", i++) ; PORTB=0xcc; } else { scanf("%c",&temp); printf("%c->ok\t",temp); } };}