九齐单片机NY8B062D单按键控制4种LED状态


/* =========================================================================
 * Project:       单按键控制4种LED状态
 * File:          单按键控制4种LED状态
 * Description:   单按键控制4种LED状态
 *                 		                      
 *                
 * Author:        Jimp
 * Version:       V1.0		                      
 * Date:          2020/05/22
 =========================================================================*/
//--------------- File Include ---------------------------------------------
//--------------------------------------------------------------------------
#include 
#include 
#include "ny8_constant.h"
#define UPDATE_REG(x)	__asm__("MOVR _" #x ",F")
#define unchar unsigned char
#define unint unsigned int 

uint8_t myvar;
__sbit flag = myvar:0;		//定义控制运行符
__sbit key = PORTB:0;
__sbit light1 = PORTA:7;
__sbit light2 = PORTA:6;
unchar Mod;		//定义亮灯模式

void isr(void) __interrupt(0)
{
	if(INTFbits.T0IF)
	{ 
		static unsigned int cnt= 0;
		TMR0 = 0;
		INTF= (unsigned char)~(C_INT_TMR0);	// Clear T0IF flag bit	
		cnt ++;
		if(cnt >= 250)
			{
				cnt = 0;
				light1 = ~light1;
				light2 = ~light2;
			}
	}	
}

void lnit_Mod()		//初始化函数
{
	IOSTA = ~IOSTA;                                     //设置A口为输出
	PORTA = 0xFF;
    IOSTB = C_PB0_Input;
    flag = 1;
	key = 1; 
	Mod = 0;
	light1 = 0;
	light2 = 0;           
}

void lnit_Timer()	//中断初始化
{
 	DISI();		//禁用中断
 	PCON1 = C_TMR0_Dis;
 	TMR0 = 0;
 	T0MD = C_PS0_TMR0 | C_PS0_Div8;		//预分频分配给TMR0
 	INTE = C_INT_TMR0;
 	ENI();		//启用中断					
}

void delay(unint z)		//延时函数ms
{
	unint x,y;
	for(x = z;x > 0;x--)
		for(y = 100;y > 0;y--);
}

void lnput_Mod()		//状态模式标记函数
{
	if(Mod == 4)
		Mod = 0;
	if(key == 0)
	{
		delay(5);
		if(key == 0)	//延时消抖
			Mod++;		//按键次数标记
	}
	while(!key)			//松手检测
	{;;}
}

void run()			//4种模式下的执行
{
	switch(Mod)
	{
		case 0:{PCON1 = C_TMR0_Dis;light1 = 0;light2 = 0;}break;		//全亮
		case 1:{PCON1 = C_TMR0_En;}break;		//同步闪烁
		case 2:{PCON1 = C_TMR0_Dis;light1 = 1;light2 = 1;}break;		//全灭
		case 3:{PCON1 = C_TMR0_Dis;light1 = 0;light2 = 1;PCON1 = C_TMR0_En;}break;		//异步闪
	}
}

void main(void)
{
	lnit_Mod();
	lnit_Timer();
	while(1)
	{
		lnput_Mod();
		if(flag == 1) 		//控制RUN运行
		{
			run();
			if(Mod == 1||Mod == 3)
			flag = 0;
		}
		if(Mod == 0||Mod == 2)
		flag = 1;
	}
			
}









你可能感兴趣的:(九齐单片机NY8B062D单按键控制4种LED状态)