#include <c8051f340.h>
sbit S1=P2^4;
sbit S2=P2^5;
sbit S3=P2^6;
sbit S4=P2^7;
sbit D1=P2^0;
sbit D2=P2^1;
sbit D3=P2^2;
sbit D4=P2^3;
void OSCILLATOR_Init (void);
void PORT_Init (void);
void main (void)
{
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer
// enable)
PORT_Init(); // Initialize Port I/O
OSCILLATOR_Init (); // Initialize Oscillator
while (1)
{
if (S1 == 0) // If switch depressed
{
D1 = 1; // Turn on LED
}
else
{
D1 = 0; // Else, turn it off
}
if (S2 == 0) // If switch depressed
{
D2 = 1; // Turn on LED
}
else
{
D2 = 0; // Else, turn it off
}
if (S3 == 0) // If switch depressed
{
D3 = 1; // Turn on LED
}
else
{
D3 = 0; // Else, turn it off
}
if (S4 == 0) // If switch depressed
{
D4 = 1; // Turn on LED
}
else
{
D4 = 0; // Else, turn it off
}
} // end of while(1)
} // end of main()
void OSCILLATOR_Init (void)
{
OSCICN |= 0x03; // Configure internal oscillator for
// its maximum frequency (24.5 Mhz)
}
void PORT_Init (void)
{
P2MDIN |= 0xF0; // Lower four pins on P2 are digital
P2MDOUT = 0x0F; // enable LEDs as push-pull outputs
// enable Switches as open-drain
P2 |= 0xF0; // Set port latches for P2.0
// and P2.1 to '1'
XBR1 = 0x40; // Enable crossbar and enable
// weak pull-ups
}
|