几乎全部的有效内容都在“LED.c”中:
#include "LED.h"
#include <rtthread.h>
#include "board.h"
//定义一个结构体类型
struct LED_RGB
{
uint8_t LED_R;
uint8_t LED_G;
uint8_t LED_B;
};
//实例化一个结构体对象
struct LED_RGB led_rgb;
//结构体对象的初始化
void LED_RGB_Init(void){
//将结构体对象的成员对应到管脚
led_rgb.LED_R = rt_pin_get("PE.1");
led_rgb.LED_G = rt_pin_get("PE.4");
led_rgb.LED_B = rt_pin_get("PA.1");
//设置结构体对象的成员管脚的输出方式
rt_pin_mode(led_rgb.LED_R, PIN_MODE_OUTPUT);
rt_pin_mode(led_rgb.LED_G, PIN_MODE_OUTPUT);
rt_pin_mode(led_rgb.LED_B, PIN_MODE_OUTPUT);
}
//RGB三色LED各自的驱动程序
//if(toggle==1) LED亮
//if(toggle==0) LED灭
void RGB_Red(rt_bool_t toggle){
rt_pin_write(led_rgb.LED_G, PIN_HIGH);
rt_pin_write(led_rgb.LED_B, PIN_HIGH);
if(toggle){
rt_pin_write(led_rgb.LED_R, PIN_LOW);
} else{
rt_pin_write(led_rgb.LED_R, PIN_HIGH);
}
}
void RGB_Green(rt_bool_t toggle){
rt_pin_write(led_rgb.LED_R, PIN_HIGH);
rt_pin_write(led_rgb.LED_B, PIN_HIGH);
if(toggle){
rt_pin_write(led_rgb.LED_G, PIN_LOW);
} else{
rt_pin_write(led_rgb.LED_G, PIN_HIGH);
}
}
void RGB_Blue(rt_bool_t toggle){
rt_pin_write(led_rgb.LED_R, PIN_HIGH);
rt_pin_write(led_rgb.LED_G, PIN_HIGH);
if(toggle){
rt_pin_write(led_rgb.LED_B, PIN_LOW);
} else{
rt_pin_write(led_rgb.LED_B, PIN_HIGH);
}
}
//编写线程入口函数(主程序文件)
static void led_rgb_thread_entry(void* p){
LED_RGB_Init();
while(1){
RGB_Red(1);
rt_thread_mdelay(1000);
RGB_Red(0);
rt_thread_mdelay(1000);
RGB_Red(1);
rt_thread_mdelay(1000);
RGB_Red(0);
rt_thread_mdelay(1000);
RGB_Green(1);
rt_thread_mdelay(1000);
RGB_Green(0);
rt_thread_mdelay(1000);
RGB_Green(1);
rt_thread_mdelay(1000);
RGB_Green(0);
rt_thread_mdelay(1000);
RGB_Blue(1);
rt_thread_mdelay(1000);
RGB_Blue(0);
rt_thread_mdelay(1000);
RGB_Blue(1);
rt_thread_mdelay(1000);
RGB_Blue(0);
rt_thread_mdelay(1000);
}
}
//创建线程启动函数,用于启动此前创建的线程主体
static int Thread_led_rgb(void){
rt_thread_t thread = RT_NULL;
thread = rt_thread_create("led_rgb", led_rgb_thread_entry, RT_NULL, 512, 10, 10);
if(thread == RT_NULL){
rt_kprintf("Thread_led_rgb initiate ERROR");
return RT_ERROR;
}
rt_thread_startup(thread);
}
//将线程的初始化加入系统初始化,启动线程
INIT_APP_EXPORT(Thread_led_rgb);
|