microcontroller - INPUT AND OUTPUT
Input and Output (I/O) of a micro-controller is the main and basic concept to be understood before programming. Micro-controllers use Registers (Seperate RAM or Part of RAM assigned for the I/O data. status etc.) to send or receive data to/from the external pins. So, these Registers has to be programmed to function the pins as digital input or digital output or analog input or analog output etc. Some Registers are used as buffer to send or receive the data.
Digital Input and Output:
Almost all the pins may be used as either digital output or digital input signal pins. The direction of signal (i.e., input or output) has to be declared once in the specific Register, before using the pin for the purpose. The reading (for input) or writing (for output) of signal may be used any number of times throughout the program by reading from or writing to the specific register(s).
If the pin is set as output, then the pin number and signal high/low will be passed as arguments (parameters) to the function. In case, same pin is set as input, the pin number is set as argument (parameter) to the function and the function returns the value, high/low, at that instant (or moment).
Analog Input or Output :
Some of the micro-controllers support Analog Input and/or Analog Output. Similar to Digital Input and Output, Analog Input or Output should be declared and initiated once, using some specific set of commands, before calling the function to read or write Analog value. Some of the pins only support Analog Input or Output, which are marked for ADC/DAC pins (unlike Digital Input/Ouput).
Analog to Digital Converter (ADC) : The micro-controller reads Analog input in volts, that is applied at the pin set as Analog Input and converts it to digital code. The digital code size or accuracy varies from 8 bit to 16 bit. So many types of algorithms are used to convert the Analog input to Digital data, depending on the micro-controller.
Digital to Analog Converter (DAC) : Some micro-controllers support DAC, which will send a specific analog voltage as per the digital data set in the specific register. The required registers has to be initiated before using DAC functions of the micro-controller.
Pulse Width Modulation (PWM) : PWM is a ouput feature supported by some micro-controllers. The output is similar to digital output, but, the time of HIGH signal and time of LOW signal, continuously varies, which depends on the values set in the PWM control register(s). By using this feature, the speed of a DC motor or brightness of a light etc., may be varied without changing the supply voltage to them.
The allowed amount of current flow through the pins of a micro-controller is very less, which may be approximately 3mA (please refer datasheet of particular micro-controller). So, the micro-controller may drive an LED through a current limiting resistor. To drive heavy loads like motors, coils, relays etc., a transistor or MOSFET or driver IC has to be used. There is every chance to damage the micro-controller, if heavy current (more than the limit) are flowing through the pin(s).
The syntax and keywords may vary for the micro-controller and IDEs (integrated Development Environments) used for programming.