#ifdef __USE_CMSIS
#include "LPC8xx.h"
#endif
#include <cr_section_macros.h>
#include <NXP/crp.h>
// Variable to store CRP value in. Will be placed automatically
// by the linker when "Enable Code Read Protect" selected.
// See crp.h header for more information
__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;
void GPIOSetDir( uint32_t portNum, uint32_t bitPosi, uint32_t dir )
{
if( dir )
{
LPC_GPIO_PORT->DIR0 |= (1<<bitPosi);
}
else
{
LPC_GPIO_PORT->DIR0 &= ~(1<<bitPosi);
}
return;
}
void GPIOSetBitValue( uint32_t portNum, uint32_t bitPosi, uint32_t bitVal )
{
if ( bitVal )
{
LPC_GPIO_PORT->SET0 = 1<<bitPosi;
}
else
{
LPC_GPIO_PORT->CLR0 = 1<<bitPosi;
}
return;
}
void SwitchMatrix_Init()
{
/* I decided to put:
* pin8 = PIO_11 = UART0_RXD
* pin9 = PIO_10 = UART0_TXD
* pin12 = PIO_1 = SPI0_SCK
* pin13 = PIO_9 = SPIO_SSEL
* pin18 = PIO_6 = SPIO_MISO
* pin19 = PIO_0 = ACOMP_I0 (variable resistor)
* pin20 = PIO_14 = SPI0_MOSI
*/
/* Enable SWM (switch matrix) clock */
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7);
/* Pin Assign 8 bit Configuration */
/* U0_TXD */
/* U0_RXD */
LPC_SWM->PINASSIGN0 = 0xffff0b0aUL;
/* SPI0_SCK */
LPC_SWM->PINASSIGN3 = 0x01ffffffUL;
/* SPI0_MOSI */
/* SPI0_MISO */
/* SPI0_SSEL */
LPC_SWM->PINASSIGN4 = 0xff09060eUL;
/* Pin Assign 1 bit Configuration */
/* ACMP_I1 */
/* SWCLK */
/* SWDIO */
/* RESET */
LPC_SWM->PINENABLE0 &= ~(1<<0); // clear the ACOMP_I1 bit to 0 to enable it
/* SWM clock no longer needed now it's configured */
LPC_SYSCON->SYSAHBCLKCTRL &= ~(1<<7);
}
int readComparator(void) {
int i, prev_output;
LPC_CMP->LAD = 0x1; // enable with ladder = 0
prev_output = LPC_CMP->CTRL & (1<<21);
for(i=0; i < (0x20<<1); i += 2){
LPC_CMP->LAD = 0x1 | i; // enable and set ladder voltage
if ((LPC_CMP->CTRL & (1<<21)) != prev_output) {
return (i>>1);
}
}
return (i>>1);
}
int main(void) {
volatile static int i = 0, j, k;
/* Enable AHB clock to the GPIO domain. */
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<6);
/* Peripheral reset control to GPIO and GPIO INT, a "1" bring it out of reset. */
LPC_SYSCON->PRESETCTRL &= ~(0x1<<10);
LPC_SYSCON->PRESETCTRL |= (0x1<<10);
GPIOSetDir(0, 7, 1); // RED
GPIOSetDir(0, 16, 1); // BLUE
GPIOSetDir(0, 17, 1); // GREEN
/* Comparator should be powered up first. Use of comparator requires BOD */
/* NB: apart from the library code I cannot find this BOD requirement anywhere */
LPC_SYSCON->PDRUNCFG &= ~( (0x1 << 15) | (0x1 << 3) );
/* Enable AHB clock to the Comparator. */
LPC_SYSCON->SYSAHBCLKCTRL |= (0x1 << 19);
/* Peripheral reset control to Comparator, a "1" bring it out of reset. */
LPC_SYSCON->PRESETCTRL &= ~(0x1 << 12);
LPC_SYSCON->PRESETCTRL |= (0x1 << 12);
/* switch off the pull-up on the pin that will be used for comp input */
LPC_IOCON->PIO0_0 &= ~(0x3 << 3);
/* connect voltage ladder to one input and ACOMP_I1 to the other input */
LPC_CMP->CTRL |= (1 << 8); // +ve = ACOMP_I1 and default is -ve = ladder
SwitchMatrix_Init();
// Enter an infinite loop, just incrementing a counter
while(1) {
if (i == 0) {
GPIOSetBitValue( 0, 7, 0 );
GPIOSetBitValue( 0, 16, 1 );
GPIOSetBitValue( 0, 17, 1 );
}
else if (i == 1) {
GPIOSetBitValue( 0, 7, 1 );
GPIOSetBitValue( 0, 16, 0 );
GPIOSetBitValue( 0, 17, 1 );
}
else if (i == 2) {
GPIOSetBitValue( 0, 7, 1 );
GPIOSetBitValue( 0, 16, 1 );
GPIOSetBitValue( 0, 17, 0 );
}
i++ ;
if (i >= 3) {
i = 0;
}
k = readComparator();
for (j=0; j < (10000 * k); j++);
}
return 0 ;
}
|