STM32杂交版(HAL库、音乐盒、闹钟、点阵屏、温湿度)

一、设计描述        

        本设计精心构建了一个以STM32MP157A高性能单片机为核心控制单元的综合性嵌入式系统。该系统巧妙融合了蜂鸣器、数码管显示器、点阵屏、温湿度传感器、LED指示灯以及按键等多种外设模块,形成了一个功能丰富、操作便捷的杂交版智能设备。通过串口通信,用户可以灵活地切换系统的工作模式,轻松实现闹钟、音乐盒播放及温湿度监测与调控等基本功能。

核心硬件

  • 主控单元:采用STM32MP157A单片机,凭借其强大的处理能力和丰富的外设接口,为系统提供了坚实的硬件基础。

软件平台

  • 开发工具:利用STM32CUBEIDE这一直观易用的集成开发环境,极大地提升了软件编程与调试的效率,确保了系统软件的稳定可靠。

系统功能亮点

  1. 模式灵活切换:通过串口通信,用户可以轻松地在闹钟、音乐盒播放及温湿度监测三种模式之间自由切换,满足不同场景下的使用需求。

  2. 动态信息显示:点阵屏作为系统的信息展示窗口,能够根据当前的工作模式显示相应的汉字(如“钟”代表闹钟模式,“音”代表音乐盒模式,“传”可视为温湿度监测的简化标识),为用户提供了直观的操作反馈。

  3. 按键交互体验:设计中充分考虑了用户的交互体验,通过按键即可在各模式下执行对应的功能操作,如音乐盒的速度与音量调节、歌曲切换、暂停/播放控制,以及闹钟的时间调整、设置多个闹钟、关闭闹钟等。

  4. 温湿度智能调控:系统内置温湿度传感器,能够实时监测环境状况,并通过串口接收用户指令调节温湿度的上下限阈值。一旦环境参数超出设定范围,LED指示灯将亮起作为边界提示,帮助用户及时采取措施。

二、基本配置信息

         音乐盒在之前做过所以配置不做改变:STM32音乐盒

        

三、STM32CUBEIDE配置

1、定时器--100ms

2、PWM配置(蜂鸣器 -- PB6)

3. 串口配置

注意针脚

4. IIC配置(温湿度,数码管,点阵屏)

5. GPIO配置(LED和按键)

6. NVIC

四、程序编写

(1)音乐盒代码

        音乐盒在之前已经写过,所以这里不再重复之前的操作,我们将串口和模式转换加进去。

         STM32音乐盒

        串口音乐控制函数


//串口音乐控制函数
void music_kz(){
	  if(EN_music == 1)//启动
		  play_music(list,Low_volume);
	  else
		  __HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_1,0);//设置音量



	if(strcmp("music volume increase",(char *)uart4_data)==0){
		uart4_data[0] = '0';
		Low_volume = Low_volume + Low_volume_cnt;
		if(Low_volume >= 10)
			Low_volume = 10;
	}

	if(strcmp("music volume reduction",(char *)uart4_data)==0){
		Low_volume = Low_volume - Low_volume_cnt;
		if(Low_volume <= 0)
			Low_volume = 0;
	}


	if(strcmp("music speed increase",(char *)uart4_data)==0){
		uart4_data[0] = '0';
		music_speed_i++;
		music_speed_i = music_speed_kz(music_speed_i);
	}
	if(strcmp("music speed reduction",(char *)uart4_data)==0){
		uart4_data[0] = '0';
		music_speed_i--;
		music_speed_i = music_speed_kz(music_speed_i);
	}

	if(strcmp("music next song",(char *)uart4_data)==0){
		uart4_data[0] = '0';
		list++;
		if(list > list_max){
			list = list_max;
		}
	}
	if(strcmp("music previous song",(char *)uart4_data)==0){
		list--;
		uart4_data[0] = '0';
		if(list < 0){
			list = 0;
		}
	}

	if(strcmp("music start",(char *)uart4_data)==0){
		EN_music = 1;
	}
	if(strcmp("music stop",(char *)uart4_data)==0){
		EN_music = 0;
	}


}

按键模式控制

用mode变量代表模式,后面三个按键同理。


void EXTI0_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI0_IRQn 0 */

	if(HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_0) == 0 && mode == 0)//确保数据稳定
	{

		//每次按下解决 音量�??????? Low_volume_cnt
		Low_volume = Low_volume + Low_volume_cnt;
		if(Low_volume >= 10)
			Low_volume = 0;
	}

	if(HAL_GPIO_ReadPin(GPIOG,GPIO_PIN_0)==GPIO_PIN_RESET && mode == 1) {

		shi_clock++;
		fen_shi_clock=fen_clock/10;
		fen_ge_clock=fen_clock%10;
		shi_shi_clock=shi_clock/10;
		shi_ge_clock=shi_clock%10;
		if(shi_clock>=24)
		{
			shi_clock=0;
		}

		miao_shi_clock=miao_clock/10;
		miao_ge_clock=miao_clock%10;
		fen_shi_clock=fen_clock/10;
		fen_ge_clock=fen_clock%10;
		shi_shi_clock=shi_clock/10;
		shi_ge_clock=shi_clock%10;
		buf[0]=smg_number[shi_shi_clock];
		buf[1]=smg_number[shi_ge_clock];
		buf[3]=smg_number[fen_shi_clock];
		buf[4]=smg_number[fen_ge_clock];
		buf[6]=smg_number[miao_shi_clock];
		buf[7]=smg_number[miao_ge_clock];
		}
  /* USER CODE END EXTI0_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
  /* USER CODE BEGIN EXTI0_IRQn 1 */

  /* USER CODE END EXTI0_IRQn 1 */
}

(2)模式切换

        mode变量切换


void uart_mode(){

	if(strcmp("mode = music",(char *)uart4_data)==0){
		mode = 0;
	}
	if(strcmp("mode = clock",(char *)uart4_data)==0){
		mode = 1;
	}
	if(strcmp("mode = sensor",(char *)uart4_data)==0){
		mode = 2;
	}
}

点阵屏字库


uint8_t DZP_data[6][34]={
		{0xAA,0x55,
		0xFD,0xFF,0xFE,0xFF,0xC0,0x07,0xFF,0xFF,0xF7,0xDF,0xFB,0xBF,0x00,0x01,0xFF,0xFF,
		0xE0,0x0F,0xEF,0xEF,0xEF,0xEF,0xE0,0x0F,0xEF,0xEF,0xEF,0xEF,0xE0,0x0F,0xEF,0xEF},//音

		{0xAA,0x55,
		0xEF,0xDF,0xEF,0xDF,0xC3,0xDF,0xDF,0xDF,0xBE,0x03,0x42,0xDB,0xEE,0xDB,0xEE,0xDB,
		0x02,0xDB,0xEE,0x03,0xEE,0xDB,0xEF,0xDF,0xEB,0xDF,0xE7,0xDF,0xEF,0xDF,0xFF,0xDF},//钟//1//

		{0xAA,0x55,
		0xF7,0xBF,0xF7,0xBF,0xF7,0xBF,0xEC,0x07,0xEF,0xBF,0xCF,0x7F,0xC8,0x01,0xAF,0x7F,
		0x6E,0xFF,0xEC,0x07,0xEF,0xF7,0xEE,0xEF,0xEF,0x5F,0xEF,0xBF,0xEF,0xDF,0xEF,0xDF}//传//2//

};

点阵屏显示

		if(mode_n != mode){
			mode_n = mode;
			for(int i = 0; i<34;i++){
			//printf("afgsbgafdffag");
				HAL_I2C_Master_Transmit(&hi2c1, 0xA0 , (uint8_t*)&DZP_data[mode][i], 1, 300);
				HAL_Delay(2);
			}
		}

(3)闹钟代码编写

        1. 基础变量

         main.c


//数码管闹钟基础变量
extern int buf[8];
extern int shi_shi;
extern int shi_ge ;
extern int fen_shi;
extern int fen_ge ;
extern int miao_shi ;
extern int miao_ge ;

extern int miao ;
extern int shi ;
extern int fen;
//闹钟保存数组
extern int alarm_clock_array[20][4];
extern int alarm_clock_array_cnt;

        stm32mp1xx_it.c 基础变量



//数码管闹钟基础设置
int smg_number[10] = {0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xE0,0xFE,0xF6};
int buf[8] = {0};

//闹钟保存数组
int alarm_clock_array[20][4] = {0};
int alarm_clock_array_cnt = 0;
//实时时钟信息
int shi_shi = 0;
int shi_ge = 0;
int fen_shi = 0;
int fen_ge = 0;
int miao_shi = 0;
int miao_ge = 0;
int miao = 0;
int shi = 0;
int fen = 0;

int EN_clock = 0;//闹钟设置使能
extern int en_clock;//用于控制闹钟响铃

//闹钟设置信息
int shi_shi_clock = 0;
int shi_ge_clock = 0;
int fen_shi_clock = 0;
int fen_ge_clock = 0;
int miao_shi_clock = 0;
int miao_ge_clock = 0;
int miao_clock = 0, shi_clock = 0, fen_clock = 0;

        2. TIM2定时器


void TIM2_IRQHandler(void)
{
  /* USER CODE BEGIN TIM2_IRQn 0 */
	if(EN_music == 1)
		time_100ms_cnt++;
	else
		time_100ms_cnt = time_100ms_cnt;	//其余状�?�不计数

	if(time_100ms_cnt >= Beat_speed_n * Beat_num){	//这个音节结束
		time_100ms_cnt = 0;
		flag = 1;	//发�?�音节结束信�???????
	}


	//数码�????
	static int smg_time_100ms = 0;
	smg_time_100ms++;
	if(smg_time_100ms>=10){
		miao++;
		smg_time_100ms = 0;
	}


	if (miao>=60)
	{
		miao=0;
		fen++;
		if(fen>=60)
		{
			fen=0;
			shi++;
			if(shi>=24)
			{
				shi=0;
			}
		}
	}


	if(miao >= 60){
		miao = miao-60;
		fen++;
	}
	if(fen>=60){
		fen = fen-60;
		shi ++;
	}
	if(shi>= 24){
		shi = shi -24;

	}


	miao_shi=miao/10;
	miao_ge=miao%10;

	fen_shi=fen/10;
	fen_ge=fen%10;

	shi_shi=shi/10;
	shi_ge=shi%10;


	if(EN_clock == 0){
	buf[0]=smg_number[shi_shi];
	buf[1]=smg_number[shi_ge];
	buf[3]=smg_number[fen_shi];
	buf[4]=smg_number[fen_ge];
	buf[6]=smg_number[miao_shi];
	buf[7]=smg_number[miao_ge];
	  HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_RESET);
	  //HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, GPIO_PIN_RESET);
	  //HAL_GPIO_WritePin(GPIOI, GPIO_PIN_11|GPIO_PIN_10, GPIO_PIN_RESET);
	}
	else{
		  HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_SET);
		  //HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, GPIO_PIN_SET);
		  //HAL_GPIO_WritePin(GPIOI, GPIO_PIN_11|GPIO_PIN_10, GPIO_PIN_SET);
	}

  /* USER CODE END TIM2_IRQn 0 */
  HAL_TIM_IRQHandler(&htim2);
  /* USER CODE BEGIN TIM2_IRQn 1 */

  /* USER CODE END TIM2_IRQn 1 */
}

        3. 按键控制设置闹钟和保存闹钟        


