Comparator module in flowcode

Forum for problems or queries regarding other Flowcode Components. Eg LEDs, Switches, LCD, Gfx LCD etc

Moderators: Benj, Mods

Post Reply
MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Comparator module in flowcode

Post by MJU »

Maybe I didn't found it, but is there a module in Flowcode that I can use to get a comparator working?
I have two PIC's that have this functionality but I don't seem to find a module in Flowcode for PIC's 3.4.7.48

What I want to do is offer a reference voltage to the comparator, and another voltage to the other pin.
Everytime the voltage is a certain amount higher or lower than the reference voltage, it should be counted.

Thanks

User avatar
Steve
Matrix Staff
Posts: 3421
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Comparator module in flowcode

Post by Steve »

Sorry, but you will have to do this by inserting your own C code into your Flowcode program.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Comparator module in flowcode

Post by Benj »

Hello

Currently there is no comparitor component for Flowcode. The main reason for this is the lack of interest in this peripheral. I think that this component will be available in Flowcode V4 when it is released. In the mean time you can use C code to directly control the comparitor module registers onboard the chip. You should even be able to set up a custom interrupt so that an interrupt is triggered each time the comparitor is equal or greater etc. There is a lot of information on the comparitor module in the device datasheet. There is also a lot of help with custom interrupts in the Flowcode V3 help file. If you get stuck or run into difficulties then let us know.

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Re: Comparator module in flowcode

Post by MJU »

Flowcode is a great program for people like me that want to play with PIC's but don't like the code.
Times like these when I want to use the comparator brings me back on my feet.
PIC's are much complex than what you see using Flowcode.


I was looking at the datasheet for the 16F628A

Code: Select all

FLAG_REG EQU 0X20
CLRF FLAG_REG ;Init flag register
CLRF PORTA ;Init PORTA
MOVF CMCON, W ;Load comparator bits
ANDLW 0xC0 ;Mask comparator bits
IORWF FLAG_REG,F ;Store bits in flag register
MOVLW 0x03 ;Init comparator mode
MOVWF CMCON ;CM<2:0> = 011
BSF STATUS,RP0 ;Select Bank1
MOVLW 0x07 ;Initialize data direction
MOVWF TRISA ;Set RA<2:0> as inputs
;RA<4:3> as outputs
;TRISA<7:5> always read ‘0’
BCF STATUS,RP0 ;Select Bank 0
CALL DELAY10 ;10ΞΌs delay
MOVF CMCON,F ;Read CMCON to end change
;condition
BCF PIR1,CMIF ;Clear pending interrupts
BSF STATUS,RP0 ;Select Bank 1
BSF PIE1,CMIE ;Enable comparator interrupts
BCF STATUS,RP0 ;Select Bank 0
BSF INTCON,PEIE ;Enable peripheral interrupts
BSF INTCON,GIE ;Global interrupt enable
What should I use from this code to make the comparator change a variable whenever the voltage on the input is <> than the reference voltage (Vref) on a pin?
Correct me if i'm wrong, but when I want to use "Two Common Reference Comparators" I need to use this: CM<2:0> = 011
This is the configuration for two comparators with a common input.

What I can make of this code is that it generates a byte with several possibilities.
CMCON is a register where the result of the comparator is being stored?
So if I can use the value of this register I can make decisions and use it as a variable?

Is it possible to alter the code above to use in Flowcode, so I can use it in my project?
An Flowcode example would be great to start me up. With a little explanation, this would be great for everyone that want to use this feature of PIC's...

Thanks!

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Comparator module in flowcode

Post by Benj »

Hello

Here is how you do the above program with Flowcode.

1) C code block to initialise Capture / Compare:

Code: Select all

char temp;
porta = 0;
cmcon = 0x03;
trisa = 0x07;
delay_10us(1);
temp = cmcon;
2) Custom Interrupt to handle the comparison

Enable

Code: Select all

clear_bit(pir1,CMIF);
set_bit(pie1, CMIE);
set_bit(intcon, PEIE);
set_bit(intcon, GIE);
Disable

Code: Select all

clear_bit(pie1, CMIE);
Handler

Code: Select all

if (pir1 & (1 << CMIF))
{
FCM_%n(); // call selected macro
clear_bit(pir1, CMIF); // clear interrupt
}
You can then read back the comparitor values by using the following C code block and 2 variables named C1 and C2.

Code: Select all

FCV_C1 = test_bit(cmcon, C1OUT);
FCV_C2 = test_bit(cmcon, C2OUT);

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Re: Comparator module in flowcode

Post by MJU »

Thank you very much!

Post Reply