Haas edu板子day2 按键亮灯和蜂鸣器

单独亮红灯

/*
 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
 */

#include <aos/errno.h>
#include <aos/kernel.h>
#include <unistd.h>
#include <k_api.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <uservice/uservice.h>
#include <uservice/eventid.h>
#include "aos/init.h"
#include "board.h"
#include "aos/hal/gpio.h"
#include "aos/hal/pwm.h"

//#include "sh1106.h"
#include "aos/hal/adc.h"
#include "aos/hal/i2c.h"
#include "drv_als_ps_ir_liteon_ap3216c.h"
#include "ulog/ulog.h"
#include "netmgr.h"
#include "hal_iomux_haas1000.h"
#include "../../../components/lwip/lwip2.0.0/include/lwip/sockets.h"


gpio_dev_t red;
gpio_dev_t key1;



void handler_irq(void* arg){
    if(1 == (int)arg)
{
    hal_gpio_output_toggle(&red);//led电平反转
}
}





void myled_init(){
    red.port=HAL_GPIO_PIN_P4_4;
    red.config=OUTPUT_PUSH_PULL;
    red.priv=NULL;

    hal_gpio_init(&red);//初始化
    hal_gpio_output_low(&red);//默认低电平
}



void mykey_init(){


    key1.port = HAL_GPIO_PIN_P2_7;
    key1.config =IRQ_MODE;
    key1.priv=NULL;

    hal_gpio_init(&key1);

    hal_gpio_enable_irq(&key1,IRQ_TRIGGER_FALLING_EDGE,handler_irq,(void *)1);


}




int application_start(int argc, char *argv[])
{
    myled_init();
    mykey_init();

    while (1)
    {
        aos_msleep(1000);

        
    };
    
    
}

红绿蓝灯都亮

/*
 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
 */

#include <aos/errno.h>
#include <aos/kernel.h>
#include <unistd.h>
#include <k_api.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <uservice/uservice.h>
#include <uservice/eventid.h>
#include "aos/init.h"
#include "board.h"
#include "aos/hal/gpio.h"
#include "aos/hal/pwm.h"

#include "sh1106.h"
#include "aos/hal/adc.h"
#include "aos/hal/i2c.h"
#include "drv_als_ps_ir_liteon_ap3216c.h"
#include "ulog/ulog.h"
#include "netmgr.h"
#include "hal_iomux_haas1000.h"
#include "../../../components/lwip/lwip2.0.0/include/lwip/sockets.h"


gpio_dev_t red, green, blue;
gpio_dev_t key1, key2, key3;




void handler_irq(void* arg){
    switch ((int)arg)
    {
    case 1:
        hal_gpio_output_toggle(&red);
        break;
    case 2:
        hal_gpio_output_toggle(&green);
        break;
    case 3:
        hal_gpio_output_toggle(&blue);
        break;   
    }

}





void myled_init(){
    red.port=HAL_GPIO_PIN_P4_4;
    red.config=OUTPUT_PUSH_PULL;
    red.priv=NULL;

    hal_gpio_init(&red);//初始化
    hal_gpio_output_low(&red);//默认低电平



    green.port=HAL_GPIO_PIN_P4_3;
    green.config=OUTPUT_PUSH_PULL;
    green.priv=NULL;

    hal_gpio_init(&green);//初始化
    hal_gpio_output_low(&green);//默认低电平


    blue.port=HAL_GPIO_PIN_P4_2;
    blue.config=OUTPUT_PUSH_PULL;
    blue.priv=NULL;

    hal_gpio_init(&blue);//初始化
    hal_gpio_output_low(&blue);//默认低电平

}



void mykey_init(){


    key1.port = HAL_GPIO_PIN_P2_7;
    key1.config =IRQ_MODE;
    key1.priv=NULL;

    hal_gpio_init(&key1);

    hal_gpio_enable_irq(&key1,IRQ_TRIGGER_FALLING_EDGE,handler_irq,(void *)1);


    key2.port = HAL_GPIO_PIN_P2_4;
    key2.config =IRQ_MODE;
    key2.priv=NULL;

    hal_gpio_init(&key2);

    hal_gpio_enable_irq(&key2,IRQ_TRIGGER_FALLING_EDGE,handler_irq,(void *)2);


    key3.port = HAL_GPIO_PIN_P2_5;
    key3.config =IRQ_MODE;
    key3.priv=NULL;

    hal_gpio_init(&key3);

    hal_gpio_enable_irq(&key3,IRQ_TRIGGER_FALLING_EDGE,handler_irq,(void *)3);


}




int application_start(int argc, char *argv[])
{
    myled_init();
    mykey_init();

    while (1)
    {
        aos_msleep(1000);

        
    };
    
    
}

蜂鸣器

/*
 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
 */

#include <aos/errno.h>
#include <aos/kernel.h>
#include <unistd.h>
#include <k_api.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <uservice/uservice.h>
#include <uservice/eventid.h>
#include "aos/init.h"
#include "board.h"
#include "aos/hal/gpio.h"
#include "aos/hal/pwm.h"

#include "sh1106.h"
#include "aos/hal/adc.h"
#include "aos/hal/i2c.h"
#include "drv_als_ps_ir_liteon_ap3216c.h"
#include "ulog/ulog.h"
#include "netmgr.h"
#include "hal_iomux_haas1000.h"
#include "../../../components/lwip/lwip2.0.0/include/lwip/sockets.h"


#define PWM_PORT 0



int application_start(int argc, char *argv[])
{
    pwm_dev_t buzzer;      
    buzzer.port=PWM_PORT;              //端口号
    buzzer.config.duty_cycle=0.5;      //占空比
    buzzer.config.freq=2000;         //频率
    buzzer.priv=NULL;                //私有数据

    hal_pwm_init(&buzzer);//初始化引脚

    while (1)
    {
        hal_pwm_start(&buzzer);//开是输出pwm方波
        aos_msleep(100);    //延时100ms
        hal_pwm_stop(&buzzer);//停止输出pwm方波
        aos_msleep(100);    //延时100ms
    };
    


}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值