void EXTI9_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI9_IRQn 0 */
	if(HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_9) == 0 && mode == 0){//确保数据稳定
		EN_music = !EN_music;
	}

	if(HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_9) == 0 && mode == 1 ){//确保数据稳定
		if(EN_clock == 1){
			//闹钟设置成功
			alarm_clock_array[alarm_clock_array_cnt][0] = shi_clock;
			alarm_clock_array[alarm_clock_array_cnt][1] = fen_clock;
			alarm_clock_array[alarm_clock_array_cnt][2] = miao_clock;
			alarm_clock_array[alarm_clock_array_cnt][3] = 3;	//默认播放第三首音�????
			alarm_clock_array_cnt++;
			if(alarm_clock_array_cnt >= 20) alarm_clock_array_cnt = 0;
			EN_clock = 0;
		}
		else if(EN_clock == 0){
			//设置闹钟
			shi_shi_clock = shi_shi;
			shi_ge_clock = shi_ge;
			fen_shi_clock = fen_shi;
			fen_ge_clock = fen_ge;
			miao_shi_clock = 0;
			miao_ge_clock = 0;
			miao_clock = 0;
			shi_clock = shi;
			fen_clock = fen;
			EN_clock = 1;
		}
	}






  /* USER CODE END EXTI9_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_9);
  /* USER CODE BEGIN EXTI9_IRQn 1 */

  /* USER CODE END EXTI9_IRQn 1 */
}

        4. 时分按键+


void EXTI0_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI0_IRQn 0 */

	if(HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_0) == 0 && mode == 0)//确保数据稳定
	{

		//每次按下解决 音量�??????? Low_volume_cnt
		Low_volume = Low_volume + Low_volume_cnt;
		if(Low_volume >= 10)
			Low_volume = 0;
	}

	if(HAL_GPIO_ReadPin(GPIOG,GPIO_PIN_0)==GPIO_PIN_RESET && mode == 1) {

		shi_clock++;
		fen_shi_clock=fen_clock/10;
		fen_ge_clock=fen_clock%10;
		shi_shi_clock=shi_clock/10;
		shi_ge_clock=shi_clock%10;
		if(shi_clock>=24)
		{
			shi_clock=0;
		}

		miao_shi_clock=miao_clock/10;
		miao_ge_clock=miao_clock%10;
		fen_shi_clock=fen_clock/10;
		fen_ge_clock=fen_clock%10;
		shi_shi_clock=shi_clock/10;
		shi_ge_clock=shi_clock%10;
		buf[0]=smg_number[shi_shi_clock];
		buf[1]=smg_number[shi_ge_clock];
		buf[3]=smg_number[fen_shi_clock];
		buf[4]=smg_number[fen_ge_clock];
		buf[6]=smg_number[miao_shi_clock];
		buf[7]=smg_number[miao_ge_clock];
		}
  /* USER CODE END EXTI0_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
  /* USER CODE BEGIN EXTI0_IRQn 1 */

  /* USER CODE END EXTI0_IRQn 1 */
}

/**
  * @brief This function handles EXTI line1 interrupt.
  */
void EXTI1_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI1_IRQn 0 */
	if(HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_1) == 0 && mode == 0)//确保数据稳定
		{
		music_speed_i++;
		music_speed_i = music_speed_kz(music_speed_i);
		}

	if(HAL_GPIO_ReadPin(GPIOG,GPIO_PIN_1)==GPIO_PIN_RESET && mode == 1) {
		fen_clock++;
		fen_shi_clock=fen_clock/10;
		fen_ge_clock=fen_clock%10;
		if(fen_clock>=60)
		{
			fen_clock=0;
			shi_clock++;
			fen_shi_clock=fen_clock/10;
			fen_ge_clock=fen_clock%10;
			shi_shi_clock=shi_clock/10;
			shi_ge_clock=shi_clock%10;
			if(shi_clock>=24)
			{
				shi_clock=0;
			}
		}

		miao_shi_clock=miao_clock/10;
		miao_ge_clock=miao_clock%10;
		fen_shi_clock=fen_clock/10;
		fen_ge_clock=fen_clock%10;
		shi_shi_clock=shi_clock/10;
		shi_ge_clock=shi_clock%10;
		buf[0]=smg_number[shi_shi_clock];
		buf[1]=smg_number[shi_ge_clock];
		buf[3]=smg_number[fen_shi_clock];
		buf[4]=smg_number[fen_ge_clock];
		buf[6]=smg_number[miao_shi_clock];
		buf[7]=smg_number[miao_ge_clock];
		}
  /* USER CODE END EXTI1_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_1);
  /* USER CODE BEGIN EXTI1_IRQn 1 */

  /* USER CODE END EXTI1_IRQn 1 */
}

/**
  * @brief This function handles EXTI line2 interrupt.
  */
void EXTI2_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI2_IRQn 0 */
	if(HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_2) == 0 && mode == 0)//确保数据稳定
		{
			list++;
			if(list > list_max){
				list = 0;
			}
		}

	if(HAL_GPIO_ReadPin(GPIOG,GPIO_PIN_2)==GPIO_PIN_RESET && mode == 1) {
			//在此处关闭闹�????
			en_clock = 0;
		}
  /* USER CODE END EXTI2_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_2);
  /* USER CODE BEGIN EXTI2_IRQn 1 */

  /* USER CODE END EXTI2_IRQn 1 */
}

        5. 时钟相加函数(将后三的时分秒加入左三的对应时分秒)

//通过输入不同的n,返回shi fen miao
int clock_compute(int time_shi,int time_fen,int time_miao,int add_shi,int add_fen,int add_miao,int n){

	time_miao = time_miao + add_miao;
	time_fen = time_fen + time_miao/60;
	time_miao = time_miao % 60;

	time_fen = time_fen + add_fen;
	time_shi = time_shi + time_fen / 60;
	time_fen = time_fen%60;

	time_shi = time_shi + add_shi;
	time_shi = time_shi%24;

	if(n == 0) return time_shi;
	if(n == 1) return time_fen;
	if(n == 2) return time_miao;

	return -1;
}

        6. 提取对应字符串后两位数字


// 函数定义:从字符串中提取两位数字
int extract_two_digits(const char *str, const char *prefix, int *value) {
    char *pos = strstr(str, prefix); // 查找前缀的位�?????
    if (pos == NULL) return 0; // 如果没找到前�?????,返�?????0表示失败

    // 跳过前缀的长度,找到数字�?????始的位置
    pos += strlen(prefix);

    // �?????查接下来的两个字符是否是数字
    if (pos[0] >= '0' && pos[0] <= '9' && pos[1] >= '0' && pos[1] <= '9') {
        // 转换字符为数�?????
        *value = (pos[0] - '0') * 10 + (pos[1] - '0');
        return 1; // 成功提取,返�?????1
    }

    return 0; // 提取失败,返�?????0
}

        7. 串口设置目前时钟,定时闹钟,延时闹钟


//判断是否到底闹钟
int en_clock = 0;//用于控制闹钟响铃
int en_clock_cnt = 0;
int clock_end[3] = {0};//记录闹钟无人时关闭的时间
//串口设置闹钟
void uart_clock(){
	int ci = 0;
	int ci_n = 0;

	//ci = number_char_come(uart4_data,(uint8_t *)"clock shi = ",2);

	ci = extract_two_digits((char *)uart4_data, (char *)"clock shi = ", &ci_n);
	if(ci == 1){
		uart4_data[0] = '1';
		shi = ci_n;
	}

	//ci = number_char_come(uart4_data,(uint8_t *)"clock fen = ",2);
	ci = extract_two_digits((char *)uart4_data, (char *)"clock fen = ", &ci_n);
	if(ci == 1){
		uart4_data[0] = '1';
		fen = ci_n;
	}

	//ci = number_char_come(uart4_data,(uint8_t *)"clock miao = ",2);
	ci = extract_two_digits((char *)uart4_data, (char *)"clock miao = ", &ci_n);
	if(ci == 1){
		uart4_data[0] = '1';
		miao = ci_n;
	}

	//设置�?????个多少时间后的闹�?????
	//ci = number_char_come(uart4_data,(uint8_t *)"clock delay shi = ",2);
	ci = extract_two_digits((char *)uart4_data, "clock delay shi = ", &ci_n);
	if(ci == 1){
		uart4_data[0] = '1';
		alarm_clock_array[alarm_clock_array_cnt][0] = clock_compute(shi,fen,miao,ci_n,0,0,0);
		alarm_clock_array[alarm_clock_array_cnt][1] = clock_compute(shi,fen,miao,ci_n,0,0,1);
		alarm_clock_array[alarm_clock_array_cnt][2] = clock_compute(shi,fen,miao,ci_n,0,0,2);
		alarm_clock_array_cnt++;
	}
	//ci = number_char_come(uart4_data,(uint8_t *)"clock delay fen = ",2);
	ci = extract_two_digits((char *)uart4_data, "clock delay fen = ", &ci_n);
	if(ci == 1){
		uart4_data[0] = '1';
		alarm_clock_array[alarm_clock_array_cnt][0] = clock_compute(shi,fen,miao,0,ci_n,0,0);
		alarm_clock_array[alarm_clock_array_cnt][1] = clock_compute(shi,fen,miao,0,ci_n,0,1);
		alarm_clock_array[alarm_clock_array_cnt][2] = clock_compute(shi,fen,miao,0,ci_n,0,2);
		alarm_clock_array_cnt++;
	}

	ci = extract_two_digits((char *)uart4_data, "clock delay miao = ", &ci_n);
	if(ci == 1){
		uart4_data[0] = '1';
		alarm_clock_array[alarm_clock_array_cnt][0] = clock_compute(shi,fen,miao,0,0,ci_n,0);
		alarm_clock_array[alarm_clock_array_cnt][1] = clock_compute(shi,fen,miao,0,0,ci_n,1);
		alarm_clock_array[alarm_clock_array_cnt][2] = clock_compute(shi,fen,miao,0,0,ci_n,2);
		alarm_clock_array_cnt++;
	}

	// time shi = 12;fen = 10;miao = 12;music = 1;
	ci = 0;
	ci = ci + extract_two_digits((char *)uart4_data, "time shi = ", &alarm_clock_array[alarm_clock_array_cnt][0]);
	ci = ci + extract_two_digits((char *)uart4_data, ";fen = ", &alarm_clock_array[alarm_clock_array_cnt][1]);
	ci = ci + extract_two_digits((char *)uart4_data, ";miao = ", &alarm_clock_array[alarm_clock_array_cnt][2]);
	//ci = ci + extract_two_digits((char *)uart4_data, ";music = ", &alarm_clock_array[alarm_clock_array_cnt][2]);
	if(ci == 3){
		//完美对应
		uart4_data[0] = '1';
		ci = extract_two_digits((char *)uart4_data, ";music = ", &alarm_clock_array[alarm_clock_array_cnt][3]);
		if(ci > list_max && ci<0) //如果大于音乐总数
			alarm_clock_array[alarm_clock_array_cnt][3] = 3;//默认�?????3

		alarm_clock_array_cnt++;
	}


	if(strcmp("clock delay list",(char *)uart4_data)==0){

		uart4_data[0] = '0';
		for(int i = 0; i< alarm_clock_array_cnt;i++){
			if(alarm_clock_array[i][0] != -1 && alarm_clock_array[i][1] != -1 && alarm_clock_array[i][2] != -1)
			printf("%d : time -> %d/%d/%d  \r\n",i,	alarm_clock_array[i][0],
															alarm_clock_array[i][1],
															alarm_clock_array[i][2]
															);
		}
	}

	//读取关闭第几位闹�?????
	//ci = number_char_come(uart4_data,(uint8_t *)"clock stop list = ",2);
	ci = extract_two_digits((char *)uart4_data, "clock stop list = ", &ci_n);
	if(ci == 1){
		alarm_clock_array[ci_n][0] = -1;
		alarm_clock_array[ci_n][1] = -1;
		alarm_clock_array[ci_n][2] = -1;
	}

	//关闭闹钟
	if(strcmp("clock stop stop",(char *)uart4_data)==0){
		en_clock = 0;
	}


	if(alarm_clock_array_cnt >= 20) alarm_clock_array_cnt = 0;
}

        8. 闹钟实现和停止(数码管显示)


