基于语音控制的智能家居系统设计(毕业设计初版)
基于语音控制的智能家居系统设计(毕业设计初版)
1、项目组成
1.上位机:安卓手机
2.下位机:STM32单片机(STM32F103C8T6)
3.外接模块:语音识别模块(LD3320),蓝牙模块(BLE CC2541)
4.开发软件:Android Studio 3.1.2,Keil uVision5,Keil uVision4
5.其他实物:LED灯泡2个,直流小电机1个,L298N驱动模块1个,面包板1块,面包板供电模块1个,杜邦线若干
6.下载链接:https://download.****.net/download/qq_40093925/11020346
2、参考博客
安卓手机与蓝牙模块联合调试
STM32串口接收字符串并控制LED
感谢以上两位博主的开源项目
3、安卓APP工程(只贴主程序和主布局文件)
3.1、MainActivity.java
package com.cjt.bluetoothscm;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.github.zagum.switchicon.SwitchIconView;
import com.inuker.bluetooth.library.Constants;
import com.inuker.bluetooth.library.connect.options.BleConnectOptions;
import com.inuker.bluetooth.library.connect.response.BleConnectResponse;
import com.inuker.bluetooth.library.connect.response.BleNotifyResponse;
import com.inuker.bluetooth.library.connect.response.BleWriteResponse;
import com.inuker.bluetooth.library.model.BleGattProfile;
import java.io.IOException;
import java.util.UUID;
import static com.inuker.bluetooth.library.Constants.REQUEST_SUCCESS;
/*****************
* 包名:com.cjt.bluetoothscm
* 类名:MainActivity.java
* 时间:2018/9/11 23:28
* 作者:Cao Jiangtao
* 首页:https://1989jiangtao.github.io/index.html
******************/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final int REQUEST_CONNECT_DEVICE = 0x100;
TextView mainTitle ;
// 灯组01 ,灯组02 , 电源开关, 风扇开关
SwitchIconView lamp01 , lamp02 , powerSw , fanSw ;
TextView lamp01Name , lamp02Name ,powerName , fanName;
// 蓝牙通信的地址和两个UUID
String MAC = "" ;
UUID serviceUuid , characterUuid ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
// 运行的时候检查是否打开蓝牙,没有打开就开启蓝牙
if(!MyApp.getBluetoothClient().isBluetoothOpened())
MyApp.getBluetoothClient().openBluetooth();
}
private void initView() {
mainTitle = findViewById(R.id.main_title); //findViewById(R.id.xml文件中对应的id)
lamp01 = findViewById(R.id.sw_lamp_01); //灯1开关
lamp02 = findViewById(R.id.sw_lamp_02); //灯2开关
powerSw = findViewById(R.id.sw_power); //灯开关
fanSw = findViewById(R.id.sw_fan); //风扇开关
lamp01Name = findViewById(R.id.lamp_01_name); //名字
lamp02Name = findViewById(R.id.lamp_02_name);
powerName = findViewById(R.id.power_name);
fanName = findViewById(R.id.fan_name);
// 为按钮设置点击事件
lamp01.setOnClickListener(this);
lamp02.setOnClickListener(this);
powerSw.setOnClickListener(this);
fanSw.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main , menu); // 加载菜单页面
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.action_scan){
Intent intent = new Intent(MainActivity.this , ScanResultActivity.class);
startActivityForResult(intent , REQUEST_CONNECT_DEVICE);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onDestroy() {
// 关闭蓝牙
MyApp.getBluetoothClient().closeBluetooth();
super.onDestroy();
}
@Override
public void onClick(View v) {
// 当蓝牙有连接,并且MAC地址存在,两个UUID都不为空的情况下,点击按钮才有效
// 以下只要有一个条件不满足,就不让点击按钮发送数据
if(!MyApp.getBluetoothClient().isBleSupported()
|| TextUtils.isEmpty(MAC)
|| TextUtils.isEmpty(serviceUuid.toString())
|| TextUtils.isEmpty(characterUuid.toString())){
Toast.makeText(MainActivity.this , "请先检查蓝牙设备与手机是否连接正常",Toast.LENGTH_SHORT).show();
return;
}
switch (v.getId()){
case R.id.sw_lamp_01: // 灯组01
lamp01.switchState();
lamp01Name.setText(lamp01.isIconEnabled() ? "客厅灯开" : "客厅灯关");
writeCmd(MAC , serviceUuid , characterUuid , lamp01.isIconEnabled() ? "L1ON\n" :"L1OFF\n");
break;
case R.id.sw_lamp_02: // 灯组02
lamp02.switchState();
lamp02Name.setText(lamp02.isIconEnabled() ? "房间灯开" : "房间灯关");
writeCmd(MAC , serviceUuid , characterUuid , lamp02.isIconEnabled() ? "L2ON\n" :"L2OFF\n");
break;
case R.id.sw_power: // 电源
powerSw.switchState();
powerName.setText(powerSw.isIconEnabled() ? "电源开" : "电源关");
writeCmd(MAC , serviceUuid , characterUuid , powerSw.isIconEnabled() ? "ON\n" :"OFF\n");
break;
case R.id.sw_fan: // 风扇
fanSw.switchState();
fanName.setText(fanSw.isIconEnabled() ? "风扇开" : "风扇关");
writeCmd(MAC , serviceUuid , characterUuid , fanSw.isIconEnabled() ? "FANON\n" :"FANOFF\n");
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("CJT" , "requestCode = "+ requestCode +" , resultCode = "+ resultCode + " , data ="+data);
if(requestCode == REQUEST_CONNECT_DEVICE) {
// 响应结果
switch (resultCode) {
case Activity.RESULT_CANCELED:
Toast.makeText(this , "取消了扫描!",Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_OK:
// 选择连接的设备
final BluetoothDevice device = data.getParcelableExtra(RecycleAdapter.EXTRA_DEVICE);
// 得到选择后传过来的MAC地址
MAC = device.getAddress();
Log.d("CJT" , "address ===================== " +MAC);
// 设置BLE设备的连接参数
BleConnectOptions options = new BleConnectOptions.Builder()
.setConnectRetry(3) // 连接如果失败重试3次
.setConnectTimeout(30000) // 连接超时30s
.setServiceDiscoverRetry(3) // 发现服务如果失败重试3次
.setServiceDiscoverTimeout(20000) // 发现服务超时20s
.build();
// 开始连接操作
MyApp.getBluetoothClient().connect(MAC, options, new BleConnectResponse() {
@Override
public void onResponse(int code, BleGattProfile data) {
Log.d("CJT" , "getBluetoothClient().connect --- code ----- " + code);
// 表示连接成功
if(code == REQUEST_SUCCESS){
mainTitle.setText("当前连接设备 :"+device.getName());
serviceUuid = data.getServices().get(3).getUUID();
Log.d("CJT" , "getBluetoothClient().connect --- serviceUuid : "+serviceUuid);
characterUuid = data.getService(serviceUuid).getCharacters().get(0).getUuid();
Log.d("CJT" , "getBluetoothClient().connect --- characterUuid : "+characterUuid);
// 下发数据
writeCmd(MAC , serviceUuid , characterUuid , "finish\r\n");
}else{
mainTitle.setText("当前暂无蓝牙设备连接");
Toast.makeText(MainActivity.this , "蓝牙连接不成功!",Toast.LENGTH_SHORT).show();
}
}
});
break;
}
}
}
/***
* 向设备下发指令
* @param address 设备MAC地址
* @param serviceUuid 服务UUID
* @param characterUuid 特征UUID
* @param cmd 待下发的命令
*/
private void writeCmd(String address , UUID serviceUuid , UUID characterUuid , String cmd){
MyApp.getBluetoothClient().write(address, serviceUuid, characterUuid, cmd.getBytes(), new BleWriteResponse() {
@Override
public void onResponse(int code) {
if(code == Constants.REQUEST_SUCCESS){
}
}
});
}
}
3.2、activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/main_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:textSize="18sp"
android:text="@string/scan_hint"
android:textColor="@color/colorAccent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/top_div2"
android:layout_width="wrap_content"
android:layout_height="8dp"
android:layout_marginBottom="20dp"
android:layout_marginEnd="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="8dp"
android:background="@color/arc1"
app:layout_constraintBottom_toTopOf="@+id/sw_lamp_01"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/main_title"
app:layout_constraintVertical_chainStyle="spread_inside" />
<com.github.zagum.switchicon.SwitchIconView
android:id="@+id/sw_lamp_01"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginEnd="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toStartOf="@+id/sw_lamp_02"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/top_div2"
app:si_animation_duration="200"
app:si_disabled_alpha=".5"
app:si_disabled_color="@color/colorOff"
app:si_enabled="false"
app:si_no_dash="true"
app:si_tint_color="@color/colorOn"
app:srcCompat="@drawable/ic_lamp" />
<com.github.zagum.switchicon.SwitchIconView
android:id="@+id/sw_lamp_02"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginEnd="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toStartOf="@+id/sw_fan"
app:layout_constraintStart_toEndOf="@+id/sw_lamp_01"
app:layout_constraintTop_toBottomOf="@+id/top_div2"
app:si_animation_duration="200"
app:si_disabled_alpha=".5"
app:si_disabled_color="@color/colorOff"
app:si_enabled="false"
app:si_no_dash="true"
app:si_tint_color="@color/colorOn"
app:srcCompat="@drawable/ic_lamp" />
<com.github.zagum.switchicon.SwitchIconView
android:id="@+id/sw_fan"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginEnd="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toStartOf="@+id/sw_power"
app:layout_constraintStart_toEndOf="@+id/sw_lamp_02"
app:layout_constraintTop_toBottomOf="@+id/top_div2"
app:si_animation_duration="200"
app:si_disabled_alpha=".5"
app:si_disabled_color="@color/colorOff"
app:si_enabled="false"
app:si_no_dash="true"
app:si_tint_color="@color/colorOn"
app:srcCompat="@drawable/ic_fan" />
<com.github.zagum.switchicon.SwitchIconView
android:id="@+id/sw_power"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginEnd="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/sw_fan"
app:layout_constraintTop_toBottomOf="@+id/top_div2"
app:si_animation_duration="200"
app:si_disabled_alpha=".5"
app:si_disabled_color="@color/colorOff"
app:si_enabled="false"
app:si_no_dash="true"
app:si_tint_color="@color/colorOn"
app:srcCompat="@drawable/ic_switch" />
<TextView
android:id="@+id/lamp_01_name"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
android:text="@string/lamp_01"
app:layout_constraintEnd_toEndOf="@+id/sw_lamp_01"
app:layout_constraintStart_toStartOf="@+id/sw_lamp_01"
app:layout_constraintTop_toBottomOf="@+id/sw_lamp_01"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/lamp_02_name"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
android:text="@string/lamp_02"
app:layout_constraintEnd_toEndOf="@+id/sw_lamp_02"
app:layout_constraintStart_toStartOf="@+id/sw_lamp_02"
app:layout_constraintTop_toBottomOf="@+id/sw_lamp_02" />
<TextView
android:id="@+id/power_name"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
android:text="@string/power_sw"
app:layout_constraintEnd_toEndOf="@+id/sw_power"
app:layout_constraintStart_toStartOf="@+id/sw_power"
app:layout_constraintTop_toBottomOf="@+id/sw_power" />
<TextView
android:id="@+id/fan_name"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
android:text="@string/fan_sw"
app:layout_constraintEnd_toEndOf="@+id/sw_fan"
app:layout_constraintStart_toStartOf="@+id/sw_fan"
app:layout_constraintTop_toBottomOf="@+id/sw_fan" />
</android.support.constraint.ConstraintLayout>
4、STM32单片机工程(只贴主函数)
/*----------------------------------------------------
´®¿Ú1·¢ËÍ×Ö·û´®¿ØÖÆLED
BaudRate -- 9600
WordLength-- 8b
StopBit -- 1
Parity -- No
·¢ËÍ LED1+ON µãÁÁLED1
·¢ËÍ LED1+OFF ¹Ø±ÕLED1
·¢ËÍÆäËû×Ö·û´®·×ªLED2״̬
ÐèÒª´®¿Ú µ÷ÊÔÖúÊÖÉϹ´Ñ¡·¢ËÍÐÂÐУ¬ÒòΪ´®¿Ú½ÓÊÕÊÇÒÔ/n×÷Ϊ½ÓÊÕÍê³É±êÖ¾µÄ
------------------------------------------------------*/
#include "stm32f10x.h"
#include "led.h"
#include "key.h"
#include "usart.h"
#include "delay.h"
#include "string.h"
extern char Rec_Buffer_1[];
extern char Rec_Buffer_2[];
extern char SendData_1[];
extern char SendData_2[];
extern u8 USART_REC_Finish_FLAG_1;
extern u8 USART_REC_Finish_FLAG_2;
int main(void)
{
char ch='0';
char LED1ON[] = "L1ON"; //¶¨ÒåÏà¹ØµÄ×Ö·û´® \n ±»³ÔµôÁË
char LED1OFF[] = "L1OFF";
char LED2ON[] = "L2ON";
char LED2OFF[] = "L2OFF";
char FANON[] = "FANON";
char FANOFF[] = "FANOFF";
LED_GPIO_Config(); //³õʼ»¯LEDÏà¹ØµÄIO
LED_ALL_OFF(); //²¢¹Ø±ÕËùÓеÄLED
USART1_Config(); //´®¿Ú1µÄ³õʼ»¯
USART1_NVIC_Config(); //´®¿Ú1µÄÖжÏÅäÖÃ
USART2_Config(); //´®¿Ú2µÄ³õʼ»¯
USART2_NVIC_Config(); //´®¿Ú2µÄÖжÏÅäÖÃ
printf("USART1 TEST\r\n");
printf("USART2 TEST\r\n");
while(1)
{
while((USART_REC_Finish_FLAG_1 == 1)||(USART_REC_Finish_FLAG_2 == 1)) //µÈ´ý´®¿Ú½ÓÊÕÍê³É±ê־λÖÃ1
{
delay(1000); //ÑÓʱ1ms
USART_REC_Finish_FLAG_1 = 0; //ʹÓÃÍê³Éºó½«´®¿Ú±ê־λÖé–
USART_REC_Finish_FLAG_2 = 0;
if((strcmp(SendData_1, LED1ON)==0)||(strcmp(SendData_2, LED1ON)==0))//±È½Ï×Ö·û´®
ch = '1';
else if((strcmp(SendData_1, LED1OFF)==0)||(strcmp(SendData_2, LED1OFF)==0))
ch = '2';
else if((strcmp(SendData_1, LED2ON)==0)||(strcmp(SendData_2, LED2ON)==0))
ch = '3';
else if((strcmp(SendData_1, LED2OFF)==0)||(strcmp(SendData_2, LED2OFF)==0))
ch = '4';
else if((strcmp(SendData_1, FANON)==0)||(strcmp(SendData_2, FANON)==0))
ch = '5';
else if((strcmp(SendData_1, FANOFF)==0)||(strcmp(SendData_2, FANOFF)==0))
ch = '6';
else
ch = '7';
printf("%c \r\n",ch);
printf(SendData_1);
printf(SendData_2);
switch(ch)
{
case '1':
LED1_ON;
printf("LED1 ON\r\n");
break;
case '2':
LED1_OFF;
printf("LED1 OFF\r\n");
break;
case '3':
LED2_ON;
printf("LED2 ON\r\n");
break;
case '4':
LED2_OFF;
printf("LED2 OFF\r\n");
break;
case '5':
FAN_ON;
printf("FAN ON\r\n");
break;
case '6':
FAN_OFF;
printf("FAN OFF\r\n");
break;
case '7':
LED4_TOGGLE;
printf("LED4 TOGGLE\r\n");
break;
default:
printf("Error!\r\n");
break;
}
strcpy(SendData_1,NULL);
strcpy(SendData_2,NULL);
}
}
}
5、语音识别模块代码(只贴主函数)
/***************************·ÉÒôÔÆµç×Ó****************************
** ¹¤³ÌÃû³Æ£ºYS-V0.7ÓïÒôʶ±ðÄ£¿éÇý¶¯³ÌÐò
** CPU: STC11L08XE
** ¾§Õñ£º22.1184MHZ
** ²¨ÌØÂÊ£º9600 bit/S
** ÅäÌײúÆ·ÐÅÏ¢£ºYS-V0.7ÓïÒôʶ±ð¿ª·¢°å
** http://yuesheng001.taobao.com
** ×÷Õߣºzdings
** ÁªÏµ£º[email protected]
** ÐÞ¸ÄÈÕÆÚ£º2013.9.13
** ˵Ã÷£ºÆÕͨģʽ£ºÖ±½Ó·¢ÓïÒôʶ±ð
/***************************·ÉÒôÔÆµç×Ó******************************/
#include "config.h"
/************************************************************************************/
// nAsrStatus ÓÃÀ´ÔÚmainÖ÷³ÌÐòÖбíʾ³ÌÐòÔËÐеÄ״̬£¬²»ÊÇLD3320оƬÄÚ²¿µÄ״̬¼Ä´æÆ÷
// LD_ASR_NONE: ±íʾûÓÐÔÚ×÷ASRʶ±ð
// LD_ASR_RUNING£º ±íʾLD3320ÕýÔÚ×÷ASRʶ±ðÖÐ
// LD_ASR_FOUNDOK: ±íʾһ´Îʶ±ðÁ÷³Ì½áÊøºó£¬ÓÐÒ»¸öʶ±ð½á¹û
// LD_ASR_FOUNDZERO: ±íʾһ´Îʶ±ðÁ÷³Ì½áÊøºó£¬Ã»ÓÐʶ±ð½á¹û
// LD_ASR_ERROR: ±íʾһ´Îʶ±ðÁ÷³ÌÖÐLD3320оƬÄÚ²¿³öÏÖ²»ÕýÈ·µÄ״̬
/***********************************************************************************/
uint8 idata nAsrStatus=0;
void MCU_init();
void ProcessInt0(); //ʶ±ð´¦Àíº¯Êý
void delay(unsigned long uldata);
void User_handle(uint8 dat);//Óû§Ö´ÐвÙ×÷º¯Êý
void Delay200ms();
void Led_test(void);//µ¥Æ¬»ú¹¤×÷ָʾ
uint8_t G0_flag=DISABLE;//ÔËÐбêÖ¾£¬ENABLE:ÔËÐС£DISABLE:½ûÖ¹ÔËÐÐ
sbit LED=P4^2;//ÐźÅָʾµÆ
/***********************************************************
* Ãû ³Æ£º void main(void)
* ¹¦ ÄÜ£º Ö÷º¯Êý ³ÌÐòÈë¿Ú
* Èë¿Ú²ÎÊý£º
* ³ö¿Ú²ÎÊý£º
* ˵ Ã÷£º
* µ÷Ó÷½·¨£º
**********************************************************/
void main(void)
{
uint8 idata nAsrRes;
uint8 i=0;
Led_test();
MCU_init();
LD_Reset();
UartIni(); /*´®¿Ú³õʼ»¯*/
nAsrStatus = LD_ASR_NONE; // ³õʼ״̬£ºÃ»ÓÐÔÚ×÷ASR
#ifdef TEST
PrintCom("L1ON\n");
PrintCom("L1OFF\n");
PrintCom("L2ON\n");
PrintCom("L2OFF\n");
PrintCom("FANON\n");
PrintCom("FANOFF\n");
#endif
while(1)
{
switch(nAsrStatus)
{
case LD_ASR_RUNING:
case LD_ASR_ERROR:
break;
case LD_ASR_NONE:
{
nAsrStatus=LD_ASR_RUNING;
if (RunASR()==0) /* Æô¶¯Ò»´ÎASRʶ±ðÁ÷³Ì£ºASR³õʼ»¯£¬ASRÌí¼Ó¹Ø¼ü´ÊÓÆô¶¯ASRÔËËã*/
{
nAsrStatus = LD_ASR_ERROR;
}
break;
}
case LD_ASR_FOUNDOK: /* Ò»´ÎASRʶ±ðÁ÷³Ì½áÊø£¬È¥È¡ASRʶ±ð½á¹û*/
{
nAsrRes = LD_GetResult(); /*»ñÈ¡½á¹û*/
User_handle(nAsrRes);//Óû§Ö´Ðк¯Êý
nAsrStatus = LD_ASR_NONE;
break;
}
case LD_ASR_FOUNDZERO:
default:
{
nAsrStatus = LD_ASR_NONE;
break;
}
}// switch
}// while
}
/***********************************************************
* Ãû ³Æ£º LEDµÆ²âÊÔ
* ¹¦ ÄÜ£º µ¥Æ¬»úÊÇ·ñ¹¤×÷ָʾ
* Èë¿Ú²ÎÊý£º ÎÞ
* ³ö¿Ú²ÎÊý£ºÎÞ
* ˵ Ã÷£º
**********************************************************/
void Led_test(void)
{
LED=~ LED;
Delay200ms();
LED=~ LED;
Delay200ms();
LED=~ LED;
Delay200ms();
LED=~ LED;
Delay200ms();
LED=~ LED;
Delay200ms();
LED=~ LED;
}
/***********************************************************
* Ãû ³Æ£º void MCU_init()
* ¹¦ ÄÜ£º µ¥Æ¬»ú³õʼ»¯
* Èë¿Ú²ÎÊý£º
* ³ö¿Ú²ÎÊý£º
* ˵ Ã÷£º
* µ÷Ó÷½·¨£º
**********************************************************/
void MCU_init()
{
P0 = 0xff;
P1 = 0xff;
P2 = 0xff;
P3 = 0xff;
P4 = 0xff;
P1M0=0XFF; //P1¶Ë¿ÚÉèÖÃÎªÍÆÍìÊä³ö¹¦ÄÜ£¬¼´Ìá¸ßIO¿ÚÇý¶¯ÄÜÁ¦£¬´ÓÇý¶¯¼ÌµçÆ÷Ä£¿é¹¤×÷
P1M1=0X00;
LD_MODE = 0; // ÉèÖÃMD¹Ü½ÅΪµÍ£¬²¢ÐÐģʽ¶Áд
IE0=1;
EX0=1;
EA=1;
}
/***********************************************************
* Ãû ³Æ£º ÑÓʱº¯Êý
* ¹¦ ÄÜ£º
* Èë¿Ú²ÎÊý£º
* ³ö¿Ú²ÎÊý£º
* ˵ Ã÷£º
* µ÷Ó÷½·¨£º
**********************************************************/
void Delay200us() //@22.1184MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 5;
j = 73;
do
{
while (--j);
} while (--i);
}
void delay(unsigned long uldata)
{
unsigned int j = 0;
unsigned int g = 0;
while(uldata--)
Delay200us();
}
void Delay200ms() //@22.1184MHz
{
unsigned char i, j, k;
i = 17;
j = 208;
k = 27;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
/***********************************************************
* Ãû ³Æ£º Öжϴ¦Àíº¯Êý
* ¹¦ ÄÜ£º
* Èë¿Ú²ÎÊý£º
* ³ö¿Ú²ÎÊý£º
* ˵ Ã÷£º
* µ÷Ó÷½·¨£º
**********************************************************/
void ExtInt0Handler(void) interrupt 0
{
ProcessInt0(); /* LD3320 ËͳöÖжÏÐźţ¬°üÀ¨ASRºÍ²¥·ÅMP3µÄÖжϣ¬ÐèÒªÔÚÖжϴ¦Àíº¯ÊýÖзֱð´¦Àí*/
}
/***********************************************************
* Ãû ³Æ£ºÓû§Ö´Ðк¯Êý
* ¹¦ ÄÜ£ºÊ¶±ð³É¹¦ºó£¬Ö´Ðж¯×÷¿ÉÔڴ˽øÐÐÐÞ¸Ä
* Èë¿Ú²ÎÊý£ºÎÞ
* ³ö¿Ú²ÎÊý£ºÎÞ
* ˵ Ã÷£º
**********************************************************/
void User_handle(uint8 dat)
{
//UARTSendByte(dat);//´®¿Úʶ±ðÂ루ʮÁù½øÖÆ£©
switch(dat) /*¶Ô½á¹ûÖ´ÐÐÏà¹Ø²Ù×÷,¿Í»§ÐÞ¸Ä*/
{
case 1: /*ÃüÁî¡°¿ÍÌüµÆ´ò¿ª¡±*/
PrintCom("L1ON\n");
break;
case 2: /*ÃüÁî¡°¿ÍÌüµÆ¹Ø±Õ¡±*/
PrintCom("L1OFF\n");
break;
case 3: /*ÃüÁî¡°·¿¼äµÆ´ò¿ª¡±*/
PrintCom("L2ON\n");
break;
case 4: /*ÃüÁî¡°·¿¼äµÆ¹Ø±Õ¡±*/
PrintCom("L2OFF\n");
break;
case 5: /*ÃüÁî¡°¿ª·çÉÈ¡±*/
PrintCom("FANON\n");
break;
case 6: /*ÃüÁî¡°¹Ø·çÉÈ¡±*/
PrintCom("FANOFF\n");
break;
default:PrintCom("ÇëÖØÐÂʶ±ð·¢¿ÚÁî\r\n"); /*text.....*/break;
}
}
6、实物图及效果视频
6.1、连接
6.1.1、电源模块+5V供电给32单片机
6.1.2、电源模块+5V供电给L298N驱动模块
6.1.3、蓝牙模块与单片机连接(左边蓝牙针脚,右边单片机针脚)
VCC----->电源+5v
GND----->电源地
RXD----->TXD(PA9)
TXD----->RXD(PA10)
6.1.4、语音模块与单片机连接(左边语音模块针脚,右边单片机针脚)
VCC----->电源+3.3v
GND----->电源地
RXD----->TXD(PA2)
TXD----->RXD(PA3)
6.1.5、LED与单片机连接
LED1----->PA15
LED2----->PA14
6.1.6、L298N驱动模块与单片机连接
IN1----->PB11
IN2----->GND
6.1.7、L298N驱动模块与直流电机连接
OUT1的两个端子分别接电机的红黑线