新手求stc89c52单片机中断和定时器一起用的问题。

li_rui_1220 2012-04-26 03:29:22
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar a=0,i=0xfe;
void deng();

void main()
{
TMOD = 0x01;
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;

EA = 1; //开总中断
EX0 = 1; //开外部中断0
IT0 = 1; //中断触发为跳沿触发
PT1 = 1; //设定定时器中断优先
P1 = 0;
while(1);
}
void duan() interrupt 0 using 0
{

EX0 = 0;
ET0 = 1;
deng();
EX0 = 1;
}

void deng()
{

if(a == 20)
{a = 0;
P1 = i;
i = i<<1;
i = i+ 1;
if(i == 0xff)
i = 0xfe;
}
}
void timer() interrupt 1 using 0
{
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
a++;
}
我写的意思是要终端中执行流水灯,灯的间隔时间为1秒,用定时器实现。求各位大神帮忙看看怎么运行不对啊!谢谢了!
...全文
1456 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
li_rui_1220 2012-05-03
  • 打赏
  • 举报
回复
我的意思是,在进入中断然后执行流水灯,流水灯闪烁的时间用定时器实现,如果用延时函数时间不是不准确吗?所以用的是定时器去实现![Quote=引用 8 楼 的回复:]
定时器时间够了,去申请中断,lz就不要再开外部中断了!!单单用定时器中断就可以了!!!


把删掉~~~
***********************************
void duan() interrupt 0 using 0
{

EX0 = 0;
ET0 = 1;
deng();
EX0 = 1;
}
***********************……
[/Quote]
田子方 2012-05-01
  • 打赏
  • 举报
回复
我觉得你应该在ET0=1;的后面对TH0和TL0重新赋初值。
Xxxxxxx.. 2012-05-01
  • 打赏
  • 举报
回复
if(a==20)
{
a=0;
temp=_cror_(temp,1);
P0=temp;
if(temp==0xfe)
{
P0=temp;
TR0=0;
ET0=0;
TMOD=0x01;
EA=1; //开总中断
EX0=1; //开外部中断0
IT0=1; //中断触发为跳沿触发
}
}
这样可以,中断一次,就移动一次。而已移动过程为流水灯一个个来,最后仲会停系0Xfe度,等待中断再一次中断...
mudongwu 2012-05-01
  • 打赏
  • 举报
回复
定时器时间够了,去申请中断,lz就不要再开外部中断了!!单单用定时器中断就可以了!!!


把删掉~~~
***********************************
void duan() interrupt 0 using 0
{

EX0 = 0;
ET0 = 1;
deng();
EX0 = 1;
}
********************************




还有好多地方错的~~~~汗!!!!
Xxxxxxx.. 2012-05-01
  • 打赏
  • 举报
回复
我的灯系接P0口..如果要再中断就要再改进程序..
Xxxxxxx.. 2012-05-01
  • 打赏
  • 举报
回复
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar a=0,temp=0xfe;

void delay(uint time)
{
uint i,j;
for(i=time;i>0;i--)
for(j=100;j>0;j--)
{;}
}

void main()
{
while(1)
{
TMOD=0x01;
EA=1; //开总中断
EX0=1; //开外部中断0
IT0=1; //中断触发为跳沿触发
}
}

void duan() interrupt 0 using 0
{
EX0=0;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
TR0=1;
EA=1;
}

void timer() interrupt 1 using 0
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
a++;
P0=temp;
if(a==20)
{
a=0;
temp=_cror_(temp,1);
P0=temp;
if(temp==0xff)
temp=0xfe;
}
}
不知道楼主要的是不是这样的功能。 中断后,流水灯亮,而且按1秒时间去移动...如果系,你试试上面代码,我自己试调过,应该可以..
li_rui_1220 2012-04-27
  • 打赏
  • 举报
回复
开定时器0呀!我是这样想的,只有进入中断才才调用流水灯,在那个时候在开定时器啊!帮我想想那里出问题了吧!谢谢了,我刚开始接触单片机![Quote=引用 2 楼 的回复:]
void duan() interrupt 0 using 0
{

EX0 = 0;
ET0 = 0;//您是这个意思么?
deng();
EX0 = 1;
}
[/Quote]
lbing7 2012-04-27
  • 打赏
  • 举报
回复
void duan() interrupt 0 using 0
{

EX0 = 0;
ET0 = 0;//您是这个意思么?
deng();
EX0 = 1;
}
li_rui_1220 2012-04-27
  • 打赏
  • 举报
回复
求帮助啊!自己来顶下!!!!

27,375

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 单片机/工控
社区管理员
  • 单片机/工控社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