Upload
kristian-dawson
View
215
Download
0
Embed Size (px)
Citation preview
Micro-Controllers &
Robotics
Robotics Club
IT-BHU
Agenda
Session I• Introduction to Microcontrollers(uCs)• Introduction to Compiler & Downloader• Atmega16 Components• Programming of PORTs• Glowing LEDs n making dancing patterns• Introduction to Motors & Motor Drivers• Controlling Motors using Atmega16• Lab Session
Session I• Introduction to Microcontrollers(uCs)• Introduction to Compiler & Downloader• Atmega16 Components• Programming of PORTs• Glowing LEDs n making dancing patterns• Introduction to Motors & Motor Drivers• Controlling Motors using Atmega16• Lab Session
Session II• Introduction to IR LED & Rx• Making IR Sensor• Using IR sensor with ATmega16• Introduction to ADC• Programming of Inbuilt ADC• Making Line Follower Robot• Lab Session
Session II• Introduction to IR LED & Rx• Making IR Sensor• Using IR sensor with ATmega16• Introduction to ADC• Programming of Inbuilt ADC• Making Line Follower Robot• Lab Session
Session III• Introduction to LCD• Programming of LCD• Intro & Prog of Timers• Making Digital Clock • Lab Session
Session IV• Doubt Clearing• Completing programs / projects• Algorithms for various Robots• Advanced topics – Keypad &
Communication
What is a uC ?
In simple words -- a single chip Computer
Introduction
Computer
• uP• RAM• HDD• Clock• Ports (Serial/USB)• Ports (Parallel)• Mic/Headphone Jack (ADC/
DAC)• Power Supply Unit• Reset• Mother-board
uC
• uP• Flash Memory• EEPROM• Clock Unit• SPI / UART Controllers• Digital I/O Ports• ADC• Power Supply Unit• Reset• IC
Introduction
From uP to uC
1. uP2. Memory3. Oscillator4. Buffers5. ADC6. Comparator7. UART8. Interrupt controllers9. SPI10.Two Wire connector11. JTEG Connectors
Atmega16L : An Overview
8-bit Micro-cotroller
40-pin DIP
32 Programmable I/O Lines
Operating Voltages 2.7 - 5.5V
Speed Grades 0 - 8 MHz
16K Bytes of In-System Self-programmable Flash program memory
512 Bytes EEPROM
Two 8-bit Timer/Counters with Separate Prescalers and Compare Modes
One 16-bit Timer/Counter with Separate Prescaler, Compare Mode, and Capture Mode
8-channel, 10-bit ADC
Programmable Serial USART
Master/Slave SPI Serial Interface
Programmable Watchdog Timer with Separate On-chip Oscillator
On-chip Analog Comparator
Pin diagram
Block Daigram
Simplified Diagram
4 8bit Parallel Input-Output ports through which you can i/p or o/p digital data.
Port Architecture
PORTA
DDRA
PINA
uC External world
Programming the Ports
Normal C program
int x;float y;
x= 10;y= 8.97;
Program for Ports
DDRA = 0b11111111 or 0xFF // o/pDDRC = 0b00000000 or 0x00 // i/p
PORTA = 27; // decimalPORTC= 0b11010110; // binary
DDRx defines whether the port will act as input port or o/p port. Just as ‘int’ declares a variable as integer type. It does not assign any value to it.
PORTx assigns the value to be output. If DDRx=0 ie port is defined as input port, PORTx will store the value but will not o/p it.
More Examples
PORTA=0x5A ;PORTA=0b01011010;PORTA=5;
DDRA=0xFF;DDRA=0b11110000; // pins A7-A4 as o/p & A3-A0 as i/p
You can even address individual pins of any PortDDRA.3= 1; // Only Pin 3 of Port A (A4) is configured as o/p pin , rest untouched
PORTA.0=1; // pin 0 of Port A outputs 1, rest retain there previous values
Taking Inputs
To take Digital i/p from external world first configure the port as i/p port using DDRx=0
Use PINx to read the digital value.
x = PINA // read 8 bit integer value from Port A. 0 < x < 255y= PINB
x,y must be defined as unsigned integers in ur C program.
Individual pins can be read in the same way.
x= PINA.2 // as the individual pins are read n the value is digital, therefore x can either be 0 or 1
y= PINC.6
Complete Program
A program to o/p 33 (hex) on PortD and configure and read pins 2 and 7 of Port A
#include <mega16.h>
void main( ){unsigned int x,y;
DDRD=0xFF; // all pins o/pDDRA=0b01111011; // pin 7 & 2 i/p rest immaterial
PORTD=0x33;x=PINA.2;y=PINA.7;}
LED
+
-
+ V
GND
Glowing LED
PortA
+
-01
To glow LED set PORTA.0=0;PORTA.1=1; or PORTA=0b00000010;or PORTA=0x02;
Glowing LED contd..
PortA
+-0
1
LED Panel on PCB
GND
+V
Blinking Pattern
While (1) { PORTA=0xFF; delay_ms(500); PORTA=0x00; delay_ms(500);}
While (1) { PORTA=0xFF; PORTA=0x00;}
Complete Program
A program to make a blinking pattern
#include <mega16.h>#include <delay.h>
void main( ){
DDRA=0xFF; // all pins o/p
While (1) {PORTA=0xFF; // all LEDs ONdelay_ms(500);PORTA=0x00; // all LEDs OFFdelay_ms(500);}
}
Motors &
Motor Drivers
Motors and Motor Drivers
Types Motors AC Motor DC MotorStepper MotorServo Motor
DC motor
Stepper Motor
• Similar principle to DC motors• More accurate control than DC motors• Rotate in fixed angles• Speed control is in our hands• Multiple coils – so excitation done in
pattern for rotation
Stepper Motor
Servo Motors
• DC motors with built in gearing and feedback Control loop
• Generally rotate through 90 and 180 deg. (360 deg also available)
• Used for extreme precision
Why do we need the motor driver?
The total output current that Atmega16 can provide is 200mA. But motors need much higher current to run. Therefore a driver circuit is required which provides the necessary current.
Moreover the o/p from uC can either be 0V or +5V, but motors come in various ratings 2V-36V (typically small ones). Atmega cannot provide this voltage hence we need external supply to drive motor depending upon i/p from uC
Basically a motor driver connects or disconnects the motor from some external supply depending upon the i/p from uC.
If the uC sends ‘1’ , driver connects the motor to some external supply ( battery in our case) thus motor actually runs by drawing current from the battery. If the uC sends a ‘0’ , o/p is connected to ground and hence the motor does not get the supply.
Motor Driver
I/p 1 ( O/p from uC)
I/p 2
I/p 3
I/p 4O/p 4
O/p 3
O/p 2
O/p 1
If uC send ‘1’ to I/p 1, O/p is conn to + VPPGND
+VPP
10
If uC send ‘0’ to I/p 1, O/p is conn to GND
Working of Motor Driver
I/p 1 ( O/p from uC)
I/p 2
I/p 3
I/p 4O/p 4
O/p 3
O/p 2
O/p 1
If uC send ‘1’ to I/p 2, O/p is conn to + VPPGND
+VPP
10
If uC send ‘0’ to I/p 2, O/p is conn to GND
Working of Motor Driver
Similarly other 2 o/p are connected /disconnected by I/p 3 & I/P 4
All the O/p’s operate independently, ie if all I/p 1-I/p 4 are 1, all O/p1 – O/p4 will be connected to +VPP
L298 can provide 1A current / Output channel , therefore total of 4A. But heat sinks should be installed for such high currents .
VPP can be anything in the range 2V- 46V , therefore any motor can be driven even though Atmega provides only 0/5 V.
L298N Motor Driver
ATMEGA
MOTORDRIVER
MOTOR
4 Inputs 4 Outputs
To drive a motor
A motor has two wires : +ve & -ve. To drive it u need to apply a voltage between themLets connect +ve wire to O/p1 & -ve to O/p2 of L298
If we give I/p1 =1 & I/p2 = 0, O/p1 will be +VPP & O/p2 GND
The voltage diff between two wires of motor = +VPP , therefore it will run in one direction
If we give I/p1 =1 & I/p2 = 1, O/p1 will be +VPP & O/p2 +VPP
The voltage diff between two wires of motor = 0, therefore it will NOT run
If we give I/p1 =0 & I/p2 = 0, O/p1 will be GND& O/p2 GND
The voltage diff between two wires of motor = 0, therefore it will NOT run
If we give I/p1 =0 & I/p2 = 1 , O/p1 will be GND & O/p2 +VPP
The voltage diff between two wires of motor = -VPP , therefore it will run in reverse direction
Driving a Motor
Lets Connect I/p 1 to PortB.0 & I/p 2 to PortB.1
As these ports will o/p data from uC, therefore their DDR should be 1
DDRB= 0b00000011;Or DDRB=0x03;
For running in forward dir, PORTB.0 =1 & PORTB.1=0
PORTB=0b00000001;Or PORTB=0x01;
For running in reverse dir, PORTB.0 =0 & PORTB.1=1
PORTB=0b00000010;Or PORTB=0x02;
Sample program for running motor forward for 1s , reverse for 1s & stop for 1s in sequence
#include<mega16.h>#include<delay.h>
void main(){DDRB=0x03;// other part included by CVAVR comes here
while (1){ PORTB=0x01; // forwarddelay_ms(1000);
PORTB=0x02; // reversedelay_ms(1000);
PORTB=0x00; // stopdelay_ms(1000);}}
Complete Program
Session II
Sensors
• A sensor is a device which measures a physical quantity and converts it into a signal which can be read by an observer or by an instrument.
• They are used to provide feedback from the external environment
IR Sensor Pair
Transmitter (Tx)
Receiver (Rx)
IR sensor PairObject / Line
Transmitter = LED( Light Emitting Diode)
Receiver = Photodiode
Principle of Operation
R1< R2
WHITE surfaceMaximum reflectionT1 is turned ONVout is LOW
BLACK surfaceMinimum or No ReflectionT1 is turned OFFVout is HIGH
Interfacing IR sensor with ATmega16
ATMEGA
PORTA
Out: 1
Out: 0
In
10
2
Programming IR sensor with ATmega16
#include <mega16.h>
void main(){
int x;
DDRA=0b00000011; // last two pins as o/p for Vcc & GNDPORTA.0 =1; // VccPORTA.1= 0; // Gnd
x= PINA.2;
// now you can use x for any calculation u want}
• Vout is ANALOG
But uC being digital can only read it as 0 or 1.
0 if Vout < 1.2 V
1 if Vout > 1.2 V
However most applications require much higher resolution n multiple levels.
Analog to Digital Convertor (ADC)
• The ATmega16 has an inbuilt 10 bit ADC (1024 Levels)
• 8 Multiplexed Single Ended Input Channels
• 7 Differential Input Channels
Registers used in the ADCADMUX – ADC Multiplexer Selection
RegisterADCSRA – ADC Control and Status
Register A
These registers are used to configure ADC
However when using CVAVR this will be done by code wizard
Configuring ADC using Code Wizard
1. Open a new project2. Select ‘yes’ to use code wizard3. Goto ADC tab on Code Wizard
ADC
Configuring ADC
Set the parameters as shown or as per your requirement.
Finally Generate Save & Exit
Programming ADC
When you generate the program after setting the code wizard you will a find a function
unsigned int read_adc(unsigned char adc_input) declared & defined just after #include
It accepts an Unsigned character as argument which is basically the hex address of the pin on which Analog i/p is applied. However we don’t need to give the address but just the name of pin and the conversion is done by compiler.
Return value is the digital value ( 0-1023 or 0-255) of the analog i/p.
Programming ADC
To use ADC just call the function read_adc( ) as shown
unsigned int s; Argument specifies which pin the analog i/p (ie o/p of sensor) is connected
s = read_adc(PINA.2) ;
S can now be used for any calculations. Its value ranges from 0-1023 (if Use 8 bits was NOT checked in configuration) 0-255 (if Use 8 bits was checked in configuration)
Interfacing IR sensor with ATmega16
ATMEGA
PORTA
Out: 1
Out: 0
In
10
2
GND
76
Sample program
This program glows Red Led if the digital value is less than 200 and green otherwise.
#include<mega16.h>#include<delay.h>
read_adc as generated by Code Wizard
Void main ( ) {unsigned int x;
DDRA=0b11000011; // bits 0,1,6,7 as o/p coz dey will o/p data to glow LEDs
while(1) { PORTA=0b00000001; // glow only IR LED , rest switched offx=read_adc( PINA.2); // read analog i/p & conv to digital value
if ( x<200)PORTA.7=1; // glow RED LED if val <200else PORTA.6=1; // glow GREEN LED if val > 200
delay_ms(500);}
Points to note
ADC is multiplexed only on Port A. Therefore analog inputs can only be applied to Port A.
The LEDs used in prev example can be connected to any port A/B/C/D as they require a digital o/p.
Even the Vcc & GND of IR sensor can be connected to any other port as they simply require 1 & 0 to be outputted. However it is advised to connect the Vcc & GND pins of sensor directly to the Vcc/GND points on PCB or battery so that uC is not loaded. (although it consumes more power as LED is always ON)
As there are 8 pins on Port A, therefore max of 8 sensors can be connected,(with their Vcc & Gnd connected to directly battery).
For more than 8 sensors, external MUX is to used.
Line Follower
Line Follower is a robot that can follow a black line drawn on a white/bright surface or a white line on black/dark surface without any human intervention.
It is the simplest autonomous robot.
Line Follower : Design
The bot has two IR sensors facing downwards (towards the floor).
The separation between the sensors is such that in normal (correct) position of the bot , both the sensors are on white surface. ie separation > strip width
The bot is driven by two wheel differential drive ie the two back wheels are driven by independent motors.
SleftSright
Line Follower : Algo
When the bot is in correct pos, both sensors are on white & read LOW. The robot should move forward in this case.
Sleft Sright Movement
LOW LOW Forward
SleftSright
Line Follower : Algo
When the bot is over line from right side, left sensor is on white & reads LOW whereas right sensor is on black & reads HIGH The robot should take a right turn in this case to come back in correct pos.
Sleft Sright Movement
LOW HIGH Right Turn
SleftSright
Line Follower : Algo
When the bot is over line from left side, right sensor is on white & reads LOW whereas left sensor is on black & reads HIGH The robot should take a left turn in this case to come back in correct pos.
Sleft Sright Movement
HIGH LOW Left Turn
Sleft
Sright
Line Follower
Sleft Sright Movement Left Motor Right Motor
LOW LOW Forward Forward Forward
LOW HIGH Right Turn Forward Back
HIGH LOW Left Turn Back Forward
ADC Advanced topic
Differential Mode
Single Ended Vs Differential Mode
In single ended mode the absolute value of the voltage (wrt to ground) applied to the ADC pin is converted to digital.
But if we want to measure differential voltage ie the voltage difference between two points and convert this difference to digital value.. Like in digital voltmeter …
What are the options ?
Single Ended Vs Differential Mode
Option 1:
X = read_adc(PINA.0)y = read_adc(PINA.1)
D= x-y;
But each conv takes 13 cycles. So total time = 26 cycles or 26us
Moreover the quantization error of two i/p gets added, hence ‘d’ has double error in it.
Differential Mode
Atmega provides an efficient way of doing this
In Differential Mode, the difference in the analog voltage between two pins is quantized, instead of the absolute value of potentials on each pin.
To use ADC in Differential mode, only the value of ADMUX is to be changed.
Session III
LCD
It is basically an array of LEDs.
Already has a inbuilt diver/controller, so you just need to specify the location and character to be printed.
Two broad divisions:-• Character LCD’s : ex calculators• Graphic LCD’s : ex Mobile phones, Xerox Machines.
Commonly available config for character LCD’s16 X 2 , 20 X 2 , 40 X 4
The LCD that has been given to you comes under character LCD and has 2 rows with 16 columns each. The coordinates mapping is shown below.
0,0
0,1 15,1
15,0
It has 16 pins whose description in given on the next slide.
Character LCD
Pin Configuration of character LCD’s
All the connections for LCD are already printed on PCB, except you have to connect them to any port. As shown below
LCD ATMEGA
0 1 2 3 4 5 6 7
Connect this to any of the ports but the pin alignment must be there, ie PORTx.0 should be connected to ‘0’ shown above and so on
LCD Commands
lcd_clear( ); clears the LCD
lcd _gotoxy(col,row); places cursor at cordinates (col,row) , see slide2 for cord
lcd_putsf(“ Hello”); prints the string on LCD. If the string is more than 16 chars it overflows to next line
lcd_puts(d); prints the string in array d on LCD.(simlar to puts function in C) If the string is more than 16 chars it overflows to next line
lcd_putchar(s); prints the character in variable ‘s’ on LCD.
Related functions
itoa(x,s); converts interger ‘x’ to string ‘s’. This is required when u want to print an integer. You must first convert it to string as puts accepts only strings
ftoa(x,s); converts float ‘x’ to string ‘s’. This is required when u want to print a float no. You must first convert it to string as puts accepts only strings
Note: you must include “stdlib.h” for using these functions
Programming of LCD
Goto LCD tab in CodeWizard . Select the port on which you have connected LCD . Keep the Chars/Line to 16 as the given LCD is 16X2 . Generate the code
Initializing LCD
// whatever Code wizard generates#include<delay.h>
void main() {
// whatever code wizard generates
lcd_clear();While (1){ lcd_gotoxy(0,0); lcd_putsf(“Hello”); delay_ms(1000); lcd_clear( );delay_ms(1000);}}
Blinking LCD, it prints Hello for 1s and then is cleared for 1s
Sample Program
Timer & Counter
A timer/Counter is a circuit that repeatedly counts from BOTTOM to TOP
A simple way to do this is run a loop…
Is there any harm???
Timer & Counter
Atmega16 has 3 counters, namely
TIMER0 ( 8 bit) : 0- 255
TIMER1 (16 bit) : 0- 65535
TIMER2 (8 bit) : 0- 255
Each timer has 3 registers associated with them that hold the count values and used for configuration.
TCNT0- Timer/Counter Register
TCNT0[7:0]
01234567
It holds the counter value at any point of time.
Similarly we have TCNT1[15:0] & TCNT2[7:0] .
TCNT1 [15:0] is also written as TCNT1H[7:0] & TCNT1L[7:0]
OCR0 – Output Compare Register
OCR0[7:0]
01234567
OCR holds the value to which the count is to be compared.
Similarly we have OCR1[15:0] & OCR2[7:0].
Timer 1 has two compare match registers – OCR1A & OCR1B
TCCR0 – Timer/Counter Control Register
FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00
01234567
FOC0 Force Output Compare
WGM00:01 Waveform Generation Mode
COM01:00 Compare Match Output Mode
CS02 : 01 Clock Select
TCCR is used for configuring the timer, deciding modes of operation etc.
Timers : Modes of Operation
Normal Mode
Clear Timer on Compare Match (CTC)
Fast PWM
Phase Correct
Commonly used are : Normal & CTC
Normal Mode
The timer counts from Initial value to 0xFF ( 0xFFFF for timer 1) and then loops back to initial value. If the interrupt on compare match is enabled, an interrupt is given when the value of TCNT = OCR.
CTC Mode
The timer counts from Initial value to OCR and then loops back to initial value.ie whenever TCNT = OCR the timer is cleared to zero. If the interrupt on compare match is enabled, an interrupt is given when the value of TCNT = OCR.
Timers : Modes of Operation
Overflow Interrupt
This interrupt is issued at the next clock after the timer value reaches 0xFF (0xFFFF for time 1). ie when TCNT=0xFF
Compare match interrupt
This interrupt is issued when the timer value equals OCR. ie when TCNT=OCR
Timers : Interrupts
Two types of interrupts are available in timers.
Timers : Configuration
1. Goto Timers tab.
2. Select the appropriate timer
3. Set the parameters as required.
Programming Timers
When you generate the program after setting the code wizard you will a find a function
interrupt [TIM0_COMP] void timer0_comp_isr(void) *
& / or interrupt [TIM0_OVF] void timer0_ovf_isr(void) **
declared & defined just after #include
* If compare match interrupt is enabled** If overflow interrupt is enabled
These functions are automatically called whenever the interuppt is generated by the timer, irrespective of where the execution of main program was. Just place the code for corresponding actions in these functions.
Sample Program
This program glows LEDs attached on PORTD for 5us after every 250us
Lets use timer 0
Clock : 1000 KHz // timer increments every 1usCounter Value : 0 // initial valueCompare Value: 250 = FAh // as timer is incrementing every 1us, n we
need to measure 250us so Compare value of 250 will do that
CTC Mode: // otherwise counter will counter upto 255 before going to zero, so it will measure 255us when it comes to 250 next time
Sample Program
#include <mega16.h>#include<delay.>
interrupt [TIM0_COMP] void timer0_comp_isr(void){ PORTD=0xFF; // glow LEDs delay_us(5); // wait 5us PORTD=0x00; // Put off LEDs}
Void main () {DDRD = 0xFF; // as uC o/ps data on all pins to glow LED// gen by codewizardTCNT0=0;OCR0=0xFA;TCCR0= something
While (1) { }}
Contact
Robotics ClubIT-BHU
For any queries:-
Contact: Ritesh HarjaniE-mail : [email protected] no. 9453313165
Contact: Sourabh Malwe E-mail : [email protected] no. 9795356073