2557 views|5 replies

2

Posts

0

Resources
The OP
 

Use T0 timer to write back and forth water lamp, but why only one lamp continues to light up? [Copy link]

I used the T0 timer to write the water lamp back and forth, but only one lamp is on and the water does not flow. I don’t know what the problem is. Can you help me analyze it and see where I went wrong.

#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
	uint x;
	char tmp;
void delayms();
void main()
{
	uint i;
	tmp=0x7f;
	P2=tmp;
	delayms();
	while(1)
	{
		for(i=0;i<7;i++)
		{
			tmp=_cror_(tmp,1);
			P2=tmp;
			delayms();
		}
		for(i=7;i>0;i--)
		{
			tmp=_crol_(tmp,1);
			P2=tmp;
			delayms();
		}
	}
}

void delayms()
{
	TMOD=0x01;
	TH0=(65536-45872)/256;
	TL0=(65536-45872)%256;
	EA=1;
	ET0=1;
	TR0=1;
	while(1);
}

void time() interrupt 1
{
	TH0=(65536-45872)/256;
	TL0=(65536-45872)%256;
	x++;
	if(x==20)
	{
		x=0;
	}
}

This post is from 51mcu

Latest reply

You can't jump out without interruption, and of course it won't be executed later.   Details Published on 2020-7-15 15:41
 

785

Posts

0

Resources
2
 

What else can be done by adding whie(1); in delayms()?

This post is from 51mcu
 
Personal signature我从不担心我努力了不优秀,只担心优秀的人都比我更努力。如果你无法忍受孤独,就不要追逐梦想。每一个优秀的人,都有一段沉默的时光。在那一段时光,你付出了很多努力,忍受孤独和寂寞,不抱怨不诉苦,最后渡过了这
 
 

2

Posts

0

Resources
3
 
swustlx86 published on 2020-4-11 12:14 Add whie(1); to delayms(), what else can you do

Add while(1) to delayms() to wait for interruption. Now the interruption doesn't seem to have any effect.

This post is from 51mcu
 
 
 

7

Posts

0

Resources
4
 

So what if the interruption is over? Your program is still looping endlessly in delayms(). . . . And the interrupt function does nothing. . .

  • uint i;
  • tmp=0x7f;
  • P2=tmp;
  • delayms();
  • After executing 1 P2 output, it will enter the infinite loop of delayms. Anyway, you can judge the entire condition in while and jump out of the loop when the requirements are met.
This post is from 51mcu
 
 
 

16

Posts

0

Resources
5
 

You are writing interrupts and delays together and using them together. While (1) cannot appear in an interrupt or delay, it means that your program is stuck. Instead of adding delayms and interrupts (in fact, your interrupts are not running, the following two functions are useless), just write a delay() function with a delay of 1S.

This post is from 51mcu
 
 
 

7422

Posts

2

Resources
6
 

You can't jump out without interruption, and of course it won't be executed later.

This post is from 51mcu
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list