Hetai Cup-Serial Communication of Hetai MCU Project 5

Hetai Cup-Serial Communication of Hetai MCU Project 5


Preface

What is updated in this chapter is about the serial communication of Hetai MCU. The chip and board of the HT66F2390 are used, and the e-link download program is used for debugging. The program is modified and debugged with the help of Hetai official usat routines. The effect of the program is expected to send "Guangdong University of Technology MFT Microfluidic Control Team Hetai MCU HT66F2390 Serial Communication Experiment Debugging" to the serial debugging assistant at a fixed time of 1 second. The serial debugging assistant sends the corresponding characters, and the microcontroller returns the corresponding characters to the debugging assistant. Plus one sends 0x01 to light up LED1.


Tip: The following is the content of this article, the following cases are for reference

1. Usat routine analysis?

Let's take a look at the program first. There is nothing to say before, that is, the definition and declaration of data count, data length, etc., focusing on the part of conditional compilation below, which is also the part I marked.
Insert picture description here
Conditional compilation is used here, and the functions are as follows
#if const expression block 1
#else block 2
#endif
Its function is, if the value of the constant expression is true (not 0), then block 1 is compiled, otherwise Compile program segment 2. Therefore, the program can perform different functions under different conditions.
BRGH_0 in the routine has been macro-defined as 1 in uart.h. So here is the program _u0cr2=0b11101100;
Insert picture description here
look at the U0CR2 register again, its main function is to control the enable or disable of the transmitter, receiver and various UART0 interrupt sources. It’s explained in detail here. Those who are interested can look at the corresponding functions they have chosen in the technical manual so that they can have a deeper understanding.
Insert picture description here
For the function of sending a byte, if you want to send an array, you can define a string array, and then use the for loop to send it.

void UART_Send_Byte(unsigned char Data_byte)
{
    
    	
	_ur0e=0;
	while(_txif0==0);
	_txr_rxr0=Data_byte;
	_ur0e=1;
}

Receive data function

//	Receive serial data   接收一系列数据   
//  这里是一个中断函数
void __attribute((interrupt(0x3c))) UART_Receive(void)
{
    
    
	GCC_NOP();
	if(_ur0f==1)
	{
    
    
		if(_perr0==1||_oerr0==1||_nf0==1||_ferr0==1)
		{
    
    
			asm("lmov a,___txr_rxr0");	//read RXR0 register to clear RXIF0 bit
		}else
		{
    
    
			Rcv_Time_Counter=0;
			while(_rxif0==0);
			if(Data_Counter<16)
			{
    
    
				Rcv_Data[Data_Counter] = _txr_rxr0;	
				Data_Counter++;
			}else
			{
    
    
				Data_Counter=0;
				Rcv_Data[Data_Counter] = _txr_rxr0;	
			}
			
		}
		_ur0f=0;
	}
}

Two, write the program

Main function

#include "HT66F2390.h"
#include "delay.h"
#include "uart.h"
#include "LED.h"
#include "string.h"   //调用strlen函数要引入字符串的头文件

void main()
{
    
    
	int t=0,i=0;    //用于计时
	char date[]="广东工业大学MFT团队合泰单片机串口实验";
	LED_Init();  //LED初始化
	UART_SET();   //串口函数初始化
	while(1)
	{
    
    
		GCC_CLRWDT();   //清除看门狗
		delay_ms(10);
		t++;
		if(RECEIVE_Finish_Flag==1)
		{
    
    
			if(Rcv_Data[0]==0x01)
			{
    
    
				LED1 =!LED1;
			}
			for(i=0;i<RCV_Data_Length;i++)UART_Send_Byte(Rcv_Data[i]);
			RECEIVE_Finish_Flag=0;   //清除接收标志位		
		}
		if(t==100)
		{
    
    
			for(i=0;i<strlen(date);i++)
			{
    
    
				UART_Send_Byte(date[i]);
			}
			LED2 =!LED2;    //LED2取反用来显示工作是否正常
			t=0;
		}
		GCC_CLRWDT();   //清除看门狗
	}

}

to sum up

This article of serial communication is here for the time being, because it has not been downloaded for debugging, so there may be errors, which will be changed in time. The next article is the timer section.
The project file will be uploaded here after it is downloaded and debugged.

Guess you like

Origin blog.csdn.net/newpeopie/article/details/114382794