Presentation is loading. Please wait.

Presentation is loading. Please wait.

5. Serial Communication 충북인력개발원 전자과 공학박사 강원찬.

Similar presentations


Presentation on theme: "5. Serial Communication 충북인력개발원 전자과 공학박사 강원찬."— Presentation transcript:

1 5. Serial Communication 충북인력개발원 전자과 공학박사 강원찬

2

3 PORTA=J30(S/W &LED) PORTC=J19(LCD) USB=(Serial) [SW49 DIP=6,8 ON] ** USB 2.0 TO RS232 Cable 프로그램설치

4

5 PC-> Hyper Terminal 설치
[시작]-[프로그램]-[보조프로그램]-[통신]-[하이퍼터미널]

6 COM 5, 9600bps 8bit, no parity 하드웨어 흐름제어 사용않음 제어판에서 포트번호 확인

7 새 프로젝트 생성(File->New)

8

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: 9600 UCSR0A=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;

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); }; 예제1

11 #include <mega128. h> #include <stdio
#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: 9600 UCSR0A=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(); }

12 연습문제 1) 다음과 같이 PC화면을 나타내시오. 2) Switch 문을 사용하여 다음의 기능을 수행
========================= Wel come to My AVR World Press 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에 전송

13 -끝-

14 #include <mega128. h> #include <stdio. h> #include <lcd
#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; 

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);           }       }; }


Download ppt "5. Serial Communication 충북인력개발원 전자과 공학박사 강원찬."

Similar presentations


Ads by Google