TA的每日心情 | 开心 2017-6-17 09:42 |
---|
签到天数: 3 天 连续签到: 1 天 [LV.2]偶尔看看I
|
GD32_MINI智能家居系统
前言:本小玩意纯原创,虽然简单但五脏俱全,而且小弟我也整整写了两个星期的代码,愿大家喜欢。
一、系统框图
二、功能介绍
从上图我们可以看到我们有OLED液晶显示器,蜂鸣器,RGB_LED灯,蓝牙,MQ2烟雾传感器还有我们的GD32F190小红板。他们之间的通讯如下:
OLED:SPI MQ:ADC
RGB:PWM 蓝牙:USART
蜂鸣器:PWM
我们的小红板采集MQ传感器的值然后将数据发送给手机并在手上显示,同时我们的OLED也会显示。当这烟雾浓度过高时,我们蜂蜜器将会报警,同时我们的手机也会进行电话和短信报警。
当然作为智能家居我们也需要能够控制我们的家电。这里我们加了一个全彩的LED来模拟我们的家庭电器,我们可以通过滑动手机APP上的滑条,来控制我们的全彩LED。通过改变红绿蓝的亮度来显示不同的颜色。我们还提供一个网络平台这个我们也同时通过网络平台来时候监控我们的家庭环境,同样也能控制我们的家电。
三、APP与OLED界面介绍
四、核心代码讲解
1、RGB_LED部分
- /* Includes ------------------------------------------------------------------*/
- #include <stdlib.h>
- #include <stdio.h>
- #include "RGB_LED.h"
- void RGB_LED_INT(void)
- {
- // SysTick_Configuration();
- PWM_Timer_Int();
- }
- void Led_R(uint16_t ling)
- {
- TIMER_Compare4Config(TIMER2 , ling );
- }
- void Led_B(uint16_t ling)
- {
- TIMER_Compare1Config(TIMER17 , ling );
- }
- void Led_G(uint16_t ling)
- {
- TIMER_Compare3Config(TIMER2 , ling );
- }
我们的RGB_LED主要是通过PWM来进行控制其亮度的,上面我们我们三个函数用来改变三个LED的占空比从而形成不同的颜色。
2、蜂蜜器部分
- #include "buzzer.h"
- void Buzzer_INT(void)
- {
- PWM_Timer_Int();
- TIMER_Compare1Config(TIMER16 , 1000 );
- }
- void Buzzer(uint16_t ling)
- {
- TIMER_Compare1Config(TIMER16 , ling );
- }
我们的蜂鸣器也是通过通过PWM来控制,我们先设定一个频率周期,然后通过改变占空比来改变响度。
3、蓝牙部分
- void USART1_IRQHandler(void)
- {
- static char Tata_floag;
- static uint8_t USART_DATA;
- static char i = 0;
- if( USART_GetIntBitState(USART1, USART_INT_RBNE) != RESET)
- {
- USART_DATA = ( uint8_t )USART_DataReceive( USART1 );
- if( USART_DATA == 0xff)
- {
- Tata_floag = 1;
- i = 1;
- }
- else if(i == 0)
- {
- Tata_floag = 0;
- }
- else
- {
- i++;
- switch(i)
- {
- case 2: LED_G = USART_DATA;
- break;
- case 3: LED_B = USART_DATA;
- break;
- case 4: LED_R = USART_DATA;
- break;
- case 5: Buzzen_V = USART_DATA;
- i = 0;
- break;
- }
- }
- }
- USART_ClearBitState( USART1, USART_FLAG_TC);
- USART1->CMD |=0x18;
- USART1->SCR |=0x18;
- }
我们的蓝牙是直接和串口连接的,当手机数据发送过来,我们的小红板就进入接收中断,将手机发来的数据一一接收并存储下来。
4、MQ部分
- <div>/* Includes ------------------------------------------------------------------*/
- #include "gd32f1x0.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include "ADC.h"
- /* Private define ------------------------------------------------------------*/
- #ifdef GD32F130_150
- #define BOARD_ADC_CHANNEL ADC_CHANNEL_11
- #define ADC_GPIO_PIN GPIO_PIN_1
- #elif defined GD32F170_190
- #define BOARD_ADC_CHANNEL ADC_CHANNEL_10
- #define ADC_GPIO_PIN GPIO_PIN_0
- #endif
- #define ADC_RDTR_Address ((uint32_t)0x4001244C)
- /* Private variables ---------------------------------------------------------*/
- __IO uint16_t ADCConvertedValue;
- ADC_InitPara ADC_InitStructure;
- DMA_InitPara DMA_InitStructure;
- /* Private function prototypes -----------------------------------------------*/
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- /* Private functions ---------------------------------------------------------*/
- /**
- * @brief Main program.
- * @param None
- * @retval None
- */
- void GD_ADC_INT(void)
- {
- /* Configure System clocks -----------------------------------------------*/
- RCC_Configuration();
- /* Configure GPIO ports --------------------------------------------------*/
- GPIO_Configuration();
- /* Configure DMA1 channel1 -----------------------------------------------*/
- DMA_DeInit(DMA1_CHANNEL1);
- DMA_InitStructure.DMA_PeripheralBaseAddr = ADC_RDTR_Address;
- DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADCConvertedValue;
- DMA_InitStructure.DMA_DIR = DMA_DIR_PERIPHERALSRC;
- DMA_InitStructure.DMA_BufferSize = 1;
- DMA_InitStructure.DMA_PeripheralInc = DMA_PERIPHERALINC_DISABLE;
- DMA_InitStructure.DMA_MemoryInc = DMA_MEMORYINC_DISABLE;
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PERIPHERALDATASIZE_HALFWORD;
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MEMORYDATASIZE_HALFWORD;
- DMA_InitStructure.DMA_Mode = DMA_MODE_CIRCULAR;
- DMA_InitStructure.DMA_Priority = DMA_PRIORITY_HIGH;
- DMA_InitStructure.DMA_MTOM = DMA_MEMTOMEM_DISABLE;
- DMA_Init(DMA1_CHANNEL1, &DMA_InitStructure);
- /* Enable DMA1 channel1 */
- DMA_Enable(DMA1_CHANNEL1, ENABLE);
- /* Configure ADC ---------------------------------------------------------*/
- ADC_InitStructure.ADC_Mode_Scan = DISABLE;
- ADC_InitStructure.ADC_Mode_Continuous = ENABLE;
- ADC_InitStructure.ADC_Trig_External = ADC_EXTERNAL_TRIGGER_MODE_NONE;
- ADC_InitStructure.ADC_Data_Align = ADC_DATAALIGN_RIGHT;
- ADC_InitStructure.ADC_Channel_Number = 1;
- ADC_Init(&ADC_InitStructure);
- /* Configure ADC regular channelx */
- ADC_RegularChannel_Config( BOARD_ADC_CHANNEL, 1, ADC_SAMPLETIME_239POINT5);
- /* Enable ADC DMA */
- ADC_DMA_Enable(ENABLE);
- /* Enable ADC */
- ADC_Enable(ENABLE);
- ADC_Calibration();
- /* Start ADC Software Conversion */
- ADC_SoftwareStartConv_Enable(ENABLE);
- }</div><div>
- </div><div>/**
- * @brief Configure system clocks.
- * @param None
- * @retval None
- */
- void RCC_Configuration(void)
- {
- /* ADCCLK = PCLK2/6 */
- RCC_ADCCLKConfig(RCC_ADCCLK_APB2_DIV6); </div><div>
- </div><div> /* Enable DMA1 and GPIOC clock */
- RCC_AHBPeriphClock_Enable(RCC_AHBPERIPH_DMA1|RCC_AHBPERIPH_GPIOC, ENABLE);
- /* Enable ADC clock */
- RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_ADC1, ENABLE);
- }</div><div>
- </div><div>/**
- * @brief Configure GPIO ports.
- * @param None
- * @retval None
- */
- void GPIO_Configuration(void)
- {
- GPIO_InitPara GPIO_InitStructure;
- /* Configure PC1 or PC0 (ADC Channel11 or Channel10) as analog input -------------------------*/
- GPIO_InitStructure.GPIO_Pin = ADC_GPIO_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AN;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- }</div>
对于烟雾传感器,我们只要简单的进行AD采集就可以了。5、OLED部分
- void OLEO_GPIO_Int(void)
- {
- GPIO_InitPara GPIO_InitStructure;
- SPI_InitPara SPI_InitStructure;
- /* Enable the LED Clock */
- RCC_AHBPeriphClock_Enable(RCC_AHBPERIPH_GPIOB, ENABLE);
- RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_SPI1, ENABLE);
- /* GPIO AF configuration */
- GPIO_PinAFConfig(GPIOB, GPIO_PINSOURCE3, GPIO_AF_0);
- GPIO_PinAFConfig(GPIOB, GPIO_PINSOURCE4, GPIO_AF_0);
- GPIO_PinAFConfig(GPIOB, GPIO_PINSOURCE5, GPIO_AF_0);
- /* SPI1_CLK(PB3), SPII_MISO(PB4) and SPII_MOSI(PB5) GPIO pin configuration */
- GPIO_InitStructure.GPIO_Pin = GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5;
- GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
- GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_NOPULL;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- /* SPII_NSS(PB12,) GPIO pin configuration */
- GPIO_InitStructure.GPIO_Pin = GPIO_PIN_12;
- GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
- GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT;
- GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_NOPULL;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- /* PB0--OLED RST Òý½Å¶¨Òå */
- GPIO_InitStructure.GPIO_Pin = GPIO_PIN_15;
- GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
- GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT;
- GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_NOPULL;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- /* PB15--OLED C/D Òý½Å¶¨Òå */
- GPIO_InitStructure.GPIO_Pin = GPIO_PIN_0;
- GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
- GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT;
- GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_NOPULL;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- /* SPI1 configuration */
- SPI_InitStructure.SPI_TransType = SPI_TRANSTYPE_FULLDUPLEX;
- SPI_InitStructure.SPI_Mode = SPI_MODE_MASTER;
- SPI_InitStructure.SPI_FrameFormat = SPI_FRAMEFORMAT_8BIT;
- SPI_InitStructure.SPI_SCKPL = SPI_SCKPL_LOW;
- SPI_InitStructure.SPI_SCKPH = SPI_SCKPH_1EDGE;
- SPI_InitStructure.SPI_SWNSSEN = SPI_SWNSS_SOFT;
- SPI_InitStructure.SPI_PSC = SPI_PSC_8;
- SPI_InitStructure.SPI_FirstBit = SPI_FIRSTBIT_MSB;
- SPI_InitStructure.SPI_CRCPOL = 7;
- SPI_Init(SPI1, &SPI_InitStructure);
- SPI_Enable(SPI1, ENABLE);
- }
6、主函数部分
- int main(void)
- {
- unsigned char i;
- SysTick_Configuration();
- RGB_LED_INT();
- Buzzer_INT();
- GD_usart_Int();
- GD_usart1_Int();
- NVIC_Configurationuration();
- OLED_Init();
- OLED_CLS();
- OLED_P8x16Str(12,0,"MiNi");
- OLED_P16x16Ch(42,0,4,fd1);
- OLED_P8x16Str(8,2,"shamozhihuong");
- OLED_P16x16Ch(12,4,4,fd2);
- OLED_P6x8Str(12,7,"407550350@QQ.COM");
- GD_ADC_INT();
- {
- for(i=0;i<sizeof(TxBuffer1);i++)
- {
- USART_DataSend( USART1,TxBuffer1);
- while(USART_GetBitState( USART1, USART_FLAG_TC) == RESET);
- }
- for(i=0;i<sizeof(TxBuffer2);i++)
- {
- USART_DataSend( USART2,TxBuffer2);
- while(USART_GetBitState( USART2, USART_FLAG_TC) == RESET);
- }
- }
- while (1)
- {
- while(ADC_GetBitState(ADC_FLAG_EOC) != SET)
- #if 1
- /***************µçѹ²É¼¯ÏÔʾ²¿Ö*****************/
- Led_R(LED_R*4);//??LED
- Led_G(LED_G*4);//??LED
- Led_B(LED_B*4);//??LED
- electrect_V = Elec_adc_converted ();
- printf("%f \n\r",electrect_V);
- values_V = (int)(electrect_V*1000);
- Temp_ADC = int_adc_converted()>>3;
- USART_DataSend( USART1,Temp_ADC);
- if(Temp_ADC > 150) //???????140???
- {
- Buzzer((Buzzen_V / 2)*4);
- }
- else{
- Buzzer(0);
- }
- ge = Temp_ADC;
- shi = Temp_ADC/10;
- bai = Temp_ADC/100;
- itoa(bai, dis_STR1, 10);
- OLED_P8x16Str(78,4,dis_STR1);
- itoa(shi, dis_STR1, 10);
- OLED_P8x16Str(86,4,dis_STR1);
- itoa(ge, dis_STR1, 10);
- OLED_P8x16Str(94,4,dis_STR1);
- OLED_P8x16Str(102,4,"ppm");
- /***************µçѹ²É¼¯ÏÔʾ²¿Ö*****************/
- #endif
- }
- }
- /******************* (C) COPYRIGHT 2016 沙漠之风*****END OF FILE****/
五、实物演练
https://v.youku.com/v_show/id_XMTYwNjU2NTU1Mg==.html
上面视频中,我是将一个根烟点燃后直接放在了烟雾传感器下面,这时我们烟雾直接超标,APP中的红心直接变黑,手机直接短信报警和电话报警(其中的手机号我随意虚设);当把烟拿开后我们烟雾浓度就降下来了,APP中的心的颜色就慢慢变成了红色。
六、程序与APP
1、程序部分
代码已经全部贴上面了,大家可以复制到Keil中配置运行下
2、APP
GD32智能家居APP.rar (2.49 MB, 下载次数: 9)
七、项目合作附语
暑假马上要到,本人暑假比较闲。若哪位兄台有项目,忙不完的可以找我哦!本人很愿意从事GD32、手机APP开发、DSP、FPGA、STM32、STM8、MPS430、PLC、PCB绘图等项目开发;小弟最近经济比较拮据,大家多多关照哦。
|
|