void alarm_clock(){
    //时钟显示(数码管)
	static int pos = 0;
	HAL_I2C_Mem_Write(&hi2c1,0x70,0X10+pos, 1, (uint8_t*)&buf[pos],1,100);
	HAL_Delay(1);
	pos++;
	if(pos == 3 && pos == 6) pos++;
	if(pos == 8) pos = 0;


	uart_clock();//调用串口控制

	for(int j=0;j<alarm_clock_array_cnt && en_clock == 0;j++){
		//int cnt_clock = 0;
		if(alarm_clock_array[j][0] == shi && alarm_clock_array[j][1] == fen && alarm_clock_array[j][2] == miao) {
			en_clock_cnt = j;
			en_clock = 1;
			clock_end[0] = clock_compute(shi,fen,miao,0,0,30,0);
			clock_end[1] = clock_compute(shi,fen,miao,0,0,30,1);
			clock_end[2] = clock_compute(shi,fen,miao,0,0,30,2);
			break;
		}
	}

	//当闹钟响�?????30S
	if(shi == clock_end[0] && fen == clock_end[1] && miao == clock_end[2]){
		en_clock = 0;//关闭闹钟
		//EN_music = 1;
	}

	if(en_clock == 1 ){
			motor(10);
			HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, GPIO_PIN_SET);
		}
		else{
			HAL_GPIO_WritePin(GPIOF, GPIO_PIN_6, GPIO_PIN_RESET);
			HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, GPIO_PIN_RESET);
		}

}

(4)温湿度代码编写

        1. 温湿度基础变量     


uint8_t add1=0xFE,add2=0xE5,add3=0xE3;
//0xFE复位 0xE5启动湿度转换 0xE3启动温度转换
uint16_t RH_Code,RH_Code_low=0,RH_Code_high=0;
uint16_t Temp_Code,Temp_Code_low=0,Temp_Code_high=0;

int humidity_min = 50;//能仍受最低干燥程度
int temperature_max = 50;//能仍受的最高温度
int en_t = 0; //温度使能
int en_r = 0; //湿度使能

        2. 温湿度计算


//计算出温湿度
void Temperature_humidity(){
	//湿度
			  HAL_I2C_Master_Transmit(&hi2c1, 0x80, &add2, 1,100);
			  //写命�??????? ox40里面写命�??????? 0xe5 启动湿度转换
			  HAL_I2C_Master_Receive(&hi2c1, 0x81, &RH_Code, 1, 100);
			  //读命�??????? �???????0x40读取出湿度的数据 存入变量RH_CODE
			  HAL_Delay(30);
			  //进行高低字节转换
			  RH_Code_low=(RH_Code & 0xff);
			  RH_Code_high=(RH_Code >> 8)& 0xff;
			  RH_Code=(RH_Code_low << 8)+RH_Code_high;

			  //温度
			  HAL_I2C_Master_Transmit(&hi2c1, 0x80, &add3, 1,100);
			  HAL_I2C_Master_Receive(&hi2c1, 0x81, &Temp_Code, 1, 100);
			  //读命�??????? �???????0x40读取出温度的数据 存入变量Temp_CODE
			  HAL_Delay(30);
			  //进行高低字节转换
			  Temp_Code_low=(Temp_Code & 0xff);
			  Temp_Code_high=(Temp_Code >> 8)& 0xff;
			  Temp_Code=(Temp_Code_low << 8)+Temp_Code_high;

			  Temp_Code=17572*Temp_Code/65535-4685;//扩大�???????百�??
			  RH_Code=125*RH_Code/65536-6;//计算出湿度�??
			  //printf("Temp_Code = \r%d.%d     RH_Code = %d%%\n",Temp_Code/100,Temp_Code%100,RH_Code%100);
			  //串口输出温湿�???????
			  HAL_Delay(2);
}

        3. 温湿度串口控制


void uart_sensor(){
	int tr=0;
	int tr_i = 0;
	tr = extract_two_digits((char *)uart4_data, "sensor  humidity_min = ", &tr_i);
	if(tr != 0){
		humidity_min = tr_i;
	}

	tr = extract_two_digits((char *)uart4_data, "sensor  temperature_max = ", &tr_i);
	if(tr != 0){
		temperature_max = tr_i;
	}


	if(strcmp("sensor temperature start",(char *)uart4_data)==0){
		en_t = 1;
	}
	if(strcmp("sensor humidity start",(char *)uart4_data)==0){
		en_r = 1;
	}
	if(strcmp("sensor temperature stop",(char *)uart4_data)==0){
		en_t = 0;
	}
	if(strcmp("sensor humidity stop",(char *)uart4_data)==0){
		en_r = 0;
	}


	if(strcmp("sensor list",(char *)uart4_data)==0){
		uart4_data[0] = '0';
		printf("Temp_Code = \r%d.%d     RH_Code = %d%%\r\n",Temp_Code/100,Temp_Code%100,RH_Code%100);
		printf("sensor en_t : %d\r\n",en_t);
		printf("sensor en_r : %d\r\n",en_r);
		printf("sensor temperature_max : %d\r\n",temperature_max);
		printf("sensor humidity_min : %d\r\n",humidity_min);
	}

	if(strcmp("sensor Temp_Code RH_Code",(char *)uart4_data)==0){
		uart4_data[0] = '0';
		printf("Temp_Code = \r%d.%d     RH_Code = %d%%\n",Temp_Code/100,Temp_Code%100,RH_Code%100);
	}


}

        4. 温湿度主函数


void sensor(){
	static int iii = 0;
	if(iii == 0){
		HAL_I2C_Master_Transmit(&hi2c1, 0x80, &add1, 1, 100);
		HAL_Delay(2);
		iii++;
	}
	Temperature_humidity();
	uart_sensor();

	if(RH_Code < humidity_min && en_r == 1){
		//motor(10);
		HAL_GPIO_WritePin(GPIOI, GPIO_PIN_11, GPIO_PIN_SET);
	}
	else{
		HAL_GPIO_WritePin(GPIOI, GPIO_PIN_11, GPIO_PIN_RESET);
	}

	if(Temp_Code/100 >= temperature_max && en_t == 1){
		  HAL_GPIO_WritePin(GPIOI, GPIO_PIN_10, GPIO_PIN_SET);
	}
	else{
		  HAL_GPIO_WritePin(GPIOI, GPIO_PIN_10, GPIO_PIN_RESET);
	}
}

(5)主函数


void end_main(){

	  tone_init(); //初始化音量频�??????
	  list_max = music_init();//更新乐谱
	  HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1);	//启动蜂鸣器定时器
	  HAL_TIM_Base_Start_IT(&htim2);		  	//启动定时�??????2
	  HAL_TIM_Base_Start_IT(&htim3);		  	//启动定时�??????2

	  //1 使能串口空闲中断
	  __HAL_UART_ENABLE_IT(&huart4,UART_IT_IDLE);
	  //2.使能串口中断接收数据
	  HAL_UART_Receive_IT(&huart4,rx_buf,sizeof(rx_buf));
	  int mode_n = 1;

	while(1){
		music_kz();

		alarm_clock();
		uart_mode();

		sensor();

		if(mode_n != mode){
			mode_n = mode;
			for(int i = 0; i<34;i++){
			//printf("afgsbgafdffag");
				HAL_I2C_Master_Transmit(&hi2c1, 0xA0 , (uint8_t*)&DZP_data[mode][i], 1, 300);
				HAL_Delay(2);
			}
		}

	}
}

五、总代码

main.c

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */


#include <string.h>


uint8_t rx_buf[200]={0};	//接收不定长数
uint8_t uart4_data[200] = {0};

extern int mode;	//模式
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;

TIM_HandleTypeDef htim2;
TIM_HandleTypeDef htim3;
TIM_HandleTypeDef htim4;

UART_HandleTypeDef huart4;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_TIM2_Init(void);
static void MX_TIM4_Init(void);
static void MX_UART4_Init(void);
static void MX_TIM3_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */



//重写标准输出函数
int __io_putchar(int ch)
{
	HAL_UART_Transmit(&huart4, (uint8_t *)&ch, 1, 10);
	return ch;
}


// 自定义空闲中断处理函�????????
void uart4_idle_func(void)
{
	int len = 0;
	//判定 是否为空闲中�????????
	if(  __HAL_UART_GET_FLAG(&huart4, UART_FLAG_IDLE) == SET )
	{
		// 清除空闲中断标志,因为是自己定义的函数 系统不会清标
		__HAL_UART_CLEAR_IDLEFLAG(&huart4);
		// 计算接收数据的长
		len = sizeof(rx_buf) - huart4.RxXferCount;
		//第二个参数是 还剩下的空间
		// 打印接收到时数据  数据处理
		//printf("uart rx len = %d, data: %s\r\n",len, rx_buf);

	    // 使用strcpy复制字符�????????
	    strcpy((char *)uart4_data, (char *)rx_buf);

	    printf("%s instructions success\r\n", uart4_data);
		// 准备接收下一次数�?????????
		memset(rx_buf,0,len); // 清理接收容器
		//重置接收指针 剩余容器大小
		huart4.pRxBuffPtr = rx_buf;
		huart4.RxXferCount = sizeof(rx_buf);
	}
}

//控制马达
void motor(int d){
	HAL_GPIO_TogglePin(GPIOF, GPIO_PIN_6);//
	HAL_Delay(d);
}


// 音乐
// 音乐盒基�??????变量
extern int time_100ms_cnt; //0.1s计数�??????
extern int Beat_speed;		//节拍速度,代表半个节拍需要多少个0.1s
extern int Beat_speed_n;	//实际执行的节拍数

extern int Beat_num;		//这个�??????个音�??????要多少个 半拍
extern int flag; 			//当其等于 1 时,表示�??????个音结束
extern int EN_music ;				//使能信号,用于开启整个音乐盒
extern int list ;			//音乐列表
extern int list_max ;		//音乐总数
extern int Low_volume ;		//音量大小
extern int Low_volume_cnt;
extern int music_speed_i; 	//音乐播放速度模式保存
extern int music_speed_kz(int i);

int tone[3][8];
//初始化高中低音频�??????
void tone_init(){
	tone[1][0] = 0;	//不执行音�??????
	tone[1][1] = 191;
	tone[1][2] = 170;
	tone[1][3] = 151;
	tone[1][4] = 143;
	tone[1][5] = 127;
	tone[1][6] = 113;
	tone[1][7] = 101;
    // 低音 (Low)
    for (int i = 0; i < 8; i++) {
        tone[0][i] = tone[1][i] * 2; // 只是低音 近似的�??
    }

    // 高音 (High)
    for (int i = 0; i < 8; i++) {
        tone[2][i] = tone[1][i] / 2; // 只是高音  近似的�??
    }
}

#define MAX_unit_num 200 //�????????大乐谱数�????????
//创建结构体保存乐�????????
struct music_unit{
	char name[50];		//乐谱名称
	int unit[MAX_unit_num];		//发什么音
	int unit_HL[MAX_unit_num];	//发高音或者其�????????
	int time[MAX_unit_num];		//发音时间
	//int time_4[MAX_unit_num];	//判断是否�????????1/4�????????
	int num;			//记录有多少个
}music[25];

//创建乐谱 返回有多少首音乐
int music_init(){
	int cnt = 0;
	//第一首音�???????? 生日快乐
	strcpy(music[0].name, "生日快乐"); 				// 使用strcpy复制字符�???????? 给音乐命�????????
	int music0_unit[29] = {0,0, 5,5,6,5,1,7, 5,5,6,5,2,1,
								5,5,6,3,1,7, 6,4,4,3,1,2,1,
								0,0};		//基础乐谱
	int music0_time[29] = {1,1, 1,1,2,2,2,3, 1,1,2,2,2,3,
								2,2,2,2,2,2, 2,2,2,2,2,2,3,
								1,1};		//乐谱节拍
	music[0].num = 29;										//乐谱总数
	int music0_unit_HL[29] = {1,1,
								0,0,0,0,1,0, 0,0,0,0,1,1,
								0,0,1,1,1,0, 0,1,1,1,1,1,1,
								1,1}; 	//乐谱全为中音

	//第二首音�???????? �????????闪一闪亮晶晶
	cnt++;
	strcpy(music[1].name, "�????????闪一闪亮晶晶"); 					// 使用strcpy复制字符�???????? 给音乐命�????????
	int music1_unit[44] = {0,
						   1,1,5,5,6,6,5, 4,4,3,3,2,2,1,
						   5,5,4,4,3,3,2, 5,5,4,4,3,3,2,
						   1,1,5,5,6,6,5, 4,4,3,3,2,2,1,
						   0};		//基础乐谱
	int music1_time[44] = {2,
						   2,2,2,2,2,2,3, 2,2,2,2,2,2,3,
						   2,2,2,2,2,2,3, 2,2,2,2,2,2,3,
						   2,2,2,2,2,2,3, 2,2,2,2,2,2,3,
						   2};		//乐谱节拍
	int music1_unit_HL[44] =
						  {1,
						   1,1,1,1,1,1,1, 1,1,1,1,1,1,1,
						   1,1,1,1,1,1,1, 1,1,1,1,1,1,1,
						   1,1,1,1,1,1,1, 1,1,1,1,1,1,1,
						   1}; 		//乐谱全为中音
	music[1].num = 44;											//乐谱总数



	//第三首音�???????? 两只老虎
	cnt++;
	strcpy(music[2].name, "两只老虎"); 					// 使用strcpy复制字符�???????? 给音乐命�????????
	int music2_unit[38] = {0,
						   1,2,3,1, 1,2,3,1, 3,4,5,5, 3,4,5,5,
						   5,6,5,4, 3,1,5,6, 5,4,3,1, 1,5,1,1,
						   1,5,1,1, 0};		//基础乐谱
	int music2_time[38] = {2,
						   1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1,
						   0,0,0,0, 1,1,0,0, 0,0,1,1, 1,1,1,2,
						   1,1,1,2, 2};		//乐谱节拍
	int music2_unit_HL[38] =
						  {1,
					       1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1,
						   1,1,1,1, 1,1,1,1, 1,1,1,1, 1,0,1,1,
						   1,0,1,1, 1}; 		//乐谱�????????   中音
	music[2].num = 38;											//乐谱总数


	//第四首音�???????? 青花瓷片�????????
	cnt++;
	strcpy(music[3].name, "青花瓷片"); 					// 使用strcpy复制字符�???????? 给音乐命�????????
	int music3_unit[100] = {0,0,0,0, 0,5,5,3, 2,3,6,2, 3,5,3,2, 2,5,5,3,
						    2,3,5,2, 3,5,2,1, 1,1,2,3, 5,6,5,4, 5,3,3,2,
						    2,2,1,2, 1,1,2,1, 2,3,5,3, 3,3,5,5, 3,2,3,6,
						    2,3,5,3, 2,2,5,5, 3,2,3,5, 2,3,5,2, 1,1,1,2,
						    3,5,6,5, 4,5,3,3, 2,2,5,3, 2,2,2,1, 1,0,0,0};		//基础乐谱

	int music3_time[100] = {0,0,0,0, 0,0,0,0, 0,0,1,0, 0,0,0,2, 0,0,0,0,
							0,0,1,0, 0,0,0,2, 0,0,0,0, 0,0,0,0, 0,0,0,0,
							2,0,0,0, 0,0,0,0, 0,1,0,0, 2,0,0,0, 0,0,0,1,
							0,0,0,0, 2,0,0,0, 0,0,0,1, 0,0,0,0, 2,0,0,0,
							0,0,0,0, 0,0,0,0, 0,2,0,1, 0,0,0,1, 2,1,1,1};		//乐谱节拍

	for(int i =0;i<100;i++)
		music3_time[i] = music3_time[i]+1;

	int music3_unit_HL[100] =
						  { 1,1,1,1, 1,1,1,1, 1,1,0,1, 1,1,1,1, 1,1,1,1,
							1,1,0,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1,
							1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,0,
							1,1,1,1, 1,1,1,1, 1,1,1,0, 1,1,1,1, 1,1,1,1,
							1,1,1,1, 1,1,1,1, 1,1,0,1, 1,1,1,1, 1,1,1,1}; 		//乐谱�????????   中音
	music[3].num = 100;											//乐谱总数




	for (int i = 0; i < MAX_unit_num; i++) {
		//将乐谱保存进结构�????????
		if(i<music[0].num){//确保数据正确
			music[0].unit[i] =music0_unit[i];
			music[0].unit_HL[i] =music0_unit_HL[i];
			music[0].time[i] =music0_time[i];
		}


		//将乐谱保存进结构�????????
		if(i<music[1].num){//确保数据正确
			music[1].unit[i] =music1_unit[i];
			music[1].unit_HL[i] =music1_unit_HL[i];
			music[1].time[i] =music1_time[i];
		}

		//将乐谱保存进结构�????????
		if(i<music[2].num){//确保数据正确
			music[2].unit[i] =music2_unit[i];
			music[2].unit_HL[i] =music2_unit_HL[i];
			music[2].time[i] =music2_time[i];
		}


		//将乐谱保存进结构�????????
		if(i<music[3].num){//确保数据正确
			music[3].unit[i] =music3_unit[i];
			music[3].unit_HL[i] =music3_unit_HL[i];
			music[3].time[i] =music3_time[i];
		}
	}


	return cnt;
}

//播放�???? N首音�???? 音量�???? X 0 - 100
void play_music(int n, int x){
	static int ni = 0; 		//用于判断 是否换了音乐
	static int cnt = 0;		//记录播放到哪�????�???? 音节
	if(ni != n ){//如果音乐换了
		ni = n;
		cnt = 0;
		__HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_1,0);//设置音量
		HAL_Delay(1000);//
	}

	//
	int value = tone[music[n].unit_HL[cnt]][music[n].unit[cnt]];	//获取频率
	if(flag == 1){	//接受到一个音节结�????
		flag = 0;	//复位
		Beat_num = music[n].time[cnt]; 				//这个音需要多少个半拍
		//LED_BEEP(music[n].unit[cnt]);				//LED随音节变动�?�变�????

		if(music[n].time[cnt] == 0){//如果�???? 1/4�????
			Beat_speed_n = Beat_speed /2;
		}
		else{//如果没有1/4�????
			Beat_speed_n = Beat_speed;
		}

		//if(value != 0)//如果有频率�?�执行,没有者只更新 时间�????
		__HAL_TIM_SET_AUTORELOAD(&htim4,value);		//自动加载频率�????

		cnt ++; 	//可进行下�????次音�????
		if(cnt >= music[n].num){ //如果�????个音节播放完�????
			cnt = 0;//重新播放
			//__HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_1,0);//设置音量
			//HAL_Delay(500);//
		}
	}
	//__HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_1,x * (value/100));//设置音量
	__HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_1,(value/10)*x);//设置音量
}


//串口音乐控制函数
void music_kz(){
	  if(EN_music == 1)//启动
		  play_music(list,Low_volume);
	  else
		  __HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_1,0);//设置音量



	if(strcmp("music volume increase",(char *)uart4_data)==0){
		uart4_data[0] = '0';
		Low_volume = Low_volume + Low_volume_cnt;
		if(Low_volume >= 10)
			Low_volume = 10;
	}

	if(strcmp("music volume reduction",(char *)uart4_data)==0){
		Low_volume = Low_volume - Low_volume_cnt;
		if(Low_volume <= 0)
			Low_volume = 0;
	}


	if(strcmp("music speed increase",(char *)uart4_data)==0){
		uart4_data[0] = '0';
		music_speed_i++;
		music_speed_i = music_speed_kz(music_speed_i);
	}
	if(strcmp("music speed reduction",(char *)uart4_data)==0){
		uart4_data[0] = '0';
		music_speed_i--;
		music_speed_i = music_speed_kz(music_speed_i);
	}

	if(strcmp("music next song",(char *)uart4_data)==0){
		uart4_data[0] = '0';
		list++;
		if(list > list_max){
			list = list_max;
		}
	}
	if(strcmp("music previous song",(char *)uart4_data)==0){
		list--;
		uart4_data[0] = '0';
		if(list < 0){
			list = 0;
		}
	}

	if(strcmp("music start",(char *)uart4_data)==0){
		EN_music = 1;
	}
	if(strcmp("music stop",(char *)uart4_data)==0){
		EN_music = 0;
	}


}


//数码管闹�?????
extern int buf[8];
extern int shi_shi;
extern int shi_ge ;
extern int fen_shi;
extern int fen_ge ;
extern int miao_shi ;
extern int miao_ge ;

extern int miao ;
extern int shi ;
extern int fen;
//闹钟保存数组
extern int alarm_clock_array[20][4];
extern int alarm_clock_array_cnt;

//通过输入不同的n,返回shi fen miao
int clock_compute(int time_shi,int time_fen,int time_miao,int add_shi,int add_fen,int add_miao,int n){

	time_miao = time_miao + add_miao;
	time_fen = time_fen + time_miao/60;
	time_miao = time_miao % 60;

	time_fen = time_fen + add_fen;
	time_shi = time_shi + time_fen / 60;
	time_fen = time_fen%60;

	time_shi = time_shi + add_shi;
	time_shi = time_shi%24;

	if(n == 0) return time_shi;
	if(n == 1) return time_fen;
	if(n == 2) return time_miao;

	return -1;
}


//将字符解成数�?????
int char_number(uint8_t c){
    if(c >= '0' && c <= '9')
        return c-'0';
    else
        return -1;
}

// zfc 为当前传入字符串
// zfc_n为比较字符串
// num为如果两字符串最初相等,则取字符串后面多少位的数�?????
int number_char_come(uint8_t zfc[200], uint8_t zfc_n[200], int num){
	size_t len = strlen((char *)zfc_n);//无符号整数类�?????

	int cnt = 0;
	for(int i = 0;i < len;i++){
		if(zfc[i] != zfc_n[i]) return -1; //不相�?????
		else cnt++;
	}
	if(cnt != len)	 return -1;//两字符串不等

	size_t shen_len = strlen((char *)zfc) - len;//剩余字符串长�?????
	size_t hig_num = 0;//用以保存实际有效位数

	if(shen_len > num) hig_num = num;
	else hig_num = shen_len;
	//int number[200];



    int number1 = 0;
    int multiplier = 1; // 用于计算10的幂的变�?????
	for(int i = len + hig_num - 1; i >= len;i--){
		//number[i-len] = char_number(zfc[i]);
		if(char_number(zfc[i])== -1) {
			printf("\r\r\r number error\r\n");
			return -1;
		}

		multiplier = multiplier*10;
		number1 = number1 + char_number(zfc[i])*multiplier;
	}

	return number1;

}

// 函数定义:从字符串中提取两位数字
int extract_two_digits(const char *str, const char *prefix, int *value) {
    char *pos = strstr(str, prefix); // 查找前缀的位�?????
    if (pos == NULL) return 0; // 如果没找到前�?????,返�?????0表示失败

    // 跳过前缀的长度,找到数字�?????始的位置
    pos += strlen(prefix);

    // �?????查接下来的两个字符是否是数字
    if (pos[0] >= '0' && pos[0] <= '9' && pos[1] >= '0' && pos[1] <= '9') {
        // 转换字符为数�?????
        *value = (pos[0] - '0') * 10 + (pos[1] - '0');
        return 1; // 成功提取,返�?????1
    }

    return 0; // 提取失败,返�?????0
}



