We are going to dig out some simple logics out of all these complexities of programming syntaxes, otherwise called grammar of programming language. At this moment let us stop the study of grammar for a while. It is possible to implement different types of logics within the knowledge of last three sessions. In this session let us try to use the commands which we have already learned, to implement different tasks.
We often see flashing of LEDs in a series which give an effect of running light from one LED to other, when it is kept in a line. The program below realizes this task. It needs only a different logic with no new commands other than we discussed. Now let us think about the logic working behind this program.
Here we are using a property of binary numbers. The series given below gives the basic idea.
In the 'Result' part we can see the position of '1' is transfered to the left by a single bit position, in adjacent steps. In the 'Process' part result of the above process is multiplied with '2' to find the new result. It is logical to represent the whole calculations through the following equation.
i=i*2 where 'i' is an integer variable
The value of 'i' is updated each time the statement executes.
Now let us think about the real situation with LEDs. Consider the 8 bits in the 'Result' part represent the data in 8 pins of B Port, which is connected to 8 LEDs. It is interesting to see that the output obtained now was our aim.
Now going back to the logic part. Since 'i' is an integer variable, its value will increase greater than 0b10000000, if the process continues. But we have only 8 LEDs connected. So if the value reaches 0b10000000, it needs to be reset to 0b00000001 to restart the operation. Thus LEDs will flash continuously.
It is time to think about program coding. The value of variable 'i' (declared as integer) is set to '1' initially. There is no need to discuss about the functioning of each statement since we are familiar with all of them in previous sessions.
Here also a 'for loop' is used to obtain the desired delay in between flashing of each LEDs. Since the value of 'i' represent the Result part in the series, 'i' is directly copied to PORTB. Because Result part is the desired output. After having the desired delay, value of 'i' is updated through the equation: i=ix2. An 'if condition' is given to limit the value of 'i'. When 'i' reaches 0b10000000, it is reset to 0b00000001 which enables the program to work from the beginning of the cycle.
Many kinds of logics can be developed like this to make different patterns. Another program producing a different pattern is given below. The changes in this program from the previous program are colored red. Readers can dig into it and find the output.
As my friend asked last day, Is blinking LED is the only function of these costly microcontrollers? The answer is a big 'no' with a small 'yes'. The matter of fact is that microcontrollers can only generate these kind of small signals having a voltage ranging around 5V with a little mA current. We need additional circuit arrangements to do other physical needs.
For example, if we need to drive a motor, we need a motor driving circuit. This circuit is fed by the microcontroller signal to know the rotating direction of motor. The power for driving the motor is given to the circuit separately. Now we can understand the functioning of a fully automatic washing machine. Its different motors are rotated at different timings, at different speeds and at different directions. All these functions are controlled by processors and hence we say it has 'artificial intelligence'. We can compare our processor to the brain of a human body. It produces signals to organs and physical work is done by the organs. However the possibilities of AVR microcontrollers are beyond limits. It gives many more facilities in a ready-made package to make the programming simpler. Let us discuss about them in the upcoming sessions.