Sie sind auf Seite 1von 38

Copyright Robotix Team, IIT Kharagpur

February 3rd-5th, 2006

Copyright Robotix Team, IIT Kharagpur

A Tutorial on Robotics
Part IV : Microcontrollers & Computer Control

Copyright Robotix Team, IIT Kharagpur

AVR Family of Microcontrollers


8-bit RISC Architecture, single cycle instructions Principle features include
Internal oscillators, timers, UART, SPI, PWM generation, ADC, analog comparator and more Amenability to C programming In-System-Programmable (ISP) Flash Memory

http://www.atmel.com/products/AVR/

Copyright Robotix Team, IIT Kharagpur

ATmega8 AVR Family Member


Up to 16 MIPS throughput 8KB ISP Flash Memory, 1KB SRAM, 512Byte EEPROM 8bit / 16bit timers / counters Three PWM channels 8 Channel 10 bit ADC, Analog Comparator Programmable UART, SPI, TWI

Copyright Robotix Team, IIT Kharagpur

ATmega8 PDIP Package

Copyright Robotix Team, IIT Kharagpur

ATmega8 TQFP Package

Copyright Robotix Team, IIT Kharagpur

ISP Flash Memory


ISP : In-System-Programmable Flash ROM No need of specialized programmer hardware No need to remove processor from circuit for reprogramming

Copyright Robotix Team, IIT Kharagpur

STK200 ISP Dongle

PonyProg Programming Software http://www.lancos.com/prog.html

Copyright Robotix Team, IIT Kharagpur

WinAVR
WinAVR is a open source software development suite for Atmel AVR series of RISC microcontrollers GUI Integrated Development Environment Includes the GNU GCC compiler for C and C++. http://winavr.sourceforge.net/

Copyright Robotix Team, IIT Kharagpur

WinAVR IDE Screenshot

Copyright Robotix Team, IIT Kharagpur

Procyon AVRlib
AVRlib is a library of easy-to-use C functions for AVR controllers AVRlib aims to allow programmers to work quickly towards their end goal by reducing the time needed to write basic support functions and code http://www.procyonengineering.com/ avr/avrlib/

Copyright Robotix Team, IIT Kharagpur

PWM DC Motor Control


//Dual 8-bit PWM mode setup of timer 1 timer1PWMInit (8); // Frequency Prescaling to F_CPU_CLOCK / 256 timer1SetPrescaler (4); // Left and right motor speed set timer1PWMASet (lSpeed); // Pin 15 (OC1A) timer1PWMBSet (rSpeed); // Pin 16 (OC1B) // Start on the PWM generation timer1PWMAOn (); timer1PWMBOn ();

Copyright Robotix Team, IIT Kharagpur

ADCs & Analog Comparator


DDRA = 0x00; // Configure Port A (ADC) as Input PORTA = 0x00;// Disable internal pull up resistors // Initialize Analog to Digital Converter a2dInit(); // Set conversion speed a2dSetPrescaler(ADC_PRESCALE_DIV32); // Set analog voltage reference a2dSetReference(ADC_REFERENCE_AVCC); // Acquire analog data into digital format for (i = 0; i < 8; i ++) adcData [i] = a2dConvert8bit(i);

Copyright Robotix Team, IIT Kharagpur

UART - Serial Communication


// Initialize UART at 2400 bps uartInit(); uartSetBaudRate (2400); // Check is data received if (!uartReceiveBufferIsEmpty ()) { // if data received then get it in a variable uartReceiveByte (&dataIn); // do whatever u wish with dataIn }

Copyright Robotix Team, IIT Kharagpur

Crystal Oscillator Clock Source

Copyright Robotix Team, IIT Kharagpur

AVR Fuse settings


CKOPT 1 1 1 0 CKSEL3:0 1010 1101 1111 1011, 1101, 1111 Frequency (MHz) 0.4 0.9 0.9 3.0 3.0 8.0 >= 1.0 C1 & C2 12 22 pf 12 22 pf 12 22 pf

SUT 1:0 = 11

Copyright Robotix Team, IIT Kharagpur

Computer Control
Parallel & Serial Port Communication

Copyright Robotix Team, IIT Kharagpur

PC Parallel Port - Features


Commonly known as the printer port 25 pin D-Type connector It has 12 digital output pins, 5 digital input pins Pins operate at the TTL voltage level i.e. 0 5V Port identified by a base address in the computer I/O memory space

Copyright Robotix Team, IIT Kharagpur

PC Parallel Port Pin Layout

Copyright Robotix Team, IIT Kharagpur

PC Parallel Port I/O Registers


Most PCs have 378 Hex base address 8 output pins are accessed via the DATA port (Base address) 4 output pins are accessed via the CONTROL port (Base address + 2) 5 input pins are accessed via the STATUS port (Base address + 1)

Copyright Robotix Team, IIT Kharagpur

PC Parallel Port I/O Registers

- marked bits in register byte are unused

Copyright Robotix Team, IIT Kharagpur

Accessing Parallel Port VC ++


#include <conio.h> #define Data 0x378 // Base address #define Status 0x379 // Base address + 1 #define Control 0x37a // Base address + 2 int main () { unsigned char Bits; Bits = 0b11001010; _outp(Data, Bits); Bits = _inp(Status);/* return (0); }

/* output data */ input data */

Copyright Robotix Team, IIT Kharagpur

PC Serial Port - Features


Most commonly 9 pin D-Type connector. 25 pin D-Type is rare Pins operate at -25 to + 25 voltage levels Data transmitted as a bit sequence Known as the EIA RS232C port or simply RS232

Copyright Robotix Team, IIT Kharagpur

EIA RS232C Specifications


Logic 0 is between +3 to +25 volts Logic 1 is between -3 to -25 volts -3 to +3 volts region is undefined Maximum data rate of 19,600 bps

Copyright Robotix Team, IIT Kharagpur

Serial Port vs. Parallel Port


Serial cables can be much longer that Parallel cables
50V swing compared to 5V swing

Null Modem Configuration just needs 3 core cable compared to 19 core parallel port cable Serial suited for wireless transmission Microcontrollers just need two pins (RXD & TXD) for communication

Copyright Robotix Team, IIT Kharagpur

Null Modem Configuration


Pin Pin Pin Pin Pin 3 Transmit Data 2 Receive Data 5 Signal Ground 4, 6, 1 shorted together 7, 8 shorted together

Copyright Robotix Team, IIT Kharagpur

RS-232 Waveforms

Asynchronous Communication i.e. separate clock at both ends Frame synchronization done by the start / stop bit Line is kept logic 1 when idle. Start bit is logic 0 Bit order from LSB to MSB

Copyright Robotix Team, IIT Kharagpur

RS-232 Level Converters


For using the RS232 data, digital devices need TTL/CMOS voltage levels Level conversion is done using IC MAX232 / DS232

Copyright Robotix Team, IIT Kharagpur

RS-232 frame decoding


The decoding of the bit sequence frame is done by computer hardware When communicating with robots, decoding done by onboard microcontroller Microcontroller Serial communication interface is known as UART (Universal Asynchronous Receiver Transmitter)

Copyright Robotix Team, IIT Kharagpur

Accessing Serial Port - VB


MSComm Control 6.0 is used for serial port communication

Copyright Robotix Team, IIT Kharagpur

MSComm Control Properties

Copyright Robotix Team, IIT Kharagpur

MSComm Control Properties


Com Port identification number Settings: Baud Rate, Parity Check, Data Bits, Stop Bits Hand Shaking Method : Xon/Xoff for null-modem configuration

Copyright Robotix Team, IIT Kharagpur

MSComm Control Properties

Copyright Robotix Team, IIT Kharagpur

MSComm Control Properties


InBufferSize : Input buffer size OutBufferSize : Output buffer size RThreshhold : Receive size for interrupt generation SThreshhold : Min. transmit size InputLen : Number of bytes to be picked up from the input buffer EOFEnable : Search for EOF character

Copyright Robotix Team, IIT Kharagpur

MSComm Control Properties

Copyright Robotix Team, IIT Kharagpur

MSComm Control Properties


ParityReplace : Parity Error replacement character NullDiscard : Null characters sent to the receive buffer or not RTSEnable : Request to Send enable DTREnable : Data Terminal Ready

Copyright Robotix Team, IIT Kharagpur

Example Code
Receive Data Private Sub CommunicationTerminal_OnComm() Dim InputBuffer As String If CommunicationTerminal.CommEvent = comEvReceive Then InputBuffer = CommunicationTerminal.Input End If Use InputBuffer as per requirement End Sub Transmit Data If CommunicationTerminal.PortOpen = True Then CommunicationTerminal.Output = Hello End If

Copyright Robotix Team, IIT Kharagpur

Thank You
End of presentation