本章目的是把开发板上的3个三色LED灯点亮。
1.硬件连接
先看下硬件的LED接线如下:
由上图可知:
IO_12:LED_G
IO_13:LED_R
IO_14:LED_B
IO输出低电平"0",LED点亮,IO输出高电平"1",LED熄灭。
2.软件编写
在 kendryte-standalone-sdk/src 目录下创建gpio_led文件夹,如下:
main.c 代码如下:
/* Copyright 2018 Canaan Inc.
*
* 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.
*/
#include <stdio.h>
#include <unistd.h>
#include "fpioa.h"
#include "gpio.h"
//这个对应芯片的真实引脚 总共有IO0~IO47个可编程引脚
#define LED_RED_IO (13)
#define LED_GREEN_IO (12)
#define LED_BLUE_IO (14)
//这个是芯片真实引脚映射到GPIO引脚,GPIO有GPIO0~GPIO7
#define LED_RED_PIN (1)
#define LED_GREEN_PIN (0)
#define LED_BLUE_PIN (2)
//定义高电平关闭LED,低电平点亮LED
#define LED_ON (GPIO_PV_LOW)
#define LED_OFF (GPIO_PV_HIGH)
int main(void)
{
//将IO 映射到 GPIO
fpioa_set_function(LED_GREEN_IO, FUNC_GPIO0);
fpioa_set_function(LED_RED_IO, FUNC_GPIO1);
fpioa_set_function(LED_BLUE_IO, FUNC_GPIO2);
gpio_init();
//设置GPIO 为推挽输出模式
gpio_set_drive_mode(LED_RED_PIN, GPIO_DM_OUTPUT);
gpio_set_drive_mode(LED_GREEN_PIN, GPIO_DM_OUTPUT);
gpio_set_drive_mode(LED_BLUE_PIN, GPIO_DM_OUTPUT);
//设置GPIO为 高电平,默认所有LED熄灭
gpio_pin_value_t value = LED_OFF;
gpio_set_pin(LED_RED_PIN, value);
gpio_set_pin(LED_GREEN_PIN, value);
gpio_set_pin(LED_BLUE_PIN, value);
while (1)
{
//点亮红灯,其它灯熄灭
gpio_set_pin(LED_RED_PIN, LED_ON);
gpio_set_pin(LED_GREEN_PIN, LED_OFF);
gpio_set_pin(LED_BLUE_PIN, LED_OFF);
//延时1秒,堵塞延时
sleep(1);
//点亮绿灯,其它灯熄灭
gpio_set_pin(LED_RED_PIN, LED_OFF);
gpio_set_pin(LED_GREEN_PIN, LED_ON);
gpio_set_pin(LED_BLUE_PIN, LED_OFF);
sleep(1);
//点亮蓝灯,其它灯熄灭
gpio_set_pin(LED_RED_PIN, LED_OFF);
gpio_set_pin(LED_GREEN_PIN, LED_OFF);
gpio_set_pin(LED_BLUE_PIN, LED_ON);
sleep(1);
}
return 0;
}
3.编译
在kendryte-standalone-sdk文件夹中新建build文件夹并进入,执行如下命令
(.env) user@pc:~/kendryte-standalone-sdk/build$ cmake .. -DPROJ=gpio_led -DTOOLCHAIN=/opt/kendryte-toolchain/bin && make
编译成功后,会在build文件夹下生成烧录文件:gpio_led.bin
4.烧录程序
注意:要先激活kendryte-standalone-sdk 文件夹中 .env/bin/activate 它是python的虚拟环境,kflash安装在此虚拟环境中,参考 <<01. k210-命令行环境搭建(ubuntu环境)>> 。
(.env) user@pc:~/kendryte-standalone-sdk/build$ kflash gpio_led.bin -p /dev/ttyUSB0
等待烧录进度条OK,如下图
开发板自动重启后,可看见三个颜色的LED交替闪烁!