CHAPTER 6 Samples Introduction 6.1 Supplying the microcontroller 6.2 Macros used in programs Macros WAIT, WAITX Macro PRINT 6.3 Samples LED diodes Keyboard Optocoupler Optocouplering the input lines Optocouplering the output lines Relays Generating a sound Shift registers Input shift register Output shift register 7-segment Displays (multiplexing) LCD display 12-bit AD converter Serial communication Introduction Examples given in this chapter will show you how to connect the PIC microcontroller with other peripheral components or devices when developing your own microcontroller system. Each example contains detailed description of the hardware part with electrical outline and comments about the program. All programs can be taken directly from the from copied from 'MikroElektronika' internet presentation. 6.1 Supplying the microcontroller Generally speaking, the correct voltage supply is of utmost importance for the proper functioning of the microcontroller system. It can easily be compared to a man breathing in the air. It is more likely that a man who is breathing in fresh air will live longer than a man who lives in a polluted environment. For a proper function of any microcontroller, it is necessary to provide a stable source of supply, a sure reset when you turn it on and an oscillator. According to technical specifications by the maker of PIC microcontroller, supply voltage should move between 2.0V to 6.0V in all versions. The simplest solution to the source of supply is using the voltage stabilizer LM7805 which gives stable +5V on its output. One such source is shown in the picture below. In order to function properly, or in order to have stable 5V at the output (pin 3), input voltage on pin 1 of LM7805 should be between 7V through 24V. Depending on current consumption of device we will use the appropriate type of voltage stabilizer LM7805. There are several versions of LM7805. For electricity consumption of up to 1A we should use the version in TO-220 housing with the capability of additional cooling. If the total consumption is 50mA, we can use 78L05 (stabilizer version in small TO - 92 packaging for electricity of up to 100mA).