PDA

View Full Version : what is wrong in this code



chitalula
- 7th March 2012, 12:27
i want to light the led at portb.0 when the output of comparator is high and to light led at portb.1 when comparator output is low, my code compiled successful and on running this code light a ked at portb.1 always without depending on the output of the copmarator, the output of the comparator doesn't depend on the inputs...what is wrong hear

this is my code and i attach my circuit



C10UT VAR CMCON.6
ADCON1=7
CMCON=2
PORTB=%00000000
TRISB=%00000000

lop:
if c10ut=1 then
portb.0=1

else
portb.1=1

endif

goto lop
end

sayzer
- 7th March 2012, 13:02
@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _BODEN_OFF & _LVP_OFF & _CP_ON

CLEAR

TRISA = 255
TRISB = 0



' Comparator Control
CMCON=100101
CompOut var CMCON.7

' CM7 C2OUT Comparator Output 2 (Read Only)
' CM6 C1OUT Comparator Output 1 (Read Only)

' Voltage Reference Control
VRCON = 0
Pauseus 10 ' VREF to settle.


PORTB = 0

LED_A var PORTB.0
LED_B var PORTB.6


Start:
led_a = compout
led_B = ~compout

pause 1

goto start

end


6330

chitalula
- 7th March 2012, 19:19
i want to light the led at portb.0 when the output of comparator is high and to light led at portb.1 when comparator output is low, my code compiled successful and on running this code light a ked at portb.1 always without depending on the output of the copmarator, the output of the comparator doesn't depend on the inputs...what is wrong hear

this is my code


C10UT VAR CMCON.6
ADCON1=7
CMCON=2
PORTB=000000
TRISB=000000
lop:
if c10ut=1 then
portb.0=1
else
portb.1=1
endif
goto lop
end

HenrikOlsson
- 7th March 2012, 20:45
When C1Out is 1 you set PortB.0 HIGH and when C1Out is 0 you set PortB.1 HIGH. But there is no code in the loop to set either of the outputs LOW again.


lop:
if C1Out = 1 THEN
PortB.0 = 1 ' One on
PortB.1 = 0 ' The other off
ELSE
PortB.0 = 0 ' On off
PortB.1 = 1 ' The other on
ENDIF
GOTO lop

chitalula
- 8th March 2012, 01:35
When C1Out is 1 you set PortB.0 HIGH and when C1Out is 0 you set PortB.1 HIGH. But there is no code in the loop to set either of the outputs LOW again.


lop:
if C1Out = 1 THEN
PortB.0 = 1 ' One on
PortB.1 = 0 ' The other off
ELSE
PortB.0 = 0 ' On off
PortB.1 = 1 ' The other on
ENDIF
GOTO lop

yah HenrikOlsson help me on that edit where necessary so that it can work as i want, iam a begginer in the pics progrmming, thanx indvance

chitalula
- 8th March 2012, 01:40
yah HenrikOlsson help me on that edit where necessary so that it can work as i want, iam a begginer in the pics progrmming, thanx indvance

sayzer (http://www.picbasic.co.uk/forum/member.php?u=2126) i tried to run your but it didn't do anything, i don't know why. and i need a comparison in my code can you add too?

HenrikOlsson
- 8th March 2012, 06:11
yah HenrikOlsson help me on that edit where necessary so that it can work as i want, iam a begginer in the pics progrmming, thanx indvance
I already DID just that. The code in my previous message IS edited so it should work the way I think you want.

chitalula
- 8th March 2012, 07:25
I already DID just that. The code in my previous message IS edited so it should work the way I think you want.

i tried to do as you told me but it didn't work as i want, led at portb.1 is always on even if the voltages at RA0 pin is changed. it seems the comparison is not working on the if statement, still have a problem.
thanx in advance

sayzer
- 8th March 2012, 09:38
The code and the circuit in my post works with no problem in simulation.

chitalula
- 9th March 2012, 07:04
yah HenrikOlsson help me on that edit where necessary so that it can work as i want, iam a begginer in the pics progrmming, thanx indvance

thanx for your suggestion. on stimulation if the input voltage equal to vref else statement always and if the input voltage is higher than or less than Vref else statement is executed too. it seems the "if statement" is not functioning, have you any solution on that case, i have no way to go forward , help me please

HenrikOlsson
- 9th March 2012, 09:57
I'm pretty sure that the IF statement executes as it should. The problem is likely that the C1Out doesn't actually toggle.

According to your schematic in the first post you have one input to RA0 and the other to RA2. As far as I can see in the datasheet (http://ww1.microchip.com/downloads/en/devicedoc/39582b.pdf), section 12.1, the inputs to comparator 1 is RA0 and RA3.

/Henrik.

chitalula
- 9th March 2012, 14:21
Thanx alot henrikolsson and sayzer for all of your suggestions, the problem was in configuration of cmcon, now it work as i want, thanx in advance

HenrikOlsson
- 9th March 2012, 15:29
How can it work when the two inputs, according to your schematic, are wired to two different comparators? What did you change CMCON to to make it work?

chitalula
- 12th March 2012, 09:26
How can it work when the two inputs, according to your schematic, are wired to two different comparators? What did you change CMCON to to make it work?
HenrikOsson my big problem was in CMCON register configuration, i leave my previous circuit and i took the circuit uploaded by sayzer. and i edit some code there by puting my own code