Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Wait for key pressed

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
bjornj

Bascom Member



Joined: 30 Apr 2009
Posts: 33

sweden.gif
PostPosted: Wed Nov 23, 2022 10:50 am    Post subject: Wait for key pressed Reply with quote

Hello
Is there a command in bascom that let's the program stop and
wait for any key connected to a port to be pressed. That the port simply has
changed it's value.
I have looked at bitwait, waitkey, inkey getkbd but I don't think they
will do want I want to achieve, but I may be wrong there
Now I use a construction like "while port is FF wend" but I
think that is a bit clumsy even if its works ok.

(BASCOM-AVR version : 2.0.8.5 )
Back to top
View user's profile
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 1084
Location: France

france.gif
PostPosted: Wed Nov 23, 2022 11:25 am    Post subject: Reply with quote

Hi,
I do some test long time ago to understand interrupt



Code:
'JP Duval le 04/10/2004
' pour tous les exemples, j'utilise un Lcd aussi j'ai besoin de temps pour afficher
'c'est pourquoi j'utilise Wait et waitms si souvent.
'for all samples I write, I use a LCD so I need time to display and
' that why I use wait or waitms so often.

' 2° programme : interruption externe INT1 et INT0 sur le M8
' 2° sample : interrups INT0 and INT1 on M8

' Int0 = PortD.2
'Int1 = PortD.3
' montage:
'test setting:
'VCC-------------^^^^^^^---------portd.2
'                            10k
'VCC-------------^^^^^^^---------portd.3
'                            10K
' si PortD.2 ou PortD.3 est à mis à O le compteur correspondant s'incremente
' if PortD.2 or PortD.3 is forced to 0 the corresponding counter grows up.
'---------------------------------------------
Dim J As Word , K As Word
' -------[ config ]-----------------------------------
' les config doivent être écrites sans retour à la ligne.
'config must be writen on 1 lign
Config Lcdmode = Port       ' config  4 bit
Config Lcdpin = Pin , Db4 = Portc.3 , Db5 = Portc.2 , Db6 = Portc.1 , Db7 = Portc.0 , E = Portc.4 , Rs = Portc.5
Config Lcd = 16 * 2
Config Int0 = Rising       'could be falling, rising or low level!
Config Int1 = Rising
' ------------------------------------------------------
'nous n'utilisons pas le NOSAVE bien que cela consomme plus de mémoire, la
'récupération des registres si elle est mal faite, peut provoquer des catastophes.

'we dont use NOSAVE to avoid bad recovery of register
Enable Interrupts
Enable Int0
Enable Int1
On Int0 Suiteint0
On Int1 Suiteint1
Wait 1
Cls
Do
 Waitms 100
Loop
End       'end program

Suiteint0:
J = J + 1
Locate 1 , 1 : Lcd "int0: " ; J       'nb of int
Waitms 200
Return    'one Return and
Return    'another one to return on an interrupts  I'm not sure it is necessary ?

Suiteint1:
K = K + 1
Locate 2 , 1 : Lcd "int1: " ; K       'nb of int
Waitms 200
Return    'one Return and
Return    'another one to return on an interrupts


I hope it can help you
JP Wink

_________________
pleasure to learn, to teach, to create
Back to top
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2105

blank.gif
PostPosted: Wed Nov 23, 2022 4:35 pm    Post subject: Re: Wait for key pressed Reply with quote

bjornj wrote:
Now I use a construction like "while port is FF wend" but I
think that is a bit clumsy even if its works ok.

You have a special requirement, which is not covered by a pre-made function, so that's ok, but only if you use the PIN-register for while/wend.
Generally it's disadvantageous to block execution of other code for waiting for some keys.
Quote:
I don't think they will do want I want to achieve, but I may be wrong there

You're not wrong, but because of some other reasons:
Quote:
I have looked at bitwait, waitkey, inkey getkbd

- bitwait() as the name suggests, waits for a pin, not a port
- waitkey(), inkey() are used for serial UART communication
- getkbd() is used for a matrix of keys

