So I've been having trouble getting the ADC to work. I have a light sensor which is giving an analog value from 0 to 3.3V connected to Pin 59 (which is ADC_CH_2).
Here is my code so far:
In PinMux() -
// not sure if these are needed, or even correct HWREG(GPRCM_BASE + GPRCM_O_ADC_CLK_CONFIG) = 0x00000043; HWREG(ADC_BASE + ADC_O_ADC_CTRL) = 0x00000004; HWREG(ADC_BASE + ADC_O_ADC_SPARE0) = 0x00000100; HWREG(ADC_BASE + ADC_O_ADC_SPARE1) = 0x0355AA00; //PRCMPeripheralClkEnable(PRCM_ADC, (PRCM_RUN_MODE_CLK|PRCM_SLP_MODE_CLK)); // enable ADC clock // // Configure PIN_59 for ADC_CH_2 // PinTypeADC(PIN_59, 0xFF);
In my separate ADCfunctions.c -
void adcGetValue(char *pADCsample) { char ucSample; char ucIndex = 0; while(ucIndex < 8) { if(ADCFIFOLvlGet(ADC_BASE, ADC_CH_2)) { ucSample = ADCFIFORead(ADC_BASE, ADC_CH_2); *pADCsample = ucSample; pADCsample++; } ucIndex++; } } void adcInitialize(void) { //void *adcint = &adcGetValue(); // ADC_CH_2 is Pin 59 ADCChannelEnable(ADC_BASE, ADC_CH_2); // interrupts disabled for now //ADCIntEnable(ADC_BASE, ADC_CH_2, ADC_FIFO_FULL); //ADCIntRegister(ADC_BASE, ADC_CH_2, adcint); ADCEnable(ADC_BASE); }
As you can see I have a lot of debug code going on. The HWREG lines in the PinMux function is pulled directly from the adc.c sample code which is in the CC3200 SDK. Besides that code, which seems to show more about the UART than the ADC, there isn't much else in the way of resources yet (which is understandable at this stage).
However, when I run my above code, all I get is random results which clearly do not correlate to the analog values that I measure with my oscilloscope. I've tried using just the first HWREG line, I've tried leaving them out entirely, nothing seems to change.
Also -- if I add the name of a function directly to statup_ccs.c, can I skip the ADCIntRegister line? Or is it doing something else? The problem I'm having here is that I can't seem to figure out what variables it requires. Or rather, I know that it wants the ADC_base, the channel, and then a pointer to the function, but I'm still learning about pointers (and as you can see in my above code, I'm clearly doing it wrong -- I haven't worked with pointers to functions yet). In the adc.h it is declared as
extern void ADCIntRegister(unsigned long ulBase, unsigned long ulChannel, void (*pfnHandler)(void));
I'm guessing it needs a void type pointer, which points to a function that has no arguments (hence the second (void)), which makes sense as most interrupt functions can't have values passed directly to them, they need to be passed through pointers. But no matter how I try to create a pointer to the adcGetValue() function, it never works. I know this is more of a C question, but I really need to get this ADC working! Again, if the ADCIntRegister function is not required if you put the function directly into startup_ccs.c then the point is moot.
Even if someone can write a very simple function that correctly sets up the ADC and does a single sample, I would be greatly, greatly appreciative!
Thanks!