Mengambil Perintah dan Parameter Setting pd AVR microcontroller melalui Komunikasi serial rs232 (bag 2/2)

Sekarang kita akan mencoba langsung pada microcontroller dengan program c codevision,  pada artikel bagian 1 sebelumnya kita sudah mencoba simulasi pada program di PC.

Hubungan Kabel PC dan Microcontroller

mengirim nilai ke microcontroller

Membuat Program di PC , untuk pengiriman ke Microcontroller : 

Buatlah form spt gambnar dibawah ini:

Form mengirim nilai parameter

Kode program dgn vb6:

'Buka port serial:
Private Sub cmdOpen_Click()

If MSComm1.PortOpen = False Then
   MSComm1.Settings = "9600,N,8,1"
   MSComm1.CommPort = 1
   MSComm1.PortOpen = True
 End If
End Sub
'kirim perintah dan nilai parameter

Private Sub cmdSend_Click()

MSComm1.Output = Trim(txtperintah.Text) & "=" & Trim(txtnilaiParameter.Text) & "x"

”diakhiri dgn ‘x’.

End Sub

‘tutup port jika sudah selesai

Private Sub cmdClose_Click()

tmrtransferinterval.Enabled = False
tmrTimeout.Enabled = False
If MSComm1.PortOpen = False Then
Exit Sub
End If
MSComm1.PortOpen = False
End Sub

Sekarang kita akan mencoba langsung pada microcontroller dengan program c codevision,  pada artikel bagian 1 sebelumnya kita sudah mencoba simulasi pada program di PC.

 
Program C dgn codevision untuk microcontroler Atmega8535 

/*****************************************************
Date    : 6/30/2011
Author  : PCCONTROL.WORDPRESS.COM
Comments: PEMISAHAN PERINTAH DAN NILAI PARAMETER SETTING

Chip type           : ATmega8535
Program type        : Application
Clock frequency     : 4.000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 256
*****************************************************/

#include <mega8535.h>

// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>
#include <string.h>
#include <stdio.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 30
unsigned char rx_buffer[RX_BUFFER_SIZE];
unsigned char tampung1[30];
unsigned char tampung2[30];  

unsigned char r_ready;

unsigned char rx_wr_index;

// 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)
   {
     if (data != 'x')
       {

        rx_buffer[rx_wr_index]=data;
        rx_wr_index++;
        }
        else
        {
         rx_buffer[rx_wr_index]='';
         rx_wr_index=0;
         r_ready=1;                  //signal cmd processor
         UCSRB.7=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
 char *daftar_perintah[] = {"PANJANG", "LEBAR", "TINGGI"};
#define PANJANG 0
#define LEBAR 1
#define TINGGI 2
int panjang,lebar,tinggi;
// Declare your global variables here

  void ambil_parameter1();

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

// 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=0x98;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x19;

// LCD module initialization
lcd_init(16);
//lcd_gotoxy(0,0);
//lcd_putsf("data diterima:");
// Global enable interrupts
#asm("sei")

 r_ready=0;
 rx_wr_index=0;
 UCSRB.7=1;  

while (1)
      {
      // Place your code here

        //jika ada data di buffer serial comm ambil parameter
        if(r_ready==1)  //==============
           {
            lcd_clear();
            lcd_gotoxy(0,0);
            lcd_puts(rx_buffer);  //tampilkan di baris pertama LCD

            ambil_parameter1();
           } 

      }; //end of while(1)
}

void ambil_parameter1() { 

   int int_param, i;

   char cmd;

   char Str_perintah[50];

   char param[50];

   char *batas;

   int IndexBatas;  

   if (r_ready != 1) return;

    batas = strchr(rx_buffer,'=');
     if (batas != NULL)
        {
        IndexBatas = batas - rx_buffer;        // jumlah command
        strncpy(Str_perintah, rx_buffer, IndexBatas);
        Str_perintah[IndexBatas] = '';
        strcpy(param, batas + 1);
        }
     else
     {
     // tidak ada nilai parameter , copy perintah
      strcpy(Str_perintah, rx_buffer);
     //isi parameter dgn 0 , untuk replace nilai sebelumnya
      sprintf(param,"%i",0);
      }

    // cmd = -1  jika perintah yg dikirim PC tdk ada di daftar.

      cmd = -1;

      for (i = 0; i < 3; i++)
        {
         if (strcmp(Str_perintah, daftar_perintah[i])==0)
          { cmd = i;
            break;
          }
        } 

        lcd_gotoxy(0,1);
        lcd_putsf("cmd=");
        lcd_gotoxy(4,1);
        sprintf(tampung2,"%i",cmd);
        lcd_puts(tampung2);

        switch(cmd) {

        case PANJANG:     //time =0
        lcd_gotoxy(6,1);
        lcd_putsf("par=");
         sscanf(param, "%d", &panjang);
         sprintf(tampung1,"%i",panjang);
         lcd_gotoxy(10,1);
         lcd_puts(tampung1);

        break;

        case LEBAR:
         lcd_gotoxy(6,1);
        lcd_putsf("par=");
        sscanf(param, "%d", &lebar);
        sprintf(tampung1,"%i",lebar);
        lcd_gotoxy(10,1);
        lcd_puts(tampung1);
        break;

        case TINGGI:
         lcd_gotoxy(6,1);
        lcd_putsf("par=");
         sscanf(param, "%d", &tinggi);
         sprintf(tampung1,"%i",tinggi);
         lcd_gotoxy(10,1);
         lcd_puts(tampung1);

       break;

default:
         lcd_gotoxy(0,1);
         lcd_putsf("perintah tdk ada");

       } 

       r_ready=0;

       rx_wr_index=0;

       UCSRB.7=1; 

   }// end of sub function ambil parameter

referensi:

					
Advertisements

About pccontrol

Berisi Tutorial Menggunakan PC untuk mengontrol Peralatan dengan cara mudah & praktis.

Posted on 05/07/2011, in Menengah-3. Bookmark the permalink. 2 Comments.

  1. saya udah coba pak…, sy buat pke Codevisionavr dan simulasi pke Proteus… dan interface pakai virtual serial port emulator, tapi waktu run proteus.., lcd nya blank aja…, klo ada project yg berhasil pak…, mohon uplot…mkasih pak…….

  2. Mantap Gan…

Komentar ,Saran atau Pertanyaan

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: