Page 1 of 1

Port Pin Toggle

Posted: Sat Sep 21, 2013 3:29 pm
by asbase
Please advice

While 1
portb.0=!portb.0
delay 100ms
end while

portb.0 connect to led.

flowcode simulator work but real hardware fail.

Thanks.

Re: PORT PIN TOGGLE

Posted: Sat Sep 21, 2013 5:13 pm
by kersing
Please post your flowchart so we can take a look at it to see what the issue might be.

Re: Port Pin Toggle

Posted: Sun Sep 22, 2013 10:26 am
by Kenrix2
Since I am new to Flowcode V5 I am both curious and confused by your post. Is this this what you are talking about?

Re: Port Pin Toggle

Posted: Sun Sep 22, 2013 1:15 pm
by brandonb
first you have to turn off the analog selects, then use tris to make pin output, then use toggle like this, this is just to toggle b0 pin, with out any other pins accounted for

anselb=0;// disable bport analog module
trisb&=~(1<<0);// set b0 as output
latb&=~(1<<0);// set b0 as low
while(1){
latb^=1<<0;// toggle b0 pin
delay_ms(100);// delay 100 ms
}

Re: Port Pin Toggle

Posted: Mon Sep 23, 2013 4:10 am
by asbase
This one also not working as simulator in the real hardware. First of all, it can turn off but cannot turn off without go into another switch. Please comment.
Thanks.

Re: Port Pin Toggle

Posted: Mon Sep 23, 2013 4:12 am
by asbase
brandonb wrote:first you have to turn off the analog selects, then use tris to make pin output, then use toggle like this, this is just to toggle b0 pin, with out any other pins accounted for

anselb=0;// disable bport analog module
trisb&=~(1<<0);// set b0 as output
latb&=~(1<<0);// set b0 as low
while(1){
latb^=1<<0;// toggle b0 pin
delay_ms(100);// delay 100 ms
}
Can we just do it without C in simple way?

Re: Port Pin Toggle

Posted: Mon Sep 23, 2013 6:04 am
by brandonb
Can we just do it without C in simple way?
yes, sorry about that, i thought that was what you were going after, i noticed the mikroc coding,
to toggle a bit just make the pin or port an output with a flowcode output icon, then use this if your gonna toggle the bit on B0

Code: Select all

 latb^=1<<0;
don't know if this is already defined in flowcode but you can put this in the supplimentary code window in project options under defines

Code: Select all

#define togglebit(port,bit) port^=(1<<bit)
then you can do this in c code

Code: Select all

togglebit(latb,0);

Re: Port Pin Toggle

Posted: Mon Sep 23, 2013 8:23 am
by Kenrix2
Hello asbase,
I tried your program in hardware using a different micro and it works just like in simulation. You have the switches set for active high so each will need a pull down resistor, I usually use 4.7k to 10k ohm. Could you double check your wiring, configuration settings and clock speed. I am not familiar with the micro your using, is there anything unusual about the ports?
The reason for the "C code" being mentioned was because of your post using "portb.0" which sure confused me.

Re: Port Pin Toggle

Posted: Wed Sep 25, 2013 2:06 pm
by asbase
Kenrix2 wrote:Hello asbase,
I tried your program in hardware using a different micro and it works just like in simulation. You have the switches set for active high so each will need a pull down resistor, I usually use 4.7k to 10k ohm. Could you double check your wiring, configuration settings and clock speed. I am not familiar with the micro your using, is there anything unusual about the ports?
The reason for the "C code" being mentioned was because of your post using "portb.0" which sure confused me.
my problem here is.
switch 1 press, led 1 on
switch 2 press, led 2 on
switch 2 press again, led 2 on (not toggle to off)
switch 2 press again remain the same...
switch 1 press, led 1 off
switch 1 press, led 1 remain off
switch 2 press, led 2 off

the switch only functioning after back from another switch....

on my 18f4550

how about your?

Re: Port Pin Toggle

Posted: Wed Sep 25, 2013 2:53 pm
by medelec35
Hi asbase,
I have attached a Flowchart which should allow more switches to be added if necessary.
There are other ways of doing this, If you want an alternative then let me know.

Martin

Re: Port Pin Toggle

Posted: Wed Sep 25, 2013 11:30 pm
by Kenrix2
Hello again asbase,
When I said your program works "just like" in simulation, I really meant "exactly". Your text description of how it operates does not match how it simulates or how it works in hardware. Nothing is supposed to happen when the switch is pressed, only on release. So I can only conclude your using toggle type switches that are installed upside down or your using momentary switches that are normally pulled high instead of low. Perhaps you could attach a schematic so we see where the problem is. I hope you try medelec35's solution (thank you for sharing that medelec35) since it seems to be what your really after. The problem with working with microcontrollers is that they do exactly what you tell them to do. The trick is to get them to do what you want them to do. Good luck with your project.