Lompat ke konten Lompat ke sidebar Lompat ke footer

Komunikasi PC dengan USART AVR

Suatu waktu mungkin kita akan membutuhkan beberapa mikrokontroler untuk saling berkomunikasi atau berkomunikasi dengan dengan perangkat lain seperti PC dalam perancangan sistem elektronika. USART bisa dijadikan salah satu pilihannya.
Penggunaan USART untuk komunikasi antar mikrokontroler adalah dengan menghubungkan pin RX pada TX lawan dan pin TX pada RX lawan. Begitu pula jika mikrokontroler berkomunikasi dengan PC. Akan tetapi, karena level teganan pada PC berbeda dengan mikrokontroler maka dibutuhkan suatu rangkaian untuk menyesuaikannya. Biasanya digunakan rangkaian standar RS232.
EXAMPLE
Deklarasi library
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdint.h>
Library bisa ditambahka sesuai kebutuhan penggunaan.
___________________________________________________________________
Fungsi Inisialisasi USART
void USART_Init( )
{
// Set baud rate
// Operating Mode Asynchronous Normal mode (U2X = 0)
// Equation for Calculating UBRR Value UBRR = (fosc/16BAUD) – 1
//fosc 4MHz UBRR = 25 Desimal = 19 Hex
UBRRH=0×00;
UBRRL=0×19;
//UCSRA
// Bit 7 – RXC: USART Receive Complete 0
// Bit 6 – TXC: USART Transmit Complete 0
// Bit 5 – UDRE: USART Data Register Empty 0
// Bit 4 – FE: Frame Error 0
// Bit 3 – DOR: Data OverRun 0
// Bit 2 – PE: Parity Error 0
// Bit 1 – U2X: Double the USART transmission speed 0
// Bit 0 – MPCM: Multi-processor Communication Mode 0
UCSRA=0×00;
//UCSRB
// Bit 7 – RXCIE: RX Complete Interrupt Enable 0
// Bit 6 – TXCIE: TX Complete Interrupt Enable 0
// Bit 5 – UDRIE: USART Data Register Empty Interrupt Enable 0
// Bit 4 – RXEN: Receiver Enable 1
// Bit 3 – TXEN: Transmitter Enable 1
// Bit 2 – UCSZ2: Character Size 0
// Bit 1 – RXB8: Receive Data Bit-8  0
// Bit 0 – TXB8: Transmit Data Bit-8 0
UCSRB=0×18;
//UCSRC
// Bit 7 – URSEL: Register Select 1
// Bit 6 – UMSEL: USART Mode Select 0 Asynchronous Operation
// Bit 5:4 – UPM1:0: Parity Mode 0|:0 Disabled
// Bit 3 – USBS: Stop Bit Select 0 1-bit
// Bit 2:1 – UCSZ1:0: Character Size 1:1 8-bit
// Bit 0 – UCPOL: Clock Polarity 0
UCSRC=0×86;
}
__________________________________________________________________________________
 Fungsi USART  RECEIVE
unsigned char USART_Receive( void )
{
// Wait for data to be received
while ( !(UCSRA & (1<<RXC)) );
// Get and return received data from buffer
return UDR;
}
__________________________________________________________________________________
Fungsi USART TRANSMIT
void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) );
/* Put data into buffer, sends the data */
UDR = data;
}
__________________________________________________________________________________
Pseudo code komunikasi USART
#include library
void inisialisasi_USART(){…}
unsigned char USART_receive(){…}
void USART_transmit(){…}
int main(void)
{
inisialisasi_USART();
unsigned char data_terima = USART_receive();
unsigned char data_kirim;
USART_Transmit( data_kirim )
}
Daftar Pustaka:
Datasheet atmega8, Atmel Corporation, http://www.atmel.com
// tambahan :
utk program yg terhubung dg hyperterminal PC. Mikro menerima data lalu mengirim data yg sama ke PC. Utk pengaturan master slavenya bisa disesuaikan:
#include
#include
#include
#include
void USART_Init( )
{
// Set baud rate
// Operating Mode Asynchronous Normal mode (U2X = 0)
// Equation for Calculating UBRR Value UBRR = (fosc/16BAUD) – 1
//fosc 4MHz UBRR = 25 Desimal = 19 Hex
UBRRH=0×00;
UBRRL=0×19;
//UCSRA
// Bit 7 – RXC: USART Receive Complete 0
// Bit 6 – TXC: USART Transmit Complete 0
// Bit 5 – UDRE: USART Data Register Empty 0
// Bit 4 – FE: Frame Error 0
// Bit 3 – DOR: Data OverRun 0
// Bit 2 – PE: Parity Error 0
// Bit 1 – U2X: Double the USART transmission speed 0
// Bit 0 – MPCM: Multi-processor Communication Mode 0
UCSRA=0×00;
//UCSRB
// Bit 7 – RXCIE: RX Complete Interrupt Enable 0
// Bit 6 – TXCIE: TX Complete Interrupt Enable 0
// Bit 5 – UDRIE: USART Data Register Empty Interrupt Enable 0
// Bit 4 – RXEN: Receiver Enable 1
// Bit 3 – TXEN: Transmitter Enable 1
// Bit 2 – UCSZ2: Character Size 0
// Bit 1 – RXB8: Receive Data Bit-8 0
// Bit 0 – TXB8: Transmit Data Bit-8 0
UCSRB=0×18;
//UCSRC
// Bit 7 – URSEL: Register Select 1
// Bit 6 – UMSEL: USART Mode Select 0 Asynchronous Operation
// Bit 5:4 – UPM1:0: Parity Mode 0|:0 Disabled
// Bit 3 – USBS: Stop Bit Select 0 1-bit
// Bit 2:1 – UCSZ1:0: Character Size 1:1 8-bit
// Bit 0 – UCPOL: Clock Polarity 0
UCSRC=0×86;
}
unsigned char USART_Receive( void )
{
// Wait for data to be received
while ( !(UCSRA & (1<<RXC)) );
// Get and return received data from buffer
return UDR;
}
void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) );
/* Put data into buffer, sends the data */
UDR = data;
}
int main(void)
{
unsigned char data_buff;
USART_Init( );
while(1)
{
data_buff = USART_Receive();
USART_Transmit(data_buff );
}
return 1;
}

Posting Komentar untuk "Komunikasi PC dengan USART AVR"

Profit Blogger
INCOME BLOGGER