CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

comparator not working (16F887)

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
C0J



Joined: 05 Nov 2008
Posts: 25

View user's profile Send private message

comparator not working (16F887)
PostPosted: Mon Mar 02, 2009 2:18 pm     Reply with quote

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
Kenny



Joined: 07 Sep 2003
Posts: 173
Location: Australia

View user's profile Send private message

PostPosted: Mon Mar 02, 2009 6:50 pm     Reply with quote

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);
C0J



Joined: 05 Nov 2008
Posts: 25

View user's profile Send private message

PostPosted: Tue Mar 03, 2009 9:36 am     Reply with quote

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);}
}
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 03, 2009 11:27 am     Reply with quote

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.
C0J



Joined: 05 Nov 2008
Posts: 25

View user's profile Send private message

PostPosted: Tue Mar 03, 2009 12:00 pm     Reply with quote

Thanks PCM,

have tried out, but not working

Thank you (& Kenny) for taking your time looking at the problem

Smile
CJ
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group