Relay Control

I/O stands for Input/Output function. Example ATmega8 has 23 Programmable I/O Lines that can define as both input and output device. The I/O registers bellow-

Let’s build our first program that content

DDRx(Data Directions Register x) define the I/O initialization condition. If DDRx-> 1(set) the Output and DDRx-> 0(clear) Input.

Conditional operator
            expression ? True execution : False execution;
Example:-       x=(a>b) ? a:b;

If the expression is true than x=a

If the expression is false than x=b

In C language we can check whether the pin is logic 1 or logic 0. The C format is to check if the bit is set

C:\>          (register_name&(1<<bit_number))

Let check how it works-

            Suppose the default value of PINB=0b11110001. Now from the fundamental of C as we know non-zero is true and zero is false.

Similarly to check if the bit is clear

C:\>          !(register_name&(1<<bit_number))

Let’s go to our main program. In real life after we press a switch there is a debouncing effect. So we need some delay function for every time switch is pressed. AVR has build in function that control delay operation. 

Full Code->

In programming we use the delay function to avoid noise but in real life we can minimize the noise effect by using capacitor or RC/LC circuit that is called filter. Noise reduces the lifecycle of the switch so if possible use RC filer circuit.