📄 ledbutton.c
字号:
/*********************************************************************************
Copyright(c) 2005 Analog Devices, Inc. All Rights Reserved.
This software is proprietary and confidential. By using this software you agree
to the terms of the associated Analog Devices License Agreement.
$RCSfile: LEDButton.c,v $
$Revision: 1.1 $
$Date: 2007/03/28 17:55:35 $
Description:
This is a little demo to show how to use the EZ-Kit utilities for the
LEDs and push buttons.
On the ADSP-BF533 EZ-Kit, the push buttons on the EZ-Kit must be
enabled by having switches 1 through 4 turned on on the SW9 DIP switch
on the board.
On the ADSP-BF537 EZ-Kit, the push buttons on the EZ-Kit must be
enabled by having switches 1 through 4 turned on on the SW5 DIP switch
on the board.
On the ADSP-BF561 EZ-Kit, the push buttons on the EZ-Kit must be
enabled by having switches 1 through 4 turned on on the SW4 DIP switch
on the board.
*********************************************************************************/
/*********************************************************************
Include files
*********************************************************************/
#include "services\services.h" // system services
#include "ezkitutilities.h" // ezkit utilities
/*********************************************************************
Globals
*********************************************************************/
u32 Debounce_Time = 500; //value in ms
/*********************************************************************
Static data
The LEDs and pushbuttons interface with the processor via the programmable
flags. The EZ-Kit utilities utilize the flag service to control the
LEDs and pushbuttons. Because the demo uses interrupts for the
pushbuttons, we need to give the flag service memory to control the
interrupts, and the interrupt manager memory for secondary handlers. Note
that if the flags were using separate IVG levels, we wouldn't need to
provide the interrupt service with memory. But because all the flag
interrupts are ganged up on a single IVG, we need to supply memory for
the secondary handlers (note that one handler is installed as the primary
and the rest are secondary so we need 1 less secondary handler).
*********************************************************************/
// flag service
static u8 FlagServiceData[ADI_FLAG_CALLBACK_MEMORY * (EZ_LAST_BUTTON - EZ_FIRST_BUTTON + 1)];
// interrupt service
static u8 IntMgrData[ADI_INT_SECONDARY_MEMORY * (EZ_LAST_BUTTON - EZ_FIRST_BUTTON)];
/*********************************************************************
Peripheral ID Macros
This program works on the EZ-Kits. The macros
below are used to identify which EZ-Kit we're targeting. Specifically,
the FLAG_PERIPHERAL_ID macro is set to the peripheral ID to which the
interrupt driven push buttons are mapped. See the adi_int.h file
within the system services library (blackfin/include/services) for
more information on peripheral IDs.
*********************************************************************/
#if defined(__ADSP_EDINBURGH__)
#define FLAG_PERIPHERAL_ID (ADI_INT_PFA)
#endif
#if defined(__ADSP_BRAEMAR__)
#define FLAG_PERIPHERAL_ID (ADI_INT_PORTFG_A)
#endif
#if defined(__ADSP_TETON__)
#define FLAG_PERIPHERAL_ID (ADI_INT_PF0_15_A)
#endif
/*********************************************************************
Function: ButtonCallback
Description: This function is a callback used to process the
push buttons on the EZ-Kit when they are generating
interrupts.
See the appropriate section in main() for how to set
up interrupt driven push buttons.
This function lights the LED corresponding to the button
that was pushed.
*********************************************************************/
static void ButtonCallback( // call back function specific for UART0
void *ClientHandle,
u32 Event,
void *pArg)
{
u32 Button; // button that was pushed
// get the button that was pushed
Button = (u32)ClientHandle;
// light only the LED corresponding to the button that was pressed
ezTurnOffAllLEDs();
ezTurnOnLED((Button - EZ_FIRST_BUTTON) + EZ_FIRST_LED);
// return
}
/*********************************************************************
Function: PolledPushButtons
Description: This function illustrates how to use the push buttons
on the EZ-Kit by polling them.
To use polling, all one has to do is initialize the
buttons which really just configures the Programmable
Flag registers of the processor.
This demo simply turns on the LED corresponding to the
button that was pressed. When the last button is
pressed, the demo moves on to the next part.
*********************************************************************/
void PolledPushButtons(void) {
// counter
volatile u32 i;
// turn off all LEDs
ezTurnOffAllLEDs();
// LOOP
while (1) {
// FOR (all buttons except the last one)
for (i = EZ_FIRST_BUTTON; i < EZ_LAST_BUTTON; i++) {
// IF (the button is pushed)
if (ezIsButtonPushed(i)) {
// clear the latch for the button
ezClearButton(i);
// turn off all LEDs except the one corresponding to the button that was pushed
ezTurnOffAllLEDs();
ezTurnOnLED((i - EZ_FIRST_BUTTON) + EZ_FIRST_LED);
// ENDIF
}
// ENDFOR
}
// IF (the last button is pushed)
if (ezIsButtonPushed(EZ_LAST_BUTTON)) {
// clear the latch for the last button and move on to the next part of the demo
ezClearButton(EZ_LAST_BUTTON);
break;
// ENDIF
}
// ENDLOOP
}
// turn off all LEDs
ezTurnOffAllLEDs();
ezDelay(Debounce_Time);
// return
}
/*********************************************************************
Function: InterruptDrivenPushButtons
Description: This function illustrates how to use the push buttons
on the EZ-Kit by using interrupts.
To use interrupt driven push buttons, the following
steps need performed:
o initialize the Interrupt Manager since we'll
be using interrupts
o initialize the push buttons (if not already initialized)
o hook an interrupt handler onto the IVG that the
programmable flags are configured to
o configure the SIC to pass the flag interrupts
to the CEC
o use the EZ-Kit utility to enable the push button
interrupts
The operation of this demo is identical to the polled
demo except we're using interrupts this time.
*********************************************************************/
void InterruptDrivenPushButtons(void) {
u32 IVG;
// counter
volatile u32 i;
// turn off all LEDs
ezTurnOffAllLEDs();
// install callbacks for each button
for (i = EZ_FIRST_BUTTON; i <= EZ_LAST_BUTTON; i++) {
ezClearButton(i);
adi_flag_InstallCallback(ezButtonToFlag[i], FLAG_PERIPHERAL_ID, ADI_FLAG_TRIGGER_RISING_EDGE, TRUE, (void*)i, NULL, ButtonCallback);
}
// LOOP
while (1) {
// all the processing is done in the callback function
// when the last is pushed we'll move on to the next part
// of the demo
// rather than do anything fancy to detect the last button, we'll just
// peek and see if the LED corresponding to the last button is lit
if (ezIsLEDOn(EZ_LAST_BUTTON - EZ_FIRST_BUTTON + EZ_FIRST_LED)) {
break;
}
// ENDLOOP
}
// remove callbacks for each push button
for (i = EZ_FIRST_BUTTON; i <= EZ_LAST_BUTTON; i++) {
adi_flag_RemoveCallback(ezButtonToFlag[i]);
}
// turn off all LEDs
ezTurnOffAllLEDs();
ezDelay(Debounce_Time);
// return
}
/*********************************************************************
Function: LEDs
Description: This function illustrates how to use the LED functions
in the EZ-Kit utilities.
This demo shows how to turn LED's on, off and cycle
through them. It also uses polled push buttons.
The demo first turns off all LED's. Then each time
the first button is pressed, the LED corresponding to the
first button is toggled. When the second button is
pressed, all LED's are cycled until either the first
button is pressed again or until the last button is
pressed, at which point we move on to the next part
of the demo.
*********************************************************************/
void LEDs(void) {
// counter
volatile u32 i;
// turn off all LEDs
ezTurnOffAllLEDs();
// LOOP
while (1) {
// close this demo when the last push button is pressed
if (ezIsButtonPushed(EZ_LAST_BUTTON)) {
ezClearButton(EZ_LAST_BUTTON); // clear the last button's latch
break;
}
// IF (first button is pressed)
if (ezIsButtonPushed(EZ_FIRST_BUTTON)) {
// clear the first button's latch
ezClearButton(EZ_FIRST_BUTTON);
// toggle the first LED
ezToggleLED(EZ_FIRST_LED);
// ENDIF
}
// IF (the second button is pressed)
if (ezIsButtonPushed(EZ_FIRST_BUTTON + 1)) {
// clear the second button's latch
ezClearButton(EZ_FIRST_BUTTON + 1);
// turn off all LEDs
ezTurnOffAllLEDs();
// LOOP
while (1) {
// cycle LEDs till some other button is pressed
ezCycleLEDs();
// delay so we can see the LED's cycle
for (i = 0; i < 3000000; i++) ;
// keep looping till the first or last button pressed
if (ezIsButtonPushed(EZ_FIRST_BUTTON) || ezIsButtonPushed(EZ_LAST_BUTTON)) break;
// ENDLOOP
}
// ENDIF
}
// ENDLOOP
}
// turn off all LEDs
ezTurnOffAllLEDs();
ezDelay(Debounce_Time);
// return
}
/*********************************************************************
Function: main
Description: This function is main function of the demo. It
doesn't do anything other than initialize flash
on the EZ-Kit and then call the demo subroutines.
In the unlikely event we get an error somewhere,
this demo uses the ezErrorCheck function to light
all the LEDs and spin in a loop.
*********************************************************************/
void main(void) {
u32 ResponseCount; // number of things a service can control
u32 i; //loop variable
// initialize the EZ-Kit
ezInit(1);
// initialize the Interrupt Manager
ezErrorCheck(adi_int_Init(IntMgrData, sizeof(IntMgrData), &ResponseCount, NULL));
// initialize the flag manager because the LEDs and buttons connect via flags
ezErrorCheck(adi_flag_Init(FlagServiceData, sizeof(FlagServiceData), &ResponseCount, NULL));
// initialize Buttons being used
for (i = EZ_FIRST_BUTTON; i < EZ_NUM_BUTTONS; i++){
ezInitButton(i);
}
// initialize LEDS being used
for (i = EZ_FIRST_LED; i < EZ_NUM_LEDS; i++){
ezInitLED(i);
}
// show how to use polled push buttons
PolledPushButtons();
// show how to use interrupt-driven push buttons
InterruptDrivenPushButtons();
// show how to use the LEDs
LEDs();
// terminate the flag service
adi_flag_Terminate();
// terminate the Interrupt Manager
adi_int_Terminate();
// that's all folks!
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -