⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 misc.c

📁 AVR单片机C语言程序设计实例精粹
💻 C
字号:
//******************************************************************************
// File Name : Gpio.c
// Author    : Steaven
// Created   : 2008-06-09
// Modified  : 
// Revision  : V0.0
//******************************************************************************

#include "app.h"

//local function(s) declaration
INT16U swGet_Key1(void);
INT16U swGet_Key2(void);
INT16U swGet_Key3(void);
INT16U swGet_Key4(void);

//***************************************************************
// Function    : LED_OFF
// Input       : Index - LED Index,from 1 to 4
// Output      : none
// Description : Turn off specific LED indexed by Index variable
//***************************************************************
void LED_OFF(INT8U Index)
{
    PORTD |= (1 << Index);
}

//***************************************************************
// Function    : LED_ON
// Input       : Index - LED Index,from 1 to 4
// Output      : none
// Description : Turn on specific LED indexed by Index variable
//***************************************************************
void LED_ON(INT8U Index)
{
    PORTD &= ~(1 << Index);
}

//***************************************************************
// Function    : LED_ON
// Input       : Index - LED Index,from 1 to 4
// Output      : none
// Description : Turn on specific LED indexed by Index variable
//***************************************************************
void LED_TOGGLE(INT8U Index)
{
    static INT8U bLED1_Status = cOFF_Status;
    static INT8U bLED2_Status = cOFF_Status;
    static INT8U bLED3_Status = cOFF_Status;
    static INT8U bLED4_Status = cOFF_Status;
    switch(Index)
    {
        case cLED1 :
        {
            if(bLED1_Status == cOFF_Status)
            {
                LED_ON(cLED1);
                bLED1_Status = cON_Status;
            }
            else if(bLED1_Status = cON_Status)
            {
                LED_OFF(cLED1);
                bLED1_Status = cOFF_Status;
            }
            break;
        }
        case cLED2 :
        {
            if(bLED2_Status == cOFF_Status)
            {
                LED_ON(cLED2);
                bLED2_Status = cON_Status;
            }
            else if(bLED2_Status = cON_Status)
            {
                LED_OFF(cLED2);
                bLED2_Status = cOFF_Status;
            }
            break;
        }
        case cLED3 :
        {
            if(bLED3_Status == cOFF_Status)
            {
                LED_ON(cLED3);
                bLED3_Status = cON_Status;
            }
            else if(bLED3_Status = cON_Status)
            {
                LED_OFF(cLED3);
                bLED3_Status = cOFF_Status;
            }
            break;
        }
        case cLED4 :
        {
            if(bLED4_Status == cOFF_Status)
            {
                LED_ON(cLED4);
                bLED4_Status = cON_Status;
            }
            else if(bLED4_Status = cON_Status)
            {
                LED_OFF(cLED4);
                bLED4_Status = cOFF_Status;
            }
            break;
        }
        default : break;
    }
}

//****************************************************************
// Function    : swGet_Key1
// Input       : none
// Output      : true - Key1 active; false - Key1 - passitive
// Description : Key1 Detection,rising edge active
//****************************************************************
INT16U swGet_Key1(void)
{
	static INT16U wKeyStatus = 1;
	if(wKeyStatus == 1)
	{
		if((PINB & (1 << cKey1)) == 0) //Key Pressed
		{
			wKeyStatus = 0;
		}
	}
	else if(wKeyStatus == 0)
	{
		if(PINB & (1 << cKey1))       //Key Released
		{
			wKeyStatus = 1;
			return(true);
		}
	}
	return(false);
}

//******************************************************************************
// Function    : swGet_Key2
// Input       : none
// Output      : true - Key2 active; false - Key2 - passitive
// Description : Key1 Detection,rising edge active
//******************************************************************************
INT16U swGet_Key2(void)
{
	static INT16U wKeyStatus = 1;
	if(wKeyStatus == 1)
	{
		if((PINB & (1 << cKey2)) == 0) //Key Pressed
		{
			wKeyStatus = 0;
		}
	}
	else if(wKeyStatus == 0)
	{
		if(PINB & (1 << cKey2))       //Key Released
		{
			wKeyStatus = 1;
			return(true);
		}
	}
	return(false);
}

//******************************************************************************
// Function    : swGet_Key3
// Input       : none
// Output      : true - Key3 active; false - Key3 - passitive
// Description : Key3 Detection,rising edge active
//******************************************************************************
INT16U swGet_Key3(void)
{
	static INT16U wKeyStatus = 1;
	if(wKeyStatus == 1)
	{
		if((PINB & (1 << cKey3)) == 0) //Key Pressed
		{
			wKeyStatus = 0;
		}
	}
	else if(wKeyStatus == 0)
	{
		if(PINB & (1 << cKey3))       //Key Released
		{
			wKeyStatus = 1;
			return(true);
		}
	}
	return(false);
}

//******************************************************************************
// Function    : swGet_Key4
// Input       : none
// Output      : true - Key4 active; false - Key4 - passitive
// Description : Key4 Detection,rising edge active
//******************************************************************************
INT16U swGet_Key4(void)
{
	static INT16U wKeyStatus = 1;
	if(wKeyStatus == 1)
	{
		if((PINB & (1 << cKey4)) == 0) //Key Pressed
		{
			wKeyStatus = 0;
		}
	}
	else if(wKeyStatus == 0)
	{
		if(PINB & (1 << cKey4))       //Key Released
		{
			wKeyStatus = 1;
			return(true);
		}
	}
	return(false);
}

//******************************************************************************
// Function    : Key_Detection
// Input       : none
// Output      : none
// Description : Keys Detection.if active,send Key Event to task
//******************************************************************************
void Key_Detection(void)
{
	if(swGet_Key1() == true)
	{
		OS_Event_Post(cPrioTask1,eTask1_Key);
	}
	if(swGet_Key2() == true)
	{
		OS_Event_Post(cPrioTask2,eTask2_Key);
	}
	if(swGet_Key3() == true)
	{
		OS_Event_Post(cPrioTask3,eTask3_Key);
	}
	if(swGet_Key4() == true)
	{
		OS_Event_Post(cPrioTask4,eTask4_Key);
	}
}

//===============================END OF FILE==================================//

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -