View previous topic :: View next topic |
Author |
Message |
C0J
Joined: 05 Nov 2008 Posts: 25
|
comparator not working (16F887) |
Posted: Mon Mar 02, 2009 2:18 pm |
|
|
Hi,
have used the following code to test out on comparator1 of 16F887
but it does not work
have replaced setup_vref with:
#byte VRCON=0x97
VRCON=0x8f;
but it also doesn't work
Any advise on this problem?
the compiler ver is 4.038
Code: | #include <16F887.h>
#FUSES NOWDT,INTRC_IO,NOPROTECT,NOBROWNOUT,NOMCLR,NOCPD,NOPUT,NOIESO,NOFCMEN,NOLVP
#use delay(clock=8,000,000)
void main()
{
setup_comparator(CP1_A1_VREF);
setup_vref(VREF_HIGH|15);
while(1)
{
delay_us(1);
if(C1OUT==1)
{
output_high(PIN_C6);
}
else if(C1OUT==0)
{
output_low(PIN_C6);}
}
} |
Thanks,
CJ |
|
![](templates/subSilver/images/spacer.gif) |
Kenny
Joined: 07 Sep 2003 Posts: 173 Location: Australia
|
|
Posted: Mon Mar 02, 2009 6:50 pm |
|
|
Could be a compiler bug. With version 4.084 the startup code clears all the ANSEL bits but the appropriate bit for the selected pin doesn't get set by setup_comparator().
As a work-around try
Code: |
#byte ANSEL = 0x188
|
and
Code: |
setup_comparator(CP1_A1_VREF);
ANSEL = 0x02; // Set RA1 as analog input
setup_vref(VREF_HIGH|15);
|
|
|
![](templates/subSilver/images/spacer.gif) |
C0J
Joined: 05 Nov 2008 Posts: 25
|
|
Posted: Tue Mar 03, 2009 9:36 am |
|
|
Thanks Kenny,
have tried, but not working
have seen that the VRCON register value =0x00 (in MPLAB SIM) after executing "setup_vref(VREF_HIGH|15)"
Have tested out using 16F690 with similar code & 16F690 is able to work
Not sure what goes wrong?
16F690 code (workable):
Code: | #include <16F690.h>
#FUSES NOWDT,INTRC_IO,NOPROTECT,NOBROWNOUT,NOMCLR,NOCPD,NOPUT,NOIESO,NOFCMEN
#use delay(clock=8,000,000)
void main()
{
setup_comparator(CP1_A1_VR);
setup_vref(VREF_HIGH|15|VREF_COMP1); //VREF setup as 3.6V (pg253)
while(1)
{
delay_us(1);
if(C1OUT==1)
{
output_high(PIN_C6);
}
else if(C1OUT==0)
{
output_low(PIN_C6);}
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 03, 2009 11:27 am |
|
|
Your version of the compiler is not setting up the CM1CON0 register
correctly. Try adding the lines shown in bold below
Quote: |
#byte CM1CON0 = 0x107 // Register Address in 16F887
void main()
{
setup_comparator(CP1_A1_VREF);
setup_vref(VREF_HIGH|15);
CM1CON0 = 0x85; |
Your version is very buggy with the setup_comparator() function.
I don't guarantee that the changes above are the only changes required.
There may be more. I just didn't want to keep working on it. |
|
![](templates/subSilver/images/spacer.gif) |
C0J
Joined: 05 Nov 2008 Posts: 25
|
|
Posted: Tue Mar 03, 2009 12:00 pm |
|
|
Thanks PCM,
have tried out, but not working
Thank you (& Kenny) for taking your time looking at the problem
CJ |
|
![](templates/subSilver/images/spacer.gif) |
|