|
Hi3861 交通燈板(HiSpark_WiFi_IoT_SSL_VER.A)開(kāi)發(fā), 一、前言
HarmonyOS HiSpark Wi-Fi IoT 套件開(kāi)發(fā)之
交通燈板 二、場(chǎng)景描述 場(chǎng)景設(shè)定:十字路口 三、功能描述 行人過(guò)街按鈕。當(dāng)行人過(guò)街時(shí),按下按鈕,系統(tǒng)會(huì)根據(jù)實(shí)時(shí)的道路車流量情況、行人過(guò)街情況調(diào)整信號(hào)燈。
微信圖片_20201105205235.jpg (109.69 KB, 下載次數(shù): 0)
下載附件 保存到相冊(cè)
昨天 22:31 上傳 四、功能實(shí)現(xiàn) 首先,無(wú)按鈕按下時(shí),一直亮紅燈 然后,當(dāng)按鈕按下時(shí),亮綠燈 最后,綠燈熄滅,黃燈閃爍,同時(shí)音樂(lè)響起催促行人快些過(guò)街 五、代碼實(shí)現(xiàn)
- /*
- * 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,高電平點(diǎn)亮
- * GPIO_11 綠色LED,高電平點(diǎn)亮
- * GPIO_12 黃色LED,高電平點(diǎn)亮
- */
- #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 對(duì)應(yīng)的分頻系數(shù)
- 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();
- //設(shè)置KEY IO為輸入狀態(tài),并使能中斷
- 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);
- // 蜂鳴器引腳 設(shè)置為 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); // 設(shè)置時(shí)鐘源為晶體時(shí)鐘(40MHz,默認(rèn)時(shí)鐘源160MHz)
- //設(shè)置紅色,黃色,綠色 LED IO為輸出狀態(tài)
- 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);
-
復(fù)制代碼 六、編譯
Screenshot from 2020-10-24 22-39-38.png (119.84 KB, 下載次數(shù): 0)
下載附件 保存到相冊(cè)
昨天 22:31 上傳 七、燒錄
Screenshot from 2020-10-24 22-41-03.png (50.65 KB, 下載次數(shù): 0)
下載附件 保存到相冊(cè)
昨天 22:31 上傳 八、運(yùn)行
WeChat_20201105204613.gif (913.31 KB, 下載次數(shù): 0)
下載附件 保存到相冊(cè)
昨天 22:31 上傳 九、總結(jié) 1)編譯過(guò)程中報(bào)錯(cuò):undefined reference to hi_pwm_init 等幾個(gè) hi_pwm_開(kāi)頭的函數(shù),原因: 因?yàn)槟J(rèn)情況下,hi3861_sdk中,PWM的CONFIG選項(xiàng)沒(méi)有打開(kāi)。解決: 修改vendor\hisi\hi3861\hi3861\build\config\usr_config.mk文件中的CONFIG_PWM_SUPPORT行:
- # CONFIG_PWM_SUPPORT is not set
復(fù)制代碼 修改為
復(fù)制代碼 2)在開(kāi)發(fā)過(guò)程中參考了潤(rùn)和軟件許老師的作品還有其他大佬的總結(jié),在此一并感謝。 |
|