## Latching switches?

Any general or miscellaneous queries that do not fit into the other forum catagories

Moderators: Benj, Mods

Gaz
Posts: 18
Joined: Thu Dec 11, 2008 10:02 am
Contact:

### Latching switches?

Morning all,

As I'm still at the "getting to know flowcade" stage I thought I'd try and expand some of the tutorials in order to understand more of the commands.
So, I'm trying to modify the "input from switches" tutorial so that the push to make switches will latch the LED's in the on state until another switch is closed where it will then be turned off and the second switches LED will light.

I'm at a loss as to how to do this at the moment, any ideas folks?

cheers

Gaz

Benj
Matrix Staff
Posts: 14130
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4325 times
Been thanked: 4070 times
Contact:

### Re: Latching switches?

Hello Gaz

You might want to look at this.

http://www.matrixmultimedia.com/Learnin ... Flowcourse

It is our free online course on Flowcode and will probably teach you a lot about using the software and programming in general.

Benj
Matrix Staff
Posts: 14130
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4325 times
Been thanked: 4070 times
Contact:

### Re: Latching switches?

Bit manipulation of a byte can be done like this.

Bit shifting Left

say you have this 8-bit variable byte = 0b00000001

to bit shift left we could do the following.

byte = byte * 2

or

byte = byte << 1

They both will manipulate the byte to look like this byte = 0b00000010

TomasS
Posts: 75
Joined: Wed Oct 28, 2009 2:37 pm
Has thanked: 15 times
Been thanked: 1 time
Contact:

### Re: Latching switches?

Benj wrote:Bit manipulation of a byte can be done like this.

Bit shifting Left

say you have this 8-bit variable byte = 0b00000001

to bit shift left we could do the following.

byte = byte * 2

or

byte = byte << 1

They both will manipulate the byte to look like this byte = 0b00000010

In C, you can do bitmanipulation on a port like this: PORTA = PORTA ^ 0x80 to switch bit 7 on portA on and off. But how do you do this operation in Flowcode?

medelec35
Valued Contributor
Posts: 8323
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2380 times
Been thanked: 3391 times
Contact:

### Re: Latching switches?

Hiya Gaz.
Try this.
There is a very simple modification to Tut 06
If you push a switch, then release it, Only the corresponding LED Will light and stay latched. All other LED's will be off.
Al I did was add a decision branch:
If Switches > 0
send Value of Switches to portA
otherwise don't.
Switches.jpg (69.65 KiB) Viewed 7443 times
Attachments
TUT_06 Modified FlowcodeV3.fcf
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

medelec35
Valued Contributor
Posts: 8323
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2380 times
Been thanked: 3391 times
Contact:

### Re: Latching switches?

Hiya TomasS
The Very simplest way to toggle bits of a port in flowcode is like this:
Bit Toggle.jpg (88.55 KiB) Viewed 7442 times

Or you can control individual bits of a byte by:
Bit_Toggle = (Bit_Toggle XOR 0b11111111) AND 1
in a calculation box.
The value of 1 on the right is the bit of the byte you want to toggle.
E.g to toggle bit 3 use Bit_Toggle = (Bit_Toggle XOR 0b11111111) AND 4
These users thanked the author medelec35 for the post:
TomasS (Mon Jan 03, 2011 8:22 pm)
Rating: 5.56%

Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

TomasS
Posts: 75
Joined: Wed Oct 28, 2009 2:37 pm
Has thanked: 15 times
Been thanked: 1 time
Contact:

### Re: Latching switches?

medelec35 wrote:Hiya TomasS
The Very simplest way to toggle bits of a port in flowcode is like this:
The attachment Bit Toggle.jpg is no longer available

Or you can control individual bits of a byte by:
Bit_Toggle = (Bit_Toggle XOR 0b11111111) AND 1
in a calculation box.
The value of 1 on the right is the bit of the byte you want to toggle.
E.g to toggle bit 3 use Bit_Toggle = (Bit_Toggle XOR 0b11111111) AND 4

Hi medelec35

Thanks for responding. I have tried to use your example, but using Timer0. Simulating it in Flowcode - no problem!, but when I downloaded it into my target, nothing happens. I've tried both on an STK500 and in E-blocks. Same result - no action

