Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

AVR I/O programming Atmega 64 uNiboard ThinkLABS maaniq

1,079 views

Published on

AVR Family, Amega64 Microcontroller I/O - GPIO programming for beginner.
this slide includes the detailed explanation of SET, CLEAR, TOGGLE, CHECK bits and simplified notations for easy recalling.
Detailed explanation of using Pull-up resistor in any micro-controller and code examples for Enabling the internall pull up of any GPIO ports in AVR family Micro-controllers.

Published in: Education
  • Be the first to comment

AVR I/O programming Atmega 64 uNiboard ThinkLABS maaniq

  1. 1. Embedded Systems Be Ready for Tomorrow… By, PL.Manicka Raja, Embedded & Robotics Engineer, ThinkLABS Techno Solutions Pvt Ltd, SINE, IIT-Bombay, Mumbai. manicka.r@thinklabs.in
  2. 2. What is Embedded Systems? © 2013, www.thinklabs.in 2
  3. 3. © 2013, www.thinklabs.in 3
  4. 4. © 2013, www.thinklabs.in 4
  5. 5. © 2013, www.thinklabs.in 5
  6. 6. About Microcontroller ATmega64 AT - ??? Mega - ??? 64 - ??? © 2013, www.thinklabs.in 6
  7. 7. KEY Notes REGISTERS…? Types of Registers General Purpose Registers Special Registers © 2013, www.thinklabs.in 7
  8. 8. GENERAL PURPOSE – I/O DDRx ….. PORTx PINx Where X represents A,B,C,D….F,G. © 2013, www.thinklabs.in 8
  9. 9. Special Purpose UART TIMERS ADC SPI I2C EEPROM © 2013, www.thinklabs.in 9
  10. 10. 8 - BIT REGISTER ACCESSING 7 6 5 4 3 2 1 0 MSB…… …..LSB © 2013, www.thinklabs.in 10
  11. 11. © 2013, www.thinklabs.in 11
  12. 12. Output  Step 1 : Configuration or Initialization  DDRx =  1 – Output,  0 – input  Step 2 : Status  PORTx =  1 – Logic High ( 5 volts)  0 – Logic Low (0 volts)  Where X represents A,B,C,D….F,G. © 2013, www.thinklabs.in 12
  13. 13. © 2013, www.thinklabs.in 13
  14. 14. © 2013, www.thinklabs.in 14
  15. 15. © 2013, www.thinklabs.in 15
  16. 16. © 2013, www.thinklabs.in 16
  17. 17. BLINK LED - Program #include<avr/io.h> void main() { DDRC=OxFF; // Configuration PORTC=OxOO; // All LEDS - ON, Active Low PORTC=OxFF; // All LEDS - OFF, } © 2013, www.thinklabs.in 17
  18. 18. BLINK LED - Program #include<avr/io.h> void main() { DDRC=OxFF; // Configuration while(1) // super loop { PORTC=OxOO; // All LEDS - ON, Active Low PORTC=OxFF; // All LEDS - OFF, } } © 2013, www.thinklabs.in 18
  19. 19. BLINK LED - Program #include<avr/io.h> #include<util/delay.h> void main() { DDRC=OxFF; // Configuration while(1) // super loop { PORTC=OxOO; // All LEDS - ON, Active Low _delay_ms(1000); PORTC=OxFF; // All LEDS - OFF, _delay_ms(1000); } } © 2013, www.thinklabs.in 19
  20. 20. INPUT © 2013, www.thinklabs.in 20
  21. 21. INPUT © 2013, www.thinklabs.in 21
  22. 22. INPUT © 2013, www.thinklabs.in Vcc 22
  23. 23. INPUT © 2013, www.thinklabs.in Vcc 23
  24. 24. INPUT © 2013, www.thinklabs.in Vcc 24
  25. 25. © 2013, www.thinklabs.in 25
  26. 26. Functions of PORTx Register in GPIO Output Input DDRx 1 0 PORTx 1 0 1 0 Logic High Logic Low Enable Internal Pull- Up Disable Internal Pull-Up 5 volts 0 volts Stable state - 1 Floating state ( 0 or 1) © 2013, www.thinklabs.in 26
  27. 27. © 2013, www.thinklabs.in 27
  28. 28. Input  Step 1 : Configuration or Initialization  DDRx =  Step 2 : Pull – Up  PORTx =  1 – Enable Pull – UP  0 – Disable Pull – UP  Step 3 : Checking  1 – Switch is not pressed  0 – Switch is pressed  Switches are Active Low, When pressed , it gives 0. © 2013, www.thinklabs.in 28
  29. 29. © 2013, www.thinklabs.in 29
  30. 30. © 2013, www.thinklabs.in 30
  31. 31. © 2013, www.thinklabs.in 31
  32. 32. © 2013, www.thinklabs.in 32
  33. 33. © 2013, www.thinklabs.in 33
  34. 34. © 2013, www.thinklabs.in 34
  35. 35. MSB……….LSB = Big Endian. © 2013, www.thinklabs.in •7654 3210 •1010 1010 • (1<< position ) • (1<<4) BIT WISE OPERATIONS 35
  36. 36. BIT WISE OPERATIONS Why we need BITWISE operations…? To change only our desired bit without affecting any other Bits in the REGISTER. When Individual Accessing is required When the port is both Input & Output. © 2013, www.thinklabs.in 36
  37. 37. © 2013, www.thinklabs.in 37
  38. 38. BIT WISE OPERATIONS  SET BIT  To make the desired position as LOGIC 1 (ONE)  CLEAR BIT  To make the desired position as LOGIC 0 (ZERO)  CHECK BIT  To know whether the desired position is 0 or 1 (Without affecting any other bits) © 2013, www.thinklabs.in 38
  39. 39. SET Bit - Explanation  a = 01101010;  Desired Position is 4  So, OR function 0 1 1 O 1 0 1 0 is a 0 0 0 1 0 0 0 0 is (1<<pos) ….(1<<4) ----------------------- 0 1 1 1 1 0 1 0 is a= a|(1<<pos); x=x+5; ----------------------- a|=(1<<pos); x+=5; Important Note : Rest of the BITS are not affected © 2013, www.thinklabs.in 39
  40. 40. One’s complement • 0001 0000 is (1<<4) • One’s Complement…? • 1110 1111 is one’s complement of (1<<4) •~ is negation , used to produce one’s complement of a number. • 11101111 is ~(1<<4) © 2013, www.thinklabs.in 40
  41. 41. CLEAR Bit - Explanation  a = 0110 1010;  Desired Position is 5  So, AND function 0 1 1 0 1 0 1 0 is a 1 1 0 1 1 1 1 1 is ~(1<<pos) or ~(1<<5) ----------------------- 0 1 0 0 1 0 1 0 is a= a&~(1<<pos); x=x+5; ----------------------- a&=~(1<<pos); x+=5; Important Note : Rest of the BITS are not affected © 2013, www.thinklabs.in 41
  42. 42. SET BIT REGISTER |= (1<<pos) |= OR Equal to © 2013, www.thinklabs.in 42
  43. 43. CLEAR BIT REGISTER &=~ (1<<pos) &= ~ AND equal to negation © 2013, www.thinklabs.in 43
  44. 44. CHECK BIT (REGISTER & (1<<pos))== ? & AND is only for checking, not for assingning  EQUAL ‘=’ is for assigning, BUT here we don’t assign anything,  Just checking the Status of the BIT © 2013, www.thinklabs.in 44
  45. 45. Multiple SET BIT REGISTER |= ( refer below) |= ((1<<7) | (1<<5) | (1<<2)) © 2013, www.thinklabs.in 45
  46. 46. Multiple CLEAR BIT REGISTER &=~ ( refer below) &=~ ((1<<7) | (1<<5) | (1<<2)) © 2013, www.thinklabs.in 46
  47. 47. Switch Program #include<avr/io.h> #include<util/delay.h> void main() { DDRC=OxFF; // Configuration DDRD&=~(1<<6); // PORTD 6th pin is SW1 PORTD|=(1<<6); //PULL – UP resistor while(1) // super loop { if( (PIND & (1<<6)) == O ) { PORTC=OxOO; // All LEDS - ON, Active Low _delay_ms(1000); } } } © 2013, www.thinklabs.in 47
  48. 48. Switch Program #include<avr/io.h> #include<util/delay.h> #define SW1 (PIND & (1<<6)) // macro for SW1 void main() { DDRC=OxFF; // Configuration DDRD&=~(1<<PD6); // PD6 is already #defined as 6 @ avr/ io.h PORTD|=(1<<PD6); //PULL – UP resistor while(1) // super loop { if( SW1 == O ) { PORTC=OxOO; // All LEDS - ON, Active Low _delay_ms(1000); } © 2013, www.thinklabs.in 48
  49. 49. © 2013, www.thinklabs.in 49
  50. 50. Direct OPERATION for REGISTERS  7..4 – o/p  3...0 – i/p DDRC = 0b11110000; (or) DDRC = 0xF0;  0 – input  1- output © 2013, www.thinklabs.in 50
  51. 51. Bitwise OPERATION for REGISTERS  7..4 – o/p  3...0 – i/p DDRC |= ((1<<7)|(1<<6)|(1<<5)|(1<<4)); (and also) DDRC &=~ ((1<<3)|(1<<2)|(1<<1)|(1<<0)); © 2013, www.thinklabs.in 51
  52. 52. Key Notes for An Embedded Developer What is a Compiler? What is a Cross–Compiler..? Diff B/W uC & uP CISC & RISC…? Architecture…? = Program memory & Data Memory © 2013, www.thinklabs.in 52
  53. 53. Compiler  The process of converting high level language to the machine level language  For the SAME processor.  Example : Turbo C, Dev C  Programs are compiled &  Output is viewed on the SAME System © 2013, www.thinklabs.in 53
  54. 54. Cross -Compiler  The process of converting high level language to the machine level language  For the TARGET processor.  Example : winAVR, KeilC, AVR Studio  Programs are compiled in a system &  Output is viewed on ANOTHER System © 2013, www.thinklabs.in 54
  55. 55. 5 Golden Rules for Programming also called as Cross Compilation 1. Make New Separate folder for Each & Every Program 2. Only Makefile and .c (dot C) file…? 3. ‘M ‘of Makefile should be in Capital. 4. Target name in Makefile & some changes 5. Those three Switches in your uNiBoard © 2013, www.thinklabs.in 55
  56. 56. © 2013, www.thinklabs.in 56
  57. 57. Tools to Remember To Compile Tools  Make All To Program Tools  Make Program © 2013, www.thinklabs.in 57
  58. 58. ReCap  X= 6;  Right to Left Operation in C.  So, We are passing the value 6 to X,  Similarly,  DDRx=OxFF, and PORTx=OxFF;  So, We are (passing) Writing an 8 bit Value to REGISTER  But, in PINx , we are just only reading from it.  PINx & (1<< pos ) © 2013, www.thinklabs.in 58
  59. 59. R/W - Registers DDRx - Write Only PORTx - Write Only PINx - Read Only © 2013, www.thinklabs.in 59
  60. 60. THANK U… Contact maaniq1805@gmail.com www.linkedin.com/in/maaniq www.facebook.com/maaniq ©2013,www.thinklabs.in 60

×