//判断是否到底闹钟
int en_clock = 0;//用于控制闹钟响铃
int en_clock_cnt = 0;
int clock_end[3] = {0};//记录闹钟无人时关闭的时间
//串口设置闹钟
void uart_clock(){
	int ci = 0;
	int ci_n = 0;

	//ci = number_char_come(uart4_data,(uint8_t *)"clock shi = ",2);

	ci = extract_two_digits((char *)uart4_data, (char *)"clock shi = ", &ci_n);
	if(ci == 1){
		uart4_data[0] = '1';
		shi = ci_n;
	}

	//ci = number_char_come(uart4_data,(uint8_t *)"clock fen = ",2);
	ci = extract_two_digits((char *)uart4_data, (char *)"clock fen = ", &ci_n);
	if(ci == 1){
		uart4_data[0] = '1';
		fen = ci_n;
	}

	//ci = number_char_come(uart4_data,(uint8_t *)"clock miao = ",2);
	ci = extract_two_digits((char *)uart4_data, (char *)"clock miao = ", &ci_n);
	if(ci == 1){
		uart4_data[0] = '1';
		miao = ci_n;
	}

	//设置�?????个多少时间后的闹�?????
	//ci = number_char_come(uart4_data,(uint8_t *)"clock delay shi = ",2);
	ci = extract_two_digits((char *)uart4_data, "clock delay shi = ", &ci_n);
	if(ci == 1){
		uart4_data[0] = '1';
		alarm_clock_array[alarm_clock_array_cnt][0] = clock_compute(shi,fen,miao,ci_n,0,0,0);
		alarm_clock_array[alarm_clock_array_cnt][1] = clock_compute(shi,fen,miao,ci_n,0,0,1);
		alarm_clock_array[alarm_clock_array_cnt][2] = clock_compute(shi,fen,miao,ci_n,0,0,2);
		alarm_clock_array_cnt++;
	}
	//ci = number_char_come(uart4_data,(uint8_t *)"clock delay fen = ",2);
	ci = extract_two_digits((char *)uart4_data, "clock delay fen = ", &ci_n);
	if(ci == 1){
		uart4_data[0] = '1';
		alarm_clock_array[alarm_clock_array_cnt][0] = clock_compute(shi,fen,miao,0,ci_n,0,0);
		alarm_clock_array[alarm_clock_array_cnt][1] = clock_compute(shi,fen,miao,0,ci_n,0,1);
		alarm_clock_array[alarm_clock_array_cnt][2] = clock_compute(shi,fen,miao,0,ci_n,0,2);
		alarm_clock_array_cnt++;
	}

	ci = extract_two_digits((char *)uart4_data, "clock delay miao = ", &ci_n);
	if(ci == 1){
		uart4_data[0] = '1';
		alarm_clock_array[alarm_clock_array_cnt][0] = clock_compute(shi,fen,miao,0,0,ci_n,0);
		alarm_clock_array[alarm_clock_array_cnt][1] = clock_compute(shi,fen,miao,0,0,ci_n,1);
		alarm_clock_array[alarm_clock_array_cnt][2] = clock_compute(shi,fen,miao,0,0,ci_n,2);
		alarm_clock_array_cnt++;
	}

	// time shi = 12;fen = 10;miao = 12;music = 1;
	ci = 0;
	ci = ci + extract_two_digits((char *)uart4_data, "time shi = ", &alarm_clock_array[alarm_clock_array_cnt][0]);
	ci = ci + extract_two_digits((char *)uart4_data, ";fen = ", &alarm_clock_array[alarm_clock_array_cnt][1]);
	ci = ci + extract_two_digits((char *)uart4_data, ";miao = ", &alarm_clock_array[alarm_clock_array_cnt][2]);
	//ci = ci + extract_two_digits((char *)uart4_data, ";music = ", &alarm_clock_array[alarm_clock_array_cnt][2]);
	if(ci == 3){
		//完美对应
		uart4_data[0] = '1';
		ci = extract_two_digits((char *)uart4_data, ";music = ", &alarm_clock_array[alarm_clock_array_cnt][3]);
		if(ci > list_max && ci<0) //如果大于音乐总数
			alarm_clock_array[alarm_clock_array_cnt][3] = 3;//默认�?????3

		alarm_clock_array_cnt++;
	}


	if(strcmp("clock delay list",(char *)uart4_data)==0){

		uart4_data[0] = '0';
		for(int i = 0; i< alarm_clock_array_cnt;i++){
			if(alarm_clock_array[i][0] != -1 && alarm_clock_array[i][1] != -1 && alarm_clock_array[i][2] != -1)
			printf("%d : time -> %d/%d/%d  \r\n",i,	alarm_clock_array[i][0],
															alarm_clock_array[i][1],
															alarm_clock_array[i][2]
															);
		}
	}

	//读取关闭第几位闹�?????
	//ci = number_char_come(uart4_data,(uint8_t *)"clock stop list = ",2);
	ci = extract_two_digits((char *)uart4_data, "clock stop list = ", &ci_n);
	if(ci == 1){
		alarm_clock_array[ci_n][0] = -1;
		alarm_clock_array[ci_n][1] = -1;
		alarm_clock_array[ci_n][2] = -1;
	}

	//关闭闹钟
	if(strcmp("clock stop stop",(char *)uart4_data)==0){
		en_clock = 0;
	}


	if(alarm_clock_array_cnt >= 20) alarm_clock_array_cnt = 0;
}
void smg_xians(){

}


void alarm_clock(){

	static int pos = 0;
	HAL_I2C_Mem_Write(&hi2c1,0x70,0X10+pos, 1, (uint8_t*)&buf[pos],1,100);
	HAL_Delay(1);
	pos++;
	if(pos == 3 && pos == 6) pos++;
	if(pos == 8) pos = 0;


	uart_clock();//调用串口控制

	for(int j=0;j<alarm_clock_array_cnt && en_clock == 0;j++){
		//int cnt_clock = 0;
		if(alarm_clock_array[j][0] == shi && alarm_clock_array[j][1] == fen && alarm_clock_array[j][2] == miao) {
			en_clock_cnt = j;
			en_clock = 1;
			clock_end[0] = clock_compute(shi,fen,miao,0,0,30,0);
			clock_end[1] = clock_compute(shi,fen,miao,0,0,30,1);
			clock_end[2] = clock_compute(shi,fen,miao,0,0,30,2);
			break;
		}
	}

	//当闹钟响�?????30S
	if(shi == clock_end[0] && fen == clock_end[1] && miao == clock_end[2]){
		en_clock = 0;//关闭闹钟
		//EN_music = 1;
	}

	if(en_clock == 1 ){
			motor(10);
			HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, GPIO_PIN_SET);
		}
		else{
			HAL_GPIO_WritePin(GPIOF, GPIO_PIN_6, GPIO_PIN_RESET);
			HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, GPIO_PIN_RESET);
		}

}


uint8_t add1=0xFE,add2=0xE5,add3=0xE3;
//0xFE复位 0xE5启动湿度转换 0xE3启动温度转换
uint16_t RH_Code,RH_Code_low=0,RH_Code_high=0;
uint16_t Temp_Code,Temp_Code_low=0,Temp_Code_high=0;

int humidity_min = 50;//能仍受的�?????低干燥程�?????
int temperature_max = 50;//能仍受的�?????高温�?????
int en_t = 0; //温度使能
int en_r = 0; //湿度使能

//计算出温湿度
void Temperature_humidity(){
	//湿度
			  HAL_I2C_Master_Transmit(&hi2c1, 0x80, &add2, 1,100);
			  //写命�??????? ox40里面写命�??????? 0xe5 启动湿度转换
			  HAL_I2C_Master_Receive(&hi2c1, 0x81, &RH_Code, 1, 100);
			  //读命�??????? �???????0x40读取出湿度的数据 存入变量RH_CODE
			  HAL_Delay(30);
			  //进行高低字节转换
			  RH_Code_low=(RH_Code & 0xff);
			  RH_Code_high=(RH_Code >> 8)& 0xff;
			  RH_Code=(RH_Code_low << 8)+RH_Code_high;

			  //温度
			  HAL_I2C_Master_Transmit(&hi2c1, 0x80, &add3, 1,100);
			  HAL_I2C_Master_Receive(&hi2c1, 0x81, &Temp_Code, 1, 100);
			  //读命�??????? �???????0x40读取出温度的数据 存入变量Temp_CODE
			  HAL_Delay(30);
			  //进行高低字节转换
			  Temp_Code_low=(Temp_Code & 0xff);
			  Temp_Code_high=(Temp_Code >> 8)& 0xff;
			  Temp_Code=(Temp_Code_low << 8)+Temp_Code_high;

			  Temp_Code=17572*Temp_Code/65535-4685;//扩大�???????百�??
			  RH_Code=125*RH_Code/65536-6;//计算出湿度�??
			  //printf("Temp_Code = \r%d.%d     RH_Code = %d%%\n",Temp_Code/100,Temp_Code%100,RH_Code%100);
			  //串口输出温湿�???????
			  HAL_Delay(2);
}



void uart_sensor(){
	int tr=0;
	int tr_i = 0;
	tr = extract_two_digits((char *)uart4_data, "sensor  humidity_min = ", &tr_i);
	if(tr != 0){
		humidity_min = tr_i;
	}

	tr = extract_two_digits((char *)uart4_data, "sensor  temperature_max = ", &tr_i);
	if(tr != 0){
		temperature_max = tr_i;
	}


	if(strcmp("sensor temperature start",(char *)uart4_data)==0){
		en_t = 1;
	}
	if(strcmp("sensor humidity start",(char *)uart4_data)==0){
		en_r = 1;
	}
	if(strcmp("sensor temperature stop",(char *)uart4_data)==0){
		en_t = 0;
	}
	if(strcmp("sensor humidity stop",(char *)uart4_data)==0){
		en_r = 0;
	}


	if(strcmp("sensor list",(char *)uart4_data)==0){
		uart4_data[0] = '0';
		printf("Temp_Code = \r%d.%d     RH_Code = %d%%\r\n",Temp_Code/100,Temp_Code%100,RH_Code%100);
		printf("sensor en_t : %d\r\n",en_t);
		printf("sensor en_r : %d\r\n",en_r);
		printf("sensor temperature_max : %d\r\n",temperature_max);
		printf("sensor humidity_min : %d\r\n",humidity_min);
	}

	if(strcmp("sensor Temp_Code RH_Code",(char *)uart4_data)==0){
		uart4_data[0] = '0';
		printf("Temp_Code = \r%d.%d     RH_Code = %d%%\n",Temp_Code/100,Temp_Code%100,RH_Code%100);
	}


}


void sensor(){
	static int iii = 0;
	if(iii == 0){
		HAL_I2C_Master_Transmit(&hi2c1, 0x80, &add1, 1, 100);
		HAL_Delay(2);
		iii++;
	}
	Temperature_humidity();
	uart_sensor();

	if(RH_Code < humidity_min && en_r == 1){
		//motor(10);
		HAL_GPIO_WritePin(GPIOI, GPIO_PIN_11, GPIO_PIN_SET);
	}
	else{
		HAL_GPIO_WritePin(GPIOI, GPIO_PIN_11, GPIO_PIN_RESET);
	}

	if(Temp_Code/100 >= temperature_max && en_t == 1){
		  HAL_GPIO_WritePin(GPIOI, GPIO_PIN_10, GPIO_PIN_SET);
	}
	else{
		  HAL_GPIO_WritePin(GPIOI, GPIO_PIN_10, GPIO_PIN_RESET);
	}
}


