Presentation is loading. Please wait.

Presentation is loading. Please wait.

Micro-Controllers & Robotics Robotics Club IT-BHU.

Similar presentations


Presentation on theme: "Micro-Controllers & Robotics Robotics Club IT-BHU."— Presentation transcript:

1 Micro-Controllers & Robotics Robotics Club IT-BHU

2 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

3 What is a uC ? In simple words -- a single chip Computer Introduction

4 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

5 From uP to uC 1.uP 2.Memory 3.Oscillator 4.Buffers 5.ADC 6.Comparator 7.UART 8.Interrupt controllers 9.SPI 10.Two Wire connector 11. JTEG Connectors

6 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

7 Pin diagram

8 Block Daigram

9 Simplified Diagram 4 8bit Parallel Input-Output ports through which you can i/p or o/p digital data.

10 Port Architecture PORTAPORTA DDRADDRA PINAPINA uCExternal world

11 Programming the Ports Normal C program int x; float y; x= 10; y= 8.97; Program for Ports DDRA = 0b11111111 or 0xFF // o/p DDRC = 0b00000000 or 0x00 // i/p PORTA = 27; // decimal PORTC= 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.

12 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 Port DDRA.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

13 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 < 255 y= 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

14 Complete Program A program to o/p 33 (hex) on PortD and configure and read pins 2 and 7 of Port A #include void main( ) { unsigned int x,y; DDRD=0xFF; // all pins o/p DDRA=0b01111011; // pin 7 & 2 i/p rest immaterial PORTD=0x33; x=PINA.2; y=PINA.7; }

15 LED + - + V GND

16 Glowing LED PortAPortA + - 0 1 To glow LED set PORTA.0=0; PORTA.1=1; or PORTA=0b00000010; or PORTA=0x02;

17 Glowing LED contd.. PortAPortA + - 0 1

18 LED Panel on PCB GNDGND +V

19 Blinking Pattern While (1) { PORTA=0xFF; delay_ms(500); PORTA=0x00; delay_ms(500); } While (1) { PORTA=0xFF; PORTA=0x00; }

20 Complete Program A program to make a blinking pattern #include void main( ) { DDRA=0xFF; // all pins o/p While (1) { PORTA=0xFF; // all LEDs ON delay_ms(500); PORTA=0x00; // all LEDs OFF delay_ms(500); }

21 Motors & Motor Drivers

22 Motors and Motor Drivers Types Motors  AC Motor  DC Motor  Stepper Motor  Servo Motor

23 DC motor

24 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

25 Stepper Motor

26 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

27

28 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

29 I/p 1 ( O/p from uC) I/p 2 I/p 3 I/p 4 O/p 4 O/p 3 O/p 2 O/p 1 If uC send ‘1’ to I/p 1, O/p is conn to + VPP GND +VPP If uC send ‘0’ to I/p 1, O/p is conn to GND Working of Motor Driver

30 I/p 1 ( O/p from uC) I/p 2 I/p 3 I/p 4 O/p 4 O/p 3 O/p 2 O/p 1 If uC send ‘1’ to I/p 2, O/p is conn to + VPP GND +VPP If uC send ‘0’ to I/p 2, O/p is conn to GND Working of Motor Driver

31 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.

32 L298N Motor Driver

33 ATMEGAATMEGA MOTOR DRIVER MOTOR 4 Inputs 4 Outputs

34 To drive a motor A motor has two wires : +ve & -ve. To drive it u need to apply a voltage between them Lets 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

35 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;

36 Sample program for running motor forward for 1s, reverse for 1s & stop for 1s in sequence #include void main() { DDRB=0x03; // other part included by CVAVR comes here while (1) { PORTB=0x01; // forward delay_ms(1000); PORTB=0x02; // reverse delay_ms(1000); PORTB=0x00; // stop delay_ms(1000); } Complete Program

37 Session II

38 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

39 IR Sensor Pair Transmitter (Tx) Receiver (Rx) IR sensor Pair Object / Line Transmitter = LED( Light Emitting Diode) Receiver = Photodiode