I am tryin to replicate the small C-program attached. Perhaps you can see the problem?

BR.

Tomas
Small C-program made in Code Vision
Try out in Flowcode

medelec35
Valued Contributor
Posts: 8323
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2380 times
Been thanked: 3391 times
Contact:

### Re: Latching switches?

The problem I can see are:
You have selected the whole port and not the single bit as in the picture I have up loaded & you have set port B to 128.
Since ! = Not a single bit, then the 128 will go to 0, then will toggle from 0 to 1
Since you have enabled the whole port then only LSB (bit 0) will toggle.
Looking at C file what you want is to change Port_B = !Port_B
to
Port_B= (Port_B XOR 0b11111111) AND 128
And change
Port_B = 128
to
Port_B = 0
(if your interested in toggling whole port and not just a sing bit, instead of Port_B = !Port_B
use Port_B = ~Port_B)

One last thing. I don't know if it has changed by my importing your AVR file into PIC Flowcode. Your timer interrupt Clock source settings (accessed by double click on interrupt then select properties) is set for 'Transition on T0CKI pin' and should be on internal clock. (that's if AVR setting are similar to PIC)
My advice is run the flowcode simulation for a while, and see if correct LED is flashing.
If it is, but not flashing on your hardware, then problem could be with hardware or configuration settings.
Check the usual like wdt and lvp is disabled etc.
Change code temparary so the LED flashes without using timer interrupt i.e use delay component icons, then if led flashing at correct rate, then change back to timer interrupt.
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

TomasS
Posts: 75
Joined: Wed Oct 28, 2009 2:37 pm
Has thanked: 15 times
Been thanked: 1 time
Contact:

### Re: Latching switches?

Hello again medelec35

Thanks for you reply. I have tried out your suggestions. Now the LED is flashing as wanted by changing the calculation done inside the macro to Port_B= (Port_B XOR 0b11111111) AND 128. I did forget to select the clock source prescaler to 1:1 for TMR0
Unfortunate I were able to simulate the program in Flowcode before uploading the program to the Forum.

Now I need to get the timing right. That isn't quite as easy as in the C-program uploaded.

BR.

Tomas

medelec35
Valued Contributor
Posts: 8323
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2380 times
Been thanked: 3391 times
Contact:

### Re: Latching switches?

Your getting there, LED flashing is better then nothing happening at all.
The problem is the C code you posted, has got important information missing.
E.g timer0 interrupt settings. When you find that information, it would be easy to count from 6 to 250.
Although it says 250 I was a bit puzzled with:
if(++timecount == 1000)
Where is the 250?
It looks like counting from 6 to 1000, not 250?
TomasS wrote:I did forget to select the clock source prescaler to 1:1 for TMR0

That's easily done since the simulator will treat the flowchart as if it is set on internal clock, whether its set on internal clock or external pin.
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

TomasS
Posts: 75
Joined: Wed Oct 28, 2009 2:37 pm
Has thanked: 15 times
Been thanked: 1 time
Contact:

### Re: Latching switches?

medelec35 wrote:Your getting there, LED flashing is better then nothing happening at all.

medelec35 wrote:The problem is the C code you posted, has got important information missing.
E.g timer0 interrupt settings. When you find that information, it would be easy to count from 6 to 250.
Although it says 250 I was a bit puzzled with:
if(++timecount == 1000)
Where is the 250?
It looks like counting from 6 to 1000, not 250?

It's because the xtal-frequency is 4MHz. Then I devide the clock by 8, getting a period of 2μs. Therefore the counter is to start counting from 6, generating an interrupt each 500μs. Timecount is then incremented each 500μs and then toggles the LED 2 times pr. second!


TomasS wrote:I did forget to select the clock source prescaler to 1:1 for TMR0

That's easily done since the simulator will treat the flowchart as if it is set on internal clock, whether its set on internal clock or external pin.[/quote]

I don't really know if that's an advantage? By running a program that seems to work in the simulation, but a small omission will do that nothing works

Learning by doing...

BR.
Tomas