Sunday, 10 August 2014

Interfacing a light sensor module to the LPC810

In the previous posts we looked at what could be done with NXP's LPC810 having only the UART connected. In this post let us connect a simple light sensor module like this one:



Any similar sensor module could be used they are available as magnetic reed switch, IR proximity detector ect. They all work the same way and use the trusty LM393 comparator to provide a on/off value on the data line D0 in this case when the light is above or below a certain threshold that can be adjusted by the potentiometer seen in blue.

It is wired up as so with Pin 3 (pio_3) connected to the DO line of the light sensor module.


We replace the main.c built up in the previous posts with a simplified one which simply outputs are 0 or 1 when the light level goes above or below the required threshold:-



#include "LPC8xx.h"

#define BAUDRATE 57600
#define UART_CFG_DATALEN_8      (0x01 << 2)  /* UART 8 bit length mode */
#define UART_CFG_PARITY_NONE    (0x00 << 4)  /* No parity */
#define UART_CFG_STOPLEN_1      (0x00 << 6)  /* UART One Stop Bit Select */
#define UART_STAT_RXRDY         (0x01 << 0)  /* Receiver ready */
#define UART_STAT_TXRDY         (0x01 << 2)  /* Transmitter ready for data */
#define UART_CFG_ENABLE         (0x01 << 0)

uint32_t SystemCoreClock = 12000000; /* Value @ Reset */
uint32_t SystemMainClock = 12000000; /* Value @ Reset */

void uart0putc(unsigned char c) {
 /* Wait until we're ready to send */
 while (!(LPC_USART0->STAT & UART_STAT_TXRDY));
 LPC_USART0->TXDATA = c;
}

int main(void) {
 /* Enable AHB clock to the Switch Matrix , UART0 , GPIO */
 LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 7) | (1 << 14) | (1 << 6);

 /* Configure Switch Matrix so that Pin2 = U0_TXD and Pin8 = U0_RXD */
 LPC_SWM->PINASSIGN0 = 0xffff0004UL;

 /* Set the UART clock divisor to 1  */
 LPC_SYSCON->UARTCLKDIV = 1; // note on reset the value is 0. i.e clock is disabled
 /* Configure UART0 baud rate */
 LPC_USART0->BRG = ((SystemMainClock / LPC_SYSCON->UARTCLKDIV) >> 4)
   / BAUDRATE - 1;
 LPC_SYSCON->UARTFRGMULT =
   ((((SystemMainClock / LPC_SYSCON->UARTCLKDIV) >> 4) * (0x100))
     / (BAUDRATE * (LPC_USART0->BRG + 1))) - (0x100);
 LPC_SYSCON->UARTFRGDIV = 0xFF; //note on reset the value is 0

 /* Set UART0 to 8N1 and finally enable */
 LPC_USART0->CFG = UART_CFG_DATALEN_8 | UART_CFG_PARITY_NONE
   | UART_CFG_STOPLEN_1 | UART_CFG_ENABLE;

 /* Output changes to pio_03 over the uart as zeros and ones */
 char old_v = -1;
 while (1) {
  char v = LPC_GPIO_PORT->B0[3];
  if (v!=old_v){
   uart0putc(v+'0');
   uart0putc('\n');
   uart0putc('\r');
   old_v = v;
  }
 }
 return 0;
}


We enable the Clock to the GPIO peripheral in SYSAHBCLKCTRL , the GPIO's clock is actually enabled just after reset so this is not strictly necessary but makes the code a clearer at no extra cost.

Also after reset all Pins are set to GPIO and the pin direction is as input so we need to do nothing with the switch matrix or the GPIO direction register to attach a simple sensor.

The GPIO's input pin value is read by the line

char v = LPC_GPIO_PORT->B0[3];

which reads the pin value as a byte value which can be either 0 or 1

No comments:

Post a Comment