Ashok R
Published

NFC Controlled RGB LED

This project demonstrates the controlling of an RGB LED over NFC by smartphone.

BeginnerShowcase (no instructions)8 hours1,806
NFC Controlled RGB LED

Things used in this project

Hardware components

NXP LPC812 MAX board
×1
NXP NTAG I2C Board
×1
Android device
Android device
×1

Software apps and online services

Android Studio
Android Studio

Story

Read more

Schematics

RGB LED connections

NTAG I2C board connection

Code

Main

C/C++
/*
 * @brief Ntag LED example using SysTick and I2C
 *
 * @note
 * Copyright(C) Ashok, NXP Semiconductors, 2013
 * All rights reserved.
 *
 */

#include "board.h"
#include <stdio.h>

/*****************************************************************************
 * Private types/enumerations/variables
 ****************************************************************************/

#define TICKRATE_HZ (160000)	/* 10 ticks per second */


/* I2CM transfer record */
static I2CM_XFER_T  i2cmXferRec;
/* System clock is set to 24MHz, I2C clock is set to 600kHz */
#define I2C_CLK_DIVIDER         (40)
/* 100KHz I2C bit-rate */
#define I2C_BITRATE             (100000)

/* 7-bit I2C addresses of I/O expander */
/* Note: The ROM code requires the address to be between bits [6:0]
         bit 7 is ignored */

#define NTAG_SLAVE_ADDRESS (0x55U)

#define RED				0x00
#define BLUE	        0x01
#define GREEN	        0x02



/*****************************************************************************
 * Public types/enumerations/variables
 ****************************************************************************/

/*****************************************************************************
 * Private functions
 ****************************************************************************/

/*****************************************************************************
 * Public functions
 ****************************************************************************/

struct Channel{

	volatile uint8_t ONTime;
	volatile int8_t Offset;
	volatile uint8_t Enabled;
}r,g,b;

/*variables */
volatile uint32_t msTicks=0;
volatile uint32_t readTaskTicks=0;
volatile uint8_t RxData[4];
volatile uint8_t led_EN=0;

static uint8_t txData[16];
static uint8_t rxData[16];

/*****************************************************************************
 * Public types/enumerations/variables
 ****************************************************************************/

/*****************************************************************************
 * Private functions
 ****************************************************************************/

/* Initializes pin muxing for I2C interface */
static void Init_I2C_PinMux(void)
{
#if (defined(BOARD_NXP_LPCXPRESSO_812) || defined(BOARD_LPC812MAX) || defined(BOARD_NXP_LPCXPRESSO_824))
	/* Enable the clock to the Switch Matrix */
	Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SWM);
#if defined(BOARD_NXP_LPCXPRESSO_824)
	Chip_SWM_EnableFixedPin(SWM_FIXED_I2C0_SDA);
	Chip_SWM_EnableFixedPin(SWM_FIXED_I2C0_SCL);
#else
	/* Connect the I2C_SDA and I2C_SCL signals to port pins(P0.10, P0.11) */
	Chip_SWM_MovablePinAssign(SWM_I2C_SDA_IO, 10);
	Chip_SWM_MovablePinAssign(SWM_I2C_SCL_IO, 11);
#endif

	/* Enable Fast Mode Plus for I2C pins */
	Chip_IOCON_PinSetI2CMode(LPC_IOCON, IOCON_PIO10, PIN_I2CMODE_FASTPLUS);
	Chip_IOCON_PinSetI2CMode(LPC_IOCON, IOCON_PIO11, PIN_I2CMODE_FASTPLUS);

	/* Disable the clock to the Switch Matrix to save power */
	Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_SWM);
#else
	/* Configure your own I2C pin muxing here if needed */
#warning "No I2C pin muxing defined"
#endif
}

/* Setup I2C handle and parameters */
static void setupI2CMaster()
{
	/* Enable I2C clock and reset I2C peripheral */
	Chip_I2C_Init(LPC_I2C);

	/* Setup clock rate for I2C */
	Chip_I2C_SetClockDiv(LPC_I2C, I2C_CLK_DIVIDER);

	/* Setup I2CM transfer rate */
	Chip_I2CM_SetBusSpeed(LPC_I2C, I2C_BITRATE);

	/* Enable Master Mode */
	Chip_I2CM_Enable(LPC_I2C);
}

/* Function to setup and execute I2C transfer request */
static void SetupXferRecAndExecute(uint8_t devAddr,
								   uint8_t *txBuffPtr,
								   uint16_t txSize,
								   uint8_t *rxBuffPtr,
								   uint16_t rxSize)
{
	/* Setup I2C transfer record */
	i2cmXferRec.slaveAddr = devAddr;
	i2cmXferRec.status = 0;
	i2cmXferRec.txSz = txSize;
	i2cmXferRec.rxSz = rxSize;
	i2cmXferRec.txBuff = txBuffPtr;
	i2cmXferRec.rxBuff = rxBuffPtr;

	Chip_I2CM_XferBlocking(LPC_I2C, &i2cmXferRec);
}



/* Function to Read NTAG*/
static void NTAG_Read()
{
	int index = 0;

	txData[index++] = (uint8_t) 0x01;			/* I2C device regAddr for NTAG*/

	SetupXferRecAndExecute(NTAG_SLAVE_ADDRESS, txData, index, rxData, 4);
}

static void Init_LED_Cal(){

	r.Offset = 0;
	g.Offset = 0;
	b.Offset = 0;

	r.ONTime = 0xFF;
	g.ONTime = 0xFF;
	b.ONTime = 0xFF;


}

/**
 * @brief	Handle interrupt from SysTick timer
 * @return	Nothing
 */
void SysTick_Handler(void)
{
	msTicks++;

	if(readTaskTicks > 500)
	{

		NTAG_Read();

		readTaskTicks=0;

	}


}

/**
 * @brief	main routine for blinky example
 * @return	Function should not exit.
 */
int main(void)
{
	SystemCoreClockUpdate();
	Board_Init();

	Board_LED_Set(0, false);

	/* Setup I2C pin muxing */
	Init_I2C_PinMux();

	/* Allocate I2C handle, setup I2C rate, and initialize I2C clocking */
	setupI2CMaster();

	/* Disable the interrupt for the I2C */
	NVIC_DisableIRQ(I2C_IRQn);

	/* Enable SysTick Timer */
	SysTick_Config(SystemCoreClock / TICKRATE_HZ);

	/* Load Initial values for RGB LED */
	Init_LED_Cal();


	/* Loop forever */
	while (1) {



	if(msTicks >= 255){


		readTaskTicks++;
		r.ONTime = rxData[1]+r.Offset;
		g.ONTime = rxData[2]+g.Offset;
		b.ONTime = rxData[3]+b.Offset;
		led_EN = rxData[0];

		if(led_EN>0){

			Board_LED_Set(RED, true);
			Board_LED_Set(BLUE, true);
			Board_LED_Set(GREEN, true);
			r.Enabled = true;
			g.Enabled = true;
			b.Enabled = true;
		}
		msTicks = 0;

	 }
	else if((msTicks >= r.ONTime) && r.Enabled){

		Board_LED_Set(RED, false);
		r.Enabled = false;

	}
	else if((msTicks >= g.ONTime) && g.Enabled){

		Board_LED_Set(BLUE, false);
		g.Enabled = false;

	}
	else if((msTicks >= b.ONTime ) && b.Enabled){

		Board_LED_Set(GREEN, false);
		b.Enabled = false;

	}


	//	__WFI();
	}
}

Credits

Ashok R

Ashok R

37 projects • 102 followers
Hobbyist/Engineer/Director/Animatior

Comments