C # Serial Communication with 51 Single-Chip Microcomputer
51. The PC sends the data.
Display the hexadecimal data sent by the PC through the microcontroller digital tube.
51 accept dataCode:
# Include <reg51.h> <br/> # include <string. h> <br/> # include <intrins. h> </P> <p> sbit ls138a = P2 ^ 2; // define the P2.2 control of the input foot of the 138 decoder <br/> sbit ls138b = P2 ^ 3; // define the 138 decoder input foot B controlled by p2.3 <br/> sbit ls138c = P2 ^ 4; // define the 138 decoder input foot C controlled by p2.4 </P> <p> unsigned char ch; <br/> bit read_flag = 0; <br/> // This table is the led model. The common negative digital tube 0-9-<br/> unsigned char code disp_tab [] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x40 }; <br/> void d Elay (unsigned int I); <br/> void init_serialcom (void) </P> <p >{</P> <p> scon = 0x50; // scon: Serail Mode 1, 8-bit UART, enable ucvr </P> <p> // UART is in mode, accept allowed </P> <p> tmod | = 0x20; // tmod: timer 1, Mode 2, 8-bit reload </P> <p> // The timer 1 is in-bit automatic reinstallation mode </P> <p> pcon | = 0x80; // smod = 1; </P> <p> Th1 = 0xf3; // baud * 2/* the baud rate is 4800, the data bit is 8, and the stop bit is 1. No verification site (12 m) </P> <p> ie | = 0x90; // enable serial interrupt </P> <p> tr1 = 1; // timer 1 run </P> <p> Ti = 1; </P> <p >}</P> <p> // send a character to the serial port </P> <p> void send_char_com (unsigned char ch) </P> <p >{< br/> sbuf = CH; <br/> while (Ti = 0); <br/> Ti = 0; </P> <p >}</P> <p> // serial port receiving interrupt function </P> <p> void serial () interrupt 4 using 3 </P> <p >{< br/> If (RI) <br/>{</P> <p> rI = 0; </P> <p> CH = sbuf; </P> <p> read_flag = 1; // set the number of places </P> <p >}</P> <p> main () </P> <p >{< br/> int lednumval = 0; <br/> unsigned char ledout [3]; <br/> int I = 0; </P> <p> init_serialcom (); // initialize the serial port </P> <p> while (1) <br/>{</P> <p> If (read_flag) // if the number marker is set, send the read data from the serial port </P> <p >{</P> <p> read_flag = 0; // set the number to 0. <br/> send_char_com (CH); <br/> lednumval = CH; <br/>}</P> <p> ledout [0] = disp_tab [lednumval/100]; <br/> ledout [1] = disp_tab [(lednumval/10) % 10]; <br/> ledout [2] = disp_tab [lednumval % 10] | 0x80; </P> <p> for (I = 0; I <3; I ++) // implements an 8-bit dynamic scanning cycle <br/> {<br/> p0 = ledout [I]; // send the model to the P0 port for display </P> <p> switch (I) // use the switch statement to control the bit selection <br/>{< br/> case 0: ls138a = 0; ls138b = 0; ls138c = 0; break; <br/> case 1: ls138a = 1; ls138b = 0; ls138c = 0; break; <br/> case 2: ls138a = 0; ls138b = 1; ls138c = 0; break; <br/>}< br/> delay (100 ); <br/>}</P> <p> void delay (unsigned int I) <br/>{< br/> char J; <br/> for (I; I> 0; I --) <br/> for (j = 200; j> 0; j --); <br/>}
C # data sending code:
Using system; <br/> using system. collections. generic; <br/> using system. componentmodel; <br/> using system. data; <br/> using system. drawing; <br/> using system. LINQ; <br/> using system. text; <br/> using system. windows. forms; <br/> using system. io; <br/> using system. io. ports; </P> <p> namespace traffic light serial port communication <br/>{< br/> Public partial class form1: form <br/>{< br/> Public form1 () <br/>{< br/> initializecomponent (); <br/>}</P> <p> private SerialPort com; </P> <p> private void button#click (Object sender, eventargs E) <br/>{</P> <p> COM = new SerialPort (); <br/> COM. baudrate = 4800; <br/> COM. portname = "com4"; <br/> COM. databits = 8; <br/> COM. open (); <br/> byte [] DATA = new byte [4]; <br/> data [0] = 0x10; <br/> COM. write (data, 0, 1); <br/> COM. close (); <br/>}< br/>