อยากทราบว่าจะเขียน MCS51 รับค่ามาจาก VB ผ่าน RS232 ยังไงครับ ภาษา ซีคับ

Started by vuttipong, February 23, 2013, 05:07:47 PM

Previous topic - Next topic

JENG

ใช้ usart ครับ mcs-51 มี hardware usart ทุกเบอร์

ตัวอย่าง(เฉพาะส่ง)
#include <REG2051.H>
#include <stdio.h>
#define tx_pin P3_1

//*** Serial Setup ***//
void ser_setup()
{
  TR1 = 0;   //stop timer 1
  TMOD = 0x21;       //timer 1 mode 2 timer 0 mode 1
  SCON = 0x40;           //rx disable
  PCON |= 0x80;
  TH1 = 0xff;       //baud 9600 @11.0592mhz
  TL1 = 0xff;       //baud 9600 @11.0592mhz
  TR1 = 1;       //start timer 1
  SBUF = 0xff;
  RI = 0;
  TI = 0;
}

//*** Serial transmition ***//
void ser_tx(unsigned char dat)
{
  TI = 0;
  SBUF = dat;
  while(TI == 0);
  TI = 0;
}

//*** Main Program ***//
void main()
{
   ser_setup();

   ser_tx('H');
   ser_tx('e');
   ser_tx('l');
   ser_tx('l');
   ser_tx('o');
   ser_tx('!');

while(1);
}

สามารถติดตาม electoday ได้ที่

Facebook
www.facebook.com/groups/coopmicro

Google+
https://plus.google.com/communities/103482067769375459277

☺☺☺ความรู้ และความฉลาด ไม่ใช่สิ่งเดียวกัน จะมีประโยชน์อะไร ถ้าฉลาดแต่อยู่ในกะลา☺☺☺