|
void userInit(void)
{
memset((uint8_t*)¤tDataPoint, 0, sizeof(dataPoint_t));
//delayInit(72);
rgbLedInit(); //RGB LED管脚初始化
rgbKeyGpioInit(); //
dht11Init(); //温湿度初始化
/** Warning !!! DataPoint Variables Init , Must Within The Data Range **/
/*
currentDataPoint.valueLED_OnOff = ;
currentDataPoint.valueLED_Color = ;
currentDataPoint.valueLED_R = ;
currentDataPoint.valueLED_G = ;
currentDataPoint.valueLED_B = ;
currentDataPoint.valueMotor_Speed = ;
currentDataPoint.valueInfrared = ;*/
//currentDataPoint.valueTemperature =20;
// currentDataPoint.valueHumidity =25;
/*currentDataPoint.valueAlert_1 = ;
currentDataPoint.valueAlert_2 = ;
currentDataPoint.valueFault_LED = ;
currentDataPoint.valueFault_Motor = ;
currentDataPoint.valueFault_TemHum = ;
*/
}
#include <main.h>
#include "bsp_temp_hum.h"
thTypedef_t temphumTypedef;
//Reset DHT11
static void dht11Rst(void)
{
DHT11_IO_OUT(); //SET OUTPUT
DHT11_DQ_OUT=0; //GPIOA.0=0
Delay_ms(20); //Pull down Least 18ms
DHT11_DQ_OUT=1; //GPIOA.0=1
Delay_us(30); //Pull up 20~40us
}
static uint8_t dht11Check(void)
{
uint8_t retry=0;
DHT11_IO_IN(); //SET INPUT
while (DHT11_DQ_IN && (retry<100)) //DHT11 Pull down 40~80us
{
retry++;
Delay_us(1);
}
if(retry >= 100)
{
return 1;
}
else
{
retry=0;
}
while (!DHT11_DQ_IN&& (retry < 100)) //DHT11 Pull up 40~80us
{
retry++;
Delay_us(1);
}
if(retry >= 100)
{
return 1; //check error
}
return 0;
}
static uint8_t dht11ReadBit(void)
{
uint8_t retry=0;
while(DHT11_DQ_IN && (retry<100)) //wait become Low level
{
retry++;
Delay_us(1);
}
retry = 0;
while(!DHT11_DQ_IN && (retry < 100)) //wait become High level
{
retry++;
Delay_us(1);
}
Delay_us(30);//wait 40us
if(DHT11_DQ_IN)
{
return 1;
}
else
{
return 0;
}
}
static uint8_t dht11ReadByte(void)
{
uint8_t i,dat;
dat=0;
for (i=0; i<8; i++)
{
dat<<=1;
dat |= dht11ReadBit();
}
return dat;
}
static uint8_t dht11ReadData(uint8_t *temperature, uint8_t *humidity)
{
uint8_t buf[5];
uint8_t i;
dht11Rst();
if(0 == dht11Check())
{
//USART1_printf("curTem=%d curHum=%d \r\n",curTem,curHum);
USART1_printf("Programm is here\r\n");
for(i=0; i<5; i++)
{
buf = dht11ReadByte();
}
if(buf[4] == (buf[0]+buf[1]+buf[2]+buf[3]))
{
*humidity = buf[0];
*temperature = buf[2];
}
}
else
{
return 1;
}
return 0;
}
uint8_t dht11Read(uint8_t *temperature, uint8_t *humidity)
{
uint8_t curTem = 0, curHum = 0;
uint16_t temMeans = 0, humMeans = 0;
uint8_t curI = 0;
uint8_t ret = 0;
ret = dht11ReadData(&curTem, &curHum);
if(1 != ret)
{
//Cycle store ten times stronghold
if(MEAN_NUM > temphumTypedef.curI)
{
temphumTypedef.thBufs[temphumTypedef.curI][0] = curTem;
temphumTypedef.thBufs[temphumTypedef.curI][1] = curHum;
temphumTypedef.curI++;
}
else
{
temphumTypedef.curI = 0;
temphumTypedef.thBufs[temphumTypedef.curI][0] = curTem;
temphumTypedef.thBufs[temphumTypedef.curI][1] = curHum;
temphumTypedef.curI++;
}
}
else
{
return (1);
}
if(MEAN_NUM <= temphumTypedef.curI)
{
temphumTypedef.thAmount = MEAN_NUM;
}
if(0 == temphumTypedef.thAmount)
{
//Calculate Before ten the mean
for(curI = 0; curI < temphumTypedef.curI; curI++)
{
temMeans += temphumTypedef.thBufs[curI][0];
humMeans += temphumTypedef.thBufs[curI][1];
}
temMeans = temMeans / temphumTypedef.curI;
humMeans = humMeans / temphumTypedef.curI;
*temperature = temMeans;
*humidity = humMeans;
}
else if(MEAN_NUM == temphumTypedef.thAmount)
{
//Calculate After ten times the mean
for(curI = 0; curI < temphumTypedef.thAmount; curI++)
{
temMeans += temphumTypedef.thBufs[curI][0];
humMeans += temphumTypedef.thBufs[curI][1];
}
temMeans = temMeans / temphumTypedef.thAmount;
humMeans = humMeans / temphumTypedef.thAmount;
*temperature = (uint8_t)temMeans;
*humidity = (uint8_t)humMeans;
}
return (0);
}
uint8_t dht11Init(void)
{
/* Migrate your driver code */
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
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_3);
dht11Rst();
memset((uint8_t *)&temphumTypedef, 0, sizeof(thTypedef_t));
USART1_printf("dh11Init \r\n");
return dht11Check();
}
#include "main.h"
#include "bsp_protocol.h"
#include "bsp_product.h"
#include "bsp_temp_hum.h"
/********************************/
/* 变量定义 */
/********************************/
volatile u32 time = 0; // ms 计时变量
uint16_t Time_Count = 0;
uint16_t Time_nSec_Count = 0;
uint8_t usart1_flag_change_enable = 0;//USART1 标识变更使能 0: 禁止修改标识 1: 允许修改标识
uint8_t usart2_flag_change_enable = 0;//USART2 标识变更使能 0: 禁止修改标识 1: 允许修改标识
uint8_t usart3_flag_change_enable = 0;//USART3 标识变更使能 0: 禁止修改标识 1: 允许修改标识
uint8_t uart2_acceptData_timeout_count_enable = 0;//USART2 接收数据计时使能 0: 计时关闭 1: 计时打开
uint8_t Feed_Dog_Flag = 0;//喂狗标识
uint8_t usart1_acceptData_success_flag = 0;//USART1 数据接收完成标识
uint8_t usart2_acceptData_success_flag = 0;//USART2 数据接收完成标识
uint8_t usart3_acceptData_success_flag = 0;//USART3 数据接收完成标识
uint8_t uart2_acceptData_timeout_flag = 0;//USART2 接收数据超时标识
#define GPIO_KEY_NUM 2 ///< Defines the total number of key member
keyTypedef_t singleKey[GPIO_KEY_NUM]; ///< Defines a single key member array pointer
keysTypedef_t keys;
/*
*系统复位
* __set_FAULTMASK(1); // 关闭所有中端
* NVIC_SystemReset(); // 复位
*
*/
void SystemReset(void)
{
__set_FAULTMASK(1); // 关闭所有中端
NVIC_SystemReset(); // 复位
}
/**
* key1 short press handle
* @param none
* @return none
*/
void key1ShortPress(void)
{
USART1_printf("KEY1 PRESS ,Production Mode\n");
gizwitsSetMode(WIFI_PRODUCTION_TEST);
}
/**
* key1 long press handle
* @param none
* @return none
*/
void key1LongPress(void)
{
USART1_printf("KEY1 PRESS LONG ,Wifi Reset\n");
gizwitsSetMode(WIFI_RESET_MODE);
}
/**
* key2 short press handle
* @param none
* @return none
*/
void key2ShortPress(void)
{
USART1_printf("KEY2 PRESS ,Soft AP mode\n");
ledRgbControl(250,0,0);///SoftAPmode,RGB red
#if !MODULE_TYPE
gizwitsSetMode(WIFI_SOFTAP_MODE);
#endif
}
/**
* key2 long press handle
* @param none
* @return none
*/
void key2LongPress(void)
{
//AirLink mode
USART1_printf("KEY2 PRESS LONG ,AirLink mode\n");
ledRgbControl(0,250,0);//AirLink mode,RGB Green
#if !MODULE_TYPE
gizwitsSetMode(WIFI_AIRLINK_MODE);
#endif
}
/**
* Key init function
* @param none
* @return none
*/
void keyInit(void)
{
singleKey[0] = keyInitOne(NULL, KEY1_GPIO_Port,KEY1_Pin, key1ShortPress, key1LongPress);
singleKey[1] = keyInitOne(NULL, KEY2_GPIO_Port,KEY2_Pin, key2ShortPress, key2LongPress);
keys.singleKey = (keyTypedef_t *)&singleKey;
keyParaInit(&keys);
}
/*
* t : 定时时间
* Ticks : 多少个时钟周期产生一次中断
* f : 时钟频率 72000000
* t = Ticks * 1/f = (72000000/100000) * (1/72000000) = 10us
*/
void system_init(void)
{
SysTick_Init();/*配置SysTick 为10us中断一次 */
USART1_Config();/* USART2 配置模式为 115200 8-N-1,中断接收 */
USART2_Config();/* USART2 配置模式为 9600 8-N-1,中断接收 */
USART1_printf( "\r\nWF-esp8266 WiFi模块测试例程\r\n" ); //打印测试例程提示信息
TIM2_Configuration();/*定时器2中断配置*/
TIM2_NVIC_Configuration();/*TIM2中断优先级配置*/
IWDG_Configuration();//看门狗初始化
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);/* TIM2 重新开时钟,开始计时 */
//LED_Init ();
Key_GPIO_Config();
gizwitsInit();
//STM32_Flash_read();
keyInit();
userInit();
USART1_printf("MCU Init Success \n");
}
/**
* @brief 主函数
* @param 无
* @retval 无
*/
int main(void)
{
system_init();
clean_uart1_rebuff();//清空了接收缓冲区数据
clean_uart2_rebuff();//清空了接收缓冲区数据
usart1_flag_change_enable = 1;//USART1 标识变更使能 0: 禁止修改标识 1: 允许修改标识
usart2_flag_change_enable = 1;//USART1 标识变更使能 0: 禁止修改标识 1: 允许修改标识
while(1)
{
if(Feed_Dog_Flag == 1) //喂狗
{
Feed_Dog_Flag = 0;
IWDG_ReloadCounter(); //重装载IWDG计数器 喂狗
}
if(usart1_acceptData_success_flag == 1) //USART1 数据接收完成
{
usart1_flag_change_enable = 0; //USART1 标识变更使能 0: 禁止修改标识 1: 允许修改标识
usart1_acceptData_success_flag = 0;
USART1_datadeal(); //接收数据处理
clean_uart1_rebuff(); //清空了接收缓冲区数据
usart1_flag_change_enable = 1; //USART1标识变更使能 0: 禁止修改标识 1: 允许修改标识
}
userHandle();
gizwitsHandle((dataPoint_t *)¤tDataPoint);
}
}
|
|