Home | Store | Showcase | Forums | Examples | Guides | Reviews | Support | Download | About Us

Beginner's Robotics on $50 a Month - Online Project Instructions, Part 4


CIRCBot

Part 4: Serial, Sonar and Servos

This project accompanies Part 4 of Servo Magazine's series, Beginner's Robotics on $50 a Month.  It covers serial communications, controlling servos and determining distances using Sonar.
The first part of the article can be found in the February issue of Servo Magazine.

The Part 4 kit is available here.

Serial Communications

This month, we will use serial communications to pass commands and data between two ATMega48 microcontrollers.  Serial communications is a commonly used with microcontrollers; between a PC and the microcontroller and/or between a microcontroller and peripheral devices. Serial uses 2 wires for communication plus a common ground. The transmit line (TX) from the first microcontroller is connected to the receive line (RX) of the second and the transmit line (TX) of the second microcontroller is connected to the receive line (RX) of the first.  For our use, we will call our first microcontroller the primary or hub and the new microcontroller we added will be called the secondary or spoke. 


Figure 1 - Serial Communication

Data is always sent from the TX port to the RX port.  Since both devices have a TX port and an RX port, communications are bi-directional (two-way).

Processor Speed and Serial Communications

By default, the Mega48 microcontroller is running at 1MHz. At this speed, any BAUD rate over 4800 has a high error rate. We are using 9600 BAUD so we have to change the speed the processor is running. Changing the speed requires changing the microcontroller's fuse bits.  

A word of caution - setting the fuse bits incorrectly can cause the microcontroller to stop functioning. Do not change fuse bits unless you understand their purpose. If this happens, please email support@wrighthobbies.net for assistance. 

Fuse bits are special settings that tell the microcontroller how to behave at the hardware level. Fuse bits control the processor frequency, the source of the clock signal, enabling brown out detection and protecting the firmware from changes.  To change the frequency from 1MHz to 8MHz requires one fuse bit  and one line of code to be changed.

First, let's change last month's code. Open CIRCBot_sensors_v2a.bas and look at the 10th line:

$crystal = 1000000 

Change it to:

$crystal = 8000000

Click on Program/Compile from the menu. The program should compile without errors and is ready to be loaded into the microcontroller.  Follow the same procedures for loading the code outlined in the previous articles. 

 

 

Continue >>