#include "stm32f1xx_hal.h"
// ??LED?? - ????????
#define LED1_PIN GPIO_PIN_0
#define LED1_PORT GPIOA
#define LED2_PIN GPIO_PIN_1
#define LED2_PORT GPIOA
#define LED3_PIN GPIO_PIN_2
#define LED3_PORT GPIOA
#define LED4_PIN GPIO_PIN_3
#define LED4_PORT GPIOA
#define LED5_PIN GPIO_PIN_4
#define LED5_PORT GPIOA
#define LED6_PIN GPIO_PIN_5
#define LED6_PORT GPIOA
#define LED7_PIN GPIO_PIN_6
#define LED7_PORT GPIOA
#define LED8_PIN GPIO_PIN_7
#define LED8_PORT GPIOA
// ???????
#define BUZZER_PIN GPIO_PIN_8
#define BUZZER_PORT GPIOA
// ?????? (Hz)
#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
#define NOTE_A4 440
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_REST 0 // ???
// ????????
#define WHOLE_NOTE 1600
#define HALF_NOTE WHOLE_NOTE/2
#define QUARTER_NOTE WHOLE_NOTE/4
#define EIGHTH_NOTE WHOLE_NOTE/8
#define SIXTEENTH_NOTE WHOLE_NOTE/16
void HAL_delay_US(uint32_t us) ;
// «??»?????? (??)
typedef struct {
int note;
int duration;
int led; // ?????LED (1-8)
} MelodyNote;
MelodyNote qingtian[] = {
{NOTE_E4, EIGHTH_NOTE, 1},
{NOTE_G4, EIGHTH_NOTE, 2},
{NOTE_C5, QUARTER_NOTE, 3},
{NOTE_B4, EIGHTH_NOTE, 4},
{NOTE_G4, EIGHTH_NOTE, 5},
{NOTE_A4, QUARTER_NOTE, 6},
{NOTE_G4, HALF_NOTE, 7},
{NOTE_E4, QUARTER_NOTE, 8},
{NOTE_REST, QUARTER_NOTE, 0}, // ???,??LED??
// ??????????...
};
// ???GPIO
void MX_GPIO_Init(void) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
// ??LED?????
GPIO_InitStruct.Pin = LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN |
LED5_PIN | LED6_PIN | LED7_PIN | LED8_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// ???????
GPIO_InitStruct.Pin = BUZZER_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
HAL_GPIO_Init(BUZZER_PORT, &GPIO_InitStruct);
}
// ????LED?????LED
void SetLED(int ledNum) {
// ?????LED
HAL_GPIO_WritePin(LED1_PORT, LED1_PIN, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED2_PORT, LED2_PIN, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED3_PORT, LED3_PIN, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED4_PORT, LED4_PIN, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED5_PORT, LED5_PIN, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED6_PORT, LED6_PIN, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED7_PORT, LED7_PIN, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED8_PORT, LED8_PIN, GPIO_PIN_RESET);
// ?????LED
switch(ledNum) {
case 1: HAL_GPIO_WritePin(LED1_PORT, LED1_PIN, GPIO_PIN_SET); break;
case 2: HAL_GPIO_WritePin(LED2_PORT, LED2_PIN, GPIO_PIN_SET); break;
case 3: HAL_GPIO_WritePin(LED3_PORT, LED3_PIN, GPIO_PIN_SET); break;
case 4: HAL_GPIO_WritePin(LED4_PORT, LED4_PIN, GPIO_PIN_SET); break;
case 5: HAL_GPIO_WritePin(LED5_PORT, LED5_PIN, GPIO_PIN_SET); break;
case 6: HAL_GPIO_WritePin(LED6_PORT, LED6_PIN, GPIO_PIN_SET); break;
case 7: HAL_GPIO_WritePin(LED7_PORT, LED7_PIN, GPIO_PIN_SET); break;
case 8: HAL_GPIO_WritePin(LED8_PORT, LED8_PIN, GPIO_PIN_SET); break;
case 0: // ???,??LED??
default: break;
}
}
// ?????????(??????)
void Beep(int frequency, int duration) {
if (frequency == NOTE_REST) {
HAL_GPIO_WritePin(BUZZER_PORT, BUZZER_PIN, GPIO_PIN_RESET);
HAL_Delay(duration);
return;
}
int period = 1000000 / frequency; // ??(??)
int halfPeriod = period / 2;
int cycles = duration * 1000 / period;
for (int i = 0; i < cycles; i++) {
HAL_GPIO_WritePin(BUZZER_PORT, BUZZER_PIN, GPIO_PIN_SET);
HAL_delay_US(halfPeriod);
HAL_GPIO_WritePin(BUZZER_PORT, BUZZER_PIN, GPIO_PIN_RESET);
HAL_delay_US(halfPeriod);
}
}
// ???????LED
void PlayMelody(MelodyNote *melody, int length) {
for (int i = 0; i < length; i++) {
SetLED(melody[i].led); // ????LED
Beep(melody[i].note, melody[i].duration); // ????
}
}
int main(void) {
HAL_Init();
MX_GPIO_Init();
// ??????
int melodyLength = sizeof(qingtian) / sizeof(qingtian[0]);
while (1) {
PlayMelody(qingtian, melodyLength);
HAL_Delay(2000); // ????????2????
}
}
// ???????(????)
void HAL_delay_US(uint32_t us)
{
uint32_t ticks=us*(SystemCoreClock /1000000);
uint32_t start = SysTick ->VAL ;
while ((SysTick->VAL - start + (SystemCoreClock / 1000000)) % (SystemCoreClock / 1000000) < ticks);
}
最新发布