#2020征文-开发板# Hi3861 交通灯板HiSpark_WiFi_IoT开发

发布于 2021-1-7 17:35
浏览
0收藏

一、前言
HarmonyOS HiSpark Wi-Fi IoT 套件开发之交通灯板
二、场景描述
场景设定:十字路口
三、功能描述
行人过街按钮。当行人过街时,按下按钮,系统会根据实时的道路车流量情况、行人过街情况调整信号灯。

#2020征文-开发板# Hi3861 交通灯板HiSpark_WiFi_IoT开发-开源基础软件社区

四、功能实现
首先,无按钮按下时,一直亮红灯
然后,当按钮按下时,亮绿灯
最后,绿灯熄灭,黄灯闪烁,同时音乐响起催促行人快些过街
五、代码实现

/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
  * HiSpark_WiFi_IoT_SSL_VER.A板
  * GPIO_8  按键
  * GPIO_9  蜂鸣器
  * GPIO_10 红色LED,高电平点亮
  * GPIO_11 绿色LED,高电平点亮
  * GPIO_12 黄色LED,高电平点亮
*/
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include "wifiiot_pwm.h"
#include "hi_pwm.h"

#define LED_INTERVAL_TIME_US 300000
#define LED_TASK_STACK_SIZE 512
#define LED_TASK_PRIO 25
#define random(x) (rand()%x)

enum LedState {
    LED_RED = 0,
    LED_GREEN,
    LED_YELLOW,
};
enum LedState g_ledState = LED_RED;

static const uint16_t g_freqs[] = {// 40M Hz 对应的分频系数
    38223, // 1046.5
    34052, // 1174.7
    30338, // 1318.5
    28635, // 1396.9
    25511, // 1568
    22728, // 1760
    20249  // 1975.5
};

static void *LedTask(const char *arg)
{
    (void)arg;
    
    while (1) { 
        int count = 0;    
        switch (g_ledState) {
            case LED_RED:
                GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_10, 1);
                usleep(LED_INTERVAL_TIME_US);
                break;
            case LED_GREEN:
                srand((unsigned)time(NULL));
                usleep(random(1000)*1000); 
                GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_10, 0);
                GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_11, 1);
                usleep(LED_INTERVAL_TIME_US * 10);
                GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_11, 0);
                usleep(LED_INTERVAL_TIME_US);
                g_ledState = LED_YELLOW;
                break;
            case LED_YELLOW:
                do
                {
                    GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_12, 1);
                    Pwmstart(WIFI_IOT_PWM_PORT_PWM0, g_freqs[count]/2, g_freqs[count]);
                    usleep(LED_INTERVAL_TIME_US);
                    GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_12, 0);
                    PwmStop(WIFI_IOT_PWM_PORT_PWM0);
                    usleep(LED_INTERVAL_TIME_US);
                    count++;
                } while (count < 7);
                g_ledState = LED_RED;
                break;
            default:
                usleep(LED_INTERVAL_TIME_US);
                break;
        }
    }

    return NULL;
}

static void OnKeyPressed(char *arg)
{
    (void)arg;
    
    if(g_ledState == LED_RED)
    {
        g_ledState = LED_GREEN;
    }
}
static void LedExampleEntry(void)
{
    osThreadAttr_t attr;

    GpioInit();

    //设置KEY IO为输入状态,并使能中断
    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_IO_FUNC_GPIO_8_GPIO);
    GpioSetDir(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_GPIO_DIR_IN);
    IoSetPull(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_IO_PULL_UP);
    GpioRegisterIsRFunc(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_INT_TYPE_EDGE, WIFI_IOT_GPIO_EDGE_RISE_LEVEL_HIGH, OnKeyPressed, NULL);

    // 蜂鸣器引脚 设置为 PWM功能
    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_IO_FUNC_GPIO_9_PWM0_OUT);
    PwmInit(WIFI_IOT_PWM_PORT_PWM0);
    hi_pwm_set_clock(PWM_CLK_XTAL); // 设置时钟源为晶体时钟(40MHz,默认时钟源160MHz)

    //设置红色,黄色,绿色 LED IO为输出状态
    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_IO_FUNC_GPIO_10_GPIO);
    GpioSetDir(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_GPIO_DIR_OUT);

    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_FUNC_GPIO_11_GPIO);
    GpioSetDir(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_GPIO_DIR_OUT);

    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_FUNC_GPIO_12_GPIO);
    GpioSetDir(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_GPIO_DIR_OUT);

    attr.name = "LedTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = LED_TASK_STACK_SIZE;
    attr.priority = LED_TASK_PRIO;

    if (osThreadNew((osThreadFunc_t)LedTask, NULL, &attr) == NULL) {
        printf("[LedExample] Falied to create LedTask!\n");
    }
}

SYS_RUN(LedExampleEntry);

六、编译

#2020征文-开发板# Hi3861 交通灯板HiSpark_WiFi_IoT开发-开源基础软件社区

七、烧录

#2020征文-开发板# Hi3861 交通灯板HiSpark_WiFi_IoT开发-开源基础软件社区

八、运行

#2020征文-开发板# Hi3861 交通灯板HiSpark_WiFi_IoT开发-开源基础软件社区

九、总结
1)编译过程中报错:undefined reference to hi_pwm_init 等几个 hi_pwm_开头的函数,原因: 因为默认情况下,hi3861_sdk中,PWM的CONFIG选项没有打开。解决: 修改vendor\hisi\hi3861\hi3861\build\config\usr_config.mk文件中的CONFIG_PWM_SUPPORT行:

# CONFIG_PWM_SUPPORT is not set

修改为

    CONFIG_PWM_SUPPORT=y

2)在开发过程中参考了润和软件许老师的作品还有其他大佬的总结,在此一并感谢。

已于2021-1-7 17:35:43修改
收藏
回复
举报
回复
添加资源
添加资源将有机会获得更多曝光,你也可以直接关联已上传资源 去关联
    相关推荐