analog comparator in pic18f8722

Thread Starter

zargar

Joined Oct 22, 2013
7
pls help me to drive the analog comparator in c18 compiler
this program not working in proteos

why????????????????

Rich (BB code):
#include <p18f8722.h>
#include <delays.h>


#pragma config OSC     = HS

#pragma config PWRT    = ON

#pragma config WDT     = OFF
#pragma config LVP     = OFF
#pragma config DEBUG   = OFF




void main ()
{
     CMCON=0x01;

     TRISFbits.TRISF1=0;
     TRISFbits.TRISF5=1;
     TRISFbits.TRISF6=1;
     TRISFbits.TRISF2=0;
     TRISFbits.TRISF3=1;
     TRISFbits.TRISF4=1;
     TRISJbits.TRISJ0=0;

     while (1);
    }
 
Last edited by a moderator:

THE_RB

Joined Feb 11, 2008
5,438
That is a very complex PIC, you need to properly configure all port pins.

Check the datasheet chapter on the ADC module, there is setup you need to do to select pins as analog pins or digital pins.

Also check the datasheet chapter on port setup for the port pins you are using, it should tell if pins need to be handled in CONFIGs etc.
 

THE_RB

Joined Feb 11, 2008
5,438
Yes, I could look in the datasheet for you, and tell you what it says.

But for many good reasons, I am not going to.

Please read the datasheet yourself, that is the largest and most powerful 8bit PIC micro, and you need to have the ability to look in the datasheet yourself. Believe me, it is well worth getting past a fear of a large datasheet.

All the datasheet chapters are clearly labeled and I gave you some pointers already in post #2. :)
 

ErnieM

Joined Apr 24, 2011
8,377
The comparator section of the data sheet is only 7 pages long. Spend some time with it. Spend some time reading "TABLE 22-1: REGISTERS ASSOCIATED WITH COMPARATOR MODULE" and cross reference these settings so you get each one.

That way you will understand why you need to set certain things. Then you can do this on your own.

Of course, if you have a specific question on some detail we can help.
 

Thread Starter

zargar

Joined Oct 22, 2013
7
i read this chapter very very & very.....but my english is very bad ... i dont understand this chapter.....according this chapter i must configur pins (port f) as input or output and this pins are analog input as defult....what i do more than this ?????
 

Papabravo

Joined Feb 24, 2006
21,225
I guess maybe you should put down the datasheet and the software and learn enough English or other language that Microchip writes datasheets in so that you can read and understand those datasheets.

You also need to know that simulations of a complex part may not be complete and accurate; especially when it comes to peripheral devices.

Check each register in Table 22-1 then ask more specific questions to fill in the gaps in your knowledge.
 

Thread Starter

zargar

Joined Oct 22, 2013
7
this is the register 22-1
REGISTER 22-1: CMCON: COMPARATOR MODULE CONTROL REGISTER​
R-0 R-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0
C2OUT C1OUT C2INV C1INV CIS CM2 CM1 CM0
bit 7 bit 0
bit 7​
C2OUT: Comparator 2 Output bit
When C2INV =
0:

1​
= C2 VIN+ > C2 VIN-

0​
= C2 VIN+ < C2 VINWhen
C2INV =
1:

1​
= C2 VIN+ < C2 VIN-

0​
= C2 VIN+ > C2 VINbit
6
C1OUT: Comparator 1 Output bit
When C1INV =
0:

1​
= C1 VIN+ > C1 VIN-

0​
= C1 VIN+ < C1 VINWhen
C1INV =
1:

1​
= C1 VIN+ < C1 VIN-

0​
= C1 VIN+ > C1 VINbit
5
C2INV: Comparator 2 Output Inversion bit

1​
= C2 output inverted

0​
= C2 output not inverted
bit 4
C1INV: Comparator 1 Output Inversion bit

1​
= C1 output inverted

0​
= C1 output not inverted
bit 3
CIS: Comparator Input Switch bit
When CM2:CM0 =
110:

1​
= C1 VIN- connects to RF5/AN10/CVREF

C2 V​
IN- connects to RF3/AN8

0​
= C1 VIN- connects to RF6/AN11
C2 V
IN- connects to RF4/AN9

bit 2-0
CM2:CM0: Comparator mode bits

i load this register cmcon=0x01;
is this a bad loading?
 

Papabravo

Joined Feb 24, 2006
21,225
Writing 0x01 to this register is a valid thing to do. What is it that you expect the result to be and what are you observing in your simulation? So which part of the description are you having trouble with?
 

THE_RB

Joined Feb 11, 2008
5,438
I don't want to sound mean Zargar, but this PIC is 80 pins and the most powerful 8bit PIC they make.

At your beginner level of skill (and dislike of reading datasheets) you should try your first few projects on a little easy PIC like a 12F675, it also has a comparator (but needs a LOT less setting up).
 

Thread Starter

zargar

Joined Oct 22, 2013
7
thank you very much for your time & consideration ... I,ve set RF5 & RF6 as input for an analog comparator and I also I've set RF2 as output ..... However, this IC with sine wave & ground as inputs gives high level of the voltage ( 5 V)..and it dosent give Square wave...
 

ErnieM

Joined Apr 24, 2011
8,377
thank you very much for your time & consideration ... I,ve set RF5 & RF6 as input for an analog comparator and I also I've set RF2 as output ..... However, this IC with sine wave & ground as inputs gives high level of the voltage ( 5 V)..and it dosent give Square wave...
Ah... well that may not be a problem of the comparator setting, but how you are using it. When your sine input is above ground the comparator should be working as you expect, but bringing the sine input below ground can have any output whatsoever, as you are exceeding the voltage range of the input itself.

In fact, you should insure every pin stays within the range of Vdd and Vss. There are occasional times you can properly exceed these, but by no more then a diode drop.

While it was an analog comparator (not part of a PIC) I once saw a device that as you went more negative with an input would change the output state several times> Near ground it was correct, then it drove the output high, then low. Either input would force the output when very negative, it was not working as a comparator at all.

You should get a better output if you bring the pin set to ground to a higher voltage. There is an internal voltage reference (Section 23) you can use for this. I would also be curious to see how you drive the sine input pin: hopefully you have a series resistor to limit current when going negative.

Do you have a schematic to post of your work? That would help see what you are doing.

There is nothing wrong with the PIC you chose to use. I see many people starting out using a device with very few features. This part should serve you for a long time. Just concentrate on the one piece you need at the moment and you should get there.

I feel for you that you cannot get information in your own language. I've often thought about how hard it must be to learn this stuff when you first have to translate the words into your own language, then trying to puzzle out what the words mean.
 
Last edited:

Thread Starter

zargar

Joined Oct 22, 2013
7
hi..............I changed input RF5 to sine wave that has ( 2 v) as offset voltage & input RF6 to DC voltage (2 v) ..... according you said .........but the output not changed ...excuse me for my very bad english language ..
 

ErnieM

Joined Apr 24, 2011
8,377
OK, let's walk thru this step by step.

Setting CMCON = 0x01 turn on one comparator per Figure 22-1.

The inputs are RF6/AN11 for - and RF5/AN10 for +. These are analog signals.

The output is on RF2/AN7, and it is digital.

I'm not sure if you need set the inputs AN10 and AN11 as analog, of if enabling the comparator mode is enough. (If not then someone needs to tell ME how to make that setting.) Let's assume you need not use ADCON1 for AN10 and 11.

You absolutely have to set AN7 as digital. Setting ADCON1 to any value greater then 0x07 will do this; 0x0F turns all analog inputs off.

You also need to make RF2 an output (pins default to inputs) by setting TRISF.F2 to zero, and you seem to be doing this.

So that's all I have: try changing ADCON1 and see if that gets this to work.
 
Last edited:

THE_RB

Joined Feb 11, 2008
5,438
@THE RB
I can hear the gnashing of teeth from a considerable distance.:D
Nah, there's minimal gnashing from my end. :)

I was more intent on getting ocross the good advice that if someone is starting with an huge PIC with enormous amount of pins and options, they might be better off starting with a smaller easier PIC, OR at least getting some practice with reading the datasheet as that will be needed over and over before this project is completed!

I do sympathise with someone new and I know the PIC datasheets can be scary and difficult to find info in, it's a steep part of the learning curve.

Hopefully my posts did not sound like a frustrated man lashing out at the person asking for help, but sometimes things need to be said. :)

I've worked a bit with that PIC, and it has a lot of pin options where not only can many hardware modules be assigned to one pin but some hardware modules can even be assigned to different pins on different ports!

The datasheet is your friend! ;)
 

Thread Starter

zargar

Joined Oct 22, 2013
7
hi...ErineM ....how are you? thank you very much .... I set ADCON1=0X08;.... however this work changed the RF5/AN10 ...& RF6/AN11 as digital input ..... but we need RF5 & RF6 as analog input....can you help me about this?
 

ErnieM

Joined Apr 24, 2011
8,377
I saw what you mean with the ADCON1 setting before my last post. But I *think* when you set the CMCON register this overrides the ADCON settings. Either that or there is some other magic way to set these as analog.

Have you tried the code on your hardware? I don't have that PIC but I do have MPLAB, C18, and the simulator built into MPLAB. I ran the code using AN10 and AN11 as inputs and the simulator says RF2 will respond to these comparator analog inputs.

The simulator can only set analog inputs as high or low, which works just fine here. When AN10 is hi and AN11 is low RF2 will show a high, any other combo of inputs and RF2 is low.

So from what I can see this should work. If you have not tried it on your hardware then do so.

If you did try it then... best ask on the Microchip Forums. I betcha DarioG or some other clever guy will clear all this up fast.

I'm adding the MPLAB project I used to test this so you can try it. Hit F9 to run to the breakpoint in line 30. Hit it again to loop back to it. You can see some registers in the watch window. Down in the stimulus window hit the > button on either the top pair or the bottom pair of inputs, then hit F9. This simulates analog inputs so you can see their effect on PORTF.
 

Attachments

Top