赞
踩
目录
该项目主要是用语音模块通过串口USART去控制stm32单片机,然后再用STM32去控制其余的一些传感器和一些执行器
使用的是stm32f103c8t6
该模块主要的工作是显示各个功能的状态
- lcd.c文件
- ----------------------------------------------
- //首先是初始化
- void LCD_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- GPIO_SetBits(GPIOB, GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
- }
-
- //其次就是各种各样的函数,看资料给的例程
-
- -----------------------------------------------
- lcd.h文件
- #define LCD_SCLK_Clr() GPIO_ResetBits(GPIOB,GPIO_Pin_10)//SCL=SCLK
- #define LCD_SCLK_Set() GPIO_SetBits(GPIOB,GPIO_Pin_10)
-
- #define LCD_MOSI_Clr() GPIO_ResetBits(GPIOB,GPIO_Pin_11)//SDA=MOSI
- #define LCD_MOSI_Set() GPIO_SetBits(GPIOB,GPIO_Pin_11)
-
- #define LCD_RES_Clr() GPIO_ResetBits(GPIOB,GPIO_Pin_15)//RES
- #define LCD_RES_Set() GPIO_SetBits(GPIOB,GPIO_Pin_15)
-
- #define LCD_DC_Clr() GPIO_ResetBits(GPIOB,GPIO_Pin_14)//DC
- #define LCD_DC_Set() GPIO_SetBits(GPIOB,GPIO_Pin_14)
-
- #define LCD_CS_Clr() GPIO_ResetBits(GPIOB,GPIO_Pin_13)//CS1
- #define LCD_CS_Set() GPIO_SetBits(GPIOB,GPIO_Pin_13)
-
- #define LCD_BLK_Clr() GPIO_ResetBits(GPIOB,GPIO_Pin_12)//BLK
- #define LCD_BLK_Set() GPIO_SetBits(GPIOB,GPIO_Pin_12)
- void LCD_GPIO_Init(void);
- void LCD_Writ_Bus(u8 dat);
- void LCD_WR_DATA8(u8 dat);
- void LCD_WR_DATA(u16 dat);
- void LCD_WR_REG(u8 dat);
- void LCD_Address_Set(u16 x1, u16 y1, u16 x2, u16 y2);
- void LCD_Init(void);
- -----------------------------------------------------------------------
- 介绍硬件的连线,根据.h文件的定义:
- GND---G
- VCC---5V (这里最好是5v,3.3v可能驱动不了)
- SCL---PB10
- SDA---PB11
- RES---PB15
- DC---PB14
- CS---PB13
- BLK---PB12
该模块主要是读取温湿度的数据,然后传递给STM32单片机显示
- dht11.c
- ---------------------------------------------------------
- //DHT11初始化
- u8 DHT11_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_SetBits(GPIOA, GPIO_Pin_1);
-
- DHT11_Rst();
- return DHT11_Check();
- }
- //DHT11复位
- void DHT11_Rst(void)
- {
- DHT11_IO_OUT();
- DHT11_DQ_OUT = 0;
- delay_ms(20);
- DHT11_DQ_OUT = 1;
- delay_us(30);
- }
- //DHT11检测
- u8 DHT11_Check(void)
- {
- u8 retry = 0;
- DHT11_IO_IN();
-
-
- while (DHT11_DQ_IN && retry < 100)
- {
- retry++;
- delay_us(1);
- };
-
- if(retry >= 100)return 1;
- else retry = 0;
-
-
- while (!DHT11_DQ_IN && retry < 100)
- {
- retry++;
- delay_us(1);
- };
-
- if(retry >= 100)return 1;
-
- return 0;
- }
- //DHT11读bit
- u8 DHT11_Read_Bit(void)
- {
- u8 retry = 0;
-
- while(DHT11_DQ_IN && retry < 100) 、
- {
- retry++;
- delay_us(1);
- }
-
- retry = 0;
-
- while(!DHT11_DQ_IN && retry < 100) 、
- {
- retry++;
- delay_us(1);
- }
-
- delay_us(40);
-
- 、 if(DHT11_DQ_IN)return 1;
- else return 0;
- }
- //DHT11读Byte
- u8 DHT11_Read_Byte(void)
- {
- u8 i, dat;
- dat = 0;
-
- for (i = 0; i < 8; i++)
- {
- dat <<= 1;
- dat |= DHT11_Read_Bit();
- }
-
- return dat;
- }
- //DHT11读数据
- u8 DHT11_Read_Data(u8 *temp, u8 *humi)
- {
- u8 buf[5];
- u8 i;
- DHT11_Rst();
-
- if(DHT11_Check() == 0)
- {
- for(i = 0; i < 5; i++)
- {
- buf[i] = DHT11_Read_Byte();
- }
-
- if((buf[0] + buf[1] + buf[2] + buf[3]) == buf[4])
- {
- *humi = buf[0];
- *(humi + 1) = buf[1];
- *temp = buf[2];
- *(temp + 1) = buf[3];
- }
- }
- else return 1;
-
- return 0;
- }
- ----------------------------------------------------------------
-
- dht11.h
- ----------------------------------------------------------------
- #ifndef __DHT11_H
- #define __DHT11_H
-
- #include "sys.h"
- #include "delay.h"
- #include "led.h"
- #include "usart2.h"
- #include "lcd.h"
- #include "lcd_init.h"
-
- #define DHT11_IO_IN() {GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=8<<4;}
- #define DHT11_IO_OUT() {GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=3<<4;}
- #define DHT11_DQ_OUT PAout(1)
- #define DHT11_DQ_IN PAin(1)
-
- u8 DHT11_Init(void);
- u8 DHT11_Read_Data(u8 *temp, u8 *humi);
- u8 DHT11_Read_Byte(void);
- u8 DHT11_Read_Bit(void);
- u8 DHT11_Check(void);
- void DHT11_Rst(void);
-
-
- void SU03T_DHT11_Play(void);
- void MP3_DHT11_Play(void);
-
- #endif
- -----------------------------------------------------------------
- 根据GPIO的定义开始连线:
- DATA---PA1
该模块的工作主要是存储语音文件,当语音模块给STM32发送指令时,STM32再把指令传递给该模块,然后该模块开始工作,发出声音。
- 该模块主要是通过串口来和STM32通讯,所以没有特定的.c和.h的文件,下面介绍该模块会用到的一些函数。
- -----------------------------------------------------------------
- 该函数主要是STM32给MP3模块发送数据
- void Uart2_MP3_SendData(int len)
- {
- int i = 0 ;
-
- USART2_SendByte(0x7E);
-
- for(i = 0; i < len; i++)
- {
- USART2_SendByte(Send_buf[i]);
- }
-
- USART2_SendByte(0xEF);
- }
-
- 该函数主要是MP3播放文件里面的内容
- void Uart2_MP3_SendCMD(int CMD, int feedback, int dat)
- {
- Send_buf[0] = 0xff;
- Send_buf[1] = 0x06;
- Send_buf[2] = CMD;
- Send_buf[3] = feedback;
- Send_buf[4] = (int)(dat >> 8);
- Send_buf[5] = (int)(dat);
- Uart2_MP3_DoSum(&Send_buf[0], 6);
- Uart2_MP3_SendData(8);
- }
- void Uart2_MP3_SendCMD2(int CMD, int dat1, int dat2, int dat3)
- {
- Send_buf[0] = 0xff;
- Send_buf[1] = 0x06;
- Send_buf[2] = CMD;
- Send_buf[3] = (int)(dat1);
- Send_buf[4] = (int)(dat2);
- Send_buf[5] = (int)(dat3);
-
- Uart2_MP3_SendData(6);
- }
- -------------------------------------------------------------------
- 引脚的接线:
- MP3的RX---STM32的TX
- VCC--3.3v
- GND--GND
-
这是本项目的语音模块,主要是通过麦克风来接受语音,然后通过串口把数据传递给SMT32
- 串口2向SU-03T发送命令
- -----------------------------------------------------------
- void Uart2_SU03T_SendCmd(int len)
- {
- int i = 0 ;
-
- USART2_SendByte(0xAA);
- USART2_SendByte(0x55);
-
- for(i = 0; i < len; i++)
- {
- USART2_SendByte(Send_buf[i]);
- }
-
- USART2_SendByte(0x55);
- USART2_SendByte(0xAA);
- }
- ------------------------------------------------------------
-
- 串口2向SU-03T发送命令(3位数据)
- ------------------------------------------------------------
- void Uart2_SU03T_SendCMD1(int dat1, int dat2, int dat3)
- {
- Send_buf[0] = (int)(dat1); //datal
- Send_buf[1] = (int)(dat2); //data2
- Send_buf[2] = (int)(dat3); //data3
-
- Uart2_SU03T_SendCmd(3);
- }
- ------------------------------------------------------------
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。