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 CCS Technical Support

comparator on 18f14k50
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
denis_11



Joined: 13 Jul 2010
Posts: 45

View user's profile Send private message

PostPosted: Mon Jul 26, 2010 3:08 pm     Reply with quote

I understood but I obviously I wrote what I have told them to make a comparison even though I prefer to use your function to the VREF rather than setup_low_volt_detect ()... I modified the code so now but still does not work... you think?
Code:

#use delay(clock=48000000)

#define LED_PIN  PIN_B6

#define VREF_ENABLE  0x80

void setup_vref(int8 value)
{
#byte REFCON0 = 0xFBA
#byte REFCON1 = 0xFBB
#byte REFCON2 = 0xFBC

REFCON0 = 0;      // Disable fixed Vref
REFCON1 = value & 0x80;  // Enable or disable Vref
REFCON2 = value & 0x1F;  // Set Vref level from 0-31 (0 to +5v)
}

void main()
{
setup_comparator(CP1_C1_VREF);
setup_vref(VREF_ENABLE|20);

output_low(LED_PIN);  // Turn off the LED initially




delay_us(20);               


while(1)
  {
   if(C1OUT) 
      output_high(LED_PIN); 
   else
      output_low(LED_PIN);
   
   delay_ms(100);   // Debounce time
  }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jul 26, 2010 3:22 pm     Reply with quote

I will probably get an 18F14K50 PIC by mid-week. If you haven't solved
it by then, I'll make it work by directly writing to registers, and testing it
in hardware. I'm not going to make a setup_comparator() function.
I'll just do the minimum effort required to make it work the way you
specified in your post.
denis_11



Joined: 13 Jul 2010
Posts: 45

View user's profile Send private message

PostPosted: Mon Jul 26, 2010 3:32 pm     Reply with quote

ok what I've written in the code is what I need ... because I will use this pic for other applications and same operations ... I think they are just pins setted bad, if you've seen, CCS support has copied same configurations of PIC18F14k22 in the routine that they have attached me... Confused
denis_11



Joined: 13 Jul 2010
Posts: 45

View user's profile Send private message

PostPosted: Tue Jul 27, 2010 12:49 pm     Reply with quote

CCS support rewrote telling me that they were wrong on the C1OUT and C2OUT ... they have written to me to change this:
Code:

#bit C2OUT = 0xf6b.6
#bit C1OUT = 0xf6d.6

but I tried adjusting them but still does not work the comparator... Confused
I do not know what to think...
I also changed the PIC, and I did other tests but everything works except the comparator...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jul 27, 2010 1:15 pm     Reply with quote

Well, if I get my 18F14K50 on Wednesday as expected, I'll find the answer.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 28, 2010 2:25 pm     Reply with quote

I received my 18F14K50 chip and I made the following test program
for Comparator 1 and it works. I put a 5K trimpot on pin C1 (pin 15)
and put an LED on pin C4 (pin 6). The cathode of the LED goes to
ground and there is a 330 ohm series resistor between pin C4 and the
anode of the LED. The PIC is running with a Vdd of +5.0 volts.

I set the Internal Vref generator in the PIC, to make a Vref of 3.0 volts.
I then turned the trimpot so it put out a voltage around 3.0 volts and
the LED turned on. I then set the trimpot to a voltage below 3.0v, and
the LED went off. This is correct operation.

Note that to make the C12OUT pin (pin C4) work, I had to specifically
configure it as an output pin. There is a line of code in the program
below that does this. This is necessary to allow it to drive the LED.

You can look at the register descriptions in the 18F14K50 data sheet to
see how I setup the Comparator registers.

Note that I added the NOPLLEN and CPUDIV1 fuses. This forces the PIC
to run at the 4 MHz frequency that I specify in the #use delay() statement.

This was tested with CCS compiler version 4.109.
Code:

#include <18F14K50.h>
#fuses INTRC_IO, NOWDT, PUT, BROWNOUT, NOLVP, NOPLLEN, CPUDIV1
#use delay(clock=4000000)
#use rs232(uart1, baud=9600)

#define VREF_ENABLE  0x80
// Note: Use 'FALSE' as parameter to disable the Vref.

#byte REFCON0 = 0xFBA
#byte REFCON1 = 0xFBB
#byte REFCON2 = 0xFBC

void setup_vref(int8 value)
{
REFCON0 = 0;      // Disable fixed Vref
REFCON1 = value & 0x80;  // Enable or disable Vref
REFCON2 = value & 0x1F;  // Set Vref level from 0-31 (0 to +5v)
}

// Comparator registers.
#byte CM1CON0 = 0xF6D
#byte CM2CON1 = 0xF6C
#byte CM2CON0 = 0xF6B

//==============================================
void main()
{
//printf("Start\n\r");

// Set Vref = 3.0 volts.  5v * (19/32) = 2.97v
setup_vref(VREF_ENABLE | 19);

output_low(PIN_C4);  // Make pin C4 into an Output pin

CM1CON0 = 0b10111101;
CM2CON0 = 0x00;
CM2CON1 = 0x00;

while(1);
}
denis_11



Joined: 13 Jul 2010
Posts: 45

View user's profile Send private message

PostPosted: Wed Jul 28, 2010 5:21 pm     Reply with quote

perfect! After I tried your suggestions and I saw that works! I suppose I also had some problems with the compiler because I re-install solved the problem ... THANKS! ah but what is PLLEN ? and CPUDIV? but why can not I use the direct frequency of crystal?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 28, 2010 6:32 pm     Reply with quote

The fuses that I posted do give the direct frequency of the crystal.
With this PIC, you have to disable the PLL and set the CPU clock divisor
to 1, to tell the compiler to use the direct frequency of the crystal.
denis_11



Joined: 13 Jul 2010
Posts: 45

View user's profile Send private message

PostPosted: Wed Jul 28, 2010 6:53 pm     Reply with quote

ah I understand... but for example I had also tested the function of the CDC with USB, and if PLLEN disabled, the USB does not work... Why? I use a 12MHz crystal...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 28, 2010 7:10 pm     Reply with quote

Use whatever #fuses settings that you want. The only reason I put in
those fuses is so I could run the test program at 4 MHz. I only did that
because I normally run all my test programs at 4 MHz. Do whatever
you want to do.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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