Saturday 4 August 2012

PC controlled Robot


Computers have become an integrated part of our life. We use them for almost everything. So i made a bot which can be controlled from a PC or laptop. It uses the USART feature of the microcontroller. It uses serial communication to communicate between the PC/laptop and the microcontroller.

USART

USART is Universal synchronous and asynchronous serial receiver and transmitter. In Atmega8 there are 3 registers used to control the USART and a UDR register to store the transmitted or received data. The control and status register are UCSRA, UCSRB, and UCSRC. I have explained only the necessary information. A detailed description of these registers is available in the datasheet.

 UCSRA
RXC-set if there is any unread data in the buffer and cleared if buffer is empty. Can be used to check whether reception of data is complete or in progress.
TXC-set if the entire data is sent out of the buffer and automatically clear once transmission is complete.
UDRE-if set denotes that the buffer is empty and ready to receive data.
Other bits are not important for beginners.

UCSRB
RXEN-should be set to enable USART receiver to receive data.
TXEN- should be set to enable USART transmitter to transmit data.
UCSZ2- UCSZ2 bits combined with the UCSZ1:0 bit in UCSRC sets the number of data bits in a frame the Receiver and Transmitter use.

UCSRC
URSEL- must be set to enable writing in this register.
UMSEL- selects between Asynchronous and synchronous mode. If set works in synchronous mode and in Asynchronous if cleared.
USBS – selects the no of stop bits. If set use 2 stop bits and 1 stop bit if cleared.
UCSZ1:0 – character size.

UDR
UDR is a data register is where the data to be transmitted is stored temporarily and sent. During reception the received data is written to this register.

USART Initialization

The USART has to be initialized before communication can take place. The initialization process consists of setting the baud rate, setting fame format and enabling the transmitter and receiver. All this can be done by setting the appropriate bits in the USART control and status registers.

Baud rate

Baud rate can be said as a common frequency both the PC and the microcontroller has agreed to communicate with. In reality both the PC and microcontroller would be operating at different so communication between them would be impossible. To enable communication both must agree on a common frequency called the Baud rate. In PC Baud rate is set in terminal program and in microcontroller the Baud rate is calculated using a formula and the value is store in the Baud rate registers UBBRH, UBBRL. An easy way is that the data sheet has the UBBRH, UBBRL values for various frequencies and baud rate which are already calculated so u can use those values directly.   

Frame Formats

The data which is sent from or to the microcontroller is sent in the form of a frame. A frame consists of a start bit which is always low followed by data bits which can vary from 5 to 9 bits in length. Then the parity bits which are used for error checking and it is optional. Finally the stop bit which can be 1 or 2 bits and are always high.
Terminal Program

A terminal program is software which is used to access the serial pot of ur PC. You can send and receive data through this terminal program. Some terminal programs are hyper terminal, teraterm, bary’s terminal. I ham using Bary’s terminal because it is simple and easy to use. In case of a laptop which has no serial port you can use a USB to serial convertor to enable serial communication.

Building the PC control bot.

Now u know how the USART works, let’s build our PC controlled bot. Use the simple bot u built and just add the circuit board to the bot. The circuit consists of a power source a 9v battery, voltage regulator 7805 to provide a constant 5v to the microcontroller and a motor driver L293D or LMD18200 to drive the motors. Get a terminal program and install it in ur PC or laptop. Using the Winavr write the code. These are the steps u should do while writing the code.
  •         Initialize the USART by setting the Baud rate, frame format and enabling transmitter and receiver.
  •        Set any one port as output by setting its DDRx to 0xff.
  •      Check for the received character and provide the corresponding output at the port.

Received character
Output
Motion
w
1010
Forward
s
0101
Reverse
a
0010
Left
d
1000
Right
  •      In the output the first 2 bits from left are for the left motor and the next 2 bits are for the right motor
  •      After writing the code, compile it and create the hex file and burn it into the microcontroller.
  •      Connect the output to a motor driver.
  •       Then connect the bot to ur PC. Open the terminal program and check for connectivity.
  •      If connected send ‘w’ the bot should move forward.
Bot with circuit board on it  

Here is the video of my PC controlled bot.

No comments:

Post a Comment