Cheap pushbutton replaces rotary switch
The problem of setting any one of four modes of a µC-operated process has a straightforward solution: Have the µC check the states of some 4 bits of a µC register or an input port and then execute one of the four predetermined subroutines according to the result. The next question is how the operator sets these 4 bits or, specifically, selects a desirable mode of operation.
One approach is to use a rotary switch (Figure 1a). This circuit does the job but is rather expensive for consumer applications. You can also reject DIP switches because even a qualified person can choose a wrong setting when using this solution. Slide switches are another possibility, but those in the catalogs have no more than three positions (SP3T), and this application requires four.
The four µC bits PA0 through PA3 serve as a mode register and simultaneously as a mode indicator via the LEDs. After the µC resets, the initialization routine puts a low level on PA0, which sets MODE 1 and lights the corresponding LED. All other LEDs remain off, and all the rest of the bits of Port A stay unchanged. For the intended project application, it is more convenient to organize the mode-switching process as an external interrupt and let the main program perform its other functions. When an operator pushes the normally open switch, SW1, the resulting negative edge at the IRQ pin of the µC causes an external interrupt. The interrupt-service routine shifts the low level to the next output pin consecutively from PA0 to PA3. When the routine reaches PA3, it goes back to PA0, and so on. The service routine has no effect on bits PA4 through PA7, and they remain unchanged. The debouncing delay is 54 msec.
The circuit in Figure 1 uses an inexpensive µC, but the program contains the standard instruction set and therefore is applicable to any µC.