微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 51和VB数字量简易测试

51和VB数字量简易测试

时间:11-30 来源:互联网 点击:


//51代码

#include

#define uchar unsigned char
#define uint unsigned int

void delay(uint k)
{
while(k--);
}

uchar rec;//接受到的数据

sbit q1=P1^0; //输出点
sbit q2=P1^1; //输出点
sbit q3=P1^2; //输出点
sbit q4=P1^3; //输出点
sbit fk=P1^4; //反馈点
sbit j1=P2^4;//反馈发光指示


void light()
{
if(rec==1)q1=0;
else if(rec==2)q2=0;
else if(rec==3)q3=0;
else if(rec==4)q4=0;
else if(rec==5)q1=1;
else if(rec==6)q2=1;
else if(rec==7)q3=1;
else if(rec==8)q4=1;
}

void serial_init() //串口初始化
{
SM2=0;
SM1=1;
SM0=0;
TMOD|=0x20; //自动重装载
TH1=TL1=253;
TR1=1;
REN=1;
}

void str_init()//串口中断初始化
{
EA=1;
ES=1;

}

void str_send(uchar u) //串口发送函数
{
bit es;
es=ES; //保存现场
ES=0;
SBUF=u;
while(TI==0);
TI=0;
ES=es;
}

void str_event() interrupt 4
{
if(RI)
{
rec=SBUF;
RI=0;
}
else
{
;
}
}


void main()
{
serial_init();
str_init();
fk=0;
while(1)
{
if(fk==1) //正常
{
str_send(9);
j1=0;
delay(400);
}
if(fk==0)
{
str_send(a); //故障
j1=1;
delay(400);
}
light();
}
}

51单片机端口全部源码可以
从这里http://www.51hei.com/f/51vbcs.rar下载.

//---------- VB代码--------------------------------------------------------


Private Sub Command1_Click()
On Error GoTo errline:

MSComm1.Output = "1"
Shape1.FillColor = vbGreen
Exit Sub

errline:
If Err.Number = 8018 Then
Text3.Text = "串口已关"
End If
Err.Clear
End Sub

Private Sub Command10_Click()
On Error GoTo errline:

MSComm1.Output = "5"

MSComm1.Output = "6"

MSComm1.Output = "7"

MSComm1.Output = "8"


Shape1.FillColor = vbRed
Shape2.FillColor = vbRed
Shape3.FillColor = vbRed
Shape4.FillColor = vbRed

MSComm1.PortOpen = False
Timer2.Enabled = False
Timer3.Enabled = False


Text3.Text = "串口已关"
errline:
If Err.Number = 8018 Then
Text3.Text = "串口已关"
End If
Err.Clear
End Sub

Private Sub Command11_Click()
On Error GoTo errline

If Text1.Text < 0 Then
Text1.Text = 0
End If
Timer1.Enabled = True
errline:
If Err.Number = 13 Then
MsgBox ("输入出错")
Text1.Text = 0

End If


End Sub

Private Sub Command12_Click()
Text1.Text = 0
Timer1.Enabled = False

End Sub

Private Sub Command2_Click()
On Error GoTo errline:

MSComm1.Output = "5"
Shape1.FillColor = vbRed
Exit Sub

errline:
If Err.Number = 8018 Then
Text3.Text = "串口已关"
End If
Err.Clear
End Sub


Private Sub Command3_Click()
On Error GoTo errline:

MSComm1.Output = "2"
Shape2.FillColor = vbGreen
Exit Sub

errline:
If Err.Number = 8018 Then
Text3.Text = "串口已关"
End If
Err.Clear
End Sub


Private Sub Command4_Click()
On Error GoTo errline:

MSComm1.Output = "6"
Shape2.FillColor = vbRed
Exit Sub

errline:
If Err.Number = 8018 Then
Text3.Text = "串口已关"
End If
Err.Clear
End Sub


Private Sub Command5_Click()
On Error GoTo errline:

MSComm1.Output = "3"
Shape3.FillColor = vbGreen
Exit Sub

errline:
If Err.Number = 8018 Then
Text3.Text = "串口已关"
End If
Err.Clear
End Sub


Private Sub Command6_Click()
On Error GoTo errline:

MSComm1.Output = "7"
Shape3.FillColor = vbRed
Exit Sub

errline:
If Err.Number = 8018 Then
Text3.Text = "串口已关"
End If
Err.Clear
End Sub


Private Sub Command7_Click()
On Error GoTo errline:

MSComm1.Output = "4"
Shape4.FillColor = vbGreen
Exit Sub

errline:
If Err.Number = 8018 Then
Text3.Text = "串口已关"
End If
Err.Clear
End Sub


Private Sub Command8_Click()
On Error GoTo errline:

MSComm1.Output = "8"
Shape4.FillColor = vbRed
Exit Sub

errline:
If Err.Number = 8018 Then
Text3.Text = "串口已关"
End If
Err.Clear
End Sub


Private Sub Command9_Click()
On Error GoTo errline:

MSComm1.CommPort = 6
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeText
MSComm1.InputLen = 0
MSComm1.PortOpen = True
Timer2.Enabled = True
Timer3.Enabled = True


Text3.Text = "串口已开"
Exit Sub
errline:
If Err.Number = 8013 Then
Text3.Text = "串口已开"
End If
Err.Clear
End Sub


Private Sub Form_Load()

End Sub

Private Sub Text1_Change()
On Error GoTo errline:
errline:
If Err.Number = 13 Then
Text1.Text = "输入错误"
End If
Err.Clear

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top