If you redan datasheet of CNY70 you can notice that output is more digital than analog. So you dont need ADC.
#include "lpc17xx.h"
#include "type.h"
#include "adc.h"
{
LPC_GPIO2->FIODIR = 0x000000FF;
LPC_ADC->ADCR |= 1 << 24;
while((LPC_ADC->ADDR0 & (1 << 31)) == 0);
int curVal=( LPC_ADC->ADDR0 >> 4 ) & 0xFFF;
float volt = (float)(((VREFP-VREFN)*curVal)/ADC_MAX); //result in volts.
if ( volt >= 1.6 )
LPC_GPIO2->FIOSET = 1;
if ( volt =< 1.6)
LPC_GPIO2->FIOCLR = 0x000000FF;
return; /* the ADC reading is done inside the handler, return 0. */
}
It gives error like :adc.c(25): error: #169: expected a declaration
To be honest, i need it for line follower robot... for example voltage between 0 to 1.6 it activate port0.0 as high for upper than that make it low. I just want to have this sample for expanding my algorithm as line follower robot. Can u help me with that?
Also i've written a program which has problem that i dont know where
It's the sample that i've written:
So as u said i need to be precise about input analog
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 /***************************************************************************** ** Function name: ADCRead ** ** Descriptions: Read ADC channel ** ** parameters: Channel number ** Returned value: Value read, if interrupt driven, return channel # ** *****************************************************************************/ uint32_t ADCRead( uint8_t channelNum ) { #if !ADC_INTERRUPT_FLAG uint32_t regVal, ADC_Data; #endif /* channel number is 0 through 7 */ if ( channelNum >= ADC_NUM ) { channelNum = 0; /* reset channel number to 0 */ } LPC_ADC->CR &= 0xFFFFFF00; LPC_ADC->CR |= (1 << 24) | (1 << channelNum); /* switch channel,start A/D convert */ #if !ADC_INTERRUPT_FLAG while ( 1 ) /* wait until end of A/D convert */ { regVal = LPC_ADC->DR[channelNum]; /* read result of A/D conversion */ if ( regVal & ADC_DONE ) { break; } } LPC_ADC->CR &= 0xF8FFFFFF; /* stop ADC now */ if ( regVal & ADC_OVERRUN ) /* save data when it's not overrun, otherwise, return zero */ { return ( 0 ); } ADC_Data = ( regVal >> 4 ) & 0xFFF; return ( ADC_Data ); /* return A/D conversion value */ #else return ( channelNum ); /* if it's interrupt driven, the ADC reading is done inside the handler. so, return channel number */ #endif }
Furthermore, Am i concentrate to right part "ADCRead" ? Does ADCInit , ADC_IRQHandler,ADC0BurstRead play significant role in my simple program for CNY70 in ADC example(just for configuring the difference of voltages in ADC ports)?
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 #include "lpc17xx.h" #include "type.h" #include "adc.h" extern volatile uint32_t ADCValue[ADC_NUM]; extern volatile uint32_t ADCIntDone; extern volatile uint32_t OverRunCounter; /***************************************************************************** ** Main Function main() ******************************************************************************/ int main (void) { uint32_t i, j,L,volt; /* SystemClockUpdate() updates the SystemFrequency variable */ SystemClockUpdate(); LPC_GPIO2->FIODIR = 0x000000FF; /* P2.xx defined as Outputs */ LPC_GPIO2->FIOCLR = 0x000000FF; /* turn off all the LEDs */ for(i = 0; i < 8; i++) { LPC_GPIO2->FIOSET = 1 << i; for(j = 1000000; j > 0; j--); } LPC_GPIO2->FIOCLR = 0x000000FF; for(j = 1000000; j > 0; j--); /* Initialize ADC */ ADCInit( ADC_CLK ); while(1) { L = ADCRead(0); L = (L >> 4) & 0x00000FFF; volt = (float)((3.3*L)/5); if ( volt < 4 ) break; } while(1){ LPC_GPIO2->FIOCLR = 0x000000FF; /* turn off all the LEDs */ for(i = 0; i < 8; i++) { LPC_GPIO2->FIOSET = 1 << i; for(j = 1000000; j > 0; j--); } LPC_GPIO2->FIOCLR = 0x000000FF; for(j = 1000000; j > 0; j--); } }
volt = (float)((3.3*L)/5);
L = ADCRead(0);
L = (L >> 4) & 0x00000FFF;
if ( volt < 1100 )
break;
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?