本帖最后由 kufan 于 2013-1-13 20:21 编辑
VB上位机源码.zip
(9.88 KB)
18b20测温数码管显示实验.zip
(42.11 KB)
AT89S52
下面是下位机部分程序(其他功能已完毕)
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 = 0xFA ; //Baud:9600 fosc=11.0592MHz
IE |= 0x90 ; //Enable Serial Interrupt
TR1 = 1 ; // timer 1 run
}
//串口发送一个字节
void Uart_SendChar(unsigned char dat)
{
SBUF = dat; //待发送的数据写入缓冲区
while(!TI); //等待发送完成
TI = 0; //清零发送标志位
}
/*-------------------------------------------------------------*/
//串口发送一个字符串
void Uart_SendString(unsigned char *datc)
{
while(*datc != '\0')
{
Uart_SendChar(*datc);
datc++;
}
}
//串口接收中断函数
void serial () interrupt 4 using 3
{
if (RI) //RI==开始接收
{
unsigned char ch;
RI = 0 ; //软件RI=0
ch=SBUF;
if (ch> 1 )
{
count3= 0 ;
inbuf2[count3]=ch;
}
else
{
count3++;
inbuf2[count3]=ch;
}
}
}
下面是VB部分 mscomm setting部分已设置了9600,n,8,1
Private Sub cmdConnect_Click()
'查找指定端口
Dim i As Integer
For i = 1 To 16
If optComPort(i - 1).Value = True Then
ComPort.CommPort = i
Exit For '跳出循环
End If
Next
If ComPort.PortOpen = True Then ComPort.PortOpen = False '如果端口打开则先关闭
ComPort.PortOpen = True '然后打开
'状态信息
lblStatus = "已连接..."
''Text1.Text = "EXIT"
cmdConnect.Enabled = False
cmdDisconnect.Enabled = True
End Sub
Private Sub cmdDisconnect_Click()
'断开连接
If ComPort.PortOpen = True Then ComPort.PortOpen = False
lblStatus = "已断开..."
cmdDisconnect.Enabled = False
cmdConnect.Enabled = True
End Sub
Private Sub cmdExit_Click()
'先断开端口再退出程序
If ComPort.PortOpen = True Then ComPort.PortOpen = False
Unload Me
End
End Sub
Private Sub Form_Load()
Dim iComPort As Integer
'错误陷阱
On Error GoTo CommErrorHandle
'尝试列表存在端口
For iComPort = 1 To 16
ComPort.CommPort = iComPort '指定端口号
If ComPort.PortOpen = True Then ComPort.PortOpen = False '如打开先关闭
ComPort.PortOpen = True '尝试打开
ComPort.PortOpen = False '确认成功关闭
Next
'端口配置
ComPort.InputLen = 1 '1 个字符产生接收事件
ComPort.RThreshold = 1 '1 个字符产生接收事件
'跳出错误
Exit Sub
CommErrorHandle:
'68 = 设备无效
'8002 = 端口号无效
'8012 = 端口无法打开
If Err = 68 Or Err = 8002 Or Err = 8012 Then
'端口无效时则禁止单击连接按钮
optComPort(iComPort - 1).Enabled = False
End If
'继续错误
Resume Next
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
'断开连接并退出
If ComPort.PortOpen = True Then ComPort.PortOpen = False
End Sub
Private Sub Text1_Change()
If ComPort.PortOpen = True Then
Text1.Text = ComPort.Input
End If
End Sub
单片机发送的灯不断闪,证明是发送的,现倾向于接收端出现错误,附上位机程序
|