STM32CubeMX(06) STM32 controls different switches to achieve the electronic organ function.

Preface

Today I want to learn how to control different switches with STM32 to realize the function of the electronic organ.

Hardware: STM32ZET6
Software: keil 5

1. Experimental Contents

To achieve the function of the electronic piano and control different switches, you can achieve different frequency audio output, and complete the performance of the song "Flash, Shine and Crystal".

After the recommended circuit is connected, ==PB.14 (ch2 of TIM1)==CTRL of the input pin of the control buzzer. Eight switches can be used as audio output control switches of different frequencies.

2. Circuit Connections

We can see that 8 keys are connected to 8 IO ports, which is to raise the level. This means that when we press down, the IO port will become low level. This is also one of the bases of our software judgment.
Here are the PC0 to PC7, eight switches.

3. Principles of Pronunciation

1: If the frequency of PWM falls in the pronunciation area of the AC buzzer, the AC buzzer can emit tones, adjust the duty cycle of the PWM, and adjust the volume.
So it's very simple. We just need to change the frequency of the PWM wave. We can emit eight tones that we commonly use.


So how to change the frequency is definitely to change the value of PSC, ARR. Let's just change one. Just ARR. Frequent crossover coefficients are not good, so you can change the value of the counter.
Let's use the HAL library to help us write the library functions.
__HAL_TIM_SET_AUTORELOAD(&htim1,arr);
This function changes the value of the reload directly. Of course, the last principle must be to change the register directly. The official has already packaged it for us, so I can use it directly. Pass two parameters, one timer number, the other is the value of automatic reload.

Functions like these also have



Set the duty cycle.

Sets the count value. This is used more in input capture.

IV. STM32cubeMX Configuration

So many things have been said before, RCC, SYS, clock tree, we should make light cars familiar.
Fixed one configuration. Do not talk about the three above. The three above can see how to configure the previous one.
Let's talk about our key part.

4.1 Configure GPIO

First, configure our PC0 - PC7 as input mode. Pull up the input.
Because our keys are our external input, we need to check the pin status.
Then give a name.


The same is true for the other seven.

4.2 Configure Timer

Experiments clearly indicate that timer 1.Channel 2, we initially initialized at 1kHz with a duty cycle of 20%
His original GPIO was PB0, we need to remap to PB14 according to the experimental requirements


So here we're done. We're building the project. Depending on the demand, we need a timer to output PWM waves, and an IO port.

V. Software Section

In the software section, we keep checking which state of the eight pins has changed and we will perform which operation.
Let's just write one that's easy to understand
First defined in main.h header file, read our pin status

Then in the main.c file, define the key scan function and the function to modify the load value.
So how do these numbers come about? For example, 168M's main frequency, the crossover factor is 168, then it becomes 1MHz, then 1M is divided by 3816->1000000/3816=262, is that the tone of our DO, and so on.


Finally, call our Playmusic function in our main function. Note that the timer opening function must be accompanied by HAL_TIMEx_PWMN_Start (&htim1, TIM_CHANNEL_2);.Look at the code below.

/* USER CODE BEGIN 0 */
uint8_t KeyScan(void)
{
 if(KEY_0==0||KEY_1==0||KEY_2==0||KEY_3==0||KEY_4==0||KEY_5==0||KEY_6==0||KEY_7==0)
 {
  HAL_Delay(20);
	if(KEY_0==0) return KEY_0_PRES;
	else if(KEY_1==0) return KEY_1_PRES;
  else if(KEY_2==0) return KEY_2_PRES;
  else if(KEY_3==0) return KEY_3_PRES;
  else if(KEY_4==0) return KEY_4_PRES;
  else if(KEY_5==0) return KEY_5_PRES;
  else if(KEY_6==0) return KEY_6_PRES;
  else if(KEY_7==0) return KEY_7_PRES;
 }else if(KEY_0==1||KEY_1==1||KEY_2==1||KEY_3==1||KEY_4==1||KEY_5==1||KEY_6==1||KEY_7==1) return 8;
 return 9;

}

void PlayMusic(void)
{
  switch(KeyScan())
	{
	    case KEY_0_PRES:arr=3816;__HAL_TIM_SET_AUTORELOAD(&htim1,arr);break;
		case KEY_1_PRES:arr=3401;__HAL_TIM_SET_AUTORELOAD(&htim1,arr);break;
		case KEY_2_PRES:arr=3030;__HAL_TIM_SET_AUTORELOAD(&htim1,arr);break;
		case KEY_3_PRES:arr=2865;__HAL_TIM_SET_AUTORELOAD(&htim1,arr);break;
		case KEY_4_PRES:arr=2551;__HAL_TIM_SET_AUTORELOAD(&htim1,arr);break;
		case KEY_5_PRES:arr=2272;__HAL_TIM_SET_AUTORELOAD(&htim1,arr);break;
		case KEY_6_PRES:arr=2032;__HAL_TIM_SET_AUTORELOAD(&htim1,arr);break;
		case KEY_7_PRES:arr=1912;__HAL_TIM_SET_AUTORELOAD(&htim1,arr);break;
		default :       arr=0;__HAL_TIM_SET_AUTORELOAD(&htim1,arr);break;
	}
  
}
/* USER CODE END 0 */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_TIM1_Init();
  /* USER CODE BEGIN 2 */
  HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_2);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */
     PlayMusic();
  }
  /* USER CODE END 3 */

}

summary

Today's summary is here, I hope to help you.

Tags: Embedded system stm32 STM32CUBEMX

Posted on Sat, 18 Sep 2021 22:19:48 -0400 by chixsilog