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 1, 2  Next
 
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: Fri Jul 23, 2010 5:14 pm     Reply with quote

I don't have an 18F14K50 to test, but I think the following setup_vref()
routine should work. There is an example of how to call the function
given in the program below. You still have to setup the comparator
with the setup_comparator() function. The function shown below only
sets up the Vref voltage. I wrote this with compiler vs. 4.109.
Code:

#include <18F14K50.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP,NOPLLEN,CPUDIV1
#use delay(clock=4000000)

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

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()
{
// Example: Enable Vref voltage of 2.65 volts.
// Vref = 5v * (17/32) = 2.65v
setup_vref(VREF_ENABLE | 17);

// Example: Disable Vref voltage.
setup_vref(FALSE);

while(1);
}
denis_11



Joined: 13 Jul 2010
Posts: 45

View user's profile Send private message

PostPosted: Sat Jul 24, 2010 7:25 am     Reply with quote

I understand and I thank you so much for your help! else ... wondering if you saw, what were the ports for setup_comparator () function ... I did not understand what are the ports available, but the settings are these: CP1_A0_VREF, CP2_B3_VREF, CP1_B2_VREF, except inverted ports ... But if I wanted to use as inputs such as the port of CCP1 (PIN_C0) and then compare PIN_C0 with VREF is it possible?
denis_11



Joined: 13 Jul 2010
Posts: 45

View user's profile Send private message

PostPosted: Sat Jul 24, 2010 5:47 pm     Reply with quote

ok I tested the modified firmware and does not work ... :
Code:

#include <18F14K50.H>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                   
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES NOBROWNOUT               //No browno BORV19             
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOMCLR                     //Master Clear pin disabled
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES HFOFST               
#FUSES NOWRT0               
#FUSES NOWRT1               
#FUSES USBDIV1             
#FUSES BBSIZ2K                  //2K words Boot Block size
#FUSES CPUDIV1
#FUSES PLLEN           
#FUSES PCLKEN                       

#use delay(clock=48000000)
#define VREF_ENABLE  0x80
#define LED_PIN  PIN_B6

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_B2_VREF);
setup_vref(VREF_ENABLE | 20);

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


delay_us(20);               


while(1)
  {
   if(C1OUT || C2OUT || C3OUT) 
      output_high(LED_PIN); 
   else
      output_low(LED_PIN);
   
   delay_ms(100);   // Debounce time
  }
}


I used "C1OUT | | C2OUT | | C3OUT" because I did not know which of the virtual outputs was selected ... because I did not know that CP1_B2_VREF pin was referring, I tried all the pins with the positive until the LED is illuminated (compare the positive 5v with 3v of VREF), but rather nothing ... what's wrong?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 25, 2010 5:46 pm     Reply with quote

What pin do you want to use for the comparator input ?
In your post, you said this:
Quote:

I wanted to use as inputs such as the port of CCP1 (PIN_C0)

But in the 18F14K50, CCP1 is on Pin C5. That's why I need you to give
me the correct pin.
denis_11



Joined: 13 Jul 2010
Posts: 45

View user's profile Send private message

PostPosted: Sun Jul 25, 2010 6:08 pm     Reply with quote

oh sorry I was confused... yes, I would compare the PIN_C5 with the internal VREF (as I've suggested) and then output as a digital outputs such as C1OUT....PIN_C5 besides, what are the other pins that I could use? I preferred or C0 or C1 or C2...are these possible?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 25, 2010 9:52 pm     Reply with quote

It might be, but I just looked at the ASM code created by the 18F14K50
setup_comparator() function in vs. 4.109 and it's totally screwed up.
They write the wrong values to the wrong register addresses. Also,
the constants in 18F14K50.h are wrong or incomplete. I will try to
write up a usable setup_comparator() function as a work-around on
Monday.
denis_11



Joined: 13 Jul 2010
Posts: 45

View user's profile Send private message

PostPosted: Mon Jul 26, 2010 6:04 am     Reply with quote

ok so look to hear from you... I wonder how they overlooked the series PIC18F1XK50 because the same problems are also present with 18F13K50....
denis_11



Joined: 13 Jul 2010
Posts: 45

View user's profile Send private message

PostPosted: Mon Jul 26, 2010 2:40 pm     Reply with quote

Waiting in the solution, I wrote to support CCS and they said this:
"I am Attaching two files: devices4.dat and 18F14K50.h.
These two files Should fix your issue with the settings on the comparator PIC18F14K50. You Will Notice That The New 18F14K50.H contains files to use with the new Defines setup_comparator () function. To answer your other question, the setup_vref function () does not exist, You May Be Trying to use setup_low_volt_detect () function instead. "
and they have attached the 2 files as written above ... in devices4.dat I have no idea what it is but there is only 18F14K50.h changing this:
Code:
////////////////////////////////////////////////////////////////// COMP
// Comparator Variables: C1OUT, C2OUT
// Constants used in setup_comparator() are:
//
#define NC_NC_NC_NC   0x404
#define NC_NC         0x404

//Pick one constant for COMP1
#define CP1_A1_A0     0x4000088
#define CP1_C1_A0     0x0100089
#define CP1_C2_A0     0x020008A
#define CP1_C3_A0     0x040008B
#define CP1_A1_VREF   0x400008C
#define CP1_C1_VREF   0x010008D
#define CP1_C2_VREF   0x020008E
#define CP1_C3_VREF   0x040008F
//Optionally OR with one or both of the following
#define CP1_OUT_ON_A2 0x2000020
#define CP1_INVERT    0x0000010
#define CP1_FAST      0x0000008

//OR with one constant for COMP2
#define CP2_A1_C0     0x4008800
#define CP2_C1_C0     0x0108900
#define CP2_C2_C0     0x0208A00
#define CP2_C3_C0     0x0408B00
#define CP2_A1_VREF   0x4008C00
#define CP2_C1_VREF   0x0108D00
#define CP2_C2_VREF   0x0208E00
#define CP2_C3_VREF   0x0408F00
//Optionally OR with one or both of the following
#define CP2_OUT_ON_C0 0x0082000
#define CP2_INVERT    0x0001000
#define CP2_FAST      0x0000800

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


then VREF "does not exist" and not configurable?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jul 26, 2010 2:58 pm     Reply with quote

It doesn't exist in vs. 4.109 of the compiler. We already know that.
I posted a substitute setup_vref() function for you in an earlier post.
By the way, I don't work for CCS.

I was working on the comparator functions and I don't see where they
get the names for the options. For example, for Comparator 1, they have:
Code:

#define CP1_A1_A0     0x4000088
#define CP1_C1_A0     0x0100089
#define CP1_C2_A0     0x020008A
#define CP1_C3_A0     0x040008B

But in the 18F14K50 data sheet, the comparators don't use pins A0 or A1.
They only use C0, C1, C2, C3, and C4. The two comparators share
these five pins. I have no idea where they get 'A0' and 'A1' from.
I was going to give them these names:
Code:

#define CP1_C1_C0   
#define CP1_C2_C0     
#define CP1_C3_C0     
#define CP1_AGND_C0

This is a pain to do. If CCS is going to do it for you (or they already
did it), then I'm going to stop working on it. I'm not volunteering to
do this again. I suggest that you test the new features that CCS enabled
for you, and see if they work.
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);
}
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 1, 2  Next
Page 1 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