uint8_t DZP_data[6][34]={
		{0xAA,0x55,
		0xFD,0xFF,0xFE,0xFF,0xC0,0x07,0xFF,0xFF,0xF7,0xDF,0xFB,0xBF,0x00,0x01,0xFF,0xFF,
		0xE0,0x0F,0xEF,0xEF,0xEF,0xEF,0xE0,0x0F,0xEF,0xEF,0xEF,0xEF,0xE0,0x0F,0xEF,0xEF},//�?//0//

		{0xAA,0x55,
		0xEF,0xDF,0xEF,0xDF,0xC3,0xDF,0xDF,0xDF,0xBE,0x03,0x42,0xDB,0xEE,0xDB,0xEE,0xDB,
		0x02,0xDB,0xEE,0x03,0xEE,0xDB,0xEF,0xDF,0xEB,0xDF,0xE7,0xDF,0xEF,0xDF,0xFF,0xDF},//�?//1//

		{0xAA,0x55,
		0xF7,0xBF,0xF7,0xBF,0xF7,0xBF,0xEC,0x07,0xEF,0xBF,0xCF,0x7F,0xC8,0x01,0xAF,0x7F,
		0x6E,0xFF,0xEC,0x07,0xEF,0xF7,0xEE,0xEF,0xEF,0x5F,0xEF,0xBF,0xEF,0xDF,0xEF,0xDF}//�?//2//

};

void uart_mode(){

	if(strcmp("mode = music",(char *)uart4_data)==0){
		mode = 0;
	}
	if(strcmp("mode = clock",(char *)uart4_data)==0){
		mode = 1;
	}
	if(strcmp("mode = sensor",(char *)uart4_data)==0){
		mode = 2;
	}
}
void end_main(){

	  tone_init(); //初始化音量频�??????
	  list_max = music_init();//更新乐谱
	  HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1);	//启动蜂鸣器定时器
	  HAL_TIM_Base_Start_IT(&htim2);		  	//启动定时�??????2
	  HAL_TIM_Base_Start_IT(&htim3);		  	//启动定时�??????2

	  //1 使能串口空闲中断
	  __HAL_UART_ENABLE_IT(&huart4,UART_IT_IDLE);
	  //2.使能串口中断接收数据
	  HAL_UART_Receive_IT(&huart4,rx_buf,sizeof(rx_buf));
	  int mode_n = 1;

	while(1){
		music_kz();

		alarm_clock();
		uart_mode();

		sensor();

		if(mode_n != mode){
			mode_n = mode;
			for(int i = 0; i<34;i++){
			//printf("afgsbgafdffag");
				HAL_I2C_Master_Transmit(&hi2c1, 0xA0 , (uint8_t*)&DZP_data[mode][i], 1, 300);
				HAL_Delay(2);
			}
		}

	}
}
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
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 */

  if(IS_ENGINEERING_BOOT_MODE())
  {
    /* Configure the system clock */
    SystemClock_Config();
  }

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_TIM2_Init();
  MX_TIM4_Init();
  MX_UART4_Init();
  MX_TIM3_Init();
  /* USER CODE BEGIN 2 */

  end_main();
  /* USER CODE END 2 */

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

    /* USER CODE BEGIN 3 */

	  //printf("afsgbhdn\t\n");
	  //HAL_Delay(500);
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = 16;
  RCC_OscInitStruct.HSIDivValue = RCC_HSI_DIV1;
  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  RCC_OscInitStruct.PLL2.PLLState = RCC_PLL_NONE;
  RCC_OscInitStruct.PLL3.PLLState = RCC_PLL_NONE;
  RCC_OscInitStruct.PLL4.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** RCC Clock Config
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_ACLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
                              |RCC_CLOCKTYPE_PCLK3|RCC_CLOCKTYPE_PCLK4
                              |RCC_CLOCKTYPE_PCLK5;
  RCC_ClkInitStruct.AXISSInit.AXI_Clock = RCC_AXISSOURCE_HSI;
  RCC_ClkInitStruct.AXISSInit.AXI_Div = RCC_AXI_DIV1;
  RCC_ClkInitStruct.MCUInit.MCU_Clock = RCC_MCUSSOURCE_HSI;
  RCC_ClkInitStruct.MCUInit.MCU_Div = RCC_MCU_DIV1;
  RCC_ClkInitStruct.APB4_Div = RCC_APB4_DIV1;
  RCC_ClkInitStruct.APB5_Div = RCC_APB5_DIV1;
  RCC_ClkInitStruct.APB1_Div = RCC_APB1_DIV1;
  RCC_ClkInitStruct.APB2_Div = RCC_APB2_DIV1;
  RCC_ClkInitStruct.APB3_Div = RCC_APB3_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief I2C1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_I2C1_Init(void)
{

  /* USER CODE BEGIN I2C1_Init 0 */

  /* USER CODE END I2C1_Init 0 */

  /* USER CODE BEGIN I2C1_Init 1 */

  /* USER CODE END I2C1_Init 1 */
  hi2c1.Instance = I2C1;
  hi2c1.Init.Timing = 0x10707DBC;
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Analogue filter
  */
  if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Digital filter
  */
  if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN I2C1_Init 2 */

  /* USER CODE END I2C1_Init 2 */

}

/**
  * @brief TIM2 Initialization Function
  * @param None
  * @retval None
  */
static void MX_TIM2_Init(void)
{

  /* USER CODE BEGIN TIM2_Init 0 */

  /* USER CODE END TIM2_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};

  /* USER CODE BEGIN TIM2_Init 1 */

  /* USER CODE END TIM2_Init 1 */
  htim2.Instance = TIM2;
  htim2.Init.Prescaler = 6400-1;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 1000-1;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM2_Init 2 */

  /* USER CODE END TIM2_Init 2 */

}

/**
  * @brief TIM3 Initialization Function
  * @param None
  * @retval None
  */
static void MX_TIM3_Init(void)
{

  /* USER CODE BEGIN TIM3_Init 0 */

  /* USER CODE END TIM3_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};

  /* USER CODE BEGIN TIM3_Init 1 */

  /* USER CODE END TIM3_Init 1 */
  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 6399;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 10000-1;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM3_Init 2 */

  /* USER CODE END TIM3_Init 2 */

}

/**
  * @brief TIM4 Initialization Function
  * @param None
  * @retval None
  */
static void MX_TIM4_Init(void)
{

  /* USER CODE BEGIN TIM4_Init 0 */

  /* USER CODE END TIM4_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_OC_InitTypeDef sConfigOC = {0};

  /* USER CODE BEGIN TIM4_Init 1 */

  /* USER CODE END TIM4_Init 1 */
  htim4.Instance = TIM4;
  htim4.Init.Prescaler = 639;
  htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim4.Init.Period = 100-1;
  htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim4) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_TIM_PWM_Init(&htim4) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 0;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  if (HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM4_Init 2 */

  /* USER CODE END TIM4_Init 2 */
  HAL_TIM_MspPostInit(&htim4);

}

/**
  * @brief UART4 Initialization Function
  * @param None
  * @retval None
  */
static void MX_UART4_Init(void)
{

  /* USER CODE BEGIN UART4_Init 0 */

  /* USER CODE END UART4_Init 0 */

  /* USER CODE BEGIN UART4_Init 1 */

  /* USER CODE END UART4_Init 1 */
  huart4.Instance = UART4;
  huart4.Init.BaudRate = 115200;
  huart4.Init.WordLength = UART_WORDLENGTH_8B;
  huart4.Init.StopBits = UART_STOPBITS_1;
  huart4.Init.Parity = UART_PARITY_NONE;
  huart4.Init.Mode = UART_MODE_TX_RX;
  huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart4.Init.OverSampling = UART_OVERSAMPLING_16;
  huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart4.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart4) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_SetTxFifoThreshold(&huart4, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_SetRxFifoThreshold(&huart4, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_DisableFifoMode(&huart4) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN UART4_Init 2 */

  /* USER CODE END UART4_Init 2 */

}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOF_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOI_CLK_ENABLE();
  __HAL_RCC_GPIOG_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOE_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1|GPIO_PIN_6, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOI, GPIO_PIN_11|GPIO_PIN_10, GPIO_PIN_RESET);

  /*Configure GPIO pins : PF1 PF6 */
  GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_6;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);

  /*Configure GPIO pin : PC7 */
  GPIO_InitStruct.Pin = GPIO_PIN_7;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  /*Configure GPIO pins : PI11 PI10 */
  GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_10;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);

  /*Configure GPIO pins : PG2 PG0 PG1 */
  GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_0|GPIO_PIN_1;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);

  /*Configure GPIO pin : PE9 */
  GPIO_InitStruct.Pin = GPIO_PIN_9;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

  /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(EXTI0_IRQn, 3, 0);
  HAL_NVIC_EnableIRQ(EXTI0_IRQn);

  HAL_NVIC_SetPriority(EXTI1_IRQn, 3, 0);
  HAL_NVIC_EnableIRQ(EXTI1_IRQn);

  HAL_NVIC_SetPriority(EXTI2_IRQn, 3, 0);
  HAL_NVIC_EnableIRQ(EXTI2_IRQn);

  HAL_NVIC_SetPriority(EXTI9_IRQn, 2, 0);
  HAL_NVIC_EnableIRQ(EXTI9_IRQn);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

stm32mp1xx_it.c

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    stm32mp1xx_it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32mp1xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */
int mode = 0;	//模式
extern void uart4_idle_func(void);
extern void smg_xians();
// 音乐盒基�?????变量
int time_100ms_cnt = 0; //0.1s计数�?????
int Beat_speed = 5;		//节拍速度,代表半个节拍需要多少个0.1s
int Beat_speed_n = 0;	//实际执行的节拍数

int Beat_num = 2;		//这个�?????个音�?????要多少个 半拍
int flag = 0; 			//当其等于 1 时,表示�?????个音结束
int EN_music = 0;				//使能信号,用于开启整个音乐盒
int list = 0;			//音乐列表
int list_max = 0;		//音乐总数
int Low_volume = 5;		//音量大小
int Low_volume_cnt = 3;	//音量大小增加�?????
int music_speed_i = 0; 	//音乐播放速度模式保存
// 音乐播放速度控制函数
int music_speed_kz(int i){
	//倍数计算公式 1 + (1 - (新的节拍速度 / 原来的节拍�?�度))
				switch(i){
				case 0:{
					Beat_speed = 5;	//0.5s半个节拍,正�?????+�??????�度
					break;
				}
				case 1:{
					Beat_speed = 4;	//1.2倍数
					break;
				}
				case 2:{
					Beat_speed = 3;	//约等�??????? 1.5倍数
					break;
				}
				case 3:{
					Beat_speed = 1;	//约等�??????? 2 倍数
					break;
				}
				case 4:{
					Beat_speed = 6;	//约等�??????? 0.8 倍数
					break;
				}
				case 5:{
					Beat_speed = 7;	//约等�??????? 0.6 倍数
					break;
				}
				default:{
					Beat_speed = 5;	//0.5s半个节拍,正常�?�度
					i=0;
					break;
				}
				}
	return i;
}

//数码管闹�????
int smg_number[10] = {0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xE0,0xFE,0xF6};
int buf[8] = {0};

//闹钟保存数组
int alarm_clock_array[20][4] = {0};
int alarm_clock_array_cnt = 0;
//实时时钟信息
int shi_shi = 0;
int shi_ge = 0;
int fen_shi = 0;
int fen_ge = 0;
int miao_shi = 0;
int miao_ge = 0;
int miao = 0;
int shi = 0;
int fen = 0;

int EN_clock = 0;//闹钟设置使能
extern int en_clock;//用于控制闹钟响铃

//闹钟设置信息
int shi_shi_clock = 0;
int shi_ge_clock = 0;
int fen_shi_clock = 0;
int fen_ge_clock = 0;
int miao_shi_clock = 0;
int miao_ge_clock = 0;
int miao_clock = 0, shi_clock = 0, fen_clock = 0;

