Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Simplistic reaction timer

Status
Not open for further replies.
Does sword fish have a on change interrupt?
Swordfish has whatever interrupts you code it to have, just like C.
You pretty much have to do the exact same steps you would do using XC8.

I don't know what happens when you are paused in the ISR and another interrupt occurs
An ISR won't interrupt itself unless you're dumb enough to reenable GIE before returning, which you should NEVER do.
If you have interrupt priorities enabled (you're using both ISRLOW and ISRHIGH) then a high-priority interrupt can override/interrupt a low-priority one,
but otherwise the ISR will run until it hits the end and it executes a RETFIE which reenables interrupts and returns.
If a pending interrupt request flag (xxxxIF) is set (and its xxxxIE bit is enabled) you'll immediately jump back into the ISR.


MrDEB- two things...
Code:
INPUT(portb.4)
portb.4 = 1
For the umpteenth time, will you PLEASE STOP WRITING TO PORT PINS THAT YOU HAVE MADE AN INPUT.

and second-
Remove all your interrupt code. Forget about using interrupts. The monkeys will complete the Bible, Gone with the Wind, and all 32 volumes of the Encyclopedia Britannica before you ever code this properly. It's not an IF-THEN statement.
 
I got it to work by adding in a COUNT sequence and a simple DEBOUNCE sub route.
The INTERRUPT is gone, didn't like it in there anyway.
SF basic code

Yellow1 = 1
IF button1 = 0
THEN
debounce()
Count = 1
END IF

DELAYMS(1000)
TOGGLE(Yellow1)
DELAYMS(1000)



Yellow2 = 1
IF button1 = 0
THEN
debounce()
Count = Count + 1
END IF

DELAYMS(1000)
TOGGLE (Yellow2)
DELAYMS(1000)


Yellow3 = 1
IF button1 = 0
THEN
debounce()
Count = Count + 1
END IF

DELAYMS(1000)
TOGGLE (Yellow3)
DELAYMS(1000)

IF Count > 1 THEN
redlight()
END IF
now the yellow leds light in succession and if the button is pressed before the green then the red led is enabled. All the delayms statements are to keep the leds lit when needed Jon.
 
What does debounce() do?

You're already going to wait 2 secs between every read of a button press, even if it wasn't pressed.
 
...All the delayms statements are to keep the leds lit when needed Jon....

MrDEB, I know why you have the delay statements (some of them anyway). Unlike you, I also know that (without a properly constructed interrupt) nothing will happen if you press a switch during those delays. If you need time something (i.e., how long an LED is on) AND respond to switch events, you simply cannot do it that way.

So how do you do both? You can have exactly one short delay in your loop (or use a timer to trigger the loop) of say 1mS. Then you do something like:

Code:
If LEDOn = true then
     inc(LEDOnTimeCount)
     if (LEDOnTimeCount > LEDOnTime then
               LEDOn = False
               LED = (whatever state turns it off)
               LEDOnTimeCount = 0
     end if
end if

LEDOnTime is the time you want the LED to be on in mS, say 2000 if you want the LED on for 2 seconds.

Doing it this way, the switch will respond within a mS of being pushed. Remember, you can have exactly ONE delay statement, and keep track of everything else with a counter.

You realize you can edit after you post, right? And you can see that your attempts at code tags don't work? So you could see that and fix it? All you need is the word code inside square brackets at the start, and /code in square brackets at the end.

You also realize writing code is more than stringing random commands together hoping somehow they do what you want? You need to be able the THINK your way through the code. "If I have this 2 second delay, what happens when tbe button is pressed?" Nevermind. That requires a level of understanding that has not been demonstrated.

Geez oh grief. Another half an hour of my time on earth gone typing something MrDEB will skim over and ignore. Hallelujah!
 
I tried the code without the debounce sub route and the button press was spuratic (didn't register every time).
not sure where the 2 seconds to read the button press
You're already going to wait 2 secs between every read of a button press, even if it wasn't pressed.
the first delay turns on the led, the second delay could be maybe removed??
going to try out Jons suggestion.
NOTE I have been using the help file to get the code where it is.
The debounce sub route is just a simple 100ms delay. tried without it but not work very well.
 
Swordfish has whatever interrupts you code it to have, just like C.
You pretty much have to do the exact same steps you would do using XC8.

.
I dont use XC8, not that it makes a difference to what i was asking. Actually i asked the question wrong! But you answered it in a way that gives me the info anyway :D. I switched from pics to sil labs stuff, the nearest i get to XC8 now is Kiel when i use 8051 chips, so its very close close.
In C18, which was what i used to use, you set up the ISR and chose high or low vector. My understanding could be wrong but XC8 you dont do alot of the old set up? Anyway in Swordfish your saying things like the priorities etc are all catered for? From your answer to my badly framed question, unlike the old C18 there is no messing with putting a splodge (technical term) of ASM in then?

Tumbleweed I could go look, but I would prefer it if you would so kind to link me to the most recent docs to swordfish. I have a habit of finding out dated stuff! Anyone got any idea what the limits on the free version version are, in particular for the 18f4685 chip? I am tempted to give this a try, my little sister is 9 soon and smarter than me, i started at 11 and she is looking to make pink leds flash in Barbies car. Yes i could do it for her but she actually wants to do the clever bit and have me do the soldering.

I started with C, but i cant get mplabx working properly on my machine and dont really want it on the linux machines as they are not in the house. Also in some ways i think Basic might suite her better than C. Any pitfalls i need to know about? silly gotchas like X module dosnt work on a Sunday? I fancy a bit of play with this with my little sister. From vague long ago memory, can you still write your modules etc easily?
I used swordfish maybe 5-6 years ago, but i think it was at a time when the software wasnt mature like now. (yeah ironic compared to MPLABX!).

How much is the full version BTW?


Mr Deb, jons way is 100% correct, but sometimes the other way of doing a delay is just going through the program and adding a counter for each loop around, depends what your doing. I tend to use things like that mainly for heart beat leds, that way you know the program is actually looping and the led not just responding to a timer.

Interrupts are good, but its a walk and run thing, while your still crawling stay away from them. It isnt that they are difficult, but you need to follow sound logic and do things in specific ways, or it gets very easy to become locked up!

For a reaction timer you should have nailed this long ago, you have enough projects under your belt to be able to detect a switch and turn leds on and off. If your really having this much trouble then i suggest the following.

From memory you have a Junebug? And again from memory i think sword fish even had a junebug module?? Get yourself back on the Junebug and work your way through nice and easy simple things using the Bug as a dev board (its intended purpose). I am not being offensive but your running all over the place and learning nothing. Yes a reaction timer in man cave is ok, but your offering to do things you cant actually do. Take a step back and learn properly, its what EVERYONE did!

Every one turned a led on, the eventually off, then made it flashed in all manner of ways, start with easy stuff. I dont get why people think flashing leds is boring, you learn a great deal. A led is a great indicator, you can use program loops and timers and all maner of ways to flash them, but stick with each method until you have it nailed blindfolded . Interrupts for me would the last method i would use to flash it as its a bit more involved.

Then using the bug move on to the switches on the bug, again leave interrupts until last. Get a servo as well, some of the most helpful code ever on this forum was Mikes servo tester (although its in C). But study the code as its well explained line by line in the code. Once you can port that code over to swordfish (you dont need to know C to understand it), you will have a good understanding of code. Mike put a routine in the code that debouces by comparing what my memory thinks he called keys at the time.

He reads the ports and stores the value then compares it, the code dosnt move until the change stops. Its a real elegant way to debounce, the big problem with delays is literally time. Why bother running a micro at 8Meg when you stop it every few lines for a couple of seconds?

Going back to the Junebug and working through the board will help you no end, it even has the ability to act like a TV remote control! I know you say you got your way of learning, but truth is your not learning.
You get projects done because others pour code into threads, but that dosnt help you understand how to write code, I am saying this because considering the volume of projects you have done, turning leds on and off in sequence with a button press, should really be absolutely second nature by now. Infact that is my entire point, you cant do the number of projects you have done, then declare you learn your way. Leds are the first thing anyone learns so if you struggle doing it....... Time to change the learning ;)
 
The code in this post is in swordfish basic on the Junebug. That was 9 years ago and nothing has changed.

Mike.
Yeah Piezo sounders OUCH! everyone thinks your killing the cat :D, cue AG with a preamp to get the best out the sounder :p
 
LG,
For your sister swordfish would be good and it'll be a long time until she outgrows the free version. If you've still got your Junebug then she can start right now.
Another possibility is an Arduino. A Nano can be had for around £2.50 and is pretty straight forward to get started with.

Mike.
 
LGM, like Mike said, the limits of the free version of Swordfish are very generous - I think (too) many people never graduate to the paid version.

All of the modules are written in Swordfish Basic, so they can be modified. If changes are needed, it's usually to adjust for some parameter of a new chip that doesn't match the conventions of past chips. I know there have been issues which a two-byte parameter doesn't use consecutive bytes and other oddball things. Jerry on the Swordfish forum is the expert at making modules work.

Swordfish Basic main page

The full version is £50, which has come down a lot.

LGM, for you, I'd be happy to send a couple of my TAP-28 boards which support most 28 pin 18F-series parts. Or I'd be happy to send you the Gerbers for the latest version. You can see the documentation here. The latest version adds a CH340G UART-USB chip. I'm not selling the boards any longer - it doesn't make sense when you can have 10 made and delivered for 20 bucks or so.
 
LG. Buy or steal a crappy old Intel PC with 2-4 Gig ( Presario ) , Put Linux mint on it and MPLABX 4.10 xc 1.45 nothing else ! no games , no email , no forums...turn OFF wifi , attach PICkit3 and it will all be fantastic...
 
Anyway in Swordfish your saying things like the priorities etc are all catered for? From your answer to my badly framed question, unlike the old C18 there is no messing with putting a splodge (technical term) of ASM in then?
Yes, no asm required. ISRs are handled by an 'Interrupt()' type function, and if you want to use the two priorities of the pic18, they are handled as well... one 'Interrupt()' per priority.

The only limitation of the free version (SwordfishSE) is that it only supports a single bank of ram, so you have a max of 256 bytes to play with.

One caveat is that the included libraries are quite old. Since mchip changes the peripherals with almost every device it's quite probable that you'll have to make some changes at some point. Unlike in C, the SF 'libraries' are really just BASIC source code modules so doing the change is relatively easy... no rebuilding required.
 
The debounce sub route is just a simple 100ms delay. tried without it but not work very well.
If debounce() is just delayms(100), then there's almost no way that the code you posted works any differently with or without calls to debounce().
You're already waiting 2 secs between checking for a key press as you turn the leds off/on, so another 100ms basically isn't going to do squat.
 
Thx for the info, as it happens i do still have the bomb proof junebug! Also she wants to have a go at python, not my personal best so we can that together, i thought of arduino but i got pics coming out my ears!!

:D thx for the offer John.
 
Also she wants to have a go at python
Ugh!

While it's handy as all get out, whoever thought that the idea of whitespace being significant must have really enjoyed the days of FORTRAN and punch cards.
 
I am still trying to understand Jon's suggestion
text:If LEDOn = true then
inc(LEDOnTimeCount)
if (LEDOnTimeCount > LEDOnTime then
LEDOn = False
LED = (whatever state turns it off)
LEDOnTimeCount = 0
end if
end if
this looks like it might work but INC THE LEDONTIME? where is the loop?? Am sure I am missing something and the button press?
 
Guys have some respect for python!! We got an Indian rock python called Simon (just dont ask). Someone mentioned Ruby as well but know zero about it.
 
LG. Buy or steal a crappy old Intel PC with 2-4 Gig ( Presario ) , Put Linux mint on it and MPLABX 4.10 xc 1.45 nothing else ! no games , no email , no forums...turn OFF wifi , attach PICkit3 and it will all be fantastic...
Need wifi to swap chip families! I might have an old laptop i can try and run it on. At the moment i cant even download mint!!
 
I am still trying to understand Jon's suggestion

this looks like it might work but INC THE LEDONTIME? where is the loop?? Am sure I am missing something and the button press?
Eah?
What loop?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top