您的位置 首页 厂商

PC与单片机通讯(89c52 VB 操控LED)

单片机c代码includereg52h>includestringh>defineINBUF_LEN4数据长度unsignedcharinbuf1[INBUF_LEN];unsignedc

//单片机c代码

#include
#include
#define INBUF_LEN 4 //数据长度
unsigned char inbuf1[INBUF_LEN];
unsigned char checksum,count3;
bit read_flag= 0 ;
sbit p1=P1^0;
sbit p2=P1^1;
sbit p3=P1^2;
sbit p4=P1^3;
sbit p5=P1^4;
sbit p6=P1^5;
sbit p7=P1^6;
sbit p8=P1^7;
void init_serialcomm( void )
{
SCON = 0x50 ; //SCON: serail mode 1, 8-bit UART, enable ucvr
TMOD |= 0x20 ; //TMOD: timer 1, mode 2, 8-bit reload
PCON |= 0x80 ; //SMOD=1;
TH1 = 0xF4 ; //Baud:4800 fosc=11.0592MHz
IE |= 0x90 ; //Enable Serial Interrupt
TR1 = 1 ; // timer 1 run
// TI=1;
}

//串口接纳中止函数
void serial () interrupt 4 using 3
{
if (RI)
{
unsigned char ch;
RI = 0 ;
ch=SBUF;
if (ch==1 )
{
//if (P1==0xff)
//P1=0x0e;
//else
//P1=0xff;
p1=0;
}
if (ch==2 )
{
p2=0;
}
if (ch==3 )
{
p3=0;
}
if (ch==4 )
{
p4=0;
}
if (ch==5 )
{
p5=0;
}
if (ch==6 )
{
p6=0;
}
if (ch==7 )
{
p7=0;
}
if (ch==8 )
{
p8=0;
}
if (ch==11 )
{
p1=1;
}
if (ch==12 )
{
p2=1;
}
if (ch==13 )
{
p3=1;
}
if (ch==14 )
{
p4=1;
}
if (ch==15 )
{
p5=1;
}
if (ch==16 )
{
p6=1;
}
if (ch==17 )
{
p7=1;
}
if (ch==18 )
{
p8=1;
}

}
}
main()
{
init_serialcomm(); //初始化串口
while ( 1 )
{
if (read_flag) //假如取数标志已置位,就将读到的数从串口宣布
{
read_flag= 0 ; //取数标志清0
}
}
}

上位机(VB)代码

增加command数组1,2,各8个,command1(i),command2(i)

增加MSComm控件

Private Sub Command1_Click(Index As Integer)
MSComm1.Output = Chr(Index + 1)
End Sub

Private Sub Command2_Click(Index As Integer)
MSComm1.Output = Chr(Index + 11)
End Sub

Private Sub Form_Load()
For i = 0 To 7
Command1(i).Caption = “ON_LED_” & Str(i + 1)
Command2(i).Caption = “OFF_LED_” & Str(i + 1)
Next i
Me.BorderStyle = 4
Me.Caption = “LED操控”
MSComm1.CommPort = 4 端口号依据自己电脑设置
MSComm1.Settings = “4800,n,8,1”
MSComm1.PortOpen = True 翻开通信口
End Sub

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/changshang/261938.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部