Lanqiao Cup Embedded-LED Operation Summary

This article is a summary of my review stage. I have written review notes, and I am happy to share it. Xiaobai can also watch it and get started faster.

LED initialization

The initialization part is relatively simple, not the point, directly paste the code:

void  LED_Init(void)
{
    
    
  GPIO_InitTypeDef GPIO_InitStructure;
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD,ENABLE);
  // RCC_AHBPeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD,ENABLE);//时钟使能
 
 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
 GPIO_Init(GPIOC,&GPIO_InitStructure);
 
 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
 GPIO_Init(GPIOD,&GPIO_InitStructure);
}

Two different LED drive codes

The first method is to write driver functions.

void LED_Control(u16 cont,int status)
{
    
    
 if(status==1)
 {
    
    
   GPIO_ResetBits(GPIOC,cont<<8);
   GPIO_SetBits(GPIOD,GPIO_Pin_2);
   GPIO_ResetBits(GPIOD,GPIO_Pin_2);
 }
  if(status!=1)
 {
    
    
   GPIO_SetBits(GPIOC,cont<<8);
   GPIO_SetBits(GPIOD,GPIO_Pin_2);
   GPIO_ResetBits(GPIOD,GPIO_Pin_2);
 }
 
}

This writing method is more convenient, just use it directly. For details on how to write, please refer to the link description added by this big guy.
Here we need to mention that GPIO_ResetBits() and GPIO_SetBits() are operations on GPIOx_BRR and GPIOx_BSRR. I posted the introduction of these two registers below. **For details, please refer to the Chinese version of the STM32 Reference Manual. **It can be seen that the BSRR register can operate on one bit independently without affecting other bits. The simple explanation is that only one LED light can be controlled.
Insert picture description here
Insert picture description here

The second method is to directly manipulate the register

  GPIOC-> ODR = 0XFFFF;
  GPIOD-> ODR |= (1<<2);
  GPIOD-> ODR &=~(1<<2);

This is the initialization code, the specific function is to turn off all the LED lights.
The ODR register used here is different from the BSRR register. You can see that the ODR register operates on all bits. Therefore, 0XFFFF can achieve the effect of turning off all the LED lights.
Insert picture description here

Comparison of the two methods

At first I used the first function method when I learned it because of its convenience. The second method is uncomfortable. After all, operating library functions is more intuitive and convenient than operating registers, and you have to remember so many bit operations. But if you study in depth, you will find that the second method has its advantages.

See the eighth provincial contest mock questions for details.
Insert picture description here
It can be seen that the two standards of temperature and humidity can exceed their upper limits at the same time, that is, when L1 is flashing, L2 can also flash, and so does L3. The states of the three lights can be switched independently without affecting each other, which is a difficult point. If you use the first method to write a function, it will be very tedious to realize the flashing of the lights, and you need to configure many functions, and it is more difficult to switch from one flashing state to two flashing together, because I said above The BSRR register can operate on one bit alone. If you want to convert to two bits, it will not be smooth. In short, it is difficult for the function to switch 3 lights independently. You can give it a try.
But operating the register will be very easy.

	u16 LED_MODE = 0XFFFF;
   if(LED_Flag)
   {
    
    
     if(Temperature > TH_compare)//当达到温度上限
     {
    
    
       LED_MODE ^= (1<<8);//实现灯的状态反转
     }else			//没有达到温度上限
     {
    
    
       LED_MODE |= (1<<8);//熄灭
     }
     if(RH > RH_cpmpare)
     {
    
    
       LED_MODE ^= (1<<9);
     }else
     {
    
    
       LED_MODE |= (1<<9);
     }
     GPIOC-> ODR = LED_MODE;
     GPIOD-> ODR |= (1<<2);
     GPIOD-> ODR &=~(1<<2);
     LED_Flag = 0;
   }

This involves bit operations.

int led_mode =0xffff
led_mode &=~(1<<2);//第三位置零,计算结果为led_mode=0xfffB
led_mode |=(1<<2);//第三位置1,计算结果为led_mode=0xffff
led_mode ^=(1<<2);//第三位取反,计算结果为led_mode=0xfffB
led_mode ^=(1<<2);//第三位再次取反,计算结果为led_mode=0xffff

It can be seen that the state of the lamp can be reversed with only one function. And the most important thing is that the status of each lamp will not affect each other. You can do it yourself to deepen your understanding.

to sum up

I suggest you try both methods, so you can understand better. When getting started, use the function method first, and then learn how to operate the register.

Guess you like

Origin blog.csdn.net/qq_43690936/article/details/105100317