📄 f930_ports_switchled.c
字号:
//-----------------------------------------------------------------------------
// F93x_Ports_SwitchLED.c
//-----------------------------------------------------------------------------
// Copyright 2007 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This program demonstrates how to configure port pins as digital inputs
// and outputs. The C8051F930 target board has two push-button switches
// and two LEDs connected to port pins. The program constantly checks the
// status of the switches and if one or both are closed, it turns off
// one or both LEDs, respectively.
//
//
// How To Test:
//
// 1) Download code to a 'F930 target board
// 2) Ensure shorting block are installed on the first 8 pins of the
// J8 header.
// 3) Push the button (P0.2 or P0.3) and see that the corresponding
// LED (Red or Yellow) turns on.
//
//
// Target: C8051F930
// Tool chain: Generic
// Command Line: None
//
// Release 1.0
// -Initial Revision (FB)
// -12 AUG 2007
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <compiler_defs.h>
#include <C8051F930_defs.h> // SFR declarations
//-----------------------------------------------------------------------------
// Pin Declarations
//-----------------------------------------------------------------------------
SBIT (RED_LED, SFR_P1, 5); // '0' means ON, '1' means OFF
SBIT (YELLOW_LED, SFR_P1, 6); // '0' means ON, '1' means OFF
SBIT (SW2, SFR_P0, 2); // SW2 == 0 means switch pressed
SBIT (SW3, SFR_P0, 3); // SW3 == 0 means switch pressed
//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------
#define SYSCLK 20000000 // Clock speed in Hz
#define LED_ON 0
#define LED_OFF 1
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void);
void PORT_Init (void);
//-----------------------------------------------------------------------------
// main() Routine
//-----------------------------------------------------------------------------
void main (void)
{
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer
// enable)
PORT_Init(); // Initialize Port I/O
OSCILLATOR_Init (); // Initialize Oscillator
while (1)
{
// Set the state of the red LED
if (SW2 == 0) // If switch pressed
{
RED_LED = LED_OFF; // Turn off Red LED
}
else
{
RED_LED = LED_ON; // Else, turn it on
}
// Set the state of the yellow LED
if (SW3 == 0) // If switch pressed
{
YELLOW_LED = LED_OFF; // Turn off Yellow LED
}
else
{
YELLOW_LED = LED_ON; // Else, turn it on
}
} // end of while(1)
} // end of main()
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// OSCILLATOR_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function initializes the system clock to use the internal low power
// oscillator at its maximum frequency.
//
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void)
{
CLKSEL = 0x04; // Select low power oscillator with a
// clock divider value of 1 (20MHz)
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function configures the crossbar and ports pins.
//
// To configure a pin as a digital input, the pin is configured as digital
// and open-drain and the port latch should be set to a '1'. The weak-pullups
// are used to pull the pins high. Pressing the switch pulls the pins low.
//
// To configure a pin as a digital output, the pin is configured as digital
// and push-pull.
//
// An output pin can also be configured to be an open-drain output if system
// requires it. For example, if the pin is an output on a multi-device bus,
// it will probably be configured as an open-drain output instead of a
// push-pull output. For the purposes of this example, the output pins are
// configured as push-pull outputs because the pins are only connected to LEDs.
//
// P0.2 digital open-drain Switch 2
// P0.3 digital open-drain Switch 3
// P1.5 digital push-pull Red LED
// P1.6 digital push-pull Yellow LED
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
P0MDIN |= 0x0C; // P0.2, P0.3 are digital
P1MDIN |= 0x60; // P1.5, P1.6 are digital
P0MDOUT &= ~0x0C; // P0.2, P0.3 are open-drain
P1MDOUT |= 0x60; // P1.5, P1.6 are push-pull
P0 |= 0x0C; // Set P0.2, P0.3 latches to '1'
XBR2 = 0x40; // Enable crossbar and enable
// weak pull-ups
}
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -