日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當前位置:首頁 > 單片機 > 單片機
[導讀]C#與51單片機串口通信51接受數(shù)據(jù),PC發(fā)送數(shù)據(jù)。通過單片機的數(shù)碼管將PC發(fā)送的16進制數(shù)據(jù)顯示出來。51接受數(shù)據(jù)代碼:#include #include #include sbit LS138A = P2^2; //定義138譯碼器的輸入A腳由P2.2控制 sbit

C#與51單片機串口通信

51接受數(shù)據(jù),PC發(fā)送數(shù)據(jù)。

通過單片機的數(shù)碼管將PC發(fā)送的16進制數(shù)據(jù)顯示出來。

51接受數(shù)據(jù)代碼:

#include

#include

#include

sbit LS138A = P2^2; //定義138譯碼器的輸入A腳由P2.2控制

sbit LS138B = P2^3; //定義138譯碼器的輸入腳B由P2.3控制

sbit LS138C = P2^4; //定義138譯碼器的輸入腳C由P2.4控制

unsigned char ch;

bit read_flag= 0 ;

//此表為 LED 的字模, 共陰數(shù)碼管 0-9 -

unsigned char code Disp_Tab[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};

void delay(unsigned int i);

void init_serialcom( void )

{

SCON = 0x50 ; //SCON: serail mode 1, 8-bit UART, enable ucvr

//UART為模式1,8位數(shù)據(jù),允許接收

TMOD |= 0x20 ; //TMOD: timer 1, mode 2, 8-bit reload

//定時器1為模式2,8位自動重裝

PCON |= 0x80 ; //SMOD=1;

TH1 = 0xF3; // //baud*2 /* 波特率4800、數(shù)據(jù)位8、停止位1。效驗位無 (12M)

IE |= 0x90 ; //Enable Serial Interrupt

TR1 = 1 ; // timer 1 run

TI=1;

}

//向串口發(fā)送一個字符

void send_char_com( unsigned char ch)

{

SBUF=ch;

while (TI== 0);

TI= 0 ;

}

//串口接收中斷函數(shù)

void serial () interrupt 4 using 3

{

if (RI)

{

RI = 0 ;

ch=SBUF;

read_flag= 1 ; //就置位取數(shù)標志

}

}

main()

{

int LedNumVal = 0;

unsigned char LedOut[3];

int i = 0;

init_serialcom(); //初始化串口

while ( 1 )

{

if (read_flag) //如果取數(shù)標志已置位,就將讀到的數(shù)從串口發(fā)出

{

read_flag= 0 ; //取數(shù)標志清0

send_char_com(ch);

LedNumVal = ch;

}

LedOut[0]=Disp_Tab[LedNumVal / 100];

LedOut[1]=Disp_Tab[(LedNumVal / 10) % 10];

LedOut[2]=Disp_Tab[LedNumVal % 10]|0x80;

for( i=0; i<3; i++) //實現(xiàn)8位動態(tài)掃描循環(huán)

{

P0 = LedOut[i]; //將字模送到P0口顯示

switch(i) //使用switch 語句控制位選

{

case 0:LS138A=0; LS138B=0; LS138C=0; break;

case 1:LS138A=1; LS138B=0; LS138C=0; break;

case 2:LS138A=0; LS138B=1; LS138C=0; break;

}

delay(100);

}

}

}

void delay(unsigned int i)

{

char j;

for(i; i > 0; i--)

for(j = 200; j > 0; j--);

}


C#發(fā)送數(shù)據(jù)代碼:


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.IO.Ports;

namespace 交通燈串口通信

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private SerialPort com;

private void button1_Click(object sender, EventArgs e)

{

com = new SerialPort();

com.BaudRate = 4800;

com.PortName = "COM4";

com.DataBits = 8;

com.Open();

Byte[] data = new Byte[4];

data[0] = 0x10;

com.Write(data, 0, 1);

com.Close();

}

}

}


本站聲明: 本文章由作者或相關機構授權發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權益,請及時聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關閉