STM32项目分享:智能语音台灯
目录
一、前言
二、项目简介
1.功能详解
2.主要器件
三、原理图设计
四、PCB硬件设计
PCB图
五、程序设计
六、实验效果
七、包含内容
项目分享
一、前言
项目成品图片:
哔哩哔哩视频链接:
STM32智能语音台灯
(资料分享见文末)
二、项目简介
1.功能详解
基于STM32的智能语音台灯
功能如下:
- STM32F103C8T6单片机系统板作为主控单元
- 按键切换模式,支持智能/按键/远程/语音四种模式
- 光敏电阻采集光照强度,超声波传感器采集坐姿距离,所有参数显示在OLED上
- 智能模式:自动识别有人开灯,并根据环境光强度控制灯的亮度
- 按键模式:按键手动调节灯亮度(三档),可以按键控制计时
- 远程模式:通过手机蓝牙APP远程控制台灯开关与计时操作
- 语音模式:通过语音识别控制台灯调光与计时功能
- 坐姿报警:超声波距离报警,距离小于10cm提醒坐姿
2.主要器件
- STM32F103C8T6最小系统板
- OLED 屏幕
- 光敏电阻传感器
- 光电红外传感器
- 超声波传感器
- SNR016语音识别模块
- HC-SR04超声波模块
- 大功率LED模块
- 有源蜂鸣器
三、原理图设计
四、PCB硬件设计
PCB图
五、程序设计
extern u8 Res1, Res3;
extern int timer1;
u16 sonic_dis = 0;
int main(void)
{
int key = 0, flag1 = 0, flag2 = 0, flag3 = 0, flag4 = 0, guangmi = 0, flag5 = 0;
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
usart3_Init(9600);
LED_Init();
BEEP_Init();
OLED_Init();
KEY_Init();
Lsens_Init();
hongwai(); //人体
TIM3_Int_Init(9, 71); //10us
EXTIX_Init();
hc_sr04_init(); //HC_SR04初始化
TIM4_Int_Init(9999, 7199); //计时
TIM2_PWM_Init(899, 0); //调节亮度
TIM_SetCompare2(TIM2, 899);
BEEP = 0;
OLED_ShowCHinese(35, 0, 0);
OLED_ShowCHinese(50, 0, 1);
OLED_ShowCHinese(65, 0, 2);
OLED_ShowCHinese(80, 0, 3); //标题
OLED_ShowCHinese(0, 2, 6);
OLED_ShowCHinese(15, 2, 7);
OLED_ShowString(30, 2, ":", 16); //模式
OLED_ShowCHinese(0, 4, 16);
OLED_ShowCHinese(15, 4, 17);
OLED_ShowString(30, 4, ":", 16); //状态
OLED_ShowCHinese(70, 4, 22); //无人
OLED_ShowCHinese(85, 4, 23);
OLED_ShowString(100, 4, ":", 16);//状态
OLED_ShowCHinese(0, 6, 24); //时间
OLED_ShowCHinese(15, 6, 25);
OLED_ShowString(30, 6, ":", 16);
OLED_ShowString(65, 6, "s", 16);
while (1)
{
usart2_init(9600); //蓝牙通信
sonic_Trig(); //超声波
sonic_dis = sonic_distance / 10;
OLED_ShowNum(75, 6, sonic_distance / 10, 4, 16);
OLED_ShowString(110, 6, "cm", 16);
if (sonic_distance / 10 > 10)
{
BEEP = 0;
}
else if (sonic_distance / 10 <= 10)
{
BEEP = 1;
}
guangmi = Lsens_Get_Val(); //亮度
OLED_ShowNum(110, 4, guangmi, 2, 16);
key = KEY_Scan(0);
if (key == 1)
{
flag1 += 1;
if (flag1 > 3)
flag1 = 0;
}
if (flag1 == 0) //智能模式
{
OLED_ShowCHinese(45, 2, 4);
OLED_ShowCHinese(60, 2, 5);
OLED_ShowCHinese(75, 2, 6);
OLED_ShowCHinese(90, 2, 7);
if (renti == 0)
{
OLED_ShowCHinese(40, 4, 18);//有人
OLED_ShowCHinese(55, 4, 19);
flag2 = 1;
}
else if (renti == 1)
{
OLED_ShowCHinese(40, 4, 20);//无人
OLED_ShowCHinese(55, 4, 21);
flag2 = 0;
}
if (flag2 == 1)
{
if (guangmi > 20 && guangmi <= 40)
{
TIM_SetCompare2(TIM2, 200);
}
if (guangmi > 40 && guangmi <= 60)
{
TIM_SetCompare2(TIM2, 400);
}
if (guangmi > 60)
{
TIM_SetCompare2(TIM2, 800);
}
}
else if (flag2 == 0)
{
TIM_SetCompare2(TIM2, 899);//关灯
}
}
if (flag1 == 1)
{
OLED_ShowCHinese(45, 2, 8);
OLED_ShowCHinese(60, 2, 9);
OLED_ShowCHinese(75, 2, 10);
OLED_ShowCHinese(90, 2, 11); //按键模式
if (key == 2)
{
flag3 +=1 ;
if(flag3>3)
{
flag3=0;
}
}
if (flag3 == 0)
{
TIM_SetCompare2(TIM2, 899);
}
else if (flag3 == 1)
{
TIM_SetCompare2(TIM2, 800);
}
else if (flag3 == 2)
{
TIM_SetCompare2(TIM2, 600);
}
else if (flag3 == 3)
{
TIM_SetCompare2(TIM2, 400);
}
if (key == 3)
{
flag4 = !flag4; //计时
}
if (flag4 == 1)
{
TIM_Cmd(TIM4, ENABLE); //使能TIMx
OLED_ShowNum(40, 6, timer1, 3, 16);
}
if (flag4 == 0)
{
TIM_Cmd(TIM4, DISABLE); //使能TIMx
OLED_ShowNum(40, 6, timer1, 3, 16);
}
}
if (flag1 == 2)
{
OLED_ShowCHinese(45, 2, 12);
OLED_ShowCHinese(60, 2, 13);
OLED_ShowCHinese(75, 2, 14);
OLED_ShowCHinese(90, 2, 15); //远程模式
switch (Res1)
{
case 1:
TIM_SetCompare2(TIM2, 400);
break; //开灯
case 2:
TIM_SetCompare2(TIM2, 899);
break; //关灯
case 3:
TIM_Cmd(TIM4, ENABLE);
OLED_ShowNum(40, 6, timer1, 3, 16);
break; //开启计时
case 4:
TIM_Cmd(TIM4, DISABLE);
OLED_ShowNum(40, 6, timer1, 3, 16);
break; //关闭计时
case 5:
TIM_SetCompare2(TIM2, 200);
break; //三档灯光
case 6:
TIM_SetCompare2(TIM2, 500);
break; //二档灯光
case 7:
TIM_SetCompare2(TIM2, 800);
break; //一档灯光
case 8:
TIM_Cmd(TIM4, DISABLE);
timer1 = 0;
OLED_ShowNum(40, 6, timer1, 3, 16);
break; //清零
}
}
if (flag1 == 3)
{
OLED_ShowCHinese(45, 2, 26);
OLED_ShowCHinese(60, 2, 27);
OLED_ShowCHinese(75, 2, 28);
OLED_ShowCHinese(90, 2, 29); //语音模式
Uart3_SendCMD2(0x01, guangmi);
Uart3_SendCMD2(0x02, sonic_distance / 10);
switch (Res3)
{
case 1:
TIM_SetCompare2(TIM2, 400);
break; //开灯
case 2:
TIM_SetCompare2(TIM2, 899);
break; //关灯
case 3:
TIM_SetCompare2(TIM2, 800);
break; //一档灯光
case 4:
TIM_SetCompare2(TIM2, 500);
break; //二档灯光
case 5:
TIM_SetCompare2(TIM2, 200);
break; //三档灯光
case 6:
TIM_Cmd(TIM4, ENABLE);
OLED_ShowNum(40, 6, timer1, 3, 16);
break; //开启计时
case 7:
TIM_Cmd(TIM4, DISABLE);
OLED_ShowNum(40, 6, timer1, 3, 16);
break; //关闭计时
case 8:
TIM_Cmd(TIM4, DISABLE);
timer1 = 0;//清零
OLED_ShowNum(40, 6, timer1, 3, 16);
break;
}
}
if (key == 4)
{
timer1 = 0;
}
}
}