Simplest code to handle any key press - 8051 - microcontroller

I'm trying to figure out the best approach to my problem.
I have an AT89S52 microcontroller with 3 buttons connected to 3 separate GPIO pins so that the microcontroller can perform different functions based on those buttons.
I'm trying to write code that waits for the user to press any of the three keys. By key press, I mean where the system detects any one key is fully pressed down and then fully released.
The code I presented below might work if I added a schmitt trigger in my hardware but I don't want to redo my circuit board (again).
Without adding interrupts, is there a way I can modify the code shown with just a few lines to reliably detect a user key press?
I ask because keys experience a phenomenon called "bouncing" where as soon as someone presses a key, it actually jitters at high speed and the microcontroller will see it as key being pressed and released multiple times. I don't want that to happen if the user legitimately pressed the key only once.
;KEY1I, KEY2I and KEY3I = GPIO pins connected to the keys
;Pin value is low when key is held down
w4key:
;begin key scan
jnb KEY1I,w4keyend
jnb KEY2I,w4keyend
jnb KEY3I,w4keyend
;here, nothing is pressed so scan again
sjmp w4key
w4keyend:
;key pressed. Hope for release
jnb KEY1I,w4key
jnb KEY2I,w4key
jnb KEY3I,w4key
;here, key is released so return.
ret
mainline:
;do something
acall w4key
;do another thing
...

You can use a timer (AT89S52 has several timers, you can use one of them if there are not otherwise used in your project) and a synchronous state machine. The state machine has 4 states for every key and definite transitions. I found this link that explains the concept quite thorough. Although the provided example code in this link is in C, you can easily "translate" it to your assembly code. If you need help with this, just leave a comment.
https://www.eeweb.com/profile/tommyg/articles/debouncing-push-buttons-using-a-state-machine-approach

Related

What happens before Micro-controller Startup Code being executed? or Power-On/Reset Sequence?

