Sie sind auf Seite 1von 6

Komunikasi Serial AVR dan LabView

Pada artikel ini saya hendak membagikan pengalaman bagaimana cara berkomunikasi serial
antara labview dengan Microcontroler AVR. Bagi rekan-rekan komunitas Microcontroller dan
LabView tentunya sudah memahami AVR microcontroller dan LabView. Disini akan saya bahas
apa saja yang diperlukan untuk bisa berkomunikasi antara AVR dan LabView. Langsung saja ke
tujuan dibawah ini.
Peralatan yang dibutuhkan adalah
Hardware :
1. AVR Development Kit
2. AVR Downloader
3. USB to Serial Cable

Software :
1. CVAVR (CodeVisionAVR Evaluation V3.04)
2. LabView (http://www.ni.com/trylabview/)

Front Panel LabView

Block Diagram LabView


Klik Show Untuk Membuka Source Code AVR
*****************************************************/
Chip type
: ATmega8535L
Program type
: Application
AVR Core Clock frequency: 11.059200 MHz
Memory model
: Small
External RAM size
:0
Data Stack size
: 128
*****************************************************/
#include <mega8535.h>
#include <stdio.h>
#include <delay.h>
unsigned char data;
// Standard Input/Output functions
#include <stdio.h>
// Declare your global variables here
//LCD Initiation
#define LCD_RS_HI PORTC|=(1<<7)
#define LCD_RS_LO PORTC&=~(1<<7)
#define LCD_EN_HI PORTC|=(1<<6)
#define LCD_EN_LO PORTC&=~(1<<6)
#define LCD_D4_HI PORTC|=(1<<5)
#define LCD_D4_LO PORTC&=~(1<<5)
#define LCD_D5_HI PORTC|=(1<<4)
#define LCD_D5_LO PORTC&=~(1<<4)
#define LCD_D6_HI PORTC|=(1<<3)
#define LCD_D6_LO PORTC&=~(1<<3)
#define LCD_D7_HI PORTC|=(1<<2)
#define LCD_D7_LO PORTC&=~(1<<2)
//==============================================
#define line1 0x00
#define line2 0x40
#define _ALTERNATE_PUTCHAR_
void LCD_STROBE()
{
LCD_EN_HI;
LCD_EN_LO;
}
void lcd_write(unsigned char datalcd)
{
if(datalcd & 0x80) {LCD_D7_HI;} else {LCD_D7_LO;}
if(datalcd & 0x40) {LCD_D6_HI;} else {LCD_D6_LO;}
if(datalcd & 0x20) {LCD_D5_HI;} else {LCD_D5_LO;}
if(datalcd & 0x10) {LCD_D4_HI;} else {LCD_D4_LO;}
LCD_STROBE();
if(datalcd & 0x08) {LCD_D7_HI;} else {LCD_D7_LO;}
if(datalcd & 0x04) {LCD_D6_HI;} else {LCD_D6_LO;}

if(datalcd & 0x02) {LCD_D5_HI;} else {LCD_D5_LO;}


if(datalcd & 0x01) {LCD_D4_HI;} else {LCD_D4_LO;}
LCD_STROBE();
delay_ms(5);
}
void putchar(unsigned char c)
{
LCD_RS_HI;
lcd_write(c);
}
//===========================================
void init_lcd()
{
LCD_RS_LO;
delay_ms(15);
LCD_D4_HI;
LCD_D5_HI;
LCD_STROBE();
delay_ms(5);
LCD_STROBE();
delay_us(100);
LCD_STROBE();
delay_ms(5);
LCD_D4_LO;
LCD_STROBE();
delay_us(40);
lcd_write(0x28);
lcd_write(0x0C);
lcd_write(0x06);
lcd_write(0x01);
delay_ms(10);
}
void lcd_goto(unsigned char poslcd)
{
LCD_RS_LO;
lcd_write(0x80 + poslcd);
}
void lcd_clrscr()
{
LCD_RS_LO;
lcd_write(0x1);
delay_ms(2);
}
void main(void)
{
// local variables PORT
PORTA=0x00;
DDRA=0xFF;
PORTB=0x00;
DDRB=0xFF;
//PORTC=0x00;

//DDRC=0x00;
DDRC=255;
init_lcd();
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 1386.900 kHz
// Mode: Ph. correct PWM top=0x00FF
// OC1A output: Non-Inv.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0xA1;
TCCR1B=0x0C;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=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=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x47;
lcd_goto(line1);
printf("1=ECONO; 2=POWER");
lcd_goto(line2);
printf(" 3=BY PASS ");
while (1)
{
data=getchar();
if(data=='1')
{
PORTA.6=0;

PORTB=0xF0;
lcd_clrscr();
lcd_goto(line1);
printf(" ECONO MODE");
}
if(data=='2')
{
PORTA.6=1;
PORTB=0x0F;
lcd_clrscr();
lcd_goto(line1);
printf(" POWER MODE");
}
else if(data=='3')
{
PORTA.6=0;
PORTB=0xFF;
lcd_clrscr();
lcd_goto(line1);
printf(" BY PASS MODE");
}
}
}

Das könnte Ihnen auch gefallen