⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mpu_pc.c

📁 PC机与单片机之间进行通讯连接的C51代码和PC机的VC++代码
💻 C
字号:
/***************************/
/*MPU与PC机通讯程序mpu_pc.c*/
/***************************/ 
#pragma SMALL
#pragma CODE SYMBOLS NOLISTINCLUDE OPTIMIZE(3) 
#pragma PW(80) /* set screen row in *.lst*/
#pragma PL(55) /*set screen collow in *.lst*/
#include <reg52.h>
#include <absacc.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

void SendStr(char *s)
{
unsigned char i=0; 
while(s[i]!=0)
{
SBUF=s[i++];while(TI==0);TI=0;
}
}

main()
{
/* initialize MPU */
SCON = 0x52; /*Serial Port Control mode=1 ie 8 bit UART */
TMOD = 0x21; /*Timer mode control*/
/*Timer 0 set to 16 bit timer*/
/*Timer 1 set to 8 bit auto-reload timer*/
TCON = 0x50; /*Timer control*/
TH1 = 0xfd; /*calculated th1 according to formula*/
/*related to osc. freq. and baud rate*/
/*th1=a0, 160 for 300 at 11.0592M*/
/*th1=e8, 232 for 1200 at 11.0592M*/
/*th1=f4, 244 for 2400 at 11.0592M*/
/*th1=fd, 253 for 9600 at 11.0592M*/
/*th1=60, 96 for 300 at 18.432M*/
/*th1=fb, 251 for 9600 at 18.432M*/
TH0=(TIME0_VAL>>8)&0xff; /*Start Value of Timer 0 */
TL0=TIME0_VAL&0xff;
ET0 = 1; /*Timer 0 interrupt enable*/
EA = 1; /*System interrupt enable */
ES=1; /*serial port interrupt enable */
EX1=1; /* outer interrupt enable */
SendStr(":A123;");
while(1)
{ 
......
}


void serial() interrupt 4 using 1
{
unsigned char c;
EA=0;
RI=0;
c=SBUF;
SBUF=c;while(TI==0);TI=0; /*接收即返回*/
{
/* 此处对接收到的数据进行必要的处理,如放入缓存,比较等*/
}
EA=1;
}
 
/*VB---PC机通讯软件部分 
Private Sub MSComm1_OnComm()
Dim sRx_buff As String
Dim nRx_data As Integer
If MSComm1.InBufferCount > 0 Then
sRx_buff = MSComm1.Input
If Mid(sRx_buff, 1, 1) = ":" Then
Do While Len(stx_buff) < 5
If MSComm1.InBufferCount > 0 Then
sRx_buff = sRx_buff + MSComm1.Input
End If
DoEvents
Loop
End If
If Mid(sRx_buff, 2, 1) = "A" Then
nRx_data = CInt(Mid(sRx_buff, 3, 3))
End If
Text1.Text = "收到A数据" + CStr(nRx_data)
End Sub */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -