PDA

View Full Version : what is wrong am i doing in this code?



chitalula
- 15th March 2012, 19:43
in this code i want to increase the duty ratio until the input volts at RA1 is equal to the Vref, after while loop is true to light the led at portb.5. the problem here is the output is not increase and the duty ratio are not changing to make the output to vary, i need help here to make my code to do as i want


TRISA=255
TRISB=0
CMCON=%00000101

compOut VAR CMCON.7
VRCON=0
pauseus 10
duty var byte
duty=0
while (compout=0)
pwm 7,duty,1
duty=duty+1
wend
portb.5=1
pause 100

this is my circuit

HenrikOlsson
- 15th March 2012, 21:19
Hi,
The way you have CMCON configured the comparators are set up like this:
6347

You have your inputs connected to RA0 and RA2. CMCON.7, which you've aliased is the output of comparator 2...

In order to use the internal reference voltage module you must set the comparator mode to 110 instead of 101. Further on, you seem to think that RA2 is the reference input but it's not. It can optionally be the reference output. From the datasheet:

The output of the reference generator may be connected to the RA2/AN2/VREF-/CVREF pin. This can be used as a simple D/A function by the user if a very highimpedance load is used. The primary purpose of this function is to provide a test path for testing the reference generator function.

Finally, I can't seem to find a register called VRCON in the datasheet, it's CVRCON.

You need to decide what to actually do, either use the internal reference and set it up correctly or use external "reference" and connect it to correct pins.

/Henrik.

chitalula
- 16th March 2012, 05:23
Hi,
The way you have CMCON configured the comparators are set up like this:
6347

You have your inputs connected to RA0 and RA2. CMCON.7, which you've aliased is the output of comparator 2...

In order to use the internal reference voltage module you must set the comparator mode to 110 instead of 101. Further on, you seem to think that RA2 is the reference input but it's not. It can optionally be the reference output. From the datasheet:

Finally, I can't seem to find a register called VRCON in the datasheet, it's CVRCON.

You need to decide what to actually do, either use the internal reference and set it up correctly or use external "reference" and connect it to correct pins.

/Henrik.

thanx in advance, i want to use external refence , am taking deep reading on the data sheet then if i see misunderstang i will come back to you

chitalula
- 16th March 2012, 08:01
Hi,
The way you have CMCON configured the comparators are set up like this:
6347

You have your inputs connected to RA0 and RA2. CMCON.7, which you've aliased is the output of comparator 2...

In order to use the internal reference voltage module you must set the comparator mode to 110 instead of 101. Further on, you seem to think that RA2 is the reference input but it's not. It can optionally be the reference output. From the datasheet:

Finally, I can't seem to find a register called VRCON in the datasheet, it's CVRCON.

You need to decide what to actually do, either use the internal reference and set it up correctly or use external "reference" and connect it to correct pins.

/Henrik.
1. VRCON register is for internal Vref and is found within a data sheet
2. CM2:CM0=101 i found from the data sheet and is for "one independent comparator"
3. from the data sheet i found RA2 is the refereence input
4. i want to use external fererence as i show in my circuit , i used RA2 as reference
5. i use pic16f627a/628a/648a datasheet, i found that i configured well but i think the problems are in the loop, check well
6. still i have a problem Henrik, so ask help on this and it will be better when you stimulate this circuit so that you can see how it perfom wrongly

HenrikOlsson
- 16th March 2012, 09:11
Hi,
Two questions, only one needs answering:
1) Why on earth do you use the datasheet for the 16F628A when you're using the 16F877A?
2) If you actually ARE using the 16F628A why on earth do you post a schematic showing that you're using a 16F877A?

I'm sorry but you're not really making it easy for anyone to help you.

/Henrik.

chitalula
- 16th March 2012, 10:57
sorry Henrik early i used that chip but was difficult to understand, i uploaded that by mistake, 16f877a it was difficult to me thus why i leave that and i have choosen to work with 16f628a, help me by using the circuit below
6349

HenrikOlsson
- 16th March 2012, 15:40
Hi,
That would have been good to know so that *I* didn't have to waste time looking at the wrong datasheet when trying to help...

For the 16F628A with CMCON set to 5 you have one comparator enabled, positive input on RA2, negative on RA1. This matches your schematic.
CMCON.7 should be '1' when the voltage at RA1 is less than the voltage on RA2.

In your schematic you have a voltage divider connected to RA2. There's a supply symbol "feeding" the voltage divider but it doesn't say what voltage. Have you "measured" the voltage at the RA2 pin, what is the voltage? Is it 0V by any chance?

/Henrik.

PS. Why don't you wire up the circuit on a breadboard instead of that simulator? I know it's a great tool (I wish I could justify the cost) but sometimes, especially when you're new at this, it's just easier (and a lot better) to play with real life hardware.

chitalula
- 17th March 2012, 05:26
Hi,
That would have been good to know so that *I* didn't have to waste time looking at the wrong datasheet when trying to help...

For the 16F628A with CMCON set to 5 you have one comparator enabled, positive input on RA2, negative on RA1. This matches your schematic.
CMCON.7 should be '1' when the voltage at RA1 is less than the voltage on RA2.

In your schematic you have a voltage divider connected to RA2. There's a supply symbol "feeding" the voltage divider but it doesn't say what voltage. Have you "measured" the voltage at the RA2 pin, what is the voltage? Is it 0V by any chance?

/Henrik.

PS. Why don't you wire up the circuit on a breadboard instead of that simulator? I know it's a great tool (I wish I could justify the cost) but sometimes, especially when you're new at this, it's just easier (and a lot better) to play with real life hardware.

your right Henrik, input voltage at the RA2 is 3volts

/chitalula.
I will wire the circuit on the breadboard when the circuit do what i want. as you know that iam new on the world of programming. all what am doing now is just on the way to have boost converter with constant output current.
when the circuit with codes will be complete i will consult you to have that beadboard and other equipments. for now help me here so that i accomplish this task

HenrikOlsson
- 17th March 2012, 07:28
Hi,
The only thing I see in the code is that there is no main loop, if COMPOUT is 1 when it gets to the WHILE statement it sets PortB.5 and then just wanders off into la-la-land. I believe a real PIC would restart once it reaches the end of program memory but I have NO idea what the simulator does. Try

TRISA=255
TRISB=0
CMCON=%00000101

compOut VAR CMCON.7
VRCON=0
pauseus 10
duty var byte

Main:
duty=0

while (compout=0)
pwm 7,duty,1
duty=duty+1
wend

portb.5=1
pause 100
Goto Main

/Henrik.

chitalula
- 27th March 2012, 08:04
thanx in advance Henrik. now i want to continue to output constant duty ratio when the comparator input are equal, is it possible to output that constant duty cycle which make input equal to Vref?

HenrikOlsson
- 27th March 2012, 18:22
Hi,
Not sure I understand exactly what you mean. A comparator will only tell you if its positive input is EITHER above or below its negative input. There is no way to tell if the two inputs are "equal". What you COULD possibly do is to use both comparators and create a window comparator which will tell you if the input is below, within or above the window/thresholds.

Or use the ADC...

/Henrik.

chitalula
- 10th April 2012, 08:56
Hi,
Not sure I understand exactly what you mean. A comparator will only tell you if its positive input is EITHER above or below its negative input. There is no way to tell if the two inputs are "equal". What you COULD possibly do is to use both comparators and create a window comparator which will tell you if the input is below, within or above the window/thresholds.

Or use the ADC...
/Henrik.


up to now i dont need to continue to work with the comparator module because it fail to do as i want
i want to use adc but have already read the adcin command but how to use adc to compare the two inputs in the pic i stop there,
can you give me any sample code on that issue?

HenrikOlsson
- 10th April 2012, 10:18
Feedback VAR WORD
Reference VAR WORD

DeadBand CON 2 ' +/-, set to 0 if no deadband is needed.

Main:
ADCIN 0, Feedback ' Get feedback voltage from Channel 0
ADCIN 1, Reference ' Get reference voltage from Channel 1

' Now compare the feedback voltage to the reference voltage and include the deadband.

IF Feedback > (Reference + Deadband) THEN ' Feedback is higher than reference
' Do whatever is needed here.
ENDIF

If Feedback < (Reference - Deadband) THEN ' Feedback is lower than reference
' Do whatever is needed here.
ENDIF
Goto Main

chitalula
- 11th April 2012, 08:21
thaks in advance, am taking a time to concentrate with it and when i meet any difficult i will came back to you