回答

收藏

【体验】 机智云智能空调-STP3修改自动生成MCU代码

机智云GoKit 机智云GoKit 3421 人阅读 | 0 人回复 | 2018-03-10

本帖最后由 简简单单_3013189 于 2018-3-16 13:12 编辑

前提:【体验】 机智云智能空调-STP1中讲解了如何生成MCU代码

下面我们如何来修改MCU代码呢:
STEP1:智能空调APP面板与功能说明
控制面板如下:

PowerSwitch建:空调电源控制键
SteTem:设定目标温度
CurTem:当前温度
CurHtm:当前湿度
功能:APP控制空调开关,设定目标温度,并且显示房间温度和湿度
当CurTem>SteTem, 空调制冷
当CurTem<SteTem, 空调加热

STEP2:硬件实现和硬件实物图如下:

【1】RGB灯采用(WS2813)彩灯,空调加热RGB为红色光,制冷是为绿色光,空调关闭时无光
【2】LED1和LED2:软AP模式(LED1亮,LED2灭),直连模式(LED1灭,LED2亮)

STEP2:硬件驱动移植:
驱动采用HAL库写:
【1】温度传感器DHT11驱动代码如下:
void DHT11_IO_OUT(void)
{
    GPIO_InitTypeDef myGPIO_InitStruct;
    myGPIO_InitStruct.Pin = DHT11_DIN_Pin;
    myGPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    myGPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(DHT11_DIN_GPIO_Port, &myGPIO_InitStruct);
}


void DHT11_IO_IN(void)
{
    GPIO_InitTypeDef myGPIO_InitStruct;
    myGPIO_InitStruct.Pin = DHT11_DIN_Pin;
    myGPIO_InitStruct.Pull = GPIO_PULLUP;
    myGPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    myGPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(DHT11_DIN_GPIO_Port, &myGPIO_InitStruct);
}


//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(45);//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())
    {
        for(i=0; i<5; i++)
        {
            buf = dht11ReadByte();
        }
       if((buf[0] + buf[1]+ buf[2] + buf[3]) == buf[4])
                {
                        if((buf[0] >= 100) || (buf[2] >= 50))
                        {
        
                        }
                        else
                        {
                                *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 */
    dht11Rst();
   
    memset((uint8_t *)&temphumTypedef, 0, sizeof(thTypedef_t));
   
//    printf("dh11Init \r\n");
   
    return dht11Check();
}

【2】WS2813驱动如下:

#define HAL_RGB_GLOBAL
#include <string.h>
#include "delay.h"
#include "hal_rgb_ws2813.h"


#define        WS2813_DAT_1         HAL_GPIO_WritePin(WS2813_GPIO_Port, WS2813_DAT_Pin, GPIO_PIN_SET)
#define        WS2813_DAT_0          HAL_GPIO_WritePin(WS2813_GPIO_Port, WS2813_DAT_Pin, GPIO_PIN_RESET)
uint8_t light_r,light_g,light_b;


void RGB_LED_Init(void)
{
    GPIO_InitTypeDef myGPIO_InitStruct;
    myGPIO_InitStruct.Pin = WS2813_DAT_Pin | WS2813_GND_Pin;
    myGPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    myGPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(WS2813_GPIO_Port, &myGPIO_InitStruct);
        
        //SET GND
        HAL_GPIO_WritePin(WS2813_GPIO_Port, WS2813_GND_Pin, GPIO_PIN_RESET);
        RGB_rst();
}


void RGB_rst(void) {
        uint16_t i;
        
        WS2813_DAT_0;
        for(i = 0; i < 1000; i++);
}


void Send_8bits(uint8_t dat) {
        uint8_t a;
        
        for(a=0;a<8;a++) {
                WS2813_DAT_1;
                if (dat & 0x80) {
                        __asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");
                        __asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");
                        __asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");
                        __asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");
                        __asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");
                        __asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");
                        dat=dat<<1;
                        WS2813_DAT_0;
                        __asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");
                } else {
                        __asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");
                        dat=dat<<1;
                        WS2813_DAT_0;
                        __asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");
                        __asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");
                        __asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");
                        __asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");
                        __asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");
                        __asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");__asm("nop");
                }
        }
}


void LED_RGB_Control(uint8_t R, uint8_t G, uint8_t B)
{
        Send_8bits(G);
        Send_8bits(R);
        Send_8bits(B);
        RGB_rst();
}

STEP3:用户控制代码如下:
遵从功能描述:
void userHandle(void)
{   uint8_t tem, hum;
        uint8_t ret  = 0;
        
        ret = dht11Read(&tem, &hum);


        if(0 == ret) {
                currentDataPoint.valueCurTem = tem;//Add Sensor Data Collection
                currentDataPoint.valueCurHtm = hum;//Add Sensor Data Collection
        }


        if (currentDataPoint.valuePowerSwitch ) {
                        if (currentDataPoint.valueSetTem > currentDataPoint.valueCurTem) {
                        LED_RGB_Control(255, 0, 0);
                } else {
                        LED_RGB_Control(0, 255, 0);
                }
        } else {
                LED_RGB_Control(0, 0, 0);
        }
}


修改后的工程代码已经放在下一帖中,感兴趣的请自行下载!!!
分享到:
回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条