MICROCONTROLLER USART
-
Author
rizkiyuniarattubani -
Category
Documents
-
view
43 -
download
12
Embed Size (px)
description
Transcript of MICROCONTROLLER USART
-
MICROCONTROLLERUSART
POLITEKNIK ELEKTRONIKA NEGERI SURABAYAPROGRAM STUDI ELEKTRO INDUSTRI
DEPARTEMEN TEKNIK ELEKTRO
-
Microcontroller D-III ELEKTRO INDUSTRI
Remote Monitoring
2
KonverterTTL-RS232
KonverterUSB-RS232
MCU
PLANT
PC
Sarana Transmisi(wire, wireless, optik
-
Microcontroller D-III ELEKTRO INDUSTRI
Convert to RS-232
3
-
Microcontroller D-III ELEKTRO INDUSTRI
Device with RS-232 Port
4
-
Microcontroller D-III ELEKTRO INDUSTRI
Feature USART
Full Duplex Operation (Independent Serial Receive and Transmit Registers)
Asynchronous or Synchronous Operation
Master or Slave Clocked Synchronous Operation
High Resolution Baud Rate Generator
Supports Serial Frames with 5, 6, 7, 8, or 9 Data Bits and 1 or 2 Stop Bits
Odd or Even Parity Generation and Parity Check Supported by Hardware
Data OverRun Detection
Framing Error Detection
Noise Filtering Includes False Start Bit Detection and Digital Low Pass Filter
Three Separate Interrupts on TX Complete, TX Data Register Empty, and RX Complete
Multi-processor Communication Mode
Double Speed Asynchronous Communication Mode
The ATmega128A has two USARTs, USART0 and USART1
-
Microcontroller D-III ELEKTRO INDUSTRI
Block Diagram
6
DATA
DATA
Other Device
-
Microcontroller D-III ELEKTRO INDUSTRI
Direction and Flow of Data
TTL Level from MCU
FLOW/DIRECTION OF DATA/bit
-
Microcontroller D-III ELEKTRO INDUSTRI
RS-232 Level
-
Microcontroller D-III ELEKTRO INDUSTRI
Pin-out RS-232
Converter TTL-RS232
TTLRS-232
-
Microcontroller D-III ELEKTRO INDUSTRI
Wiring Connection of RS-232
-
Microcontroller D-III ELEKTRO INDUSTRI
Wiring Connection Alternative
-
Microcontroller D-III ELEKTRO INDUSTRI
Format Frame of Data
1 start bit 5, 6, 7, 8, or 9 data bits no, even or odd parity bit 1 or 2 stop bits
-
Microcontroller D-III ELEKTRO INDUSTRI 13
-
Microcontroller D-III ELEKTRO INDUSTRI
Send DATA A
14
ASCII A= 100 0001B
Data length : 7 bitParity : ODDStop bit : 1 0100000111
Arah pengiriman data
START BIT
DATA PARITY
BITSTOP BIT
-
Microcontroller D-III ELEKTRO INDUSTRI
Error Check
A single bit is appended to each data chunkmakes the number of 1 bits even/odd
Example: even parity1000000(1)
1111101(0)
1001001(1)
Example: odd parity1000000(0)
1111101(1)
1001001(0)
Peven Parity bit using even parity
Podd Parity bit using odd paritydn Data bit n of the character
-
Microcontroller D-III ELEKTRO INDUSTRI
Parity Generators
-
Microcontroller D-III ELEKTRO INDUSTRI
Parity Checker
-
Microcontroller D-III ELEKTRO INDUSTRI
Register
18
-
Microcontroller D-III ELEKTRO INDUSTRI
UCSRnA Register
Bit 7 RXCn: USART Receive Complete
This flag bit is set when there are unread data in the receive buffer and
cleared when the receive buffer is empty (that is, does not contain any
unread data). If the receiver is disabled, the receive buffer will be flushed and
consequently the RXCn bit will become zero. The RXCn flag can be used to
generate a Receive Complete interrupt.
19
Bit 6 TXCn: USART Transmit Complete
This flag bit is set when the entire frame in the Transmit Shift Register has been
shifted out and there are no new data currently present in the transmit buffer (UDRn).
The TXCn flag bit is automatically cleared when a transmit complete interrupt is
executed, or it can be cleared by writing a one to its bit location. The TXCn flag can
generate a Transmit Complete interrupt (see description of the TXCIEn bit).
-
Microcontroller D-III ELEKTRO INDUSTRI
UCSRnA Register
Bit 5 UDREn: USART Data Register EmptyThe UDREn flag indicates if the transmit buffer (UDRn) is ready to receive new data. If UDREn isone, the buffer is empty, and therefore ready to be written. The UDREnflag can generate a DataRegister Empty interrupt (see description of the UDRIEn bit).UDREn is set after a reset to indicate that the Transmitter is ready.
Bit 4 FEn: Frame ErrorThis bit is set if the next character in the receive buffer had a Frame Error when received, that is,when the first stop bit of the next character in the receive buffer is zero. This bit is valid until thereceive buffer (UDRn) is read. The FEn bit is zero when the stop bit of received data is one.Always set this bit to zero when writing to UCSRnA.
20
-
Microcontroller D-III ELEKTRO INDUSTRI
UCSRnA Register
Bit 3 DORn: Data OverRunThis bit is set if a Data OverRun condition is detected. A data overrun occurs when the receivebuffer is full (two characters), it is a new character waiting in the Receive Shift Register, and anew start bit is detected. This bit is valid until the receive buffer (UDRn) is read. Always set thisbit to zero when writing to UCSRnA.
Bit 2 UPEn: Parity ErrorThis bit is set if the next character in the receive buffer had a Parity Error when received and theparity checking was enabled at that point (UPMn1 = 1). This bit is valid until the receive buffer(UDRn) is read. Always set this bit to zero when writing to UCSRnA.
21
-
Microcontroller D-III ELEKTRO INDUSTRI
UCSRnA Register
Bit 1 U2Xn: Double the USART Transmission SpeedThis bit only has effect for the asynchronous operation. Write this bit to zero when using synchronousoperation.Writing this bit to one will reduce the divisor of the baud rate divider from 16 to 8 effectively doublingthe transfer rate for asynchronous communication.
Bit 0 MPCMn: Multi-Processor Communication ModeThis bit enables the Multi-processor Communication mode. When the MPCMn bit is written toone, all the incoming frames received by the USART Receiver that do not contain address informationwill be ignored. The transmitter is unaffected by the MPCMnsetting.
22
-
Microcontroller D-III ELEKTRO INDUSTRI
USART Setup
23
// 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=0x98;UCSR0C=0x06;UBRR0H=0x00;UBRR0L=0x33;
-
Microcontroller D-III ELEKTRO INDUSTRI
USART with Interrupt
24
interrupt [USART0_RXC] void usart0_rx_isr(void){char status,data;status=UCSR0A;data=UDR0;if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
{rx_buffer0[rx_wr_index0++]=data;
#if RX_BUFFER_SIZE0 == 256// special case for receiver buffer size=256if (++rx_counter0 == 0) rx_buffer_overflow0=1;
#elseif (rx_wr_index0 == RX_BUFFER_SIZE0) rx_wr_index0=0;if (++rx_counter0 == RX_BUFFER_SIZE0)
{rx_counter0=0;rx_buffer_overflow0=1;}
#endif}
}
-
Microcontroller D-III ELEKTRO INDUSTRI
Instruction on C Microcontroller
The serial communication is realized using the Standard Input/Output Functions getchar, gets, scanf, putchar, puts and printf.The UART0 (USART0) will use the normal putcharand getchar functions.In case of interrupt driven buffered communication, UART0 (USART0) will use the following variables:
rx_buffer0, rx_wr_index0, rx_rd_index0, rx_counter0, rx_buffer_overflow0,tx_buffer0, tx_wr_index0, tx_rd_index0, tx_counter0.
25
-
Microcontroller D-III ELEKTRO INDUSTRI
Source Code C-Code Vision(examp)
Receive character from PC
Display on LCD
Send Character to PC
char data;While(1){
data=getchar();lcd_gotoxy(0,0);lcd_putchar(data);putchar(data);
}
adc_CH4=read_adc(4);hasil=(float)adc_CH4;hasil=hasil/5;sprintf(buff,"%2.3f ",hasil);lcd_gotoxy(0,0);lcd_puts(buff);printf("%2.3f",hasil);delay_ms(3000);
Read from ADC ch4 in hasil
Display on LCD
Send hasil to PC
26
-
Microcontroller D-III ELEKTRO INDUSTRI
Circuit on MCU
27
-
Microcontroller D-III ELEKTRO INDUSTRI
Source Code on MCU
28
while (1){tegangan=(float)read_adc(0)*250/1023;arus=(float)read_adc(1)*10/1023;lcd_gotoxy(1,0);lcd_putsf(" TELEMETRY ");lcd_gotoxy(0,1);lcd_putsf("TEGANGAN: ");lcd_gotoxy(0,2);lcd_putsf("ARUS : ");lcd_gotoxy(9,1);sprintf(buff,"%3.2f",tegangan);lcd_puts(buff);lcd_gotoxy(9,2);sprintf(buff,"%3.2f",arus);lcd_puts(buff); printf("%3.2f%3.2f\r",tegangan,arus);delay_ms(100);
}
float tegangan,arus;unsigned char buff[20];
-
Microcontroller D-III ELEKTRO INDUSTRI
Display on PC
29
-
Microcontroller D-III ELEKTRO INDUSTRI
Source Code on PC
30
Dim data As StringDim tegangan, arus As Variant
Private Sub Command1_Click()If rs232.PortOpen = False Then rs232.PortOpen = TrueEnd Sub
Private Sub Command2_Click()If rs232.PortOpen = True Then rs232.PortOpen = FalseEnd Sub
Private Sub Command3_Click()If rs232.PortOpen = True Then rs232.PortOpen = FalseUnload MeEnd Sub
Private Sub rs232_OnComm()data = data + rs232.InputIf InStr(data, vbCr) ThenText2.Text = Mid$(data, 1, 6)Text3.Text = Mid$(data, 7, 6)tegangan = Val(Text2.Text)arus = Val(Text3.Text)data = ""End If
End Sub
Private Sub Timer1_Timer()If tegangan >= 230 ThenShape1.FillColor = vbRedElseShape1.FillColor = vbGreenEnd IfEnd Sub
Private Sub Form_Load()With rs232.Settings = "9600,n,8,1".RThreshold = 1.InputLen = 1.CommPort = Text1.TextEnd WithEnd Sub