What I know is as below and correct me if wrong, For the automotive bootloader based on any microcontroller, we will have
Startup code (Flash)
Primary bootloader (Flash)
Secondary bootloader (RAM)
As far as a power-on sequence is considered I know that,
From the startup code (provided by the micro vendor, Freescale, ST
Micro, etc.,) the control will be transferred to PBL (Primary
bootloader) using jump or function pointer.
PBL will download the SBL (Secondary bootloader) into RAM, which will
contain the flash driver, capable to download the application.
SBL will download the application into the flash area.
But what will happen before startup code is being executed or just after power on?
I know that each controller will have some sort of code to execute after power on POST (power-on self-test) but still not clear with sequence to operation till bootloader execution comes into execution.
It would be a great help if someone can provide a sequence of operations to reach startup code?
I find it this not uncommon confusion interesting.
POST is software in general, but your question is so vague. Usually when someone talks about POST they are talking about their x86 based computer, that is just software, happens well after the part you are confused about, and is in no way whatsoever required for a computer/processor to run, it has a purpose, adds value so it is there.
Microcontrollers in general do not have primary bootloaders nor secondary, they simply start running your application. Of the dozens/hundreds I have used/examined trying to think of any that have a primary or secondary. Can't think of any off hand. Certain brands in particular do have bootloaders that are usually programmed by them and you cant change or some that you can. How you get into the bootloader varies by brand, often a strap, sometimes a non-volatile bit in a register.
First off processors and the chip around it are dumb, very dumb. Only do what they are told by the humans. Incredibly simple machines. And while the difference between an mcu and a full blown system are at this view pretty much identical, the mcus are simpler and more reliable (for various reasons). The root of the answer starts with the processor or processor core or core or whatever term
might help you. In an mcu this is just one lego block in the whole of the chip, not necessarily even the largest block in the chip. When you look at arm based chips like the stm32 and others with a cortex-m (or older ones with ARMV7TDMI) that lego block is purchased ip from arm, the rest of the chip is either other purchased ip from one or more vendors or in-house made logic. the sram certainly and the flash probably is ip that the chip vendor buys for the specific process on the specific foundry (just like other cell library items, simple gates like AND, OR, NOT and more complicated gates).
Whatever processor core this is, it has an architecture and instruction set. While we know some architectures are implemented using microcode, unlikely that the mcus are, makes no sense the more cisc like might, but the arms and mips and such definitely not. But for this understanding it doesn't matter being microcoded or not there are bit patterns that drive the processor, machine code. We have all heard that chips are made of transistors, and they are. The transistors are part of the simplicity, the basic ones AND, OR, NOT gates you can look up on Wikipedia. You can (inefficiently) build the rest out of those fundamental blocks. A particular instruction tickles the logic, the transistors in a certain way to cause a chain of events, ones and zeros in a specific sequence that do the thing you asked. Logic is not limited to implementing processor instructions, most logic is not part of the decoding and execution of a processor instruction, most if it are equally dumb items. An sram is a lot of packed in bits (four transistors wired up a certain way per bit) with an address and data bus, the logic of an sram lights up rows and columns of these bits when writing or reading. Then there is more logic in front of that sram that decodes an address bus, etc.
As mentioned in the other answer, when power comes up then reset is released, the flip flop based items in the chip which are the registers we read in the manual plus countless others that are behind the scenes are set to their reset value which is done by wiring of the transistors. A number of state machines start which are similar to programs, but are hardwired. wait for reset to go high, once reset goes high then if this input to the state machine is this and that input to the state machine is that then I can move to the next state. The rules to get from one state to the next are implemented in logic. A chip with memory and flash for example might do a bist on the ram first, likely not in an mcu, doesn't make sense, this is logic not software doing this, this is not the post you think of in your laptop/desktop/server. The flash or ram or adcs or other logic might require some number of clocks to settle their logic before reset is released (the reset on the edge of the chip is not necessarily hard wired to all items in the chip, usually it is gated, delayed, etc). So there is a power on state machine that manages this, when the chip is ready then the processor itself will be released, this can be a few or dozens of clock cycles later. The clock itself has to settle, and the logic is designed to wait for that.
When the processor is released from reset it again may have some number of clocks to settle things in its design, it will have a state machine or many that start up the various blocks, and then based on the architectural design of that processor it does one of two things, fetches its first instruction from a known address (address within the processors address space which isn't necessarily the address in the chips view), or it uses a vector table approach and it reads a value from a known address, and that value read is the address of the first instruction and it fetches that instruction. Up to the first fetch there is no software, it is logic.
Depending on how the chip vendor has designed the chip, how they have defined the address space, and understand that addressing within a chip or board design is not some flat universal thing, to the programmer it is, but in reality it isn't. There are many busses with addresses and those address spaces are specific to that portion of the design. When you see the stm32 or others with a bootloader and a strap (boot0/boot1 pin), the logic on the other end of the processor bus may see a fetch at the well known address (meaning both the folks that implement the logic and the folks that write software for the logic know that this is the specific address where things start and if you don't put stuff there it won't boot/work) but as mentioned the chip vendor can do whatever they want with that and often do. As a programmer this can be easily understood as logic isn't any more magical than software:
if strap == 0 return flash_bank_0[address&mask]
else return flash_bank_1[address&mask]
For a certain address range that is decoded in front of this code, but also both banks may be directly addressable:
if address[24]==0 return flash_bank_0[address&mask]
else return flash_bank_1[address&mask]
And this way you can have what you see in the stm32s, that both address 0x00000000 and 0x08000000 or in other vendors chips 0x00000000 and 0x01000000 for example map to the same (flash) memory.
The reason being is that the cortex-ms is vector based, there is a table of addresses that point you at code rather than just instructions at known addresses (like the full sized arms arm7, arm9, arm11, cortex-a). The way you use that is you set your address for reset in the table to be 0x08000000 based so when the processor reads at 0x000000xx it is told to fetch instructions from 0x0800xxxx and it does. When the strap is the other way it finds a different flash which may or may not have a fixed space it may only be visible from the if-then-else. (pretty easy to see with a cortex-m and an SWD debugger and software).
The stm32s will have logic that if the strap is set to run the user application will fetch my guess is four words, if the first one or a specific one is all ones or for some chips all zeros (very often flash/rom resets to ones, because there is a logic in version saving a transistor, so the bit is a zero, but we see it as a one, the bits are all inverted, but this is not a hard and fast rule, just very common) the logic/state machine will, for the stm32 realize there is no user application and will load the bootloader. Now it is very possible the design actually always boots the bootloader and there is software there that looks at the application flash, but I think myself and others on this site decided that is not the case, but none of us work there nor have the visibility into the design. In either case the processor then starts executing what it finds and it is very dumb it is told fetch from this address and it does, the programmer had to make sure that stuff is at that address, and each and every instruction has to be laid out in order properly like train tracks, any gaps or mistakes and the trail goes off the rails, otherwise the train is stupid it just follows the tracks. As humans we call the software post or bootloader or application or whatever. It is just software. Once the processor is started if some software loads and runs other software the processor doesn't know it is stupid it just keeps performing the instructions it is fed as it rolls down the track.
Short answer:
Power ramps up to a chip specified level. At a chip specified time reset should be released. This releases state machines to get the chip ready as needed and release the processor. The processor based on its design either fetches its first instruction from a known place or it reads from a known place and that user planted value is the address where the first instruction lives. After that per the architecture of the chip the execution of that first instruction and fetching of more based on that instruction continue until it crashes or is turned off or put in reset.
There is no magic.
There are a number of good open cores out there that you can simulate with free tools and see (with free tools) the internal signals that make that chip work, you can see the post reset activity leading up to the first fetch and then all the execution from there.
Without knowing which microcontroller you are using, this should be general enough:
The hardware in the microcontroller resets several registers to their documented values. This includes the PC, the program counter.
If the microcontroller has configurable reset vectors the value can be chosen from a few alternatives, other controllers always use the same value.
The code at the location the PC points to is the startup code.
Note: It's always a good idea to read the data sheet of the controller!

Arduino how to detect if a key is held down or not

I'm trying to detect whether a key is pressed down or not.
I have a digispark connected to a rotary encoder that together form a volume control wheel. The thing is I want to change what rotating the encoder does without hard coding it, so i made a program in c# that communicates with the Arduino with the unused keyboard keys f13-f14, but if I hardcode those keys instead of the volume keys.
When I plug in the device in a computer that does not have the program the Arduino is useless. So I needed to find a way to communicate with the Arduino and tell it the program is there. This was hard because the digispark has no serial support.
But what if I use those unused keys, let's say f15 as a Boolean. I need code for the digispark(which is basically an Arduino) to find out whether f15 is held down or not(true or false).

Mapping a Key on the Keyboard to Exit Loop in Arduino

I want to create a function on the Arduino and put it in all the loops in my program such that whenever during use, the user can press the "Esc" key on the keyboard to cancel what it is currently doing. A serial monitor is being used with my current code.
My entire code is very long which is why I don't have it posted, but if you can answer the situation i described above, I will be able to implement it. You can imagine a simple loop running forever that will exit anytime the "Esc" key is pressed.

FPGA - Push button constraints

Is there any benefit/reason to apply a setup & hold constraint to a push button input to an FPGA when the button is asserted asynchronously?
From what I understand a violation can still happen regardless as the button can be pressed within the setup and hold time of the flip-flop that it's connected to inside the FPGA.
Push buttons generate very slow changing signals compared to the system clock (0.001 .. 10Hz vs. several MHz). Applying setup/hold times is a waste of effort. Just apply a timeing ignore rule.
A propper synchronizer and maybe a debouncer circuit is needed anyway.
I agree with previous poster that push button input pin shouldn't be timed (use false_path). If you want to be very safe, you should probably:
Turn on Schmitt trigger for the input pin connected to the button.
Feed the input signal through 2 stage synchronizer (2 flops in serial)
Implement debouncer either using analog circuit on the board, or doing this digitally using a counter after the synchronizer.

V-USB send special command

I've created a keyboard-like with v-usb. I wonder if it's possible to send special command like on my Logitech keyboard to open Calc, increase/decrease volume, shutdown PC, etc.
It ought to be achievable. First you may need to pretend to be a Logitech keyboard, in the event that certain scan codes are only acted on if the keyboard type matches.
Second, you would need to find out what the scan code is for those keys. You might get help at Scan Codes Demystified and USB IDs.

Resources