ไมโครคอนโทรลเลอร์รับตัวอักษรผ่าน RS232

วันที่โพสต์: Feb 10, 2011 11:22:7 AM

ไมโครคอนโทรลเลอร์รับตัวอักษรผ่าน RS232

โค๊ดตัวอย่างการรับค่า String คอมพิวเตอร์ส่งไปที่ไมโครคอนโทรลเลอร์ ผ่าน RS232 

    การสื่อสารระหว่างไมโครคอนโทรลเลอร์ กับคอมพิวเตอร์ ด้วย RS232 ยังเป็นที่นิยม เนื่องจากเป็นโปรโตคอลที่ง่าย และสะดวกรวดเร็ว เนื่องจากรับส่งข้อมูลที่ไม่ต้องการปริมาณมากๆ บ่อยครั้งที่เรามักจะใช้ RS232 ในการส่งคำสั่งบางอย่างลงไปที่ไมโครคอนโทรลเลอร์ เพื่อให้ไมโครคอนโทรลเลอร์ทำงานตามคำสั่งบางอย่างให้กับเรา

     ตัวอย่างต่อไปนี้ เป็นการติดต่อสื่อสารระหว่างไมโครคอนโทรลเลอร์ และคอมพิวเตอร์ ผ่าน RS232 โดยใช้ interrupt 

ของไมโครคอนโทรลเลอร์เป็นตัวคอยเช็คว่ามีข้อมูลถูกส่งมาจากคอมพิวเตอร์หรือยัง

Function name : void get_string(char* s, unsigned int8 max)

Complier : CCS C Complier

Purpose : Recieve data from RS232 protocal by interrupt

/*--------------------- get string from RS232 -----------------------------*/

void get_string(char* s, unsigned int8 max) {

   unsigned int8 len;

   char c;

   --max;

   len=0;

   do {

     c=getc();

     if(c==8) {  // Backspace

        if(len>0) {

          len--;

          //putc(c);

          //putc(' ');

          //putc(c);

        }

     } else if ((c>=' ')&&(c<='~'))

       if(len<=max) {

         s[len++]=c;

         //putc(c);

       }

   } while(c!=13);

   s[len]=0;

}

การประยุกต์ใช้งาน 

ตัวอย่างต่อไปนี้ เป็นการแสดงการรับส่งข้อมูลจากคอมพิวเตอร์ ผ่านพอร์ตอนุกรม โดยส่งข้อความไปที่ไมโครคอนโทรลเลอร์ผ่าน RS232 โดยในการสาธิตนี้ เราได้ทำการจำลองการทำงานผ่านโปรแกรม Proteus และใช้โปรแกรม Virtual Serial Port Emulator เมื่อทำการเชื่อมต่อพอร์ตอนุกรม ได้แล้ว จึงทำการส่งข้อมูลที่เป็นตัวอักษรผ่าน Hyper Terminal ของโปรแกรม Windows 

โค๊ด main ของโปรเจค

#include <16F877.h> 

// Configuration 

#fuses NOWDT,HS, NOPUT, NOPROTECT,NOLVP,BROWNOUT

#use delay (clock=10000000)   // Crystal 10MHz

#define  TxD         PIN_C6   // Define Transmitted Data

#define  RxD         PIN_C7   // Define Received Data

#use rs232(baud=9600, xmit=TxD,rcv=RxD,stream=PC_COM) // Use serial I/O port (RS232)

//External library

#include "lcd1.c"

// Global variables

#define  STRING_SIZE    6   // The number of characters allowed to input

char input_str[STRING_SIZE];

BOOLEAN extREQ = FALSE;

// Function prototype

void get_string(char* s, unsigned int8 max);

// Interrupt service routine

#int_rda

void rs232_isr()

{

   get_string(input_str,STRING_SIZE);     // gets the string

   extREQ = TRUE;

}

// ========================  Main ====================================//

void main(void)

{

enable_interrupts(INT_RDA);               // Enable interrupt RS232 recieve data

enable_interrupts(GLOBAL);   

lcd_init();

delay_ms(1000);

printf(lcd_putc,"\fRS232 Simple\n");

lcd_putc(".........By Mr.P");

delay_ms(1000);

while(TRUE)

{

lcd_gotoxy(1,1);  printf(lcd_putc,"Waiting for cmd ",);

if(extREQ)

{

lcd_gotoxy(1,1);  printf(lcd_putc,"\fyou put string");

lcd_gotoxy(1,2);  printf(lcd_putc,"%s",input_str);

delay_ms(1000);

extREQ = FALSE;

lcd_gotoxy(1,2);  printf(lcd_putc,"                ");

}


}

}

/*--------------------- get string from RS232 -----------------------------*/

void get_string(char* s, unsigned int8 max) {

   unsigned int8 len;

   char c;

   --max;

   len=0;

   do {

     c=getc();

     if(c==8) {  // Backspace

        if(len>0) {

          len--;

          putc(c);

          putc(' ');

          putc(c);

        }

     } else if ((c>=' ')&&(c<='~'))

       if(len<=max) {

         s[len++]=c;

         putc(c);

       }

   } while(c!=13);

   s[len]=0;

}

อย่าลืมดาวน์โหลดไฟล์ lcd1.c ที่แนบไว้นี้ เข้าไปไว้ที่เดียวกันกับในโฟวเดอร์ซอร์สไฟล์ของเราด้วยครับ