PRABAKARDEVA
Full Member level 2
- Joined
- Sep 16, 2013
- Messages
- 127
- Helped
- 2
- Reputation
- 4
- Reaction score
- 2
- Trophy points
- 1,298
- Location
- Chennai
- Activity points
- 2,168
int main(void) {
TRISD = 0x00;
PORTD = 0x00;
//adc initialize
adc_init();
unsigned int ldr_val;
while (1) {
// read channel 0
ldr_val = adc_read(0);
ldr_val /= 10;
if (ldr_val > 50)
PORTD = 0xFF;
else
PORTD = 0x00;
}
return (1);
}
hi
i am not familiar in pic. but generally, i will give some points, i hope you had correct initialization in adc code and no hard ware faults. first thing is, check the ref voltage of the micro-controller ,
second thing is, check the datatype of the ldr_val
hi jayanth.....i had just posted my main file.....i had written driver file for the adc_init() which done the everything.fosc and uc mentioned in compiler.....posted that code for your verification.TRISx is not set for ADC pin. ANSEx, ADCONx, CMCON, CM1CON0, CM2CON0, VRCON, CVRCON, registers if exists is not configured. LDR circuit o/p voltage range is not mentioned. uC and Fosc is not mentioned. Post Circuit and mikroC project files zipped.
void adc_init(void)
{
ADRESH=0;
ADRESL=0;
RA0=0;
TRISA0=1;
ADCON0=0x81;
ADCON1=0xCE;
}
int adc_read(unsigned char channel)
{
unsigned char lsb,msb;
unsigned char result;
ADCON0 &= ~(0x07 << 3);
ADCON0 |= (channel<< 3);
__delay_ms(1);
ADON=1;
GO_DONE=1;
while(GO_DONE)
{
;
}
lsb=ADRESL;
msb=ADRESH;
result=(msb>>8)+lsb;
return result;
}
Code C - [expand] 1 result = (msb << 8) + lsb;
Code C - [expand] 1 result = (msb << 8) | lsb;
Code C - [expand] 1 2 ADCON0 &= ~(0x07 << 3); ADCON0 |= (channel<< 3);
Code C - [expand] 1 2 ADCON0=0x81; ADCON1=0x8E;
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?