ledswitch.c

来自「winarm for lpc2106 blink switch」· C语言 代码 · 共 70 行

C
70
字号
//////////////////////////////////////////////////////////////////////////////
//
//             Philips LPC210X LED/Switch Example
//               for the Olimex LPC-P2106 board
//
// This example demonstrates writing to and reading from 
// the GPIO port.
// (1) flash the LED 10 times
// (2) wait for key-press, turn off LED of key is pressed
//
// by Martin THOMAS, Kaiserslautern, Germany
// (eversmith@heizung-thomas.de)
//
// based on an example by Rowley Associates Limited found somewhere
// in the "net"
//
// (all compiler optimizations should be disabled - see makefile)
//////////////////////////////////////////////////////////////////////////////

#include "lpc210x.h"

// olimex LPC-P2106: one led on P0.7 (active low)
#define LEDPIN  7
// olimex LPC-P2106: one switch on P0.31 (active low)
#define SWPIN 	31

static void ledInit()
{
	IODIR |= (1<<LEDPIN);	// define LED-Pin as output
	IODIR &= ~(1<<SWPIN);	// define Switch-Pin as input
	IOSET = (1<<LEDPIN);	// set Bit = off (active low)
}

void delay(void )
{
	int i,j;

	for (i=0;i<100;i++)
		for (j=0;j<1000;j++);
}
 
int main(void)
{
	int i;
	
	MAMCR = 2;	// MAM functions fully enabled (?)
	
	ledInit();
	
	i=0;
	while (i<10)	
	{
		IOCLR=(1<<LEDPIN);	// set all outputs in mask to 0
		delay();
		IOSET=(1<<LEDPIN);	// set all outputs in mask to 1
		delay();
		i++;
	}
	
	while (1)	
	{
		if (IOPIN & (1<<SWPIN))
			IOCLR=(1<<LEDPIN);	// key pressed (active low) -> LED off
		else
			IOSET=(1<<LEDPIN);	// key released -> LED on
	}
	
	return 0;
}

⌨️ 快捷键说明

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