2419 views|3 replies

1

Posts

0

Resources
The OP
 

MCU serial communication program help [Copy link]

I just learned about timers and serial ports. The project requires that after pressing key1, the LED lights will turn on for 5 seconds and then turn off (timer timing), then when the serial port receives 6, all the LED lights will turn on, then after pressing key2, the LED lights that were lit in front will turn off, and "Hello World!" will be sent to the serial port. Each of these three parts can be taken out separately as a small program, but there is a problem when these three parts are combined into one program.

After running this program I wrote, in the first part, after pressing key1, the LED lights up for five seconds and then turns off without any problem; in the second part, when the serial port receives 6, the LED lights can also be fully lit, but they will automatically turn off after a period of time, which should not be allowed to turn off according to the requirements; in the third part, after pressing key2, the LED lights that light up in the second part can turn off normally, but it will not send "Hello World!" to the serial port.
Please help me find out where the problem is.

#include <reg52.h>
#include <intrins.h> 
#include <stdio.h>

sbit key1=P3^0;
sbit key2=P3^1;

void delay(unsigned char z)//zms延时
{
	unsigned char i, j;
	while(z>0)
	{
		_nop_();
		i = 2;
		j = 199;
		do
		{
			while (--j);
		} while (--i);
		z--;
	}
}

void timer0Init()  //50ms
{
	TR0=1;
	TMOD|=0X01;
	TH0=(65535-46082)/256;
	TL0=(65535-46082)%256;
}
void UARTInit()
{
	EA=1;
	ES=1;
	REN=1;
	SM0=0; SM1=1;
	TR1=1;//打开定时器1
	TMOD|=0X20;
	TH1=0XFD;
	TL1=0XFD;
}
void main()
{ 		
	unsigned char msec;	

	while(1)
	{	
		UARTInit();	
		if(key1==0)
		{	
			delay(20); //消抖
			if(key1==0)
				while(!key1) ;//松手检测
			P1=0x00; 
			timer0Init();
		}	
		if(TF0==1)
		{
			TF0=0;
			msec++;
			TH0=(65535-46082)/256;
			TL0=(65535-46082)%256;
			if(msec==100)
			{
				P1=0XFF;
				TR0=0;
			}			
		}			
		if(key2==0)
		{	
			delay(20);
			if(key2==0)
				while(!key2) ;
			P1=0xff;
			TI=1;
			printf("Hello World!\n");
			while(!TI) ;
			TI=0;
		}				
	}
} 
void UART() interrupt 4
{
	unsigned char temp;
	if(RI)
	{
		RI=0;
		temp=SBUF;
		if(temp==6)
			P1=0x00;
	}
	if(TI)
		TI=0;
}

This post is from 51mcu

Latest reply

Don't use while(!key2); otherwise the microcontroller will not work.   Details Published on 2022-2-21 11:06
 

1w

Posts

25

Resources
2
 

UARTInit(); should be placed outside the loop

This post is from 51mcu
 
 
 

6841

Posts

11

Resources
3
 

You have to learn about state machines and use various states to determine what to do. If you don't mind, you can add me as a friend and I will teach you how to write it.

This post is from 51mcu
 
 
 

4005

Posts

0

Resources
4
 

Don't use while(!key2); otherwise the microcontroller will not work.

This post is from 51mcu
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

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