MICROCONTROLLERTHE BRAIN
WHAT ARE MICROCONTROLLERS ?A micro-controller (also MCU or µC) is a functional computer system-on-a-chip. It contains a processor core, memory and programmable input/output peripherals.Micro suggests that the device is small and controller tells you that the device might be used to control objects, processes or events. Another term to describe a microcontroller  is embedded controller because the microcontroller  and its support circuits are often built into or embeddedin the devices they control.
WHERE WE USE THEM…???
Basic block diagram of a microcontroller
CPU : CPU does all the arithmetic and logic operations.It controls the flow of execution of instructions.
RAM ( Random Access Memory) :RAM holds the set of instructions (program), i.e. being executed by the CPU.It holds important data required by the program.It holds some important data structures like ‘stack’.It is volatile in nature.
ROM ( Read Only Memory) :ROM holds very important data and initialization about the microcontroller.It holds the monitor program.It is written by the manufacturer.
Flash Memory :Flash memory is basically EEPROM.It holds the program written by the user.The program can be erased or written here many times. ( Specified by the manufacturer)
I/0 Ports :Every microcontroller has I/O ports.Each port is made up of n-pins ( mostly 8 pins).Each pin can be configured as either input pin or output pin.If a pin is input pin, it accepts data from the device it is connected to.If a pin is output pin, it sends the data to the device it is connected to.Thus these pins form the input/output medium for the microcontrollers.
ADC :Most of the real world signals are analog in nature.But a microcontroller is a digital device, thus it cannot process analog signals.Thus all microcontrollers have built in A-D converters.ADC digitizes an analog signal and gives it to the microcontroller for further processing.
TIMERS :In many applications, time keeping is a must. Eg. If you heating a meal in an oven.Thus microcontrollers have timers to measure time.
SOME COMMON MICROCONTROLLERS Intel - 8051 Atmel – Atmega 16  PIC ARM
Question: How a microcontroller works ??? Answer: Microcontroller consists of a Microprocessor (CPU i.e. Central processing Unit) which is interfaced to RAM (Random Access Memory) and Flash Memory (EEPROM). You feed your program in the Flash Memory on the microcontroller. Now when you turn on the microcontroller, CPU accesses the instructions from RAM which access your code from Flash. It sets the configuration of the pins and then start performing according to your program.
Question: How to make the code ? Answer: You basically write the program on your computer in any of the high level languages like C, C++, JAVA etc. Then you compile the code to generate the machine file.     All the machines understand only one language, 0 & 1 that is on and off. Now this 0 & 1 both corresponds to 2 different voltage levels for example 0 volt for 0 logic and +5 volt for 1 logic. Actually the code has to be written in this 0, 1 language and then saved in the memory of the microcontroller. But this will be very difficult task. So we write the code in the language we understand (eg. C) and then compile and make the machine file “.hex”. After we make this machine file we feed this to the memory of the microcontroller.
Question: How to feed the code in the flash of Microcontroller ? Answer: Assuming you have the machine file (.hex) ready and now you want to feed that to the flash of the microcontroller. Basically you want to make communication between your computer and microcontroller. Now computer has many communication ports such as Serial Port, Parallel Port and USB (Universal Serial Bus).      Let’s take Serial Port, it has its own definition that is voltage level to define 0 & 1. Serial Port's protocol is called as UART (Universal Asynchronous Receiver & Transmitter). Its voltage levels are : +12 volt for 0 logic and -12 volt for 1 logic.      Now the voltage levels of our microcontroller are based on CMOS (Complementary Metal Oxide Semiconductor) technology which has 0 volt for 0 logic and +5 volt for 1 logic. Two different machines with 2 different ways to define 0 & 1 and we want to exchange information between them. Consider microcontroller as a French and Computer's Serial Port as an Indian person. If they want to exchange information they basically need a mediator who knows both the language. He will listen one person and then translate to other person. Similarly we need a circuit which converts CMOS (microcontroller) to UART (serial port) and vice versa. This circuit is called as programmer. Using this circuit we can connect computer to the microcontroller and feed the machine file to the flash.
ATMEGA 16 MICROCONTROLLERManufacturer :  ATMEL CORPORATION,         	            USA.http://www.atmel.com/
ATMEGA 16 SPECIFICATIONS : Advanced RISC Architecture 131 Powerful Instructions – Most Single-clock Cycle Execution32 x 8 General Purpose Working Registers Up to 16 MIPS Throughput at 16 MHz  16K Bytes of In-System Self-Programmable Flash  512 Bytes EEPROM  1K Byte Internal SRAM  32 Programmable I/O Lines  In-System Programming by On-chip Boot Program  8-channel, 10-bit ADC  Two 8-bit Timer/Counters with Separate Prescalers and Compare Modes  One 16-bit Timer/Counter with Separate Prescaler, Compare Mode, and Capture  Four PWM Channels  Programmable Serial USART  Master/Slave SPI Serial Interface  Byte-oriented Two-wire Serial Interface  Programmable Watchdog Timer with Separate On-chip Oscillator  External and Internal Interrupt Sources
PIN CONFIGURATION :
RegistersAll the configurations in a microcontroller is set through 8 bit (1 byte) locations in RAM (RAM is a bank of memory bytes) of the microcontroller called as Registers. All the functions are mapped to its locations in RAM and the value we set at that location that is at that Register configures the functioning of microcontroller. There are total 32 x 8bit registers in Atmega-16. As Register size of this microcontroller is 8 bit, it called as 8 bit microcontroller.
For example :
Digital Input Output PortSo let’s start with understanding the functioning of AVR. We will first discuss about I/O Ports.Atmega 16 has 32 I/O (Input/Output) pins grouped as A, B, C & D with 8 pins in each group. This group is called as PORT.  PA0 - PA7 (PORTA)  PB0 - PB7 (PORTB)  PC0 - PC7 (PORTC)  PD0 - PD7 (PORTD)
Data Direction Register, DDRnDetermines the direction of individual pins of ports. If the bit of the DDR is set (1) the corresponding pin of the port is configured as output pin If the bit of the DDR is cleared (0) the    corresponding pin of the port is configured as    input pin.DDRA = 0xF0; 4 MSB pins of PORTA are output pins 4 LSB pins of PORTA are input pins
Port Pin Register, PINnReading the input pins of port is done by reading PIN registerTemp = PINA;Read the PORTA input and store in temp variable
Port Drive Register, PORTnIf the pin is configured as output (DDRn [x] = 1) PORTn register drives the corresponding value on the output pinsDDRA = 0xFF;PORTA = 0xF0;Output logic high on 4 MSB pins and logic low on 4 LSB pins
Port Drive Register, PORTnFor pins configured as input (DDRn[x] = 0) microcontroller connects a internal pull up resistor if the corresponding bit of PORTn is set If the PORTn bit is cleared, pin is Tristated DDRA = 0x00; PORTA = 0xF0;
Buzzer On / OFFBuzzer is connected to pin 7 of PORT B.Pin is configured as outputDDRB= 0x80;To turn on the buzzer output highPORTB = 0x80;To turn off the buzzer output lowPORTB = 0x00;
Buzzer Functionvoid Buzzer_ON(void){  PORTB = 0x80;}void Buzzer_OFF(void){  PORTB = PORTB & 0x7F;}
Bump Switch PORTC (0) Input portDDRC= 0x00; Internal pull upPORTC = 0x01; To read inputsportc_copy = PINC
void main(void){init_devices();   //insert your functional code here...   while(1)                                   //open continuous loop (while)   {portc_copy = PINC;        //read contents of port C into the portc_copy variable     if(portc_copy == 0x01)  //check if any switch is pressed by comparing bitwise with               	                                  //0x01 ('==' comparison)    {       PORTB = 0x00;      //if condition is true turn OFF buzzer by giving PORTB = 0x00    }    else                                      //if condition is false turn ON buzzer to    {                                            //indicate bump switch is pressed by giving PORTB = 0x80       PORTB = 0x80;     }  }}
WALL HUG USING BUMP SWITCHES:PC2PC3PC1PC4PC0
This is PORTBPin 0 is connected to LED0.Pin 1 is connected to LED1.Pin 2 is connected to LED2.Pin 3 is connected to LED3.Pin 4 is connected to LED4.
ADCAnalog To Digital Converter
Analog To Digital Converter 10 bit resolution 8 channels PORTA(0 – 7) – (ADC1-ADC7) Disable internal pull up ADCH & ADCL – Data Register ADMUX- Channel Select Register ADCSR – Control & Status Register
Theory of operation What we have seen till now that the input given to uC was digital, i.e., either +5 V (logic 1) or 0V (logic 0). But what if we have an analog input. Then we require a tool that converts this analog voltage to discrete values. Analog to Digital Converter (ADC) is such a tool.
Microcontroller

Microcontroller

  • 1.
  • 2.
    WHAT ARE MICROCONTROLLERS?A micro-controller (also MCU or µC) is a functional computer system-on-a-chip. It contains a processor core, memory and programmable input/output peripherals.Micro suggests that the device is small and controller tells you that the device might be used to control objects, processes or events. Another term to describe a microcontroller is embedded controller because the microcontroller and its support circuits are often built into or embeddedin the devices they control.
  • 3.
    WHERE WE USETHEM…???
  • 4.
    Basic block diagramof a microcontroller
  • 5.
    CPU : CPUdoes all the arithmetic and logic operations.It controls the flow of execution of instructions.
  • 6.
    RAM ( RandomAccess Memory) :RAM holds the set of instructions (program), i.e. being executed by the CPU.It holds important data required by the program.It holds some important data structures like ‘stack’.It is volatile in nature.
  • 7.
    ROM ( ReadOnly Memory) :ROM holds very important data and initialization about the microcontroller.It holds the monitor program.It is written by the manufacturer.
  • 8.
    Flash Memory :Flashmemory is basically EEPROM.It holds the program written by the user.The program can be erased or written here many times. ( Specified by the manufacturer)
  • 9.
    I/0 Ports :Everymicrocontroller has I/O ports.Each port is made up of n-pins ( mostly 8 pins).Each pin can be configured as either input pin or output pin.If a pin is input pin, it accepts data from the device it is connected to.If a pin is output pin, it sends the data to the device it is connected to.Thus these pins form the input/output medium for the microcontrollers.
  • 10.
    ADC :Most ofthe real world signals are analog in nature.But a microcontroller is a digital device, thus it cannot process analog signals.Thus all microcontrollers have built in A-D converters.ADC digitizes an analog signal and gives it to the microcontroller for further processing.
  • 11.
    TIMERS :In manyapplications, time keeping is a must. Eg. If you heating a meal in an oven.Thus microcontrollers have timers to measure time.
  • 12.
    SOME COMMON MICROCONTROLLERSIntel - 8051 Atmel – Atmega 16 PIC ARM
  • 13.
    Question: How amicrocontroller works ??? Answer: Microcontroller consists of a Microprocessor (CPU i.e. Central processing Unit) which is interfaced to RAM (Random Access Memory) and Flash Memory (EEPROM). You feed your program in the Flash Memory on the microcontroller. Now when you turn on the microcontroller, CPU accesses the instructions from RAM which access your code from Flash. It sets the configuration of the pins and then start performing according to your program.
  • 14.
    Question: How tomake the code ? Answer: You basically write the program on your computer in any of the high level languages like C, C++, JAVA etc. Then you compile the code to generate the machine file. All the machines understand only one language, 0 & 1 that is on and off. Now this 0 & 1 both corresponds to 2 different voltage levels for example 0 volt for 0 logic and +5 volt for 1 logic. Actually the code has to be written in this 0, 1 language and then saved in the memory of the microcontroller. But this will be very difficult task. So we write the code in the language we understand (eg. C) and then compile and make the machine file “.hex”. After we make this machine file we feed this to the memory of the microcontroller.
  • 15.
    Question: How tofeed the code in the flash of Microcontroller ? Answer: Assuming you have the machine file (.hex) ready and now you want to feed that to the flash of the microcontroller. Basically you want to make communication between your computer and microcontroller. Now computer has many communication ports such as Serial Port, Parallel Port and USB (Universal Serial Bus). Let’s take Serial Port, it has its own definition that is voltage level to define 0 & 1. Serial Port's protocol is called as UART (Universal Asynchronous Receiver & Transmitter). Its voltage levels are : +12 volt for 0 logic and -12 volt for 1 logic. Now the voltage levels of our microcontroller are based on CMOS (Complementary Metal Oxide Semiconductor) technology which has 0 volt for 0 logic and +5 volt for 1 logic. Two different machines with 2 different ways to define 0 & 1 and we want to exchange information between them. Consider microcontroller as a French and Computer's Serial Port as an Indian person. If they want to exchange information they basically need a mediator who knows both the language. He will listen one person and then translate to other person. Similarly we need a circuit which converts CMOS (microcontroller) to UART (serial port) and vice versa. This circuit is called as programmer. Using this circuit we can connect computer to the microcontroller and feed the machine file to the flash.
  • 16.
    ATMEGA 16 MICROCONTROLLERManufacturer: ATMEL CORPORATION, USA.http://www.atmel.com/
  • 17.
    ATMEGA 16 SPECIFICATIONS: Advanced RISC Architecture 131 Powerful Instructions – Most Single-clock Cycle Execution32 x 8 General Purpose Working Registers Up to 16 MIPS Throughput at 16 MHz 16K Bytes of In-System Self-Programmable Flash 512 Bytes EEPROM 1K Byte Internal SRAM 32 Programmable I/O Lines In-System Programming by On-chip Boot Program 8-channel, 10-bit ADC Two 8-bit Timer/Counters with Separate Prescalers and Compare Modes One 16-bit Timer/Counter with Separate Prescaler, Compare Mode, and Capture Four PWM Channels Programmable Serial USART Master/Slave SPI Serial Interface Byte-oriented Two-wire Serial Interface Programmable Watchdog Timer with Separate On-chip Oscillator External and Internal Interrupt Sources
  • 18.
  • 20.
    RegistersAll the configurationsin a microcontroller is set through 8 bit (1 byte) locations in RAM (RAM is a bank of memory bytes) of the microcontroller called as Registers. All the functions are mapped to its locations in RAM and the value we set at that location that is at that Register configures the functioning of microcontroller. There are total 32 x 8bit registers in Atmega-16. As Register size of this microcontroller is 8 bit, it called as 8 bit microcontroller.
  • 21.
  • 22.
    Digital Input OutputPortSo let’s start with understanding the functioning of AVR. We will first discuss about I/O Ports.Atmega 16 has 32 I/O (Input/Output) pins grouped as A, B, C & D with 8 pins in each group. This group is called as PORT. PA0 - PA7 (PORTA) PB0 - PB7 (PORTB) PC0 - PC7 (PORTC) PD0 - PD7 (PORTD)
  • 23.
    Data Direction Register,DDRnDetermines the direction of individual pins of ports. If the bit of the DDR is set (1) the corresponding pin of the port is configured as output pin If the bit of the DDR is cleared (0) the corresponding pin of the port is configured as input pin.DDRA = 0xF0; 4 MSB pins of PORTA are output pins 4 LSB pins of PORTA are input pins
  • 24.
    Port Pin Register,PINnReading the input pins of port is done by reading PIN registerTemp = PINA;Read the PORTA input and store in temp variable
  • 25.
    Port Drive Register,PORTnIf the pin is configured as output (DDRn [x] = 1) PORTn register drives the corresponding value on the output pinsDDRA = 0xFF;PORTA = 0xF0;Output logic high on 4 MSB pins and logic low on 4 LSB pins
  • 26.
    Port Drive Register,PORTnFor pins configured as input (DDRn[x] = 0) microcontroller connects a internal pull up resistor if the corresponding bit of PORTn is set If the PORTn bit is cleared, pin is Tristated DDRA = 0x00; PORTA = 0xF0;
  • 27.
    Buzzer On /OFFBuzzer is connected to pin 7 of PORT B.Pin is configured as outputDDRB= 0x80;To turn on the buzzer output highPORTB = 0x80;To turn off the buzzer output lowPORTB = 0x00;
  • 28.
    Buzzer Functionvoid Buzzer_ON(void){ PORTB = 0x80;}void Buzzer_OFF(void){ PORTB = PORTB & 0x7F;}
  • 29.
    Bump Switch PORTC(0) Input portDDRC= 0x00; Internal pull upPORTC = 0x01; To read inputsportc_copy = PINC
  • 30.
    void main(void){init_devices(); //insert your functional code here... while(1) //open continuous loop (while) {portc_copy = PINC; //read contents of port C into the portc_copy variable if(portc_copy == 0x01) //check if any switch is pressed by comparing bitwise with //0x01 ('==' comparison) { PORTB = 0x00; //if condition is true turn OFF buzzer by giving PORTB = 0x00 } else //if condition is false turn ON buzzer to { //indicate bump switch is pressed by giving PORTB = 0x80 PORTB = 0x80; } }}
  • 31.
    WALL HUG USINGBUMP SWITCHES:PC2PC3PC1PC4PC0
  • 32.
    This is PORTBPin0 is connected to LED0.Pin 1 is connected to LED1.Pin 2 is connected to LED2.Pin 3 is connected to LED3.Pin 4 is connected to LED4.
  • 33.
  • 34.
    Analog To DigitalConverter 10 bit resolution 8 channels PORTA(0 – 7) – (ADC1-ADC7) Disable internal pull up ADCH & ADCL – Data Register ADMUX- Channel Select Register ADCSR – Control & Status Register
  • 35.
    Theory of operationWhat we have seen till now that the input given to uC was digital, i.e., either +5 V (logic 1) or 0V (logic 0). But what if we have an analog input. Then we require a tool that converts this analog voltage to discrete values. Analog to Digital Converter (ADC) is such a tool.