Tutorial komunikasi Serial (Rx Tx /UART) pada ATMega 32 (Mikrokontroller) dengan menggunakan CV (CodeVision) AVR dan simulasi Proteus

Posted by

hai gaes....lama tak posting nih,, kemarin sudah posting tentang komunikasi dengan i2c silahkan baca kalau yang belum mengerti disini .nah sekarang kita akan bahas mengenai komuniakasi serial atmega 32. dalam ttorial kali ini menggunakan 2 buah atmega 32 .


langsung saja kita buka pertama software proteusnya dan cari komponen dibawah ini:
setelah memilih komponen tersebut maka kita buat rangkaian seperti dibawah ini untuk transmitternya (pengirim ):

 setelah berhasil dengan transmitter kita buat rangkaian receivernya atau penerimannya :
 jika keduanya digabung maka akan tampak seperti dibawah ini.. ingat RX transmiter bertemu Tx receiver dan begituula sebaliknya.

 setelah selesai mari kita buka codevision avrnya bro...
 buat project seperti postingan sebelumnya dan setting bagian uart untuk transmitter sebagai berikut :
 setelah selesai generate save dan exit ya..trus kita program seperti dibawah ini:

#include <mega32.h>
#include <delay.h>
#include <stdio.h>
#include <stdlib.h>

// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>

// Standard Input/Output functions
#include <stdio.h>

#include <delay.h>

#define ADC_VREF_TYPE 0x20

// Read the 8 most significant bits
// of the AD conversion result
unsigned char read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCH;
}

// Declare your global variables here
int a,b,c;
char ag[20];

void rutin(void)
{
    while(1)
    {
    a=150;
    b=200;
    c=50;
    if(PINB.0==0&&PINB.1==1&&PINB.2==1)
    {
        lcd_clear();
        lcd_gotoxy(0,1);
        sprintf(ag, "%d",a);
        lcd_puts(ag);
        putchar  (a);
   
    }
    if(PINB.0==1&&PINB.1==0&&PINB.2==1)
    {
        lcd_clear();
        lcd_gotoxy(0,1);
        sprintf(ag, "%d",b);
        lcd_puts(ag);
        putchar(b);
    }
    if(PINB.0==1&&PINB.1==1&&PINB.2==0)
    {
        lcd_clear();
        lcd_gotoxy(0,1);
        sprintf(ag, "%d",c);
        lcd_puts(ag);
        putchar(c);
    }
    }
}

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0xFF;
DDRB=0x00;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x08;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x4D;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC Clock frequency: 750.000 kHz
// ADC Voltage Reference: AREF pin
// Only the 8 most significant bits of
// the AD conversion result are used
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x84;

// LCD module initialization
lcd_init(16);

while (1)
      {
      // Place your code here
       rutin();
     
      };
}
setelah selesai maka build
sekarang kita buat program file baru untuk penerimannya.. yaitu sebagai berikut settingnya :

save generate dan exit ya,, setelah itu program seperti dibawah ini :

#include <mega32.h>
#include <delay.h>
#include <stdlib.h>
#include <stdio.h>
char data;
// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>
#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
// USART Receiver buffer
#define RX_BUFFER_SIZE 2
char rx_buffer[RX_BUFFER_SIZE];
#if RX_BUFFER_SIZE<256
unsigned char rx_wr_index,rx_rd_index,rx_counter;
#else
unsigned int rx_wr_index,rx_rd_index,rx_counter;
#endif                                                
// This flag is set on USART Receiver buffer overflow
bit rx_buffer_overflow;                                                                
// USART Receiver interrupt service routine

int data1, data2, data3, data4, data5;

interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
  if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
 {          
   if(data == 0 )rx_wr_index=0;  
   else
   {
       rx_buffer[rx_wr_index++]=data;
       data1  = rx_buffer[0];  //                  
       data2  = rx_buffer[1]; //                            
      // data3  = rx_buffer[2];                                                    
      // data4  = rx_buffer[3];                    
//       data5  = rx_buffer[4];  //
       if (rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
   }
 }
}
#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART Receiver buffer---
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter==0);
data=rx_buffer[rx_rd_index];
if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#asm("cli")
--rx_counter;
#asm("sei")
return data;
}
#pragma used-
#endif
// Standard Input/Output functions
#include <stdio.h>
// Declare your global variables here
int b;
char screen[20];


void tampil(void)
{
    lcd_clear();
    lcd_gotoxy(3,0);
    sprintf(screen, "TEST");
    lcd_puts(screen);
    lcd_gotoxy(0,1);
    sprintf(screen, "%3d", data1);
    lcd_puts(screen);
    lcd_gotoxy(4,1);
    sprintf(screen, "%3d", data2);
    lcd_puts(screen);
    lcd_gotoxy(4,3);

}

void gabung_data()
{
    int a;
    a=data1<<8;
    b=a|data2;
    tampil();
}

void rutine(void)
{
    gabung_data();
 
}

void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0xFF;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: Off
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x90;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x4D;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// LCD module initialization
lcd_init(16);
// Global enable interrupts
#asm("sei")
while (1)
      {
           rutine();
      };
}

build juga file tersebut

setelah itu buka kembali file proteusnya. dan masukan HEX dikedua mikrokontroller setelah itu jalankan program simulasi pada proteus dan hasilnya sebagai berikut :
ini adalah tampilan awal sebelum tombol di tekan :
 setelah tombol satu ditekan maka mikrokontroller pengirim akan mengirimkan data bernilai 150 dan mikrokontroler b akan menerima dan menampilkannya di LCD
 untuk tombol B maka nilai yang dikirim bernilai 200:


selesai deh tutorial kali ini,,, program dapat dikembangkan sendiri ya :)


FOLLOW and JOIN to Get Update!

Social Media Widget SM Widgets




kelaselektronika Updated at: 22:38

1 comments: