STM32 cubemx 에서 무원 버저 출력 PWM 웨이브 를 설정하고 음악 코드 를 두 마리 호랑이

2935 단어 STM32
이전 문서 코드 링크:https://blog.csdn.net/qq_42153903/article/details/88714155 enum { UNUSED=0, DO, RE, MI, FA, SO, LA, XI, DO1, RI1, TONE_MAX, }; uint16_t tone_freq[TONE_MAX] = {0, 1047, 1175, 1319, 1397, 1568, 1760, 1967, 4186, 4698 }; typedef struct tone_s { int tone; int delay_ms;
} tone_t;
#define DEF_DELAY 350 static tone_t tiger_notation[]= { {DO, DEF_DELAY}, {RE, DEF_DELAY}, {MI, DEF_DELAY}, {DO, DEF_DELAY*2},
{DO, DEF_DELAY},
{RE, DEF_DELAY},
{MI, DEF_DELAY},
            {DO, DEF_DELAY*2},

            {MI, DEF_DELAY},
            {FA, DEF_DELAY},
            {SO, DEF_DELAY*2},

            {MI, DEF_DELAY},
            {FA, DEF_DELAY},
            {SO, DEF_DELAY*2},

            {SO, DEF_DELAY},
            {LA, DEF_DELAY},
            {SO, DEF_DELAY},
            {FA, DEF_DELAY},
            {MI, DEF_DELAY},
            {DO, DEF_DELAY*2},

            {SO, DEF_DELAY},
            {LA, DEF_DELAY},
            {SO, DEF_DELAY},
            {FA, DEF_DELAY},
            {MI, DEF_DELAY},
            {DO, DEF_DELAY*2},

            {RE, DEF_DELAY},
            {SO, DEF_DELAY},
            {DO, DEF_DELAY*2},

            {RE, DEF_DELAY},
            {SO, DEF_DELAY},
            {DO, DEF_DELAY*2},

}; #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) int turn_passive_beep(int cmd, int freq) { int range; if(OFF == cmd) { HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_2) ; } else { if(freq<1000 || freq>5000) { printf(“Beep set invalid PWM frequency[%d]!”, freq); return -1; }
    /* Set PWM clock: 80MHz/169=500KHz */
                    htim2.Init.Prescaler = 160-1;



    /* Set PWM frequency */
                    range=500000/freq;
                    htim2.Init.Period = range;
                    if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
                    {
                            Error_Handler();
                    }
    /* Set PWM duty 50% */
                    TIM_OC_InitTypeDef sConfigOC;

                    sConfigOC.OCMode = TIM_OCMODE_PWM1;
                    sConfigOC.Pulse = (htim2.Init.Period/3);
                    sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
                    sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
                    if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
                    {
                            Error_Handler();
                    }
                    HAL_TIM_MspPostInit(&htim2);

                    HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
}
    return 0;

} static inline void play_tone(int tone, int delay) { if(toneRI1) return ;
            turn_passive_beep(ON, tone_freq[tone]);

            Delay_ms(delay);
turn_passive_beep(OFF, 0);

} void play_tiger(void) { int i;
for(i=0; i

}

좋은 웹페이지 즐겨찾기