一个单片机与PC机通信的程序

下面是一个单片机与pc机通信的程序,pc机程序用c语言来编写
1. pc机编程
pc采用toubr c 进行编写。程序如下:
#include《stdio.h》
#define port 0x3f8 /*利用串口1进行通信*/
int ch[15];
main ()
{
int a;
int i,j;
int b[6]={88,15,38,26,20,0};
char c;
clrscr();
outportb(port+3,0x80); /*准备设置波特率*/
outportb(port,0x0c); /*波特率设置为9 600 bps*/
outportb(port+1,0x00);
outportb(port+3,0x03); /*8位数据位,奇偶检验,1位停止位*/
outportb(port+1,0x00); /*关中断*/
inportb(port+5); /*读一次线路状态寄存器,使其复位*/
{
printf(“ send data or receive data: (s or r?)”);
c=getchar();
switch(c)
{
case ’s’:
case ’s’:
{
while(!(inportb(port+5)&0x20)); /*发送保持器满则等待*/
outportb(port,0x01); /*否则发送数据01通知单片机准备接收*/
for(i=0;i《6;i++) /*共发送6个数据*/
{
a=b[i];
while(!(inportb(port+5)&0x20))
delay(100); /*发送保持器满,等待*/
outportb(port,a); /*发送a*/
printf(“%d”,a); /*显示a*/
while(!(inport(port+5)&1)); /*接收单片机送回的数据*/
ch[i]=inport(port); /*保存*/
}
delay(10);
for(j=0;j《8;j++) /*显示接收的回送数据*/
printf(“%d”,ch[j]);
getch();
break;
}
case’r’: /*接收数据*/
case’r’:
{
while(!(inportb(port+5)&0x20));
outportb(port,0x02); /*发送数据02通知单片机发送数据*/
for(j=0;j《9;j++) /*共接收9个数据*/
{
while(!(inportb(port+5)&1));
ch[j]=inportb(port);
}
for(j=0;j《9;j++)
printf(“ %d”,ch[j]);
getch();
break;
}
}
}
}
2. 单片机与pc机通信之单片机程序
list p=16f876
#include p16f876.inc
cblock 0x24
count
temp
endc
org 0x0000
nop
start goto main
org 0x020
main movlw 0x30 ;将ram单元初始化
movwf fsr
bcf status,rp0
movlw 0x22
movwf count
intram movf count,0
movwf indf
incf count,1
incf fsr,1
btfss fsr,7
goto intram
banksel txsta ;波特率设置为9 600 bps
movlw 0x19
movwf spbrg ;异步高速方式
movlw 0x04
movwf txsta
bcf status,rp0
movlw 0x80 ;串行口使能
movwf rcsta
movlw 0x30
movwf fsr
bsf rcsta,cren ;接收允许
bsf status,rp0
bsf txsta,txen ;发送允许
bcf status,rp0
wait btfss pir1,rcif ;接收pc机命令
goto wait
movf rcreg,0
movwf temp
movwf indf
incf fsr
decfsz temp ;接收到的数据为1,则接收
goto txw ;否则发送数据
movlw 0x06
movwf temp
wait1 btfss pir1,rcif ;接收一个数据
goto wait1
movf rcreg,0
movwf count
movwf txreg
looptx btfss pir1,txif ;将接收到的数据送回pc机
goto looptx
movf count,0
movwf indf
incf fsr
decfsz temp
goto wait1
goto over ;处理完毕
txw movlw 0x09 ;向pc机发送9个数据
movwf temp
movlw 0x30
movwf fsr
txw0 movf indf,0
movwf txreg
incf fsr
txw1 btfss pir1,txif
goto txw1
decfsz temp
goto txw0
over bcf rcsta,cren ;通信任务完成,关接受和发送允许
bsf status,rp0
bcf txsta,txen
bcf status,rp0
call led ;调用显示子程序,将发送(或接收)的数据显示出来
end ;程序完

近半校园安全防范措施不足 校园安防建设急需加快步伐
C8051F与80C51系列单片机的初始化比较
给你破壳而出的美丽,蛋蛋面膜效果怎么样?
使用WEMOS开发板的IOT漏水检测器设计方案
5G+机器人手术顺利完成意味着什么
一个单片机与PC机通信的程序
特斯拉的全自动驾驶发展路程任重而道远
智能镜子显示屏带你全面认识智能家居的新模式
傅利叶智能纵深布局从单一产品到智能康复港标准化解决方案
谁能笑傲千元机市场,360N5傲视群雄
扫地机器人是人工智能吗
SD NAND供电电源的重要性
微软目前没有为XSX和XSS提供虚拟现实头显设备支持的计划
紫外线灯管安装有哪些要求呢?
稳压三极管工作原理
一文带你详细了解冷水机组的相关温度和压力参数,赶紧收藏!
如何增加无线模块的通讯传输距离
人体存在感应+毫米波雷达技术,你知道多少?
特斯拉model 3的空气动力学车轮将提升10%续航
福特开始与其他汽车品牌分享网联汽车数据