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

16F1847 comparator

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



Joined: 28 Feb 2006
Posts: 151

View user's profile Send private message

16F1847 comparator
PostPosted: Fri Apr 18, 2014 10:57 am     Reply with quote

Hi all! I'm migrating a firmware from the 16F648A to the 16F1847. The problem is that i cant make the comparator work like in the 648A.

I'm just using the comparator 1.

16F648A code:
setup_comparator(A0_A2_A1_A2_OUT_ON_A3_A4);

16F1847 code:
setup_comparator(CP1_C2_A0);

I need to use A2 as positive input, A0 as negative input and A3 as output. I cannot use the wizard because my compiler version doesn't support this pic.

As i read in the datasheet it can be used as i want to. Is that correct?

Anyone can help me?

Compiler version 4.120.

Thanks a lot.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Fri Apr 18, 2014 12:28 pm     Reply with quote

-post the code that DID work - as a COMPILABLE program
- and ditto for the code that does not.

you don't show enough to know what to do as is-

and
show your old and new SCHEMATIC while you are at it.
championx



Joined: 28 Feb 2006
Posts: 151

View user's profile Send private message

PostPosted: Fri Apr 18, 2014 12:44 pm     Reply with quote

Hi ASMBOY thanks for you reply.

I don't have the schematic. The board works fine, the schematic is not the problem. The problem is the configuration of the comparator module.

i cant configure the 16F1847 comparator module to use the A2 input as the positive comparator input, the A0 input as the negative comparator input and the A3 pin as the output of the comparator.


PIN A2 (+) input
PIN A3 comparator output.
PIN A0 (-) input

this is the working code for the 16F648A

Code:

#include <16f648A.h>
#fuses HS, NOMCLR, PROTECT, PUT, NOWDT, BROWNOUT, NOLVP

#use delay(clock=20000000)   
#use rs232(baud=19200, xmit=PIN_B2, rcv=PIN_B1, stream = RS232, errors)

#use fast_io(A)
#use fast_io(B)

void main(void)

{



set_tris_a (0b00000111);   //Ra7-Ra6-Ra5-Ra4-Ra3-Ra2-Ra1-Ra0
set_tris_b (0b00001011);   //Rb7-Rb6-Rb5-Rb4-Rb3-Rb2-Rb1-Rb0



fprintf(RS232,"\n\START");

setup_comparator(A0_A2_A1_A2_OUT_ON_A3_A4);


while(TRUE)
   {
}
}

 

asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Fri Apr 18, 2014 12:56 pm     Reply with quote

what evidence is there that the code you posted ACTUALLY, REALLY WORKS ??
how do you know this is true ??

and how about the new code ??
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Fri Apr 18, 2014 1:01 pm     Reply with quote

and btw:
this compiles in 5.24 also - just fine

now how about that new code and how you define "works" or NOT working - ok ??

ie the way you are testing function ??
championx



Joined: 28 Feb 2006
Posts: 151

View user's profile Send private message

PostPosted: Fri Apr 18, 2014 1:17 pm     Reply with quote

yes it compiles ok and works fine with the 16F648A.

As you may know, the comparator is set to put its output to the A3 pin... so you can test it by putting A2 and A0 to 5v or 0v and see the output on A3 changing.

The code is only to setup the comparator. If i use the 16F1847 the configuration would be the following:

setup_comparator(CP1_C2_A0);

and
Code:

#include <16f1847.h>                    // Selecciona el PIC
#fuses INTRC_IO,NOPROTECT, PUT, NOMCLR, NOWDT, BROWNOUT, NOLVP
#use delay(clock=16000000)

Note that i changed the HS fuse to INTRC_IO to use the internal oscillator.

It compile ok, but does not work. The pic initializes ok and put the START string on the UART port... but the comparator doesnt work.

thanks for your help!!!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 18, 2014 1:58 pm     Reply with quote

For compiler vs. 5.024, it looks like you should use the following setup:
Code:
setup_comparator(CP1_A0_A2 | CP1_OUT_ON_A3);

I'm not at a location where I can install your version or test it in hardware
at the moment. Maybe I can do that later this evening. Or at least I can
install your version and look closely at the .LST file. But try the above
setup. It may work.
championx



Joined: 28 Feb 2006
Posts: 151

View user's profile Send private message

PostPosted: Fri Apr 18, 2014 2:15 pm     Reply with quote

PCM, thanks for your help! it seems that the 16F1847.h file that your version has it is not the same that mine.

That config option is not allowed by my compiler version.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Apr 19, 2014 2:32 pm     Reply with quote

The problem is not fixable by simply giving you the setup_comparator()
constants from version 5.024. The setup_comparator() function operates
differently between your version and vs. 5.024.

Instead of spending a lot of time making a work-around macro that
duplicates the function of the modern compiler, I'm just going to post
a setup routine for the specific comparator mode that you want.

The .LST file from the 5.024 compilation of the following line
Code:
setup_comparator(CP1_A0_A2 | CP1_OUT_ON_A3);

was used as a guide for writing the routine shown below:
Code:

#include <16F1847.h>
#fuses INTRC_IO, NOWDT
#use delay(clock=4M)

#byte CM1CON1 = getenv("SFR:CM1CON1")
#byte CM1CON0 = getenv("SFR:CM1CON0")
#byte CM2CON1 = getenv("SFR:CM2CON1")
#byte CM2CON0 = getenv("SFR:CM2CON0")
#byte TRISA = getenv("SFR:TRISA")

void setup_comp_CP1_A0_A2_OUT_ON_A3(void)
{
output_drive(PIN_A3);
CM1CON1 = 0x30;
CM1CON0 = 0xA0;
CM2CON1 = 0;
CM2CON0 = 0;
output_float(PIN_A0);
output_float(PIN_A2);
}

//===================================
void main()
{

setup_comp_CP1_A0_A2_OUT_ON_A3();

while(1);
}


I didn't test this in hardware. It's just a port of the 5.024 routine to
your compiler. If it doesn't work, then let me know and I'll trouble-shoot
it in hardware.
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