共2条
1/1 1 跳转至页

问
前两天我想用单片机利用串口向微机发送数据,由于单片机速度比微机慢所以想用中断来做,当单片机发送数据来后,微机产生中断去处理接收来得数据。单片机我编写的程序是不停的发送字母A到T,看微机能否处理。
我将微机中中断向量0x0c就是COM1的中断向量修改成我中断服务程序的函数入口地址,但是程序运行起来微机接收到几个到几十个数据后程序就自动退出,提示执行非法操作,给出中断服务4,就是计算机执行了溢出中断,可是我的程序我觉得没有涉及到堆栈的问题,而且只是打印字母,怎么会溢出呢?程序源代码如下:是在windows98下运行的。请路过的人看看,如果能提出些建议不胜感激。谢谢!
#include<stdio.h>
#include<dos.h>
#include<conio.h>
void interrupt (*oldhandler)();
void interrupt IntServ();
unsigned char Old_Imr;
void Ser_init()
{
unsigned char New_Imr;
oldhandler=getvect(0x0c); //COM1的中断向量
setvect(0x0c,IntServ);
outportb(0x3fb,0x80);
outportb(0x3f8,0x60); //1200b/s
outportb(0x3f9,0x00);
outportb(0x3fb,0x03);
outportb(0x3fc,0x0b);
outportb(0x3f9,0x01);
Old_Imr=inportb(0x21);//获取中断屏蔽位
New_Imr=Old_Imr&0xef; //开放COM1中断
outportb(0x21,New_Imr);
}
unsigned char receive()
{
unsigned char ch,store;
/* do
{
store=inportb(0x3fd)&0x01; //DR接收数据为1测试
if(store==0x01)
ch=inportb(0x3f8);
}while(store!=0x01);
*/
ch=inportb(0x3f8);
return ch;
}
void interrupt IntServ()
{
unsigned char ch_h;
_asm{CLI};
ch_h=receive();
printf("\n the number is %c ",ch_h);
_asm{STI};
outportb(0x20,0x20);
}
void delaytime()
{
unsigned int i,j;
for(i=0;i<5000;i++)
for(j=0;j<20;j++)
;
}
void Ser_Close()
{
setvect(0x0c,oldhandler);
outportb(0x21,Old_Imr);
outportb(0x20,0x20);
}
void main()
{
clrscr();
Ser_init();
do
{
delaytime();
}while(!kbhit());
Ser_Close();
}
我把单片机的程序也付上吧。
//单片机向微机发送存储区中的数据
//Crystal = 11.0592MHz
#include<reg52.h>
void init()
{
T2CON=0x30;
RCAP2H=0xfe; //1200b/s
RCAP2L=0xe0;
// ES=1;
// EA=1;
SCON=0x50;
PCON=0x00;
TR2=1;
}
void delay()
{
unsigned int i,j;
for(i=0;i<5000;i++)
for(j=0;j<10;j++)
;
}
void main()
{ unsigned char ch=0x41,i;
init();
do
{
for(i=0;i<20;i++)
{
SBUF=ch;
while(TI==0);TI=0;
delay();
ch=ch+1;
}
ch=0x41;
delay();
/* SBUF=ch;
while(TI==0);TI=0;
delay();
*/
}while(1);
} 答 1: 一点建议.建议用 SETVECT() 和GETVECT()来设中断表(DOS.H).
在DOS下好些,不要在WIN98的COMMAND下.
这样,接上MOUSE后就可以显示数据.
void interrupt (*NewCom)(void);//声明.
int i = getvect(??);//保存.
setvect(??, (void interrupt (*)(void))NewCom);//设中断表
setvect(??,i); //恢复. 答 2: RE:好明天我去试试,不过我好象记得我在纯DOS下运行过,也没有成功,是连数据也收不到,好啦,明天再试试。
我将微机中中断向量0x0c就是COM1的中断向量修改成我中断服务程序的函数入口地址,但是程序运行起来微机接收到几个到几十个数据后程序就自动退出,提示执行非法操作,给出中断服务4,就是计算机执行了溢出中断,可是我的程序我觉得没有涉及到堆栈的问题,而且只是打印字母,怎么会溢出呢?程序源代码如下:是在windows98下运行的。请路过的人看看,如果能提出些建议不胜感激。谢谢!
#include<stdio.h>
#include<dos.h>
#include<conio.h>
void interrupt (*oldhandler)();
void interrupt IntServ();
unsigned char Old_Imr;
void Ser_init()
{
unsigned char New_Imr;
oldhandler=getvect(0x0c); //COM1的中断向量
setvect(0x0c,IntServ);
outportb(0x3fb,0x80);
outportb(0x3f8,0x60); //1200b/s
outportb(0x3f9,0x00);
outportb(0x3fb,0x03);
outportb(0x3fc,0x0b);
outportb(0x3f9,0x01);
Old_Imr=inportb(0x21);//获取中断屏蔽位
New_Imr=Old_Imr&0xef; //开放COM1中断
outportb(0x21,New_Imr);
}
unsigned char receive()
{
unsigned char ch,store;
/* do
{
store=inportb(0x3fd)&0x01; //DR接收数据为1测试
if(store==0x01)
ch=inportb(0x3f8);
}while(store!=0x01);
*/
ch=inportb(0x3f8);
return ch;
}
void interrupt IntServ()
{
unsigned char ch_h;
_asm{CLI};
ch_h=receive();
printf("\n the number is %c ",ch_h);
_asm{STI};
outportb(0x20,0x20);
}
void delaytime()
{
unsigned int i,j;
for(i=0;i<5000;i++)
for(j=0;j<20;j++)
;
}
void Ser_Close()
{
setvect(0x0c,oldhandler);
outportb(0x21,Old_Imr);
outportb(0x20,0x20);
}
void main()
{
clrscr();
Ser_init();
do
{
delaytime();
}while(!kbhit());
Ser_Close();
}
我把单片机的程序也付上吧。
//单片机向微机发送存储区中的数据
//Crystal = 11.0592MHz
#include<reg52.h>
void init()
{
T2CON=0x30;
RCAP2H=0xfe; //1200b/s
RCAP2L=0xe0;
// ES=1;
// EA=1;
SCON=0x50;
PCON=0x00;
TR2=1;
}
void delay()
{
unsigned int i,j;
for(i=0;i<5000;i++)
for(j=0;j<10;j++)
;
}
void main()
{ unsigned char ch=0x41,i;
init();
do
{
for(i=0;i<20;i++)
{
SBUF=ch;
while(TI==0);TI=0;
delay();
ch=ch+1;
}
ch=0x41;
delay();
/* SBUF=ch;
while(TI==0);TI=0;
delay();
*/
}while(1);
} 答 1: 一点建议.建议用 SETVECT() 和GETVECT()来设中断表(DOS.H).
在DOS下好些,不要在WIN98的COMMAND下.
这样,接上MOUSE后就可以显示数据.
void interrupt (*NewCom)(void);//声明.
int i = getvect(??);//保存.
setvect(??, (void interrupt (*)(void))NewCom);//设中断表
setvect(??,i); //恢复. 答 2: RE:好明天我去试试,不过我好象记得我在纯DOS下运行过,也没有成功,是连数据也收不到,好啦,明天再试试。
共2条
1/1 1 跳转至页
回复
有奖活动 | |
---|---|
发原创文章 【每月瓜分千元赏金 凭实力攒钱买好礼~】 | |
【EEPW在线】E起听工程师的声音! | |
“我踩过的那些坑”主题活动——第001期 | |
高校联络员开始招募啦!有惊喜!! | |
【工程师专属福利】每天30秒,积分轻松拿!EEPW宠粉打卡计划启动! | |
送您一块开发板,2025年“我要开发板活动”又开始了! | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
【我踩过的那些坑】电感选型错误导致的处理器连接不上被打赏50分 | |
【我踩过的那些坑】工作那些年踩过的记忆深刻的坑被打赏10分 | |
【我踩过的那些坑】DRC使用位置错误导致的问题被打赏100分 | |
我踩过的那些坑之混合OTL功放与落地音箱被打赏50分 | |
汽车电子中巡航控制系统的使用被打赏10分 | |
【我踩过的那些坑】工作那些年踩过的记忆深刻的坑被打赏100分 | |
分享汽车电子中巡航控制系统知识被打赏10分 | |
分享安全气囊系统的检修注意事项被打赏10分 | |
分享电子控制安全气囊计算机知识点被打赏10分 | |
【分享开发笔记,赚取电动螺丝刀】【OZONE】使用方法总结被打赏20分 |