uint16_t readAdc (const adc_t adcNumber) { uint16_t dataADC = 0; if ((adcNumber > 0) && (adcNumber < (sizeof (adc) / sizeof (adc_t)))) { /* Select using burst mode or not */ if (ADCSetup.burstMode) Chip_ADC_SetBurstCmd (_LPC_ADC_ID, ENABLE); else Chip_ADC_SetBurstCmd (_LPC_ADC_ID, DISABLE); /* Start A/D conversion if not using burst mode */ if (!ADCSetup.burstMode) Chip_ADC_SetStartMode (_LPC_ADC_ID, ADC_START_NOW, ADC_TRIGGERMODE_RISING); /* Waiting for A/D conversion complete */ while (Chip_ADC_ReadStatus (_LPC_ADC_ID, adc[adcNumber], ADC_DR_DONE_STAT) != SET); /* Read ADC value */ Chip_ADC_ReadValue (_LPC_ADC_ID, adc[adcNumber], &dataADC); /* Disable burst mode, if any */ if (ADCSetup.burstMode) Chip_ADC_SetBurstCmd (_LPC_ADC_ID, DISABLE); } return (dataADC); }
/* Polling routine for ADC example */ static void App_Polling_Test(void) { uint16_t dataADC; /* Select using burst mode or not */ if (Burst_Mode_Flag) { Chip_ADC_SetBurstCmd(_LPC_ADC_ID, ENABLE); } else { Chip_ADC_SetBurstCmd(_LPC_ADC_ID, DISABLE); } /* Get adc value until get 'x' character */ while (DEBUGIN() != 'x') { /* Start A/D conversion if not using burst mode */ if (!Burst_Mode_Flag) { Chip_ADC_SetStartMode(_LPC_ADC_ID, ADC_START_NOW, ADC_TRIGGERMODE_RISING); } /* Waiting for A/D conversion complete */ while (Chip_ADC_ReadStatus(_LPC_ADC_ID, _ADC_CHANNLE, ADC_DR_DONE_STAT) != SET) {} /* Read ADC value */ Chip_ADC_ReadValue(_LPC_ADC_ID, _ADC_CHANNLE, &dataADC); /* Print ADC value */ App_print_ADC_value(dataADC); } /* Disable burst mode, if any */ if (Burst_Mode_Flag) { Chip_ADC_SetBurstCmd(_LPC_ADC_ID, DISABLE); } }
/* Interrupt routine for ADC example */ static void App_Interrupt_Test(void) { /* Enable ADC Interrupt */ NVIC_EnableIRQ(_LPC_ADC_IRQ); Chip_ADC_Int_SetChannelCmd(_LPC_ADC_ID, _ADC_CHANNLE, ENABLE); /* Enable burst mode if any, the AD converter does repeated conversions at the rate selected by the CLKS field in burst mode automatically */ if (Burst_Mode_Flag) { Chip_ADC_SetBurstCmd(_LPC_ADC_ID, ENABLE); } Interrupt_Continue_Flag = 1; ADC_Interrupt_Done_Flag = 1; while (Interrupt_Continue_Flag) { if (!Burst_Mode_Flag && ADC_Interrupt_Done_Flag) { ADC_Interrupt_Done_Flag = 0; Chip_ADC_SetStartMode(_LPC_ADC_ID, ADC_START_NOW, ADC_TRIGGERMODE_RISING); } } /* Disable burst mode if any */ if (Burst_Mode_Flag) { Chip_ADC_SetBurstCmd(_LPC_ADC_ID, DISABLE); } /* Disable ADC interrupt */ NVIC_DisableIRQ(_LPC_ADC_IRQ); }
/* DMA routine for ADC example */ static void App_DMA_Test(void) { uint16_t dataADC; /* Initialize GPDMA controller */ Chip_GPDMA_Init(LPC_GPDMA); /* Setting GPDMA interrupt */ NVIC_DisableIRQ(DMA_IRQn); NVIC_SetPriority(DMA_IRQn, ((0x01 << 3) | 0x01)); NVIC_EnableIRQ(DMA_IRQn); /* Setting ADC interrupt, ADC Interrupt must be disable in DMA mode */ NVIC_DisableIRQ(_LPC_ADC_IRQ); Chip_ADC_Int_SetChannelCmd(_LPC_ADC_ID, _ADC_CHANNLE, ENABLE); /* Get the free channel for DMA transfer */ dmaChannelNum = Chip_GPDMA_GetFreeChannel(LPC_GPDMA, _GPDMA_CONN_ADC); /* Enable burst mode if any, the AD converter does repeated conversions at the rate selected by the CLKS field in burst mode automatically */ if (Burst_Mode_Flag) { Chip_ADC_SetBurstCmd(_LPC_ADC_ID, ENABLE); } /* Get adc value until get 'x' character */ while (DEBUGIN() != 'x') { /* Start A/D conversion if not using burst mode */ if (!Burst_Mode_Flag) { Chip_ADC_SetStartMode(_LPC_ADC_ID, ADC_START_NOW, ADC_TRIGGERMODE_RISING); } channelTC = 0; Chip_GPDMA_Transfer(LPC_GPDMA, dmaChannelNum, _GPDMA_CONN_ADC, (uint32_t) &DMAbuffer, GPDMA_TRANSFERTYPE_P2M_CONTROLLER_DMA, 1); /* Waiting for reading ADC value completed */ while (channelTC == 0) {} /* Get the ADC value fron Data register*/ dataADC = ADC_DR_RESULT(DMAbuffer); App_print_ADC_value(dataADC); } /* Disable interrupts, release DMA channel */ Chip_GPDMA_Stop(LPC_GPDMA, dmaChannelNum); NVIC_DisableIRQ(DMA_IRQn); /* Disable burst mode if any */ if (Burst_Mode_Flag) { Chip_ADC_SetBurstCmd(_LPC_ADC_ID, DISABLE); } }
int main(void) { //preparing the chip & board SystemCoreClockUpdate(); Board_Init(); PWM_Init(); PWM_SetCycle(400,1000); //the actual code Chip_ADC_Init(_LPC_ADC_ID, &ADCSetup); Chip_ADC_EnableChannel(_LPC_ADC_ID, _ADC_CHANNLE, ENABLE); Chip_ADC_SetBurstCmd(_LPC_ADC_ID, ENABLE); //int i; SysTick_Config(SystemCoreClock / TICKRATE_HZ1); while(1) { __WFI(); /* if(rotation_counter==MEMORY_CAPACITY){ //not rotation_cycle? for(i=0;i<MEMORY_CAPACITY;i++){ DEBUGOUT("%d \n",rotation_debug_holder[i]);//,rotation_debug_holder2[i]); } rotation_counter++; } */ } return 0 ; }
/* P0.23 -> AD0 */ void adcInit(void) { ADC_CLOCK_SETUP_T adc; Chip_ADC_Init(LPC_ADC, &adc); Chip_ADC_SetSampleRate(LPC_ADC, &adc, 22000); Chip_ADC_EnableChannel(LPC_ADC, ADC_CH0, ENABLE); Chip_ADC_Int_SetChannelCmd(LPC_ADC, ADC_CH0, ENABLE); Chip_ADC_SetBurstCmd(LPC_ADC, ENABLE); NVIC_EnableIRQ(ADC_IRQn); }
void Board_ADC_Init(void) { // ADC0 Chip_ADC_Init(LPC_ADC0, &(adcsData[0].setup)); adcsData[0].adc = LPC_ADC0; Chip_ADC_SetBurstCmd(LPC_ADC0, DISABLE); NVIC_EnableIRQ(ADC0_IRQn); // ADC1 //Chip_ADC_Init(LPC_ADC1, &(adcsData[1].setup)); //adcsData[1].adc = LPC_ADC1; //Chip_ADC_SetBurstCmd(LPC_ADC1, DISABLE); //NVIC_EnableIRQ(ADC1_IRQn); flagWaitingADCConv=0; ADCValues[0]=0; ADCValues[1]=0; ADCValues[2]=0; Chip_ADC_Int_SetChannelCmd(adcsData[0].adc, ADC_CH1, ENABLE); Chip_ADC_Int_SetChannelCmd(adcsData[0].adc, ADC_CH2, ENABLE); Chip_ADC_Int_SetChannelCmd(adcsData[0].adc, ADC_CH3, ENABLE); }
/* * @brief: enable/disable the ADC and DAC peripheral * @param: ENEABLE_AI, DISABLE_AI, ENEABLE_AO, DISABLE_AO * @return: none */ void analogConfig( uint8_t config ){ switch(config){ case ENABLE_ANALOG_INPUTS: { /* Config ADC0 sample mode */ /* ADC_CLOCK_SETUP_T ADCSetup = { 400000, // ADC rate 10, // ADC bit accuracy 0 // ADC Burt Mode (true or false) }; */ ADC_CLOCK_SETUP_T ADCSetup; /* Initialized to default values: * - Sample rate:ADC_MAX_SAMPLE_RATE=400KHz * - resolution: ADC_10BITS * - burst mode: DISABLE */ Chip_ADC_Init( LPC_ADC0, &ADCSetup ); /* Disable burst mode */ Chip_ADC_SetBurstCmd( LPC_ADC0, DISABLE ); /* Set sample rate to 200KHz */ Chip_ADC_SetSampleRate( LPC_ADC0, &ADCSetup, ADC_MAX_SAMPLE_RATE/2 ); /* Disable all channels */ Chip_ADC_EnableChannel( LPC_ADC0,ADC_CH1, DISABLE ); Chip_ADC_Int_SetChannelCmd( LPC_ADC0, ADC_CH1, DISABLE ); Chip_ADC_EnableChannel( LPC_ADC0, ADC_CH2, DISABLE ); Chip_ADC_Int_SetChannelCmd( LPC_ADC0, ADC_CH2, DISABLE ); Chip_ADC_EnableChannel( LPC_ADC0, ADC_CH3, DISABLE ); Chip_ADC_Int_SetChannelCmd( LPC_ADC0, ADC_CH3, DISABLE ); Chip_ADC_EnableChannel( LPC_ADC0, ADC_CH4, DISABLE ); Chip_ADC_Int_SetChannelCmd( LPC_ADC0, ADC_CH4, DISABLE ); } break; case DISABLE_ANALOG_INPUTS: /* Disable ADC peripheral */ Chip_ADC_DeInit( LPC_ADC0 ); break; case ENABLE_ANALOG_OUTPUTS: /* Initialize the DAC peripheral */ Chip_DAC_Init(LPC_DAC); /* Enables the DMA operation and controls DMA timer */ Chip_DAC_ConfigDAConverterControl(LPC_DAC, DAC_DMA_ENA); /* DCAR DMA access */ /* Update value to DAC buffer*/ Chip_DAC_UpdateValue(LPC_DAC, 0); break; case DISABLE_ANALOG_OUTPUTS: /* Disable DAC peripheral */ Chip_DAC_DeInit( LPC_DAC ); break; } }
/** * @brief Main entry point * @return Nothing */ int main(void) { SystemCoreClockUpdate(); Board_Init(); setupClock(); SystemCoreClockUpdate(); On = true; enableOut = false; controlFlag = false; Board_LED_Set(0, On); DEBUGOUT("Starting\n"); /* Initialize RITimer */ Chip_RIT_Init(LPC_RITIMER); LPC_IOCON->PINSEL[4] |= 0x00000555; //Change this after you know which pwm outputs are needed. LPC_IOCON->PINMODE[3] |= (3 << 6); LPC_IOCON->PINMODE[3] |= (3 << 12); LPC_IOCON->PINSEL[1] |= (1 << 14); LPC_IOCON->PINSEL[1] |= (1 << 16); LPC_IOCON->PINSEL[1] |= (1 << 18); LPC_IOCON->PINSEL[1] |= (1 << 20); LPC_IOCON->PINMODE[1] |= (2 << 14); LPC_IOCON->PINMODE[1] |= (2 << 16); LPC_IOCON->PINMODE[1] |= (2 << 18); LPC_IOCON->PINMODE[1] |= (2 << 20); LPC_SYSCTL->PCLKSEL[0] |= (1 << 12); //PCLK_PWM1 = CCLK LPC_IOCON->PINMODE[4] |= (3 << 26); LPC_SYSCTL->PCONP |= (1 << 17); //Enable clock LPC_SYSCTL->PCLKSEL[1] |= (1 << 30); //PCLKMPWM = CCLK LPC_SYSCTL->PCLKSEL[0] |= (1 << 24); Chip_PWM_Init(LPC_PWM1); LPC_PWM1->PR = 0; Chip_PWM_SetMatch(LPC_PWM1, 0, 3000); Chip_PWM_SetMatch(LPC_PWM1, 1, 1500); Chip_PWM_SetMatch(LPC_PWM1, 2, 1500); Chip_PWM_SetMatch(LPC_PWM1, 3, 1500); Chip_PWM_ResetOnMatchEnable(LPC_PWM1, 0); Chip_PWM_SetCountClockSrc(LPC_PWM1, PWM_CAPSRC_RISING_PCLK, 0); Chip_PWM_SetControlMode(LPC_PWM1, 0, PWM_SINGLE_EDGE_CONTROL_MODE, PWM_OUT_ENABLED); Chip_PWM_SetControlMode(LPC_PWM1, 1, PWM_SINGLE_EDGE_CONTROL_MODE, PWM_OUT_ENABLED); Chip_PWM_SetControlMode(LPC_PWM1, 2, PWM_SINGLE_EDGE_CONTROL_MODE, PWM_OUT_ENABLED); Chip_PWM_LatchEnable(LPC_PWM1, 0, PWM_OUT_ENABLED); Chip_PWM_LatchEnable(LPC_PWM1, 1, PWM_OUT_ENABLED); Chip_PWM_LatchEnable(LPC_PWM1, 2, PWM_OUT_ENABLED); Chip_PWM_LatchEnable(LPC_PWM1, 3, PWM_OUT_ENABLED); Chip_PWM_Enable(LPC_PWM1); Chip_PWM_Reset(LPC_PWM1); Chip_GPIO_Init(LPC_GPIO); LPC_MCPWM->CON_SET |= (1 <<3); DCACSetFreq(1074); LPC_MCPWM->DT = 12; LPC_MCPWM->INTEN_SET |= 1; LPC_MCPWM->INTF_SET |= 1; LPC_MCPWM->CON_SET |= 1; freq = 1074; NVIC_EnableIRQ(RITIMER_IRQn); Chip_ADC_Init(LPC_ADC, &ADCSetup); Chip_ADC_SetBurstCmd(LPC_ADC, DISABLE); /* Configure RIT for a 1s interrupt tick rate */ Chip_RIT_SetTimerInterval(LPC_RITIMER, TIME_INTERVAL); /* LED is toggled in interrupt handler */ vout = 0; voutOldest = 0; voutOld = 0; while (1) { if(controlFlag) { bool emergency = !Chip_GPIO_GetPinState(LPC_GPIO,2,13); emergency |= !Chip_GPIO_GetPinState(LPC_GPIO,2,13); emergency |= !Chip_GPIO_GetPinState(LPC_GPIO,2,13); emergency |= !Chip_GPIO_GetPinState(LPC_GPIO,2,13); emergency |= !Chip_GPIO_GetPinState(LPC_GPIO,2,13); emergency |= !Chip_GPIO_GetPinState(LPC_GPIO,2,13); emergency = !emergency; if(emergency) { enableOut = false; vout = 0; } else { #ifdef enableLoad enableOut = Chip_GPIO_GetPinState(LPC_GPIO,0,28); #else enableOut = true; #endif } Board_LED_Set(0, enableOut); DCDCControl(); DCACControl(); Vmeasure += readADC(VIN_PIN); Imeasure += readADC(CURRENT_PIN); times++; if(times >= delayFactor && enableOut) { DEBUGOUT("%d %d %d %d\n",readADC(VIN_PIN), readADC(VOUT_PIN), readADC(CURRENT_PIN), vout); times = 0; cycles++; if(cycles < ncycles) { #ifdef enableMPPT MPPT(Vmeasure/delayFactor, Imeasure/delayFactor); #endif Vmeasure = 0; Imeasure = 0; } else { cycles = 0; } } if(enablePrev != enableOut) { DEBUGOUT("TOGGLING %d\n",enableOut); } enablePrev = enableOut; controlFlag = false; if(emergency) return 0; } } }
void batteryInit() { Chip_ADC_SetStartMode(LPC_ADC1, ADC_START_NOW, ADC_TRIGGERMODE_RISING); //This must be before the burst cmd Chip_ADC_SetBurstCmd(LPC_ADC1, ENABLE); Chip_ADC_EnableChannel(LPC_ADC1, 1, ENABLE); }
/** * @brief main routine for blinky example * @return Function should not exit. */ int main(void) { USBD_API_INIT_PARAM_T usb_param; USB_CORE_DESCS_T desc; ErrorCode_t ret = LPC_OK; uint32_t prompt = 0; SystemCoreClockUpdate(); /* Initialize board and chip */ Board_Init(); Board_ADC_Init(); /* Initialize PWM Units */ handle0 = Chip_PWM_Init(0, 18, 100); handle1 = Chip_PWM_Init(0, 13, 100); /* enable clocks and pinmux */ Chip_USB_Init(); /* initialize USBD ROM API pointer. */ g_pUsbApi = (const USBD_API_T *) LPC_ROM_API->usbdApiBase; /* initialize call back structures */ memset((void *) &usb_param, 0, sizeof(USBD_API_INIT_PARAM_T)); usb_param.usb_reg_base = LPC_USB0_BASE; /* WORKAROUND for artf44835 ROM driver BUG: Code clearing STALL bits in endpoint reset routine corrupts memory area next to the endpoint control data. For example When EP0, EP1_IN, EP1_OUT, EP2_IN are used we need to specify 3 here. But as a workaround for this issue specify 4. So that extra EPs control structure acts as padding buffer to avoid data corruption. Corruption of padding memory doesn’t affect the stack/program behaviour. */ usb_param.max_num_ep = 3 + 1; usb_param.mem_base = USB_STACK_MEM_BASE; usb_param.mem_size = USB_STACK_MEM_SIZE; /* Set the USB descriptors */ desc.device_desc = (uint8_t *) &USB_DeviceDescriptor[0]; desc.string_desc = (uint8_t *) &USB_StringDescriptor[0]; /* Note, to pass USBCV test full-speed only devices should have both descriptor arrays point to same location and device_qualifier set to 0. */ desc.high_speed_desc = (uint8_t *) &USB_FsConfigDescriptor[0]; desc.full_speed_desc = (uint8_t *) &USB_FsConfigDescriptor[0]; desc.device_qualifier = 0; /* USB Initialization */ ret = USBD_API->hw->Init(&g_hUsb, &desc, &usb_param); if (ret == LPC_OK) { /* WORKAROUND for artf32219 ROM driver BUG: The mem_base parameter part of USB_param structure returned by Init() routine is not accurate causing memory allocation issues for further components. */ usb_param.mem_base = USB_STACK_MEM_BASE + (USB_STACK_MEM_SIZE - usb_param.mem_size); /* Initialize ADC */ Chip_ADC_Init(_LPC_ADC_ID, &ADCSetup); Chip_ADC_EnableChannel(_LPC_ADC_ID, ADC_CH0, ENABLE); Chip_ADC_Int_SetChannelCmd(_LPC_ADC_ID, ADC_CH0, ENABLE); NVIC_SetPriority(_LPC_ADC_IRQ, 1); NVIC_EnableIRQ(_LPC_ADC_IRQ); /* Init VCOM interface */ ret = vcom_init(g_hUsb, &desc, &usb_param); if (ret == LPC_OK) { /* enable USB interrupts */ NVIC_SetPriority(USB0_IRQn, 1); NVIC_EnableIRQ(USB0_IRQn); /* now connect */ USBD_API->hw->Connect(g_hUsb, 1); } } DEBUGSTR("USB CDC class based virtual Comm port example!\r\n"); /* Start BURST Mode (Continuously Convert and Interrupt) */ Chip_ADC_SetBurstCmd(_LPC_ADC_ID, ENABLE); Chip_RIT_Init(LPC_RITIMER); Chip_RIT_SetTimerInterval(LPC_RITIMER, CONTROL_INTERVAL); NVIC_EnableIRQ(RIT_IRQn); int read_mode = WAITING_HEADER; while (1) { /* Check if host has connected and opened the VCOM port */ if ((vcom_connected() != 0) && (prompt == 0)) { //vcom_write("Hello World!!\r\n", 15); prompt = 1; } if (prompt) { unsigned char c; if (vcom_bread(&c, 1) != 0) { switch (read_mode) { case WAITING_HEADER: if (c == PACKET_HEADER) { g_buffCounter = 0; read_mode = WAITING_FOOTER; } break; case WAITING_FOOTER: if (c == PACKET_FOOTER) { onReceivePacket(); read_mode = WAITING_HEADER; } else { g_rxBuff[g_buffCounter] = c; g_buffCounter++; } break; default: break; } } } /* Sleep until next IRQ happens */ //__WFI(); } }