Microcontroller Programming

What is an interrupt in a microcontroller?

An interrupt is a signal that makes the processor pause its current work, run a short piece of code called an interrupt service routine (ISR), and then go back to what it was doing. It lets a program react quickly to events without checking for them all the time.

How it works: 1) An event occurs, such as a button press, a timer reaching its value or data arriving. 2) The processor saves its place and jumps to the ISR for that event. 3) The ISR does its short task, such as setting a flag. 4) The processor returns to the main program. The alternative, called polling, checks the input again and again.

Keep ISRs short and fast. Do long work in the main loop, and be careful with variables shared between the ISR and the main program.

What event would you handle with an interrupt in a project of yours?

How do I read a button with a GPIO pin, and what are pull...

A GPIO pin is a general-purpose pin on a microcontroller that your program can read or set as high or low. To read a button, you connect it so the pin sees a clear high or low level when pressed or released.

The main ideas: 1) A pin with nothing connected “floats” and can read random values, so you add a pull-up resistor (to the supply voltage) or a pull-down resistor (to ground). Many microcontrollers have internal ones you can switch on in software. 2) With a pull-up, the pin reads high when the button is open and low when pressed. 3) Real buttons bounce: the contact opens and closes quickly for a few milliseconds. Debouncing, in hardware or software, ignores these quick changes so one press counts once.

A common software approach is to accept a change only after the pin has stayed stable for a short time, such as 10 to 50 milliseconds.

Have you wired a button to a microcontroller before?

Category