Lighting brightness control of serial communication usart

Lighting brightness control of serial communication usart

1, Communication principle

1. The communication interface mode between processor and external equipment is divided into serial communication and parallel communication

***Serial communication * * * refers to the transmission of each bit of data of the register to the receiving equipment in turn on a common data line;
***Parallel communication refers to the simultaneous transmission of each bit of data to the receiving end through multiple data lines.

2. In serial communication, according to the transmission direction of data, it is divided into simplex, half duplex and full duplex

Simplex data transmission only supports data transmission in one direction, as shown in figure (a);

Half duplex data transmission allows data to be transmitted in two directions, but at a certain time, data is only allowed to be transmitted in one direction. It is actually a simplex communication of switching directions, as shown in figure (b);

Full duplex data communication allows data to be transmitted in two directions at the same time. Therefore, full duplex communication is the combination of two simplex communication modes. It requires that both transmitting equipment and receiving equipment have independent receiving and transmitting capabilities, as shown in figure (c).

3. Synchronous transceiver and asynchronous transceiver

Synchronization means that when a process executes a request, if the request takes a period of time to return information, the process will wait until it receives the return information;
Asynchrony means that a process does not need to wait all the time, but continues to perform the following operations, regardless of the state of other processes. When a message returns, the system will notify the process to process it, which can improve the efficiency of execution.

4. Classification of common communication modes

Common serial communication modes:

2, Serial communication

1. Difference between USART and UART:

Click the link to view
Explanation from Baidu

Explanation from electronics enthusiasts

2. Serial communication wiring mode


TTL level and 232 level are introduced here
TTL level is the abbreviation of transistor transistor logic, which is the standard technology of communication between various parts of the equipment controlled by computer processor. TTL level signal is widely used because its data representation adopts binary specification, + 5V is equivalent to logic "1", and 0V is equivalent to logic "0".
In the digital circuit, the level of the circuit composed of TTL electronic components is a voltage range, which stipulates:
Output high level > = 2.4V, output low level < = 0.4V;
Input high level > = 2.0V, input low level < = 0.8V.

RS232 level is a standard of serial port; On TXD and RXD data lines:
Logic 1 is a voltage of - 3~-15V
Logic 0 is a voltage of 3~15V

Due to the different level logic, it needs to be converted. For example, I speak Chinese and you speak English, and CH340 is used as our direct translation. The following figure is CH340 chip. At the same time, USB-232 reception can be used to realize the communication between PC and ARM. Therefore, when we carry out serial communication, we should first consider whether to install CH340 driver.

The picture shows the development board of punctual atomic explorer

3. Principle block diagram of serial communication

4. Baud rate

In the field of electronic communication, Baud (modulation rate) refers to the rate at which the effective data signal modulates the carrier, that is, the number of carrier modulation state changes in unit time. [1]
Baud rate refers to the number of symbol symbols transmitted per second. It is a measure of symbol transmission rate. It is expressed by the number of carrier modulation state changes per unit time. 1 baud refers to the transmission of 1 symbol per second. Baud (unit symbol: Bd) is named after E mile Baudot (1845-1903), a French telecommunications engineer. He is one of the pioneers of digital communication and the inventor of telex and Baudot telegraph

5. Explanation of serial port program of library function version (serial port communication light control program)

Main program

//Main function
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "beep.h"
#include "pwm.h"

int pow(int x,int y)
{
		int i;
		int val=1;
		for(i=0;i<y;i++)
		{
			val = x*val;
		}
		return val;
}
int main(void)
{ 
		int a=0; 
		int n;
		u8 t=0;
		u8 len;	
		NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
		delay_init(168);		
		uart_init(115200);	
		LED_Init();		  		
    BEEP_Init();
		TIM14_PWM_Init(500-1,84-1);	
		while(1)
		{
			while(USART_RX_STA&0x8000)
			{	
				a=0;
				len=USART_RX_STA&0x3fff;//µÃµ½´Ë´Î½ÓÊÕµ½µÄÊý¾Ý³¤¶È
				for(t=0;t<len;t++)
				{
					if(USART_RX_BUF[t]<48||USART_RX_BUF[t]>57)
					{
						printf("\r\n The message you sent is:\r\n");
						for(t=0;t<len;t++)
						{
							USART_SendData(USART1, USART_RX_BUF[t]);  
							while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//µÈ´ý·¢ËͽáÊø
						}
						printf("\r\n Please enter 0-300 Integer between\r\n");
						bibi();
						USART_RX_STA=0;
						break;
					}	
				}
				if(USART_RX_STA==0)
				{
					break;
				}
				printf("\r\n The message you sent is:\r\n");
				n = len;
				a=0;
				for(t=0;t<len;t++)
				{
					n--;
					a= a + (USART_RX_BUF[t]-48)*pow(10,n);
					USART_SendData(USART1, USART_RX_BUF[t]);         
					while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
				}
				if(a<0 || a>300)
				{
					printf("\r\n Please enter 0-300 Integer between\r\n");
					bibi();
					a=0;
					USART_RX_STA=0;
					break;
				}
				printf("\r\n The light intensity is%d\r\n",a);
				printf("\r\n\r\n");//
				USART_RX_STA=0;
			}
			TIM_SetCompare1(TIM14,a);	//
		}
}

usart code

#include "sys.h"
#include "usart.h"	

#if SYSTEM_SUPPORT_OS
#include "includes.h"					
#endif

  
#if 1
#pragma import(__use_no_semihosting)             
//±ê×¼¿âÐèÒªµÄÖ§³Öº¯Êý                 
struct __FILE 
{ 
	int handle; 
}; 