/* USER CODE END TD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/
extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim3;
extern UART_HandleTypeDef huart4;
/* USER CODE BEGIN EV */

/* USER CODE END EV */

/******************************************************************************/
/*           Cortex-M4 Processor Interruption and Exception Handlers          */
/******************************************************************************/
/**
  * @brief This function handles Non maskable interrupt.
  */
void NMI_Handler(void)
{
  /* USER CODE BEGIN NonMaskableInt_IRQn 0 */

  /* USER CODE END NonMaskableInt_IRQn 0 */
  /* USER CODE BEGIN NonMaskableInt_IRQn 1 */
  while (1)
  {
  }
  /* USER CODE END NonMaskableInt_IRQn 1 */
}

/**
  * @brief This function handles Hard fault interrupt.
  */
void HardFault_Handler(void)
{
  /* USER CODE BEGIN HardFault_IRQn 0 */

  /* USER CODE END HardFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_HardFault_IRQn 0 */
    /* USER CODE END W1_HardFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Memory management fault.
  */
void MemManage_Handler(void)
{
  /* USER CODE BEGIN MemoryManagement_IRQn 0 */

  /* USER CODE END MemoryManagement_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
    /* USER CODE END W1_MemoryManagement_IRQn 0 */
  }
}

/**
  * @brief This function handles Pre-fetch fault, memory access fault.
  */
void BusFault_Handler(void)
{
  /* USER CODE BEGIN BusFault_IRQn 0 */

  /* USER CODE END BusFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_BusFault_IRQn 0 */
    /* USER CODE END W1_BusFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Undefined instruction or illegal state.
  */
void UsageFault_Handler(void)
{
  /* USER CODE BEGIN UsageFault_IRQn 0 */

  /* USER CODE END UsageFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_UsageFault_IRQn 0 */
    /* USER CODE END W1_UsageFault_IRQn 0 */
  }
}

/**
  * @brief This function handles System service call via SWI instruction.
  */
void SVC_Handler(void)
{
  /* USER CODE BEGIN SVCall_IRQn 0 */

  /* USER CODE END SVCall_IRQn 0 */
  /* USER CODE BEGIN SVCall_IRQn 1 */

  /* USER CODE END SVCall_IRQn 1 */
}

/**
  * @brief This function handles Debug monitor.
  */
void DebugMon_Handler(void)
{
  /* USER CODE BEGIN DebugMonitor_IRQn 0 */

  /* USER CODE END DebugMonitor_IRQn 0 */
  /* USER CODE BEGIN DebugMonitor_IRQn 1 */

  /* USER CODE END DebugMonitor_IRQn 1 */
}

/**
  * @brief This function handles Pendable request for system service.
  */
void PendSV_Handler(void)
{
  /* USER CODE BEGIN PendSV_IRQn 0 */

  /* USER CODE END PendSV_IRQn 0 */
  /* USER CODE BEGIN PendSV_IRQn 1 */

  /* USER CODE END PendSV_IRQn 1 */
}

/**
  * @brief This function handles System tick timer.
  */
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */

  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  /* USER CODE BEGIN SysTick_IRQn 1 */

  /* USER CODE END SysTick_IRQn 1 */
}

/******************************************************************************/
/* STM32MP1xx Peripheral Interrupt Handlers                                    */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file (startup_stm32mp1xx.s).                    */
/******************************************************************************/

/**
  * @brief This function handles EXTI line0 interrupt.
  */
void EXTI0_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI0_IRQn 0 */

	if(HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_0) == 0 && mode == 0)//确保数据稳定
	{

		//每次按下解决 音量�??????? Low_volume_cnt
		Low_volume = Low_volume + Low_volume_cnt;
		if(Low_volume >= 10)
			Low_volume = 0;
	}

	if(HAL_GPIO_ReadPin(GPIOG,GPIO_PIN_0)==GPIO_PIN_RESET && mode == 1) {

		shi_clock++;
		fen_shi_clock=fen_clock/10;
		fen_ge_clock=fen_clock%10;
		shi_shi_clock=shi_clock/10;
		shi_ge_clock=shi_clock%10;
		if(shi_clock>=24)
		{
			shi_clock=0;
		}

		miao_shi_clock=miao_clock/10;
		miao_ge_clock=miao_clock%10;
		fen_shi_clock=fen_clock/10;
		fen_ge_clock=fen_clock%10;
		shi_shi_clock=shi_clock/10;
		shi_ge_clock=shi_clock%10;
		buf[0]=smg_number[shi_shi_clock];
		buf[1]=smg_number[shi_ge_clock];
		buf[3]=smg_number[fen_shi_clock];
		buf[4]=smg_number[fen_ge_clock];
		buf[6]=smg_number[miao_shi_clock];
		buf[7]=smg_number[miao_ge_clock];
		}
  /* USER CODE END EXTI0_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
  /* USER CODE BEGIN EXTI0_IRQn 1 */

  /* USER CODE END EXTI0_IRQn 1 */
}

/**
  * @brief This function handles EXTI line1 interrupt.
  */
void EXTI1_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI1_IRQn 0 */
	if(HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_1) == 0 && mode == 0)//确保数据稳定
		{
		music_speed_i++;
		music_speed_i = music_speed_kz(music_speed_i);
		}

	if(HAL_GPIO_ReadPin(GPIOG,GPIO_PIN_1)==GPIO_PIN_RESET && mode == 1) {
		fen_clock++;
		fen_shi_clock=fen_clock/10;
		fen_ge_clock=fen_clock%10;
		if(fen_clock>=60)
		{
			fen_clock=0;
			shi_clock++;
			fen_shi_clock=fen_clock/10;
			fen_ge_clock=fen_clock%10;
			shi_shi_clock=shi_clock/10;
			shi_ge_clock=shi_clock%10;
			if(shi_clock>=24)
			{
				shi_clock=0;
			}
		}

		miao_shi_clock=miao_clock/10;
		miao_ge_clock=miao_clock%10;
		fen_shi_clock=fen_clock/10;
		fen_ge_clock=fen_clock%10;
		shi_shi_clock=shi_clock/10;
		shi_ge_clock=shi_clock%10;
		buf[0]=smg_number[shi_shi_clock];
		buf[1]=smg_number[shi_ge_clock];
		buf[3]=smg_number[fen_shi_clock];
		buf[4]=smg_number[fen_ge_clock];
		buf[6]=smg_number[miao_shi_clock];
		buf[7]=smg_number[miao_ge_clock];
		}
  /* USER CODE END EXTI1_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_1);
  /* USER CODE BEGIN EXTI1_IRQn 1 */

  /* USER CODE END EXTI1_IRQn 1 */
}

/**
  * @brief This function handles EXTI line2 interrupt.
  */
void EXTI2_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI2_IRQn 0 */
	if(HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_2) == 0 && mode == 0)//确保数据稳定
		{
			list++;
			if(list > list_max){
				list = 0;
			}
		}

	if(HAL_GPIO_ReadPin(GPIOG,GPIO_PIN_2)==GPIO_PIN_RESET && mode == 1) {
			//在此处关闭闹�????
			en_clock = 0;
		}
  /* USER CODE END EXTI2_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_2);
  /* USER CODE BEGIN EXTI2_IRQn 1 */

  /* USER CODE END EXTI2_IRQn 1 */
}

/**
  * @brief This function handles TIM2 global interrupt.
  */
void TIM2_IRQHandler(void)
{
  /* USER CODE BEGIN TIM2_IRQn 0 */
	if(EN_music == 1)
		time_100ms_cnt++;
	else
		time_100ms_cnt = time_100ms_cnt;	//其余状�?�不计数

	if(time_100ms_cnt >= Beat_speed_n * Beat_num){	//这个音节结束
		time_100ms_cnt = 0;
		flag = 1;	//发�?�音节结束信�???????
	}


	//数码�????
	static int smg_time_100ms = 0;
	smg_time_100ms++;
	if(smg_time_100ms>=10){
		miao++;
		smg_time_100ms = 0;
	}


	if (miao>=60)
	{
		miao=0;
		fen++;
		if(fen>=60)
		{
			fen=0;
			shi++;
			if(shi>=24)
			{
				shi=0;
			}
		}
	}


	if(miao >= 60){
		miao = miao-60;
		fen++;
	}
	if(fen>=60){
		fen = fen-60;
		shi ++;
	}
	if(shi>= 24){
		shi = shi -24;

	}


	miao_shi=miao/10;
	miao_ge=miao%10;

	fen_shi=fen/10;
	fen_ge=fen%10;

	shi_shi=shi/10;
	shi_ge=shi%10;


	if(EN_clock == 0){
	buf[0]=smg_number[shi_shi];
	buf[1]=smg_number[shi_ge];
	buf[3]=smg_number[fen_shi];
	buf[4]=smg_number[fen_ge];
	buf[6]=smg_number[miao_shi];
	buf[7]=smg_number[miao_ge];
	  HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_RESET);
	  //HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, GPIO_PIN_RESET);
	  //HAL_GPIO_WritePin(GPIOI, GPIO_PIN_11|GPIO_PIN_10, GPIO_PIN_RESET);
	}
	else{
		  HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_SET);
		  //HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, GPIO_PIN_SET);
		  //HAL_GPIO_WritePin(GPIOI, GPIO_PIN_11|GPIO_PIN_10, GPIO_PIN_SET);
	}

  /* USER CODE END TIM2_IRQn 0 */
  HAL_TIM_IRQHandler(&htim2);
  /* USER CODE BEGIN TIM2_IRQn 1 */

  /* USER CODE END TIM2_IRQn 1 */
}

/**
  * @brief This function handles TIM3 global interrupt.
  */
void TIM3_IRQHandler(void)
{
  /* USER CODE BEGIN TIM3_IRQn 0 */
	smg_xians();
  /* USER CODE END TIM3_IRQn 0 */
  HAL_TIM_IRQHandler(&htim3);
  /* USER CODE BEGIN TIM3_IRQn 1 */

  /* USER CODE END TIM3_IRQn 1 */
}

/**
  * @brief This function handles UART4 global interrupt.
  */
void UART4_IRQHandler(void)
{
  /* USER CODE BEGIN UART4_IRQn 0 */
	uart4_idle_func();

  /* USER CODE END UART4_IRQn 0 */
  HAL_UART_IRQHandler(&huart4);
  /* USER CODE BEGIN UART4_IRQn 1 */

  /* USER CODE END UART4_IRQn 1 */
}

/**
  * @brief This function handles EXTI line9 interrupt.
  */
void EXTI9_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI9_IRQn 0 */
	if(HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_9) == 0 && mode == 0){//确保数据稳定
		EN_music = !EN_music;
	}

	if(HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_9) == 0 && mode == 1 ){//确保数据稳定
		if(EN_clock == 1){
			//闹钟设置成功
			alarm_clock_array[alarm_clock_array_cnt][0] = shi_clock;
			alarm_clock_array[alarm_clock_array_cnt][1] = fen_clock;
			alarm_clock_array[alarm_clock_array_cnt][2] = miao_clock;
			alarm_clock_array[alarm_clock_array_cnt][3] = 3;	//默认播放第三首音�????
			alarm_clock_array_cnt++;
			if(alarm_clock_array_cnt >= 20) alarm_clock_array_cnt = 0;
			EN_clock = 0;
		}
		else if(EN_clock == 0){
			//设置闹钟
			shi_shi_clock = shi_shi;
			shi_ge_clock = shi_ge;
			fen_shi_clock = fen_shi;
			fen_ge_clock = fen_ge;
			miao_shi_clock = 0;
			miao_ge_clock = 0;
			miao_clock = 0;
			shi_clock = shi;
			fen_clock = fen;
			EN_clock = 1;
		}
	}






  /* USER CODE END EXTI9_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_9);
  /* USER CODE BEGIN EXTI9_IRQn 1 */

  /* USER CODE END EXTI9_IRQn 1 */
}

