Ram Prasadh
Member level 2
- Joined
- Feb 16, 2011
- Messages
- 51
- Helped
- 2
- Reputation
- 4
- Reaction score
- 2
- Trophy points
- 1,288
- Activity points
- 1,725
#include<htc.h>
void interrupt isr(void);
void main()
{
TRISA = 0XFF; TRISD = 0X00;
PORTD = 0X00; PORTA = 0X00;
ADCON0 = 0x09; ADCON1 = 0x0E; // Channel AN0 selected
ADCON2 = 0X3E;
T1CON = 0X00;
PEIE = 1; TMR1IE = 1; GIE = 1;
TMR1H=0XFF; TMR1L=0XF7; TMR1IF = 0;
TMR1ON = 1;
while(1);
}
void interrupt isr(void)
{
TMR1IF = 0;
GO = 1;
while(ADIF == 0);
ADIF = 0;
TMR1H=0XEF;
TMR1L=0XE5;
}
what are your values when you apply 5v?
I am using pickit3 debugger. I think it is enough if I can view the ADRES register in the watch window in mplab.I don't see ADRES being used in the code.
after this line "while(ADIF == 0)" i am seeing in Watch window,ADRES register.where are u extracting the result of adc ............
Kindly post the complete code and the device frequency you are using and we will be in a better position to help you.
#include<htc.h>
void interrupt isr(void);
void main()
{
TRISA = 0XFF; TRISD = 0X00;
PORTD = 0X00; PORTA = 0X00;
ADCON0 = 0x01; ADCON1 = 0x0E; // Channel AN0 selected
ADCON2 = 0X3E; //Fosc/64 with time delay of 20*Tad
T1CON = 0X00;
PEIE = 1; TMR1IE = 1; GIE = 1;
TMR1H=0XFF; TMR1L=0XF7; TMR1IF = 0;
TMR1ON = 1;
while(1);
}
void interrupt isr(void)
{
TMR1IF = 0;
GO = 1;
while(ADIF == 0);
ADIF = 0;
TMR1H=0XEF;
TMR1L=0XE5;
}
Is your ADCON0, ADCON1, and ADCON2 settings OK?
You have to make ADCON0 = 0x00; during settings and then before sampling the adc ADON should be turned ON.
What is the purpose of timer?
check your reference voltage.
If your input to be measured is max 5v , then the ref vtg has to be 5 so as to get a value of 3FF.
Also take into consideration what other fellow members are saying about reading the adc data.It cannot be read before the conversion is over.
By the way I dont find any statement in the posted code where u are actually reading the ADC data.
while(ADIF == 0);
You should not put statements like while(ADIF == 0); in interrupt service routines. The interrupt has to be serviced as quick as possible.
Also while interrupt is being serviced will the ADC conversion completes and set ADIF to 1?
It seems that you see the result on ADRES shifted left by several bits. If the conversion is not complete when you try to read the register you may get this kind of results. You need to monitor the flag bit that indicates data is ready to be read. If that doesn't fix it for some reason you could try shifting the result to the right using the >> operator.
while(DONE);
ADCON0bits.GO = 1;
while(ADCON0bits.GO);
ADCON0bits.ADON = 0;
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?