40

41 Principle of Operation R1< R2 WHITE surface Maximum reflection T1 is turned ON Vout is LOW BLACK surface Minimum or No Reflection T1 is turned OFF Vout is HIGH

42 Interfacing IR sensor with ATmega16 ATMEGAATMEGA PORTA Out: 1 Out: 0 In 1 0 2

43 Programming IR sensor with ATmega16 #include void main() { int x; DDRA=0b00000011; // last two pins as o/p for Vcc & GND PORTA.0 =1; // Vcc PORTA.1= 0; // Gnd x= PINA.2; // now you can use x for any calculation u want }

44 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.

45 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

46 Registers used in the ADC  ADMUX – ADC Multiplexer Selection Register  ADCSRA – ADC Control and Status Register A These registers are used to configure ADC However when using CVAVR this will be done by code wizard

47 Configuring ADC using Code Wizard 1.Open a new project 2.Select ‘yes’ to use code wizard 3.Goto ADC tab on Code Wizard ADC

48 Configuring ADC Set the parameters as shown or as per your requirement. Finally Generate Save & Exit

49 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.

50 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)

51 Interfacing IR sensor with ATmega16 ATMEGAATMEGA PORTA Out: 1 Out: 0 In 1 0 2 GND 7 6

52 Sample program This program glows Red Led if the digital value is less than 200 and green otherwise.

53 #include 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 off x=read_adc( PINA.2);// read analog i/p & conv to digital value if ( x<200) PORTA.7=1; // glow RED LED if val <200 else PORTA.6=1;// glow GREEN LED if val > 200 delay_ms(500); }

54 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.

55 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.

56 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. S left S right

57 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. S left S right Movement LOW Forward S left S right

58 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. S left S right Movement LOWHIGHRight Turn S left S right

59 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. S left S right Movement HIGHLOWLeft Turn S left S right

60 Line Follower S left S right Movement Left MotorRight Motor LOW Forward LOWHIGHRight TurnForwardBack HIGHLOWLeft TurnBackForward

61 ADC Advanced topic Differential Mode

62 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 ?

63 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.

64 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.

65 Session III

66 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’s 16 X 2, 20 X 2, 40 X 4

67 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,115,1 15,0 It has 16 pins whose description in given on the next slide. Character LCD

68 Pin Configuration of character LCD’s

69 All the connections for LCD are already printed on PCB, except you have to connect them to any port. As shown below LCDATMEGAATMEGA 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

70 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

71 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

72 // whatever Code wizard generates #include 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

73 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???

74 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.

75 TCNT0- Timer/Counter Register TCNT0[7:0] 0 1 23 45 6 7 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]

76 OCR0 – Output Compare Register OCR0[7:0] 0 1 23 45 6 7 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

77 TCCR0 – Timer/Counter Control Register FOC0WGM00COM01COM00WGM01CS02CS01CS00 0 1 23 45 6 7 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.

78 Timers : Modes of Operation Normal Mode Clear Timer on Compare Match (CTC) Fast PWM Phase Correct Commonly used are : Normal & CTC

79 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

80 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.

81 Timers : Configuration 1.Goto Timers tab. 2.Select the appropriate timer 3.Set the parameters as required.

82 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.

83 Sample Program This program glows LEDs attached on PORTD for 5us after every 250us Lets use timer 0 Clock : 1000 KHz // timer increments every 1us Counter Value :0 // initial value Compare 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

84 Sample Program #include 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 codewizard TCNT0=0; OCR0=0xFA; TCCR0= something While (1) { } }

85 Contact Robotics Club IT-BHU For any queries:- Contact: Ritesh Harjani E-mail : ritz.ritesh25@gmail.comritz.ritesh25@gmail.com Phone no. 9453313165 Contact: Sourabh Malwe E-mail : sunny.malwe6@gmail.comsunny.malwe6@gmail.com Phone no. 9795356073


Download ppt "Micro-Controllers & Robotics Robotics Club IT-BHU."

Similar presentations


Ads by Google