Download presentation
Presentation is loading. Please wait.
1
Chap.6.3 UART
2
UART Universal Asynchronous Receiver/Transmitter USART
a piece of computer hardware that translates data between parallel and serial forms parallel : inside computer serial : serial communication USART universal synchronous/asynchronous receiver/transmitter RS-232C protocol Baud Rate or bps : bits per second stop bit : 1, 1.5, 2 bit parity : error detecting code data length : number of data bits in a frame
3
USART in ATmega128 – Block Diagram
: Register : Pin-Out
4
USART in ATmega128 Frame Formats : 1 start bit 5,6,7,8 or 9 data bits
no, even or odd parity bit 1 or 2 stop bits
5
USART in ATmega128 Dual USART USART0 USART1
6
USART Register 1 data register, 3 control registers, 2 baud rate registers UDRn - USARTn I/O Data Register (n=0 or 1) C programming (if USART0 is used) when transmit, UDR0 = data; when received, data = UDR0;
7
UCSRnA :USART Control and Status Register A
RXCn : USART Receive Complete set (=‘1’) when there are unread data in the receive buffer TXCn: USART Transmit Complete UDREn: USART Data Register Empty set (=‘1’) when the buffer is empty and therefore ready to be written FEn: Frame Error DORn: Data OverRun UPEn: Parity Error U2Xn: Double the USART Transmission Speed MPCMn: Multi-Processor Communication Mode
8
UCSRnB :USARTn Control and Status Register B
RXCIEn: RX Complete Interrupt Enable TXCIEn: TX Complete Interrupt Enable UDRIEn: USART Data Register Empty Interrupt Enable RXENn: Receiver Enable set(=‘1’) then receiver is enabled TXENn: Transmitter Enable set(=‘1’) then transmitter is enabled UCSZn2: Character Size sets the number of data bits combined with UCSZn1:0 bit in UCSRnC RXB8n: Receive Data Bit 8 TXB8n: Transmit Data Bit 8
9
UCSRnC :USARTn Control and Status Register C
UMSELn: USART Mode Select asynchronous (=‘0’), synchronous (=‘1’) UPMn1:0: Parity Mode USBSn: Stop Bit Select 1 stop bit (=‘0’), 2 stop bits (=‘1’) UCPOLn: Clock Polarity
10
UCSRnC :USARTn Control and Status Register C
UCSZn1:0: Character Size UCSZn2: Character Size (in UCSRnB )
11
UBRRnL /UBRRnH - USART Baud Rate Registers
UBRRn[11:0]: USARTn Baud Rate Register % fosc = system clock freq.
12
Register Addresses and Pin-Out for USART
Address/Pin-Out USART1 UDR0 UDR1 UCSR0A UCSR1A UCSR0B UCSR1B UCSR0C UCSR1C UBRR0L UBRR1L UBRR0H UBRR1H RXD0 RXD1 TXD0 TXD1 XCK0 XCK1
13
USART Demo Program USART 통신 하이퍼 터미널 Demo program Key Board
14
USART 초기화 Port 설정 USART1 사용 DDRD의 3 bit 는 출력/2 bit 는 입력)
Baud Rate = (fosc = MHz) No parity UPM1[1:0 ]= 0 0 1 Stop bit USBS1 = 0 8 data bits UCSZ1[2:0] = 0 1 1
15
USART 초기화 - Register Setting
bit 7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 DDRD UCSR1A UCSR1B UCSR1C UBRR1H UBRR1L
16
USART 초기화 program DDRD &= 0xFB; DDRD |= 0x08; UCSR1A = 0x00; UCSR1B = 0x18; UCSR1C = 0x06; UBRR1H = 0x00; UBRR1L = 0x03;
17
Transmit / Receive function
void txd_char(unsigned char data){ while( (UCSR1A & 0x20) == 0 ); UDR1 = data; } unsigned char rxd_char(){ unsigned char data; while( (UCSR1A & 0x80) == 0 ); data = UDR1; return data;
18
USART demo program #include <avr/io.h> /* txd_char(), rxd_char()의 선언 및 정의 */ int main() { unsigned char text[]=“\r\n Micro-computer USART 1 Demo \r\n”; unsigned char echo[]=“ECHO>>”; unsigned char i=0; /* 초기화 code */ while(text[i] != ‘\0’) txd_char(text[i++]); i = 0; while(echo[i] != ‘\0’) txd_char(echo[i++]); while(1) txd_char(rxd_char()); return 0; }
19
USART demo program 실행 방법
프로그램 컴파일 & 다운로드 ISP Program 창 닫기 AVR-ISP 스위치를 UART로 변경 하이퍼터미널 실행 및 설정 시작 -> 프로그램 -> 보조프로그램 -> 통신 -> 하이퍼터미널
20
실습 과제 #1. USART0를 이용하여 demo program을 작성하시오.
#2. #include <avr/io.h>를 제거하시오. #3. 다음의 기능을 수행하는 program을 작성하시오. ‘r’ key : LED off; ‘i’ key : LED 증가 /* 00 -> 01 -> 10 -> 11 -> 00 … */ ‘d’ key : LED 감소 /* 00 -> 11 -> 10 -> 01 -> 00 -> 11 … */ ‘s’ key : LED on;
21
Reference Code /* Register 정의 */ /* txd_char(), rxd_char()의 선언 및 정의 */
int main() { /* 변수 선언 */ /* 초기화 code */ while(1) { cmd = rxd_char(); switch (cmd){ /* key 입력에 따른 작업 수행 */ } return 0;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.