请教一下关于VB与51单片机传送数据的问题
我想用VB通过串口送一个数字给单片机(STC89C52),单片机再通过数码管显示出来。但是单片机只能正确显示0—15这几个数字,后面的就乱了,比如发送"16",数码管上却...
我想用VB通过串口送一个数字给单片机(STC89C52),单片机再通过数码管显示出来。但是单片机只能正确显示0—15这几个数字,后面的就乱了,比如发送"16",数码管上却显示"48",发送"17”,数码管上显示"49”...发送“148”,数码管上又显示"112"。不知道为什么,求大神指点指点,谢谢了!!!
下面是我的VB程序
Private Sub Command1_Click()If Text1.Text = "" Then
n% = MsgBox("请输入数据", 64, "错误")
Else
MSComm1.OutBufferCount = 0
MSComm1.Output = Chr(Text1.Text)
End If
End Sub
Private Sub Command2_Click()End
End Sub
Private Sub Command3_Click()If Command3.Caption = "打开串口" Then
MSComm1.PortOpen = True
Command3.Caption = "关闭串口"
Else
MSComm1.PortOpen = False
Command3.Caption = "打开串口"
End If
End Sub
Private Sub Form_Load()MSComm1.CommPort = 1
MSComm1.InputMode = comInputModeBinary
MSComm1.InBufferSize = 40
MSComm1.InBufferCount = 0
MSComm1.Settings = "19200,N,8,1"
MSComm1.RThreshold = 1
MSComm1.InputLen = 0
End Sub
单片机(STC89C52)程序
#include<reg52.h>#define uchar unsigned char
int receivedata;
sbit dula=P2^6;
sbit wela=P2^7;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,
0x76,0x79,0x38,0x3f,0};
void display(int temp);
void delay(int x)
{
int a,b;
for(a=x;a>0;a--)
for(b=120;b>0;b--);
}
void main(){
TMOD=0x21;
TH1=0xfd;
TL1=0xfd;
TR1=1;
SM0=0;
SM1=1;
REN=1;
EA=1;
ES=1;
PCON=0x80;
while(1)
display(receivedata);
}
void serial() interrupt 4{
receivedata=SBUF;
RI=0;
}
void display(int temp)
{
unsigned char ge,shi,bai,qian,wan;
wan=temp/10000;
qian=(temp%10000)/1000;
bai=(temp%1000)/100;
shi=(temp%100)/10;
ge=temp%10;
dula=1;
P0=table[wan];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delay(1);
dula=1; P0=table[qian];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay(1);
dula=1;
P0=table[bai];
dula=0;
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
delay(1);
dula=1;
P0=table[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xf7;
wela=0;
delay(1);
dula=1; P0=table[ge];
dula=0;
P0=0xff;
wela=1;
P0=0xef;
wela=0;
delay(1);
} 展开
下面是我的VB程序
Private Sub Command1_Click()If Text1.Text = "" Then
n% = MsgBox("请输入数据", 64, "错误")
Else
MSComm1.OutBufferCount = 0
MSComm1.Output = Chr(Text1.Text)
End If
End Sub
Private Sub Command2_Click()End
End Sub
Private Sub Command3_Click()If Command3.Caption = "打开串口" Then
MSComm1.PortOpen = True
Command3.Caption = "关闭串口"
Else
MSComm1.PortOpen = False
Command3.Caption = "打开串口"
End If
End Sub
Private Sub Form_Load()MSComm1.CommPort = 1
MSComm1.InputMode = comInputModeBinary
MSComm1.InBufferSize = 40
MSComm1.InBufferCount = 0
MSComm1.Settings = "19200,N,8,1"
MSComm1.RThreshold = 1
MSComm1.InputLen = 0
End Sub
单片机(STC89C52)程序
#include<reg52.h>#define uchar unsigned char
int receivedata;
sbit dula=P2^6;
sbit wela=P2^7;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,
0x76,0x79,0x38,0x3f,0};
void display(int temp);
void delay(int x)
{
int a,b;
for(a=x;a>0;a--)
for(b=120;b>0;b--);
}
void main(){
TMOD=0x21;
TH1=0xfd;
TL1=0xfd;
TR1=1;
SM0=0;
SM1=1;
REN=1;
EA=1;
ES=1;
PCON=0x80;
while(1)
display(receivedata);
}
void serial() interrupt 4{
receivedata=SBUF;
RI=0;
}
void display(int temp)
{
unsigned char ge,shi,bai,qian,wan;
wan=temp/10000;
qian=(temp%10000)/1000;
bai=(temp%1000)/100;
shi=(temp%100)/10;
ge=temp%10;
dula=1;
P0=table[wan];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delay(1);
dula=1; P0=table[qian];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay(1);
dula=1;
P0=table[bai];
dula=0;
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
delay(1);
dula=1;
P0=table[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xf7;
wela=0;
delay(1);
dula=1; P0=table[ge];
dula=0;
P0=0xff;
wela=1;
P0=0xef;
wela=0;
delay(1);
} 展开
展开全部
不要连接,发送两个字节的接收器就行了,还有两个字节
如传感器发送数据和hfff对HF和&HFF
二进制模式
的REDIM律政司司长(1)作为字节
SJ(0)=?
SJ(1)=?
MSComm1.OutBufferCount = 0
MSComm1.Output = SJ
如传感器发送数据和hfff对HF和&HFF
二进制模式
的REDIM律政司司长(1)作为字节
SJ(0)=?
SJ(1)=?
MSComm1.OutBufferCount = 0
MSComm1.Output = SJ
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你可以把单片机和上位机的波特率设低点试试最好是9600 或者以下
追问
设置为9600还是一样
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询