/**
  * @brief This function handles RCC wake-up interrupt.
  */
void RCC_WAKEUP_IRQHandler(void)
{
  /* USER CODE BEGIN RCC_WAKEUP_IRQn 0 */

  /* USER CODE END RCC_WAKEUP_IRQn 0 */
  HAL_RCC_WAKEUP_IRQHandler();
  /* USER CODE BEGIN RCC_WAKEUP_IRQn 1 */

  /* USER CODE END RCC_WAKEUP_IRQn 1 */
}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

        串口指令集

mode = music
mode = clock
mode = sensor

music volume increase
music volume reduction
music speed increase
music speed reduction
music next song
music previous song
music start
music stop

clock shi = 
clock fen = 
clock miao = 
clock delay shi = 
clock delay fen = 
clock delay miao = 

time shi = ;fen = ;miao = 
clock delay list
clock stop list = 
clock stop stop

sensor  humidity_min = 
sensor  temperature_max = 
sensor temperature start
sensor humidity start
sensor temperature stop
sensor humidity stop
sensor list
sensor Temp_Code RH_Code

六、部分效果展示

STM32杂交版

七、总结

        本设计是一个高度集成的基于STM32MP157A单片机的多功能系统,通过整合蜂鸣器、数码管、点阵屏、温湿度传感器、LED灯、按键等多种模块,实现了丰富的交互与功能。系统利用STM32CUBEIDE作为开发平台,充分发挥了STM32MP157A单片机的高性能与灵活性,展现了其在嵌入式系统设计中的广泛应用潜力。

设计总结:

  1. 模块化设计:本设计采用了模块化设计思路,将不同功能模块(如闹钟、音乐盒、温湿度监测)独立设计后整合在一起,不仅提高了系统的可维护性和可扩展性,还使得各个模块的功能实现更加清晰明了。

  2. 灵活的模式切换:通过串口通信实现不同模式(闹钟、音乐盒、温湿度监测)之间的灵活切换,使得用户可以根据需要轻松选择所需功能,提高了系统的用户友好性和实用性。

  3. 多样化的显示与交互:点阵屏在不同模式下显示不同的汉字(如“钟”、“音”、“传”),直观展示了当前的工作模式,增强了用户体验。同时,按键和串口控制相结合的方式,使得用户可以通过多种途径对系统进行操作,如调节音乐播放速度、音量、切换歌曲,调整闹钟时间、设置多个闹钟等,极大地丰富了系统的交互方式。

  4. 温湿度监测与调节:系统集成了温湿度传感器,能够实时监测环境温湿度,并通过串口调节温湿度上下限,当温湿度超出设定范围时,通过LED灯进行边界提示,实现了对环境的智能监测与调节。

  5. 高效的开发平台:采用STM32CUBEIDE作为开发平台,利用其强大的代码编辑、编译、调试功能,以及丰富的库函数和示例项目,极大地提高了开发效率,降低了开发难度。

  6. 综合应用能力的展现:本设计不仅展示了STM32MP157A单片机在嵌入式系统设计中的强大功能,还体现了设计者在硬件选型、电路设计、软件编程、系统调试等方面的综合应用能力。

综上所述,本设计是一个集多功能性、灵活性、用户友好性于一体的嵌入式系统,充分展示了STM32MP157A单片机在复杂系统设计中的广泛应用前景和潜力。通过本设计的实施,不仅加深了对嵌入式系统设计的理解,还提升了解决实际问题的能力。

参考资料:

        1. STM32简易音乐播放器(HAL库)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/792853.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Android APT实战

Android开发中,注解平时我们用的比较多,也许我们会比较好奇,注解的背后是如何工作的,这篇文章帮大家一步步创建一个简单的注解处理器。 简介 APT(Annotation Processing Tool)即注解处理器,在编译的时候可以处理注解然后搞一些事情,也可以在编译时生成一些文件之类的。…

Linux 命令探秘:揭秘那些有趣的小命令

目录 1.发现隐藏在终端的惊喜小命令 2.小火车 1.安装EPEL 2.小火车出发准备 3.输入命令 3.linux_logo 1.安装linux_logo 2.输入命令 3.linux_logo介绍 4.牛讲话 1.安装命令 2.输入命令 5. figlet 1.安装命令 2.输入命令 “如果您在解决类似问题时也遇到了困…

stm32学习笔记---I2C通信协议(理论部分)

目录 串口通信协议和I2C通信协议的联系 同步和异步的区别 I2C通信 硬件电路 指定地址写的流程 当前地址读的流程 指定地址读的流程 声明&#xff1a;本专栏是本人跟着B站江科大的视频的学习过程中记录下来的笔记&#xff0c;我之所以记录下来是为了方便自己日后复习。如…

软航文档控件VUE示例运行及控件替换方法记录

目录 示例运行 步骤一、npm install 步骤二、npm run dev 软航文档控件替换 附 vue小白记录一下软航文档控件VUE示例的运行方法以及示例中控件的替换过程。 示例运行 在已经安装好VUE环境的电脑上&#xff0c;VUE环境部署可以参考另一篇&#xff1a;配置VUE环境过程中 …

数据结构复习计划之复杂度分析(时间、空间)

第二节&#xff1a;算法 时间复杂度和空间复杂度 算法(Algorithm)&#xff1a;是对特定问题求解方法(步骤)的一种描述&#xff0c;是指令的有限序列&#xff0c;其中每一条指令表示一个或多个操作。 算法可以有三种表示形式&#xff1a; 伪代码 自然语言 流程图 算法的五…

【正点原子i.MX93开发板试用连载体验】为什么模型不能运行在NPU上

本文最早发表于电子发烧友论坛&#xff1a;【新提醒】【正点原子i.MX93开发板试用连载体验】基于深度学习的语音本地控制 - 正点原子学习小组 - 电子技术论坛 - 广受欢迎的专业电子论坛! (elecfans.com) 昨天提到要使模型运行的NPU上&#xff0c;必须先将其量化。如果对没有量化…

编程零基础教程,从知道什么是前端开始

本文作者&#xff1a;程序员鱼皮 免费编程学习 - 编程导航网&#xff1a;https://www.code-nav.cn 鱼小皮&#xff1a;百哥&#xff0c;我想学编程&#xff0c;应该先学啥呢&#xff1f; 老百&#xff1a;小皮&#xff0c;怎么突然想学编程了&#xff0c;不会又是三分钟热度吧&…

vue学习day06-脚手架目录文件介绍与项目运行流程、组件化开发和根组件、普通组件的注册使用-局部注册、全局注册

16、脚手架目录文件介绍与项目运行流程 &#xff08;1&#xff09;脚手架目录文件介绍 &#xff08;2&#xff09;Index.html &#xff08;3&#xff09;Main.js 17、组件化开发和根组件 &#xff08;1&#xff09;组件化 1&#xff09;概念 一个页面可以拆分成一个个组件&am…

Spring源码二十二:Bean实例化流程五

上一篇Spring源码二十一&#xff1a;Bean实例化流程四&#xff0c;咱们主要分析里createBeanInstance方法Spring给我们提供给的FactoryMethod方法&#xff0c;举例说明了factoryMethod属性如何使用&#xff0c;同时简单讨论了具体实现逻辑。 这一篇咱们将进入反射实例化Bean&am…

MySQL的事务使用

文章目录 特点JDBC使用事务 特点 事务的基本属性ACID&#xff1a; 数据库事务的ACID特性是指保证数据库在执行事务操作时能够可靠和正确的四个基本属性。ACID是原子性&#xff08;Atomicity&#xff09;、一致性&#xff08;Consistency&#xff09;、隔离性&#xff08;Isol…

科研绘图之tSNE图

t-SNE&#xff08;t-Distributed Stochastic Neighbor Embedding&#xff0c;t分布随机邻域嵌入&#xff09;是一种用于数据降维和可视化的算法。它可以将高维数据映射到二维或三维空间&#xff0c;同时尽可能地保留数据点之间的局部关系。t-SNE特别适用于探索数据的内部结构和…

C语言 指针和数组——指针数组的应用:命令行参数

目录 命令行参数 演示命令行参数与main函数形参间的关系 命令行参数  什么是 命令行参数&#xff08; Command Line Arguments &#xff09;&#xff1f;  GUI 界面之前&#xff0c;计算机的操作界面都是字符式的命令行界面 &#xff08; DOS 、 UNIX 、 Linux &…

IEPE数据采集卡的作用说明

IEPE指的是一种自带电量放大器或电压放大器的加速度传感器。IEPE是压电集成电路的缩写。因为由加速度传感器产生的电量是很小的&#xff0c;因此传感器产生的电信号很容易受到噪声干扰&#xff0c;需要用灵敏的电子器件对其进行放大和信号调理。IEPE中集成了灵敏的电子器件使其…

连锁行业观察:一线门店设备如何运维?化“管理”为“服务”

连锁零售行业的数字化发展&#xff0c;离开不了大量智能设备的支撑&#xff0c;比如我们日常见到的各种门店互动终端、自助收银设备、无人值守售货机等等。 由于连锁行业的特性&#xff0c;这些设备往往位置分散&#xff0c;数量众多&#xff0c;难以集中管理。一旦这些设备遇…

ARM功耗管理之多核处理器启动

安全之安全(security)博客目录导读 思考&#xff1a;SecureBoot&#xff1f;多核处理器启动流程&#xff1f;PSCI启动方式&#xff1f; 一般嵌入式系统使用的都是对称多处理器&#xff08;Symmetric Multi-Processor, SMP&#xff09;系统&#xff0c;包含了多个cpu, 这几个cp…

YOLOv8改进 | 注意力机制| 对小目标友好的BiFormer【CVPR2023】

秋招面试专栏推荐 &#xff1a;深度学习算法工程师面试问题总结【百面算法工程师】——点击即可跳转 &#x1f4a1;&#x1f4a1;&#x1f4a1;本专栏所有程序均经过测试&#xff0c;可成功执行&#x1f4a1;&#x1f4a1;&#x1f4a1; 专栏目录 &#xff1a;《YOLOv8改进有效…

从零开始开发视频美颜SDK:实现直播美颜效果

因此&#xff0c;开发一款从零开始的视频美颜SDK&#xff0c;不仅可以节省成本&#xff0c;还能根据具体需求进行个性化调整。本文将介绍从零开始开发视频美颜SDK的关键步骤和实现思路。 一、需求分析与技术选型 在开发一款视频美颜SDK之前&#xff0c;首先需要进行详细的需求…

MongoDB本地配置分片

mongodb server version: 7.0.12 社区版 mongo shell version: 2.2.10 平台&#xff1a;win10 64位 控制台&#xff1a;Git Bash 分片相关节点结构示意图 大概步骤 1. 配置 配置服务器 副本集 &#xff08;最少3个节点&#xff09; -- 创建数据目录 mkdir -p ~/dbs/confi…

硬件开发工具Arduino IDE

招聘信息共享社群 关联上篇文章乐鑫ESPRESSIF芯片开发简介 Arduino IDE&#xff08;集成开发环境&#xff09;是为Arduino硬件开发而设计的一款软件&#xff0c;它提供了一个易于使用的图形界面&#xff0c;允许用户编写、编辑、编译和上传代码到Arduino开发板。Arduino IDE的…

【逆向基础】十、逆向工具分享之DIE(Detect It Easy)

一、简介 DIE&#xff08;Detect It Easy&#xff09;是一款可以轻松检测PE文件的程序&#xff1b;其主要作用是查壳&#xff0c;并将pe文件的内容解析出来&#xff0c;包括PE文件中包含的导入函数、导出函数的名称及地址&#xff0c;入口函数地址等&#xff0c;是技术人员分析…