新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 51和VB數(shù)字量簡易測試

51和VB數(shù)字量簡易測試

作者: 時間:2016-11-30 來源:網(wǎng)絡(luò) 收藏

//51代碼
#include
#define uchar unsigned char
#define uint unsigned int
void delay(uint k)
{
while(k--);
}
uchar rec;//接受到的數(shù)據(jù)
sbit q1=P1^0; //輸出點(diǎn)
sbit q2=P1^1; //輸出點(diǎn)
sbit q3=P1^2; //輸出點(diǎn)
sbit q4=P1^3; //輸出點(diǎn)
sbit fk=P1^4; //反饋點(diǎn)
sbit j1=P2^4;//反饋發(fā)光指示

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) //串口發(fā)送函數(shù)
{
bit es;
es=ES; //保存現(xiàn)場
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單片機(jī)端口全部源碼可以
從這里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 = "串口已關(guān)"
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

上一頁 1 2 下一頁

關(guān)鍵詞: 51VB數(shù)字

評論


技術(shù)專區(qū)

關(guān)閉