设备说明:本设备使用具有两个usart串口的stc12c5a60s2作为MCU主控,SNR3512作为语音识别模块,JQC80作为语音模块,esp8266作为联网模块。本设备可以实现非特定人声的语音识别,并且在识别之后进行自动联网获取目标城市的时间和未来七天的温度。在本装置中,可以在上电之后按下key1或者呼叫"小星,小星"启动SNR3512模块,之后呼叫"启动引擎",开启联网。联网成功后自动播放时间和温度。天气API用的是免费的,因为这个天气API需要进行解析,利用单片机进行解析比较麻烦。所以就让朋友搭建了一个新的网址,在自己搭建的网址中先把API中的天气提取完成。这样我的设备直接访问这个网址就可以获取到时间和天气了。操作说明:
1.插上电源,会启动OLED并且播放声音语句:"欢迎使用小桔智能天气时钟"
2.按键Key1或者呼叫“小星小星”+"启动引擎",播放语句"小桔正在努力联网中"
3.oled最下面会依次显示step1-ok,step2-ok,step3-ok,step4-ok,step5-ok.step6-ok.,step7-0k。
其中,step2-step3需要进行大概10~20s的时间,之后就会播放"联网成功",否则播放"联网失败,重新联网中"
,联网失败需要按下key1进行重新联网,或者重新启动设备。
4.到step7-ok后则会播放时间和天气,此时播放结束后可以按key1或者呼叫"小星,小星",则可以重新播放时间。如果需要退出联网状态,按下key2,显示step8-ok。
最后:本代码还有不少的地方需要进行完善,有兴趣的伙伴可以自己动手进行试验哦。
作者:Vence
QQ:1907913345
微信/电话:18888645003
原理图:
PCB:
主函数代码:
/*********************************************************
***************时间:2020年4月9日22点33分******************
项目名称:基于stc12c5a60s2+snr3512的智能语音天气时钟*******
说明:通过非特定人声的语音操作,让设备自动获取网络时间并进*
行语音播报 ***********************************************
*******************作者:Vence*****************************
*********************************************************/
#include<STC12C5A60S2.h>
#include "GLOBAL.h"
#include "DEFINE.h"
//全局函数
//unsigned char temp1,temp2; //串口1、2数据接收数量
volatile unsigned char idata recarray[39]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //数据缓存
volatile unsigned char idata receive_snr3512[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //接收SNR3512的数据
//获取的时间,年,月,日
unsigned char year[6] = {'2','0','2','0','-','\0'};
unsigned char date[6] = {'0','1','-','0','1','\0'};
unsigned char time[6] = {'1','2',':','0','0','\0'}; //结尾要加\0,否则不知道是否为字符串的结尾
unsigned char temp[8] = {21,0,0,0,0,0,0,0}; //宁波市未来七天最低温度
unsigned char month,dat,hour,minute;
//unsigned char step[2] = {'0','\0'};
unsigned char step,reapeat,reap;
unsigned char time_flag;
//unsigned char *time = "2020-04-17 12:15";
//主函数
void main()
{
time_flag = 0;
step = 0;
reapeat = 0;
reap = 0;
flag = 0;
count_all = 0;
snr3512_count = 0;
Init1302(); //初始化1302
InitUART(); //初始化串口,关闭串口1、2
OLED_Init(); //初始化OLED
OLED_Clear();
//delay1ms(100); //esp8266上电初始化
OLED_ShowCHinese(0,0,0);//小
OLED_ShowCHinese(18,0,1);//桔
OLED_ShowCHinese(36,0,2);//智
OLED_ShowCHinese(54,0,3);//能
OLED_ShowCHinese(72,0,4);//天
OLED_ShowCHinese(90,0,5);//气
OLED_ShowCHinese(108,0,6);//钟
OLED_ShowString(0,2,year);
OLED_ShowString(40,2,date);
OLED_ShowString(88,2,time);
OLED_ShowString(0,4,"weather: sun");
OLED_ShowString(0,6,"0");
voice_display(1);
while(1)
{
if(key1 == 0 || receive_snr3512[0] == 0x88) //按下key1或者启动引擎
{
time_flag = 0;
delay1ms(10);
if(key1 == 0 || receive_snr3512[0] == 0x88)
{
ES = 1;
IE2 =0x00; //关闭串口2中断 ES2=1
step = 1;
while(1)
{
if(step == 1)
{
voice_display(2); //小桔正在努力联网中
ClearRec();
ClearSNR3512();
SendGprsCmd(str1);
while(reapeat<30 && reap<10)
{
delay1ms(100);
reap++;
}
if(recarray[0] == 'K')
{
OLED_ShowString(0,6,"step1-ok");
ClearRec();
step = 2;
reap = reapeat = 0;
}
else
{
reapeat++;
if(reapeat>30)
{
voice_display(4); //联网失败,重新联网中
IE2 =0x01;
ClearRec();
reap = reapeat = 0;
OLED_ShowString(0,6,"step1-err");
break;
}
}
}
if(step == 2)
{
ClearRec();
SendGprsCmd(str2);
while(reapeat<5 && reap<10)
{
delay1ms(100);
reap++;
}
if(recarray[0] == 'K')
{
OLED_ShowString(0,6,"step2-ok");
ClearRec();
step = 3;
reap = reapeat = 0;
}
else
{
reapeat++;
if(reapeat>5)
{
voice_display(4); //联网失败,重新联网中
IE2 =0x01;
ClearRec();
reap = reapeat = 0;
OLED_ShowString(0,6,"step2-err");
break;
}
}
}
if(step == 3)
{
ClearRec();
SendGprsCmd(str3);
while(reapeat<5 && reap<100)
{
delay1ms(100);
reap++;
}
if(recarray[0] == 'K')
{
voice_display(3); //联网成功
OLED_ShowString(0,6,"step3-ok");
ClearRec();
step = 4;
reap = reapeat = 0;
}
else
{
reapeat++;
if(reapeat>5)
{
voice_display(4); //联网失败,重新联网中
IE2 =0x01;
ClearRec();
reap = reapeat = 0;
OLED_ShowString(0,6,"step3-err");
break;
}
}
}
if(step == 4)
{
ClearRec();
SendGprsCmd(str4);
while(reapeat<5 && reap<10)
{
delay1ms(100);
reap++;
}
if(recarray[0] == 'K')
{
OLED_ShowString(0,6,"step4-ok");
ClearRec();
step = 5;
reap = reapeat = 0;
}
else
{
reapeat++;
if(reapeat>5)
{
voice_display(4); //联网失败,重新联网中
IE2 =0x01;
ClearRec();
reap = reapeat = 0;
OLED_ShowString(0,6,"step4-err");
break;
}
}
}
if(step == 5)
{
ClearRec();
SendGprsCmd(str5);
while(reapeat<5 && reap<10)
{
delay1ms(100);
reap++;
}
if(recarray[0] == 'K')
{
OLED_ShowString(0,6,"step5-ok");
ClearRec();
step = 6;
reap = reapeat = 0;
}
else
{
reapeat++;
if(reapeat>5)
{
voice_display(4); //联网失败,重新联网中
IE2 =0x01;
ClearRec();
reap = reapeat = 0;
OLED_ShowString(0,6,"step5-err");
break;
}
}
}
if(step == 6)
{
ClearRec();
SendGprsCmd(str6);
while(reapeat<5 && reap<10)
{
delay1ms(100);
reap++;
}
if(recarray[0] == 0x3e)
{
OLED_ShowString(0,6,"step6-ok");
ClearRec();
step = 7;
reap = reapeat = 0;
}
else
{
reapeat++;
if(reapeat>5)
{
voice_display(4); //联网失败,重新联网中
IE2 =0x01;
ClearRec();
reap = reapeat = 0;
OLED_ShowString(0,6,"step6-err");
break;
}
}
}
if(step == 7)
{
SendGprsCmd(str7);
delay1ms(100);
SendGprsCmd(str8);
while(reapeat<5 && reap<20)
{
delay1ms(100);
reap++;
}
if(recarray[0] == '0')
{
OLED_ShowString(0,6,"step7-ok");
date[0] = recarray[1];
date[1] = recarray[2];
date[3] = recarray[3];
date[4] = recarray[4];
time[0] = recarray[5];
time[1] = recarray[6];
time[3] = recarray[7];
time[4] = recarray[8];
temp[1] = (Ctoi(recarray[11]))*10+Ctoi(recarray[12]);
temp[2] = (Ctoi(recarray[13]))*10+Ctoi(recarray[14]);
temp[3] = (Ctoi(recarray[15]))*10+Ctoi(recarray[16]);
temp[4] = (Ctoi(recarray[17]))*10+Ctoi(recarray[18]);
temp[5] = (Ctoi(recarray[19]))*10+Ctoi(recarray[20]);
temp[6] = (Ctoi(recarray[21]))*10+Ctoi(recarray[22]);
temp[7] = (Ctoi(recarray[23]))*10+Ctoi(recarray[24]);
month = (Ctoi(recarray[1]))*10+Ctoi(recarray[2]);
dat = (Ctoi(recarray[3]))*10+Ctoi(recarray[4]);
hour = (Ctoi(recarray[5]))*10+Ctoi(recarray[6]);
minute = (Ctoi(recarray[7]))*10+Ctoi(recarray[8]);
OLED_ShowString(0,2,year);
OLED_ShowString(40,2,date);
OLED_ShowString(88,2,time);
time_dispaly(month,dat,hour,minute); //播放时间
if(time_flag == 0)
weath_display(temp); //播放天气
voice_display(5); //小桔温馨提醒,注意疫情防护
time_flag = 0;
step = 8;
reap = reapeat = 0;
}
else
{
reapeat++;
if(reapeat>5)
{
voice_display(4); //联网失败,重新联网中
IE2 =0x01;
SendGprsCmd(str9);
ClearRec();
reap = reapeat = 0;
OLED_ShowString(0,6,"step7-err");
break;
}
}
}
if(step == 8)
{
IE2 =0x01;
if(key1 == 0 || receive_snr3512[0] == 0x7f) //重复获取时间
{
if(key1 == 0 || receive_snr3512[0] == 0x7f)
{
time_flag = 1;
voice_display(6);
ClearRec();
ClearSNR3512();
step = 7;
}
}
if(key2 == 0 || receive_snr3512[0] == 0x89) //退出透传模式
{
SendGprsCmd(str9);
ClearRec();
ClearSNR3512();
OLED_ShowString(0,6,"step8-ok");
ES = 0;
break;
}
}
}
}
}
}
}
/************串口1中断处理函数*************/
void UART_1Interrupt(void) interrupt 4
{
if(RI)
{
if(step == 1 || step == 2 || step == 3 || step == 4 || step == 5)
{
RI = 0;
recarray[count_all] = SBUF;
if(recarray[0] == 'K')
{
flag = 1;
}
if(flag == 1)
count_all++;
}
if(step == 6)
{
RI = 0;
recarray[count_all] = SBUF;
if(recarray[0] == 0x3e)
{
flag = 1;
}
if(flag == 1)
count_all++;
}
if(step == 7)
{
if(flag == 0)
{
RI = 0;
recarray[count_all] = SBUF;
if(recarray[0] == 'L')
{
flag = 1;
}
}
if(flag == 1)
{
RI = 0;
recarray[count_all] = SBUF;
if(recarray[0] == '0')
{
count_all++;
}
}
}
}
}
/************串口2中断处理函数*************/
void UART_2Interrupt(void) interrupt 8
{
if(S2CON&S2RI)
{
S2CON&=~S2RI;
receive_snr3512[0] = S2BUF;
}
}
|