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