Content originally posted in LPCWare by cthvx8@hotmail.com on Tue Mar 10 06:25:24 MST 2015
I need some help figuring out why my ADC is not working properly. I copied the code for the conversion from the periph_adc example. My code is supposed to read from PIO0_11, divide the value by 50, and then output the value to 8 pins which are connected to LEDs and are functioning as my display using the little function I named "bytewriter". I have verified that my 8 bit "display" is working properly. I am using the LPC1347 board with usb.
I am assuming that the board is providing the VREFN to VREFP/(V0 to Vdd) because the only power/ground pins on the board already have the correct voltage. Is this an incorrect assumption?
The current behavior of my code is that it alternately displays values of 0 and 81 to my output pins. 81 is 4095/50 so it seems to sometimes read high and sometimes read low. Pin PIO0_11 is currently connected to 0.3V which is being supplied by a voltage regulator and resistor voltage divider. I checked to make sure that this voltage was holding steady during operation.
#if defined (__USE_LPCOPEN)
#if defined(NO_BOARD_LIB)
#include "chip.h"
#else
#include "board.h"
#endif
#endif
#include <cr_section_macros.h>
// TODO: insert other include files here
// TODO: insert other definitions and declarations here
#ifdef __cplusplus
extern "C" {
#endif
#define _LPC_ADC_ID LPC_ADC
volatile uint32_t msTicks=0;
static uint32_t rawTemp=0;
static ADC_CLOCK_SETUP_T ADCSetup;
// Systick Handler which is called each ms
void SysTick_Handler(void)
{
msTicks++;
}
// blocks for dlyTicks ms...
static void Delay(uint32_t dlyTicks)
{
uint32_t curTicks;
curTicks = msTicks;
while ((msTicks - curTicks) < dlyTicks);
}
}
static void bytewriter(uint32_t bytevalue)
{
Chip_GPIO_SetPortMask(LPC_GPIO_PORT, 0, 0xFFFFFFF0);
Chip_GPIO_SetMaskedPortValue(LPC_GPIO_PORT, 0, bytevalue);
Chip_GPIO_SetPortMask(LPC_GPIO_PORT, 1, 0xFFFE1FFF);
bytevalue <<=9;
Chip_GPIO_SetMaskedPortValue(LPC_GPIO_PORT, 1, bytevalue);
}
//------------------------------------------------------------------------------------
int main(void) {
#if defined (__USE_LPCOPEN)
#if !defined(NO_BOARD_LIB)
// Read clock settings and update SystemCoreClock variable
SystemCoreClockUpdate();
// Set up and initialize all required blocks and
// functions related to the board hardware
Board_Init();
Board_ADC_Init();
// Set the LED to the state of "On"
Board_LED_Set(0, true);
#endif
#endif
// TODO: insert code here
//Initialize Port GPIO
[/color]Chip_GPIO_Init(LPC_GPIO_PORT);
Chip_ADC_Init(_LPC_ADC_ID, &ADCSetup);
Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 0, (IOCON_FUNC1));
Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 1, (IOCON_FUNC0));
Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 2, (IOCON_FUNC0));
Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 3, (IOCON_FUNC0));
Chip_IOCON_PinMuxSet(LPC_IOCON, 1, 13, (IOCON_FUNC0));
Chip_IOCON_PinMuxSet(LPC_IOCON, 1, 14, (IOCON_FUNC0));
Chip_IOCON_PinMuxSet(LPC_IOCON, 1, 15, (IOCON_FUNC0));
Chip_IOCON_PinMuxSet(LPC_IOCON, 1, 16, (IOCON_FUNC0));
Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 11, (IOCON_FUNC2)|(IOCON_ADMODE_EN));
//Set Port 0 pins 0-3 and Port 1 pins 13-16 to output
Chip_GPIO_SetPortDIROutput(LPC_GPIO_PORT, 0, 0x0F);
Chip_GPIO_SetPortDIROutput(LPC_GPIO_PORT, 1, 0x1E000);
SysTick_Config(SystemCoreClock/1000);
while(1){
//Clear port bits
Chip_GPIO_ClearValue(LPC_GPIO_PORT,0,0xF);
Chip_GPIO_ClearValue(LPC_GPIO_PORT,1,0x1E000);
uint16_t dataADC=0;
rawTemp=0;
//Read value from ADC Pin11
dataADC=0;
Chip_ADC_EnableChannel(_LPC_ADC_ID, ADC_CH0, ENABLE);
Chip_ADC_SetStartMode(_LPC_ADC_ID, ADC_START_NOW, ADC_TRIGGERMODE_RISING);
while (Chip_ADC_ReadStatus(_LPC_ADC_ID, ADC_CH0, ADC_DR_DONE_STAT) != SET){}
Chip_ADC_ReadValue(_LPC_ADC_ID, ADC_CH0, &dataADC);
//Convert ADC data to value small enough to display on 8 bits
rawTemp = dataADC/50;
//Set 8 bits to rawTemp value
bytewriter(rawTemp);
Board_LED_Set(0, true);
//Wait 1 second
Delay(1000);
Board_LED_Set(0, false);
//Wait 1 second
Delay(1000);
}
// Force the counter to be placed into memory
volatile static int i = 0 ;
// Enter an infinite loop, just incrementing a counter
while(1) {
i++ ;
}
return 0 ;
}