STC8H8K蓝牙智能巡线小车——2. 点亮左右转弯灯与危险报警灯

 任务调用示例

 RTX 51 TNY 可做多任务调度,API较为简单。

/* 接口API */

// 创建任务
extern unsigned char os_create_task     (unsigned char task_id);
// 结束任务
extern unsigned char os_delete_task     (unsigned char task_id);

// 等待
extern unsigned char os_wait            (unsigned char typ, 
                                         unsigned char ticks,
                                         unsigned int dummy);
extern unsigned char os_wait1           (unsigned char typ);

extern unsigned char os_wait2           (unsigned char typ,
                                         unsigned char ticks);
// 发送信号
extern unsigned char os_send_signal     (unsigned char task_id);
extern unsigned char os_clear_signal    (unsigned char task_id);




/* 用法 */

void task_led() _task_ 任务代码{
    
}


void task_main() _task_ 任务代号{

    //开启子任务
    os_create_task(task_led); 

    // 结束入口任务
    os_delete_task(task_main);

}

STC8H8K蓝牙智能巡线小车——2. 点亮左右转弯灯与危险报警灯_第1张图片

电路分析
  • 引脚为P05和P34
  • 当引脚为高电平时,行成电时差,LED会被点亮
编程思路
  • Driver中创建LED.c文件,做具体实现
  • Driver中创建LED.h文件,应有灯方向类型宏定义、GPIO初始化函数、开关和关灯函数
  • User中main.c文件为程序入口,编写子任务函数后在task_main中进行调用
代码示例
LED.h
#ifndef	__LED_H
#define	__LED_H
#include "GPIO.h"


#define LED_RIGHT P05
#define LED_LEFT P34

typedef enum led_type1{
	left,right,all
}led_type;

void LED_init();
void LED_ON(led_type typ);
void LED_OFF(led_type typ);

#endif
LED.c
#include "LED.h"


void LED_init(){

	GPIO_InitTypeDef	GPIO_InitStructure;		//结构定义
	GPIO_InitStructure.Pin  = GPIO_Pin_4;		//指定要初始化的IO,
	GPIO_InitStructure.Mode = GPIO_OUT_PP;	//指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
	GPIO_Inilize(GPIO_P3, &GPIO_InitStructure);//初始化

	GPIO_InitStructure.Pin  = GPIO_Pin_5;		//指定要初始化的IO,
	GPIO_InitStructure.Mode = GPIO_OUT_PP;	//指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
	GPIO_Inilize(GPIO_P0, &GPIO_InitStructure);//初始化

	LED_LEFT=LED_RIGHT=0;

}
void LED_ON(led_type typ){
	if(typ==left){LED_LEFT=1;}
	else if(typ==right){LED_RIGHT=1;}
	else if(typ==all){LED_LEFT=LED_RIGHT=1;}

}
void LED_OFF(led_type typ){
	switch(typ){
		case left:
			LED_LEFT=0;
			break;
		case right:
			LED_RIGHT=0;
			break;
		case all:
			LED_LEFT=LED_RIGHT=0;
			break;
		default:
			LED_LEFT=LED_RIGHT=0;
			break;
	}

}

main.c

#include "RTX51TNY.h"
#include "GPIO.h"
#include "NVIC.h"
#include "UART.h"
#include "Switch.h"
#include "LED.h"

void task_led() _task_ 1{
	
	while(1){
		
		// left
		LED_ON(left);
		os_wait2(K_TMO, 100);
		
		LED_OFF(left);
		os_wait2(K_TMO, 100);
		
		// right
		LED_ON(right);
		os_wait2(K_TMO, 100);
		
		LED_OFF(right);
		os_wait2(K_TMO, 100);
		
		// Hazard lights
		LED_ON(all);
		os_wait2(K_TMO, 100);
		
		// all off
		LED_OFF(all);
		os_wait2(K_TMO, 100);
	
	}
}

void task_main() _task_ 0{
	LED_init();
	os_create_task(1);
	os_delete_task(0);
}

你可能感兴趣的:(单片机开发(IC,Development),51单片机,嵌入式硬件)