FILE __stdout;       
  
_sys_exit(int x) 
{ 
	x = x; 
} 
//Öض¨Òåfputcº¯Êý 
int fputc(int ch, FILE *f)
{ 	
	while((USART1->SR&0X40)==0);
	USART1->DR = (u8) ch;      
	return ch;
}
#endif
 
#if EN_USART1_RX   
	
u8 USART_RX_BUF[USART_REC_LEN];   

u16 USART_RX_STA=0;       	
void uart_init(u32 bound){
   
  GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
 
	
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); 
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); 
	
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; //GPIOA9ÓëGPIOA10
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸´Óù¦ÄÜ
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	//ËÙ¶È50MHz
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //ÍÆÍ츴ÓÃÊä³ö
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //ÉÏÀ­
	GPIO_Init(GPIOA,&GPIO_InitStructure); //³õʼ»¯PA9£¬PA10

   //USART1 ³õʼ»¯ÉèÖÃ
	USART_InitStructure.USART_BaudRate = bound;//²¨ÌØÂÊÉèÖÃ
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö³¤Îª8λÊý¾Ý¸ñʽ
	USART_InitStructure.USART_StopBits = USART_StopBits_1;//Ò»¸öֹͣλ
	USART_InitStructure.USART_Parity = USART_Parity_No;//ÎÞÆæżУÑéλ
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎÞÓ²¼þÊý¾ÝÁ÷¿ØÖÆ
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;	//ÊÕ·¢Ä£Ê½
  USART_Init(USART1, &USART_InitStructure); //³õʼ»¯´®¿Ú1
	
  USART_Cmd(USART1, ENABLE);  //ʹÄÜ´®¿Ú1 
	
	//USART_ClearFlag(USART1, USART_FLAG_TC);
	
#if EN_USART1_RX	
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//¿ªÆôÏà¹ØÖжÏ

	//Usart1 NVIC ÅäÖÃ
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//´®¿Ú1ÖжÏͨµÀ
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//ÇÀÕ¼ÓÅÏȼ¶3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority =1;		//×ÓÓÅÏȼ¶3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQͨµÀʹÄÜ
	NVIC_Init(&NVIC_InitStructure);	//¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷¡¢

#endif
	
}

void USART1_IRQHandler(void)                	//´®¿Ú1ÖжϷþÎñ³ÌÐò
{
	u8 Res;
#if SYSTEM_SUPPORT_OS 		//Èç¹ûSYSTEM_SUPPORT_OSΪÕ棬ÔòÐèÒªÖ§³ÖOS.
	OSIntEnter();    
#endif
	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)  
	{
		Res =USART_ReceiveData(USART1);//(USART1->DR);	//¶ÁÈ¡½ÓÊÕµ½µÄÊý¾Ý
		
		if((USART_RX_STA&0x8000)==0)//½ÓÊÕδÍê³É
		{
			if(USART_RX_STA&0x4000)//½ÓÊÕµ½ÁË0x0d
			{
				if(Res!=0x0a)USART_RX_STA=0;//½ÓÊÕ´íÎó,ÖØпªÊ¼
				else USART_RX_STA|=0x8000;	//½ÓÊÕÍê³ÉÁË 
			}
			else //»¹Ã»ÊÕµ½0X0D
			{	
				if(Res==0x0d)USART_RX_STA|=0x4000;
				else
				{
					USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
					USART_RX_STA++;
					if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;  
				}		 
			}
		}   		 
  } 
#if SYSTEM_SUPPORT_OS 	//Èç¹ûSYSTEM_SUPPORT_OSΪÕ棬ÔòÐèÒªÖ§³ÖOS.
	OSIntExit();  											 
#endif
} 
#endif	

 pwm code
```c
#include "pwm.h"
#include "led.h"
#include "usart.h"
 

void TIM14_PWM_Init(u32 arr,u32 psc)
{		 					 
	//´Ë²¿·ÖÐèÊÖ¶¯ÐÞ¸ÄIO¿ÚÉèÖÃ
	
	GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;
//  NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE);  	//TIM14ʱÖÓʹÄÜ    
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); 	//ʹÄÜPORTFʱÖÓ	
	
	GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14); //GPIOF9¸´ÓÃΪ¶¨Ê±Æ÷14
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;           //GPIOF9
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;        //¸´Óù¦ÄÜ
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;	//ËÙ¶È100MHz
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;      //ÍÆÍ츴ÓÃÊä³ö
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;        //ÉÏÀ­
	GPIO_Init(GPIOF,&GPIO_InitStructure);              //³õʼ»¯PF9
	  
	TIM_TimeBaseStructure.TIM_Prescaler=psc;  //¶¨Ê±Æ÷·ÖƵ
	TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //ÏòÉϼÆÊýģʽ
	TIM_TimeBaseStructure.TIM_Period=arr;   //×Ô¶¯ÖØ×°ÔØÖµ
	TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; 
	
	TIM_TimeBaseInit(TIM14,&TIM_TimeBaseStructure);//³õʼ»¯¶¨Ê±Æ÷14
	
	//³õʼ»¯TIM14 Channel1 PWMģʽ	 
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 
 	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; 
	TIM_OC1Init(TIM14, &TIM_OCInitStructure);  

	TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable);  
 
  TIM_ARRPreloadConfig(TIM14,ENABLE);//ARPEʹÄÜ 
	TIM_Cmd(TIM14, ENABLE);  //ʹÄÜTIM14									  
}  

6. Experimental phenomenon

Lamp brightness control of serial communication

Posted by matbennett on Mon, 18 Apr 2022 03:18:24 +0930