sehingga mikrokontroler pada proteus dapat berkomunikasi Serial Port pada PC. Com pada mikro misalnya kita setting COM4 sedangkan pada PC kita setting COM3. Agar COM 3 dan COM dapat berkomunikasi yaitu menggunakan program “virtual serial Port”. Pada tampilan vb yaitu menggunakan scrollbar, nilai dari scrollbar ini dikirim melalui serial yaitu menggunakan mscomm.
Pada mikrokontroler yaitu menggunakan AVR ATMega16. Menggunakan fasilitas interupt serial receive. Jadi saat ada data masuk melalui serial maka otomatis AVR akan menjalankan program yang ada pada subrutin USART Receiver interrupt. Berikut setting programnya pada codevision AVR :
#include
#include
#include
unsigned char cek,input; char lcd_buffer[33];
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0×15 ;PORTC
#endasm
#include
#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<#define PARITY_ERROR (1<#define DATA_OVERRUN (1<#define DATA_REGISTER_EMPTY (1<#define RX_COMPLETE (1<
// USART Receiver buffer
#define RX_BUFFER_SIZE 8
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
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
{
rx_buffer[rx_wr_index]=data;
if (++rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
if (++rx_counter == RX_BUFFER_SIZE)
{
rx_counter=0;
rx_buffer_overflow=1;
};
PORTD.2=0; PORTD.3=1; OCR0=data; //Data serial untuk mengatur //kecepatan motor
lcd_clear();
lcd_gotoxy(0,0);
sprintf(lcd_buffer,”Data Receive:%d”,data);
lcd_puts(lcd_buffer);
lcd_gotoxy(0,1);
if(OCR0==0){lcd_putsf(“Motor Mati “);}
else if(OCR0>0 && OCR0<150){lcd_putsf(“Motor pelan”);}
else{lcd_putsf(“Motor Cepat”);}
};
}
#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
// Declare your global variables here
void main(void)
{ unsigned char data_rx[25],input,sip;
unsigned char i,j,k,l; //Declare your local variables here
PORTA=0×00;
DDRA=0×00;
PORTB=0×00;
DDRB=0×08;
PORTC=0×00;
DDRC=0×00;
PORTD=0×00;
DDRD=0xff;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 43.200 kHz
// Mode: Fast PWM top=FFh
// OC0 output: Non-Inverted PWM
TCCR0=0x6D;
TCNT0=0×00;
OCR0=0×00;
TCCR1A=0×00;
TCCR1B=0×00;
TCNT1H=0×00;
TCNT1L=0×00;
ICR1H=0×00;
ICR1L=0×00;
OCR1AH=0×00;
OCR1AL=0×00;
OCR1BH=0×00;
OCR1BL=0×00;
ASSR=0×00;
TCCR2=0×00;
TCNT2=0×00;
OCR2=0×00;
MCUCR=0×00;
MCUCSR=0×00;
TIMSK=0×00;
UCSRA=0×00;
UCSRB=0×98;
UCSRC=0×86;
UBRRH=0×00;
UBRRL=0×47;
ACSR=0×80;
SFIOR=0×00;
// LCD module initialization
lcd_init(16);
// Global enable interrupts
#asm(“sei”)
while (1)
{
};
}
Private Sub Form_Load()
Text1 = 0
MSComm1.PortOpen = True
End Sub
Private Sub out_Change()
Dim nilai As Integer
nilai = out.Value
If nilai > 0 And nilai < 125 Then
Text1.BackColor = vbGreen
ElseIf nilai = 125 Then
Text1.BackColor = vbYellow
ElseIf nilai > 175 And nilai < 255 Then
Text1.BackColor = vbRed
End If
Text1 = nilai
MSComm1.Output = “” + Chr$(nilai)
End Sub
Tidak ada komentar:
Posting Komentar
Demi Sempurnanya ^ ^