View previous topic :: View next topic |
Author |
Message |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jul 28, 2010 2:25 pm |
|
|
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
|
|
Posted: Wed Jul 28, 2010 5:21 pm |
|
|
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
|
|
Posted: Wed Jul 28, 2010 6:32 pm |
|
|
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
|
|
Posted: Wed Jul 28, 2010 6:53 pm |
|
|
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
|
|
Posted: Wed Jul 28, 2010 7:10 pm |
|
|
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. |
|
|
|