Seems you've overlooked debounce(), which is used for single pins and can be extended for a port.
Debounce() does away with the problem of bouncing contacts, which may not be a problem for the while/wend construct, as long loop-time is long enough.
Back to top
View user's profile
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 1084
Location: France

france.gif
PostPosted: Wed Nov 23, 2022 5:38 pm    Post subject: Reply with quote

also
How many key ?
I did a keyboard with 24 keys, another one with 7 . whis this keyboards I wait until the key pressed is I is released because the users work without see the keyboard
I'dont use interrupt

the 7keys keyboard :

in the main prog
Code:
'--- confi keyboard-------------------------------------------
Config Portd = Input : Set Portd


then a loop : do ...loop

Code:
 Do
   
      Touche = Clef()

      Select Case Touche
         Case 1  
----
----  
end select
       


and the function clef()



Code:

Function Clef() As Byte
   Local Keyboard As Byte , Memoireclavier As Byte
   Key = 0
   Encore2:
      Waitms 20
      Memoireclavier = 0
      Do
         Keyboard = Pind
         Memoireclavier = Keyboard
         'Locate 2 , 16 : Lcd Memoireclavier
         Select Case Keyboard
            Case 254
               Key = 1
               Reset Portd
               Exit Do
            Case 253
               Key = 2
               Reset Portd
               Exit Do
            Case 251
               Key = 3
               Reset Portd
               Exit Do
            Case 247
               Key = 4
               Reset Portd
               Exit Do
            Case 239
               Key = 5
               Reset Portd
               Exit Do
            Case 223
               Key = 6
               Reset Portd
               Exit Do
            Case 191
               Key = 7
               Reset Portd
               Exit Do
         End Select
      Loop
      Reset Portd                                    
      Waitms 30
      Set Portd
      Do
         Keyboard = Pind
      Loop Until Pind = 255
      If Key = 0 Then
         Goto Encore2
      End If
      Clef = Key
End Function


JP Wink

_________________
pleasure to learn, to teach, to create
Back to top
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2105

blank.gif
PostPosted: Wed Nov 23, 2022 6:08 pm    Post subject: Reply with quote

Duval JP wrote:
I do some test long time ago to understand interrupt

JP,

for the purpose to understand interrupt it may be somewhat ok, for an example of human-machine interfacing it's a nightmare.

First, did you read the TO wants to check the whole port and not only two pins?
Therefor - if interrupts would be suitable at all - a processor providing Pin-Change interrupts would be suitable.
One PC-interrupt allows for watching a whole port.

Your code using slooow LCD-interface routines and waits within an ISR, another no-go, explaining it in the comments doesn't make it better.
An ISR must always be as fast as possible.

Your code is also prone not to work correctly with a bouncing key, as you do not clear the matching interrupt flag before returning from the ISR.

I will explain:
From raising level to the first command within the ISR it will take, depending on clock-speed, a few microseconds, while a key bouncing may last tens of milliseconds.
The interrupt flag will be cleared as the interrupt vector is executed, however a 'bounce' of the switch generates several raising flanks, thus the interrupt-flag is set again whilst the processor dawdles in the ISR.
Result is: The ISR executes immediately after return a second time, it executes twice.

This is improper use of 'Return' within an ISR:
Code:
Return    'one Return and
Return    'another one to return on an interrupts

An ISR must have only one Return, the second Return is useless and can be dangerous.
Reason is: Only the first Return creates exit code, which consists of restoring the processor registers, ending with an RETI.
RETI switches interrupts on again, whilst they are off within the ISR.

If you do something like that:
Code:
Suiteint0:
J = J + 1
Waitms 200
Goto mylabel
Return    'one Return and
mylabel:
PortB = 255
Return    'another one to return on an interrupts  I'm not sure it is necessary ?

then the second Return returns also from ISR, but without restoring used registers and without RETI.
Interrupts stay off, stack is used up, resulting in a nice program malfunction.
Code:
' I'm not sure it is necessary ?

I am sure it is not.
Quote:
some test long time ago

Such old test code may have suited you to learn, but it is not suited to teach.
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum