本例实现用VB上位机来控制51单片机8位led灯的亮和灭
单片机端的程序:
- #include<reg52.h>
- sbit LED1=P0^0;
- sbit LED2=P0^1;
- sbit LED3=P0^2;
- sbit LED4=P0^3;
- sbit LED5=P0^4;
- sbit LED6=P0^5;
- sbit LED7=P0^6;
- sbit LED8=P0^7;
- void Uartconfiguration();
- void main()
- {
- Uartconfiguration();
- while(1);
- }
- void ser() interrupt 4
- {
- if(RI)
- {
- RI=0;
- if(SBUF=='1')
- LED1=~LED1;
- else if(SBUF=='2')
- {LED2=~LED2;}
- else if(SBUF=='3')
- {LED3=~LED3;}
- else if(SBUF=='4')
- {LED4=~LED4;}
- else if(SBUF=='5')
- LED5=~LED5;
- else if(SBUF=='6')
- LED6=~LED6;
- else if(SBUF=='7')
- LED7=~LED7;
- else if(SBUF=='8')
- LED8=~LED8;
- }
- else
- TI=0;
- }
- void Uartconfiguration()
- {
- TMOD=0x20;
- TH1=0xfd;
- TL1=0xfd;
- PCON=0x00;
- TR1=1;
- EA=1;
- ES=1;
- SCON=0x50;
- }
复制代码
VB上位机程序
Private Sub Command1_Click()
strBuff = "1"
MSComm1.Output = strBuff
End Sub
Private Sub Command2_Click()
strBuff = "2"
MSComm1.Output = strBuff
End Sub
Private Sub Command3_Click()
strBuff = "3"
MSComm1.Output = strBuff
End Sub
Private Sub Command4_Click()
strBuff = "4"
MSComm1.Output = strBuff
End Sub
Private Sub Command5_Click()
strBuff = "5"
MSComm1.Output = strBuff
End Sub
Private Sub Command6_Click()
strBuff = "6"
MSComm1.Output = strBuff
End Sub
Private Sub Command7_Click()
strBuff = "7"
MSComm1.Output = strBuff
End Sub
Private Sub Command8_Click()
strBuff = "8"
MSComm1.Output = strBuff
End Sub
Private Sub Command9_Click()
End
End Sub
Private Sub Form_Load()
form1.WindowState = 2 '界面设置为最大
MSComm1.Settings = "9600,n,8,1"
MSComm1.OutBufferSize = 2 '设置MSComm1发送缓冲区为2字节
MSComm1.OutBufferCount = 0 '清除发送缓冲区
MSComm1.CommPort = 4 '指定使用com4
MSComm1.PortOpen = True '打开接口
End Sub
Private Sub Form_Resize()
Me.PaintPicture Me.Picture, 0, 0, Me.ScaleWidth, Me.ScaleHeight
End Sub
|