# More Programs

## Draw a flowcode to display the binary equilavent of numbers from Zero to Nine

Unlike the decimal system, only two digits 0, 1 suffice to represent a number in the binary system. The binary ystem plays a crucial role in computer science and technology. The first 20 numbers in the binary notation are : 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111, 10000, 10001, 10010, 10011 and 10100, the origin of which may be better understood if they are re-written in the following way:

 1: 00001 11: 01011 2: 00010 12: 01100 3: 00011 13: 01101 4: 00100 14: 01110 5: 00101 15: 01111 6: 00110 16: 10000 7: 00111 17: 10001 8: 01000 18: 10010 9: 01001 19: 10011 10: 01010 20: 10100

In the case of digital circuit a high voltage level, for example 5v is represented by ‘1’ and low voltage level in most case represented by ‘0’ volt.

First we have to choose the ports: Here i am choosing PORTB. Since this is an eight bit port, the command PORTB=1 is equilavent to writing 00000001 to PORTB, ie the decimal number is converted to binary and then writen to the port.So the maximum possible number which can be writen to the PORTB is 11111111 = 255. Similarly in the case of PORTA(5 bit) the maximum number which can be writen to the port is 11111=31. Set PORTB pin as OUTPUT,and draw the flowcode as given below.

300px

In this case we are using a for loop. The loop will execute until the value of x becomes nine. when x becomes greater than nine then loop will exit. Circuit is given below,

400px

## Program to drive a dc motor to the left or right depending on whether PORTA,0 is high or low.

A D.C motor can be made to rotate to the left or right by changing the polarity of the d.c supply.Select two pins of PORTB as outputs and PORTA,0 pin as input. When the input is high,set PORTB,0 pin as high and PORTB,1 pin as low and vice versa.

300px

Since the d.c motors consume large current, we have to use a separate transistor circuit to drive the motor. Circuit is given below.

400px

When RB0 is high transistor T2 and T3 conduct; current path is battery → T2 → motor → T1 and gound. when RB1 is high transistor T1 and T4 conducts; current path is battery → T1 → motor →T4 and gound. Doide’s protect the transistor from back E.M.F.

## Design a counter using Microcontroller(counts from 0 to 9)

Input PORTA,0 pin is selected as input and PORTB as output.

Program logic: Declare two variables count and change. When RA0 is high count increases by one (count=count+1 ). Otherwise there is no increment. Consider the input source as square wave generator with a frequency of 1 KHz. The output willbe high for 500 millisec but the microcontroller operates at a frequency of 4MHz count will increase more than ones during a single pulse. First we check if change is zero if it is count is increase by one and change is set to one. The variable change become zero only when the clock become low. See the flowcode below.

300px

Circuit for simulation

400px

Practical cases replace the clock and include appropriate circuits, sample circuit below. The led light is cut the output will go high. We can use this circuit for counting number of persons entering the room etc.

500px

Components required IC LM 358, LDR, LED(red), Resistance(100k, 47k, 10k, 330 ohm and 220 ohm), preset(100k-2) and Capacitor(470 nF).