advance module book

Upload: rahulkumarsingh

Post on 03-Jun-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 Advance Module Book

    1/34

  • 8/11/2019 Advance Module Book

    2/34

  • 8/11/2019 Advance Module Book

    3/34

  • 8/11/2019 Advance Module Book

    4/34

    2|

    Ok this question is coming in everyone's mind first, the best answer is to check yourself, use the DTMF tone of

    your mobile without inserting any SIM in your cell phone, it'll again generate the same tone, but can you use

    G.S.M without any SIM card? since in GSM we are transferring data through our voice or text, but in DTMF there

    is nothing like data transfer, it's just the dialing of pulse, that is called the tone dialing, not an7y data transfer, ok

    just make a call to your friend and tell him/her to on the speaker, now from your side press any key, see the

    magic on the other side!! Yes your DTMF tone will be heard by your friend on the other side.

    Let's have some application of this DTMF technology with i3indya board/microcontrollers:-

    As we know the basic concepts of microcontrollers and the way to interface any input/output devices from the

    controller, so let's start interfacing DTMF,

    Just have a look of this fig.

    Since the input would be of sound and as we know that with the help of decoder IC we can convert this sound to

    binary outputs (corresponding to the frequency combination/key pressed).

    Now take a look of above figure once (frequency combination of keys)

    q If I am pressing a key let say 1, then it will give the combination of frequencies 1209 & 697, and this will

    directly be given to our sound converter IC , the output of that IC would be 0001(Q1,Q2,Q3,Q4)

    Office :G-85, Preet Vihar, New Delhi-110092. Ph. :+91 - 95 6060 5666. Website :www.i3indya.com

    Copyright All Right Reserved

  • 8/11/2019 Advance Module Book

    5/34

    | 3|

    Ok , above table is much helpful , now we are having digital outputs, which means that we are having 5 volts

    at each logic level 1,for TTL logic level (refer to i/o operations for more details) .

    Now if we are having 5volts we have everything on the microcontroller world!

    In the above diagram, we are taking the example of glowing LED according to the DTMF inputs,

    Refer to the i/o section of this book, lets say there is a key pressed 1, means frequency combination would

    be 1209 & 679 Hz, this would be the input of 8870, which would give the binary output of 0001, means at

    Q4, we are having pulse 1 means, at that point we have 5v.

    Now if we want to glow an LED connected to PORTB.3 then we can directly connect to the corresponding

    output of 8870 to the input of ATmega16 PORT. (Refer to the masking concept in input operations).

    RELAY

    Relays are amazingly simple devices. There are four parts in every relay:

    q Electromagnetq Armature that can be attracted by the electromagnet

    q Spring

    q Set of electrical contacts

    A relay is a simple electromechanical switch made up of an electromagnet and a set of contacts. Relays are

    found hidden in all sorts of devices. In fact, some of the first computers ever built used relays to implement

    Boolean gates.

    Innovation

    3indya TechnologiesIntelligence Information

    TM

    TM

    CLOUD C MPUSCLOUD CAMPUSLOUD C MPUSCLOUD CAMPUS

    +91 - 95 6060 5666Call:

    www.i3indya.comLog on to:

    Office :G-85, Preet Vihar, New Delhi-110092. Ph. :+91 - 95 6060 5666. Website :www.i3indya.com

    Copyright All Right Reserved

  • 8/11/2019 Advance Module Book

    6/34

  • 8/11/2019 Advance Module Book

    7/34

    | 5|

    Now we are going to replace the direct connection to switch controlled.

    Now this switch can be replaced by RELAY.

    One thing that can be noticed , if we are using RELAY as a switch then how it differs from the normal switches

    that we use in our home! Just see the first figure in which we have directly connected the 220v to bulb, if we are

    using a normal switch we have to attach by cutting any of the two wires, but in relays, we don't need any

    connected circuitry, see the figure of relay in which, one end is connected to 220v and another end to

    controller, there is no connection in between these two.

    Office :G-85, Preet Vihar, New Delhi-110092. Ph. :+91 - 95 6060 5666. Website :www.i3indya.com

    Copyright All Right Reserved

  • 8/11/2019 Advance Module Book

    8/34

    6|

    Types of relays:-

    Relays with contacts:-

    Other types include three and five pin relays. A 3-PIN relay instead of two input sources, this relay has one input

    at pin 1. Current splits inside the relay, supplying power to both the control and load circuits. A 5-PIN relay has

    a single control circuit, but two separate current paths for the switch: One when the relay is de-energized (OFF

    no current through the control coil) and the other the energized (ON - current is flowing through the controlcoil). When the 5-PIN relay is de-energized (OFF), pins 4 and 5 have continuity. When the relay is energized

    (ON), pins 3 and 5 have continuity.

    Detecting a Touch

    To know if the coordinate readings are valid, there must be a way to detect whether the screen is being touched

    or not. This can be done by applying a positive voltage (VCC) to Y+ through a pull up resistor and applying

    ground to X. The pull up resistor must be significantly larger than the total resistance of the touch screen,

    which is usually a few hundred ohms. When there is no touch, Y+ is pulled up to the positive voltage. When

    there is a touch, Y+ is pulled down to ground as shown in Figure1. This voltage-level change can be used to

    generate a pin-change interrupt.

    TOUCH SCREEN

    Principles of Operation

    A resistive touch screen is constructed with two transparent layers coated with a conductive material stacked

    on top of each other. When pressure is applied by a finger or a stylus on the screen, the top layer makes contact

    with the lower layer. When a voltage is applied across one of the layers, a voltage divider is created. The

    coordinates of a touch can be found by applying a voltage across one layer in the Y direction and reading the

    voltage created by the voltage divider to find the Y coordinate, and then applying a voltage across the other

    layer in the X direction and reading the voltage created by the voltage divider to find the X coordinate.

    Office :G-85, Preet Vihar, New Delhi-110092. Ph. :+91 - 95 6060 5666. Website :www.i3indya.com

    Copyright All Right Reserved

  • 8/11/2019 Advance Module Book

    9/34

    | 7|

    Reading a 4-Wire Screen

    A 4-wire resistive touch screen is constructed as shown in Figure.

    The x and y coordinates of a touch on a 4-wire touch screen can be read in two steps. First, Y+ is driven high,Yis driven to ground, and the voltage at X+ is measured. The ratio of this measured voltage to the drive voltage

    applied is equal to the ratio of the y coordinate to the height of the touch screen. The y coordinate can be

    calculated as shown in Figure3. The x coordinate can be similarly obtained by driving X+ high, driving Xto

    ground, and measuring the voltage at Y+. The ratio of this measured voltage to the drive voltage applied is

    equal to the ratio of the x coordinate to the width of the touch screen. This measurement scheme is shown in

    Figure.

    The way the touch screen is connected to the microcontroller:

    Office :G-85, Preet Vihar, New Delhi-110092. Ph. :+91 - 95 6060 5666. Website :www.i3indya.com

    Copyright All Right Reserved

  • 8/11/2019 Advance Module Book

    10/34

    8|

    RF

    You've probably heard about "AM radio" and "FM radio," "VHF" and "UHF" television, "citizens band radio,"

    "short wave radio" and so on. Have you ever wondered what all of those different names really mean? What's

    the difference between them?

    A radio wave is an electromagnetic wave propagated by an antenna. Radio waves have different

    frequencies, and by tuning a radio receiver to a specific frequency you can pick up a specific signal.

    In India Wireless and Planning and Coordination Wing (WPC), a branch of the Ministry of Communications and

    Information Technology decides who is able to use which frequencies for which purposes, and it issues

    licenses to stations for specific frequencies. In addition, the WPC allocates frequency spectrum in India. When

    you listen to a radio station and the announcer says, "You are listening to 92.7 FM BIG The Rock!," what the

    announcer means is that you are listening to a radio station broadcasting an FM radio signal at a frequency of

    92.7 megahertz, with WPC-assigned call letters of MCIT. Megahertzmeans "millions of cycles per second," so

    "92.7 megahertz" means that the transmitter at the radio station is oscillating at a frequency of 92,700,000

    cycles per second. Our FM (frequency modulated) radio can tune in to that specific frequency and give you

    clear reception of that station. All FM radio stations transmit in a bandof frequencies between 88 megahertz

    and 108 megahertz. This band of the radio spectrum is used for no other purpose but FM radio broadcasts.

    In the same way, AM radio is confined to a band from 535 kilohertz to 1,700 kilohertz (kilo meaning

    "thousands," so 535,000 to 1,700,000 cycles per second). So an AM (amplitude modulated) radio station that

    says, "This is AM 123 " means that the radio station is broadcasting an AM radio signal at 123 kilohertz and its

    WPC-assigned call letters are MCIT

    An RF Module is a (usually) small electronic circuit used to transmit, receive, or transceiver radio waves on one

    of a number of carrier frequencies, so BIG FM uses these kinds of RF modules and we too at the receiver end.

    RF module

    Office :G-85, Preet Vihar, New Delhi-110092. Ph. :+91 - 95 6060 5666. Website :www.i3indya.com

    Copyright All Right Reserved

  • 8/11/2019 Advance Module Book

    11/34

    | 9|

    Several carrier frequencies are commonly used in commercially-available RF modules, including 433.92MHz,

    315MHz, 868MHz and 915MHz.They come in Pair. One is RX or the receiver and other is a TX or Transmitter.

    Since it would be serial communication (refer to the communication protocols chapter), so we'll be needing an

    encoder & decoder IC for communication, because at transmitter end we are doing parallel communication

    with our kit but to transfer it at a long distance we have to send serially, so we will use an encoder IC HT12E at

    the transmitter side, and the same couple IC HT12D will be used at the receiver side to convert it back parallel.

    Connection through IC and Module.

    Office :G-85, Preet Vihar, New Delhi-110092. Ph. :+91 - 95 6060 5666. Website :www.i3indya.com

    Copyright All Right Reserved

  • 8/11/2019 Advance Module Book

    12/34

  • 8/11/2019 Advance Module Book

    13/34

    | 11|

    USART

    A brief description to understand the USART (Universal Synchronous Asynchronous Transmitter/Receiver).

    That is to say, the information contained here is an interpretation of the datasheet so it is easily

    understandable. The USART feature of the AVR microcontroller can communicate with another

    microcontroller (not necessarily another AVR microcontroller), multiple microcontrollers, or a computer

    using a voltage level shifter or converter. The USART can transmit data using a buffer and a shift register (more

    on that later), receive data using a shift register and buffer, create a frame of data that is recognized on both the

    receiving end and the transmitting end. All of this works according to an agreed upon speed from both sides, or

    with synchronous mode where the clock line (a wire) is directly connected.

    Sending information and receiving information in UART and USART communication is like riding on a train.

    The train sends you to a destination like it sends data to another computer or microcontroller. The train station

    is like the internal transmitter and receiver inside the microcontroller. There must be a place that the train

    riders queue to get on the train. First, a passenger will get the ticket and sit down to wait for the next train to

    arrive. The passenger then must queue up to get on the train. Then the passenger gets on the train. This is

    exactly how a USART transmitter functions. The data is sent to a buffer, which is like the waiting room for the

    train. The shift registers is like the queue to get on the train. The data moves from the buffer and onto the shift

    registers. This is after the previous data has left the shift registers. From the shift registers, the data movesalong the transmit wire, just like a train moves along a track.

    Receiving information is the same but in reverse. In the human analogy, the train arrives to the train station.

    The person gets off of the train and must wait in a queue so that the people in front of them makes space. In the

    microcontroller reception of data, the data gets off the wire and goes straight into the shift registers. With the

    Atmega32, there are two buffers that the data can use to wait to be used by the program in the microcontroller.

    With the shift register, this gives the receive area three total buffers for the data to sit, waiting to be used. This is

    established with the chip so Data Over Runs (DOR) are less likely. The DOR is a flag that you can look at.

    The train is actually similar to the baud rate. The baud is the clock that pushes the data along the line. There area couple types of clocks you can apply using UART and USART. The UART only allows you to apply an agreed

    upon baud rate from both parties (each microcontroller must be set with this specific baud rate). In USART, a

    clock wire must be connected between each microcontroller. This wire will pulse like a heartbeat. In the case of

    asynchronous, each microcontroller has its own clock and since the data is being sent with the transmitting

    microcontroller using this clock (baud rate), the receiving microcontroller must be receiving this data at the

    same pace, so its clock (baud rate) must be the same.

    Clock Modes:

    Pick between Synchronous or Asynchronous

    UCSRC = (1

  • 8/11/2019 Advance Module Book

    14/34

    | 12|

    This beat, or baud rate is determined by the UBBR formula:

    UBBR = ( (regular microcontroler clock speed) / 16 * baud ) - 1

    Example: UBBR = ( 1,000,000 / 16 * 2400 ) - 1= ( 1,000,000 / 38,400 ) - 1

    = 26.0416667 - 1

    = 25.0416667 = 25

    It's 25 because the UBBR will not accept a decimal value, but that introduces a 0.2% error, which is acceptable.

    You can find a table of commonly used UBBR numbers in the "Examples of baud rate settings" under the USART

    section of the datasheet.

    Finding the baud from the UBBR is optional:

    baud = (regular microcontroler clock speed) / ( 16 * (UBBR + 1) )

    Example: baud = 1,000,000 / (16 * (25 + 1))

    = 1,000,000 / (16 * 26)= 1,000,000 / 416= 2403.84615 =2400

    Pretty close to the standard 2400 baud with 0.2% error which is acceptable. Check your datasheet for

    acceptable error percentages.

    Setting the baud rate:

    UBBRH &= ~(1 > 8);UBBRL = (unsigned char) # from table;

    The URSEL is set to 0 because UBBRH shares the same I/O as UCSRC, the URSEL must be set to 0 to write to

    UBBRH. The UBBRH sets the high portion of the baud rate - from bit 8 to bit 11. The UBBRL puts the remaining

    bits 0 to 7 into the low UBBR register.

    Setting the Asynchronous mode:

    Normal Asynchronous

    Double Speed Asynchronous- U2X bit in UCSRA controls double speed asynchronous

    UCSRA = (1

  • 8/11/2019 Advance Module Book

    15/34

    | 13|

    Data Frame:

    This is the actual data that is transmitted. It's the train with the data riding on-board. This is with 9 bits for the

    data bits, one start bit, two stop bits, one parity bit which is the maximum frame size.

    Idle state: The train track - idle state of the signal - always high (5v)

    Bit 01: The train engine - Bit 1: Start bit - always low (0)

    Bit 02: Person #1 on the train - Data bit #0 - high or low depending on the data

    Bit 03: Person #2 on the train - Data bit #1 - high or low depending on the data

    Bit 04: Person #3 on the train - Data bit #2 - high or low depending on the data

    Bit 05: Person #4 on the train - Data bit #3 - high or low depending on the data

    Bit 06: Person #5 on the train - Data bit #4 - high or low depending on the data

    Bit 07: Person #6 on the train - Data bit #5 - high or low depending on the data

    Bit 08: Person #7 on the train - Data bit #6 - high or low depending on the data

    Bit 09: Person #8 on the train - Data bit #7 - high or low depending on the data

    Bit 10: Person #9 on the train - Data bit #8 - high or low depending on the data

    Bit 11: Train car just before the caboose watching over everything - Parity bit - high or low depending on the 1's

    of the data

    Bit 12: The caboose - Stop Bit - always highBit 13: The extra caboose - stop bit - always high and always ignored by the receiver

    back to high idle state - or to a new start bit.

    There is a maximum of 13 bits in the largest data frame.

    Setting the Data Bit Size -Number of data bits you want in the frame. Use the UCSZ2:0 (UCSZ0, UCSZ1,UCSZ2) bits in the UCSRC register. Note, keeping all the bits in UCSZ2:0 not set establishes a 5-bit data bit

    length.

    UCSZ - Character Size

    UCSRC |= (1

  • 8/11/2019 Advance Module Book

    16/34

    | 14|

    URSEL bit is set in the UBBRH if you want to put stuff in that register, or URSEL bit is set in the UCSRC if you want

    to put stuff in that register.

    Setting the Parity Mode - This is where you set the error checking feature of UART and USARTcommunication. The parity is set only by the transmitter.

    If the parity is set for "even" then there are an odd number of ones in the data bits, the transmitter will set the

    parity bit to a "1" so there will be an even number of ones including the parity bit.

    If the parity is set for "odd" then there are an even number of ones in the data bits, the transmitter will set the

    parity bit to a "1" so there will be an odd number of ones including the parity bit.

    UPM -Parity Mode

    UCSRC |= (1

  • 8/11/2019 Advance Module Book

    17/34

    | 15|

    (WORKSHOP)ROBOTICSROBOTICSOBOTICSROBOTICSOBOTICSROBOTICS

    Innovation

    3indya TechnologiesIntelligence In format ion

    TM

    TM

    For more detail log on to: www.i3indya.com

    voidUSART_Init (unsigned int baud)

    {

    //Put the upper part of the baud number here (bits 8 to 11)

    UBBRH = (unsigned char) (baud >> 8);

    //Put the remaining part of the baud number here

    UBBRL = (unsigned char) baud;

    //Enable the receiver and transmitter

    UCSRB = (1

  • 8/11/2019 Advance Module Book

    18/34

    | 16|

    Well, here is the secret! Shhhhh, don't tell anyone! The UDR actually has two places. The secret locations are

    called TXB and RXB (these are 8-bit locations). The microcontroller is so smart that when you place

    information into the UDR register, the stuff you put in is actually going into the TXB location. If you take the data

    from the UDR register, then you are actually taking the stuff out of the RXB location. You might be asking, what if

    we decided to set our data-bit lenght to 9 bits, cause TXB and RXB is 8-bits only? Those bits are located in the

    UCSRB register and their called, you guessed it, TXB8 and RXB8. Oh, one more thing, this is a polling example

    which means that we are going to wait (tap our toes) for the UDRE (USART Data Register Empty) signal to say

    its ok to go. It's like a stop light. You can't put anything into the UDR if the system is not ready for you. Ok enough

    jibber jabber, lets get to the example:

    voidUSART_Transmit (unsigned int data)

    {

    //Wait until the Transmitter is ready

    while (! (UCSRA & (1

  • 8/11/2019 Advance Module Book

    19/34

    | 17|

    Receive Someth ing !!

    The process for the microcontroller to receive data that exists on a wire from the other microcontroller goes

    like this:

    q The microcontroller detects a start bit

    q The data bits come next and there are taken in concert with the baud rate (or XCK for sychronous) heart

    beatq The data goes to the shift register (like people getting off the train and waiting in queue to get off of the

    train platform)

    q This happens until the first stop bit is received (remember, the second stop bit is ignored)

    q The contents of the data then go directly to the UDR (well, really the RXB, but we still get it from the UDR)

    Here is an example how to receive data:

    unsigned char USART_Receive( void )

    {

    while ( !(UCSRA & (1

  • 8/11/2019 Advance Module Book

    20/34

    ACCELEROMETER

    | 18|

    An accelerometer measures acceleration (change in speed) of anything that it's mounted on. How does it

    work? Inside an accelerator MEMS device are tiny micro-structures that bend due to momentum and gravity.

    When it experiences any form of acceleration, these tiny structures bend by an equivalent amount which can

    be electrically detected

    Axis of Acceleration

    The tiny micro-structures can only measure force in a single direction, or axis of acceleration. This means with

    a single axis measured, you can only know the force in either the X, Y, or Z directions, but not all. So if say your X-

    axis accelerometer endowed robot was running around and ran into a wall (in the X direction). Your robot

    could detect this collision. But if say another robot rammed into it from the side (the Y direction), your robot

    would be oblivious to it. There are many other situations where a single axis would not be enough. It is always a

    good idea to have at least 2 axes (more than one axis).

    Axis of Acceleration

    The tiny micro-structures can only measure force in a single direction, or axis of acceleration. This means with

    a single axis measured, you can only know the force in either the X, Y, or Z directions, but not all. So if say your X-

    axis accelerometer endowed robot was running around and ran into a wall (in the X direction). Your robot

    could detect this collision. But if say another robot rammed into it from the side (the Y direction), your robot

    would be oblivious to it. There are many other situations where a single axis would not be enough. It is always a

    good idea to have at least 2 axes (more than one axis).

    Gravity

    Gravity is acceleration. As such, your accelerometer will always be subject to a -9.81 m/s^2 acceleratio

    (negative means towards the ground). Because of this, your robot can detect what angle it is in respect t

    gravity. If your robot is a biped, and you want it to always remain balanced and standing up, just simply use a 2

    axis accelerometer. As long as the X and Y axes detect zero acceleration, this means your robot device

    perfectly level and balanced.

    Office :G-85, Preet Vihar, New Delhi-110092. Ph. :+91 - 95 6060 5666. Website :www.i3indya.com

    Copyright All Right Reserved

  • 8/11/2019 Advance Module Book

    21/34

    | 19|

    Calculate Acceleration and Angle wrt Gravity

    To calculate the magnitude of acceleration for a single-axis accelerometer

    acceleration_max = sqrt(x^2) = x

    2-axis accelerometer

    acceleration_max = sqrt(x^2+y^2)

    3-axis accelerometeracceleration_max = sqrt(x^2+y^2+z^2)

    To calculate the detected force on an accelerometer due to gravity:

    Force_gravity = -g*cos(angle)(depends on starting axis of sensor)

    Chances are you would have no need to measure the force, but if you reverse the equation you can calculate the

    angle by knowing the detected force:

    cos(sensor_value*conversion_constant / -g)^-1 = angle

    LAB SETUPLog on to :www.i3indya.com

    ROBOTICS

    Office :G-85, Preet Vihar, New Delhi-110092. Ph. :+91 - 95 6060 5666. Website :www.i3indya.com

    Copyright All Right Reserved

  • 8/11/2019 Advance Module Book

    22/34

    | 20|

    INTERRUPTS

    The interrupts generated from an external source are referred as external interrupts. The external sources are

    basically outside the system. Atmel generally provides 4 external interrupt pins which can be used to interrupt

    the microcontroller by an activity occurring outside the system. They are:

    q RESETq INT0

    q INT1

    q INT2

    RESET:It is the highest priority external interrupt. When it is set to zero, microcontroller get reset and theinitial memory address is loaded in PC (Program Counter). It is same as restarting your PC by pressing the

    restart button. It is used when we want that a particular eternal event should make the microcontroller starts

    its work from beginning. It is located on pin no. 9 in most of the AVR microcontrollers.

    INT0, INT1 and INT2: The priority order among these is as follows: INT0, INT1 and INT2. Theseinterrupts are used to tell the microcontroller that a particular event in the external world has happened andthe microcontroller needs to respond to that event. In AVR microcontrollers, they are available on PD2, PD3

    and PB2 pins of the microcontroller.

    Register for configuring Eternal Interrupts:

    1. MCUCR (Microcontroller Control Register):

    This register determines the nature of signal at which the interrupt 0 (INT0) and interrupt 1 (INT1) should

    occur.

    SM2, SE, SM1, SM0:These four bits of the register are used when the microcontroller operates in sleepmode.

    ISC11, ISC 10: These bits are used to configure INT1 interrupt. These bits decides, when the ISR

    corresponding to INT1 should get executed i.e. when should INT1 get enabled. The bits are chosen from thetable given below:

    Office :G-85, Preet Vihar, New Delhi-110092. Ph. :+91 - 95 6060 5666. Website :www.i3indya.com

    Copyright All Right Reserved

  • 8/11/2019 Advance Module Book

    23/34

    | 21|

    ISC01, ISC 00: These bits are used to configure INT0 interrupt. These bits decides, when the ISRcorresponding to INT1 should get executed i.e. when should INT0 get enabled. The bits are chosen from the

    table given below:

    2. MCUCSR (Microcontroller Control and Status Register):

    This register determines the nature of signal at which the external interrupt 2 (INT2) should occur. INT2 is

    edge triggered only, it cannot be used for level triggering like INT0 and INT1.

    ISC2:These bits are used to configure INT0 interrupt. These bits decides, when the ISR corresponding toINT1 should get executed i.e. when should INT0 get enabled. The bits are chosen from the table given below:

    3. GICR (General Interrupt Control Register):

    The register is used for activating and deactivating of external interrupts.

    The respective interrupt is enabled/ activated by setting the corresponding bit to 1. E.g. to activate INT0 bit 6 of

    the register is set to 1.

    Note: These eternal interrupts can only be used if the global interrupt bit in SFIOR register is set. So before

    using the eternal interrupt you need to set the global interrupt bit in SFIOR register. The bit can set by calling a

    predefined function sei(), available in . The sei() is followed by clear interrupt function, cli()

    available in the same header file

    Office :G-85, Preet Vihar, New Delhi-110092. Ph. :+91 - 95 6060 5666. Website :www.i3indya.com

    Copyright All Right Reserved

  • 8/11/2019 Advance Module Book

    24/34

    | 22|

    EEPROM

    What is the EEPROM memory and why would I use it?

    Most of the 8-bit AVRs in Atmel's product line contain at least some internal EEPROM memory. EEPROM, short

    for Electronically Erasable Read-Only Memory, is a form of non-volatile memory with a reasonably long

    lifespan. Because it is non-volatile, it will retain its information during periods of no power and thus is a

    great place for storing data such as device parameters and system configuration at runtime so that it canpersist between resets of the application processor.

    One important fact to note is that the AVR's internal EEPROM memory has a limited lifespan of

    100,000 writes per EEPROM page reads are unlimited. Keep this in mind in your application; try to keep

    writes to a minimum, so that you only write the least amount of information required for your application each

    time you update the EEPROM.

    How is accessed?

    The AVR's internal EEPROM is accessed via special registers inside the AVR, which control the address to bewritten to (EEPROM uses byte addressing), the data to be written (or the data which has been read) as well as

    the flags to instruct the EEPROM controller to perform the requested read or write operation.

    The C language does not have any standards mandating how memory other than a single flat model (SRAM in

    AVRs) is accessed or addressed. Because of this, just like storing arbitrary data into flash memory via your

    program, every compiler's standard library has a unique implementation based on what the author

    believed was the most logical system.

    This tutorial will focus specifically on the AVR-GCC compiler; other compilers may have alternative syntax and

    APIs.

    The avr-libc EEPROM functions

    Including the avr-libc EEPROM header

    The avr-libc library, included with WinAVR and (more recently) the Atmel AVR Toolchain, con- tains

    prebuilt library routines for EEPROM access and manipulation to simplify its use in user applications.

    Before we can make use of those routines, we need to include the EEPROM library header

    with the preprocessor directive #include .

    H KEDHACKED

    sM Sr p. POo

    yP Bo dwere

    G PROU

    Innovat ion

    3indya Technolog i esIntel l igence Informat ion

    TM

    TM

    ETHICAL HACKING

    CYBER SECURITY

    Office :G-85, Preet Vihar, New Delhi-110092. Ph. :+91 - 95 6060 5666. Website :www.i3indya.com

    Copyright All Right Reserved

  • 8/11/2019 Advance Module Book

    25/34

    | 23|

    Once added to your application, you now have access to the EEPROM memory, via the routines now

    provided by the module of avr-libc. There are five main types of EEPROM access: byte,

    word, dword, float and block. Each type has three types of functions; a write, an update, and a read variant. The

    names of the routines exposed by our new headers are:

    uint8_t eeprom_read_byte ( const uint8_t * addr )

    void eeprom_write_byte ( uint8_t *addr , uint8_t value )

    void eeprom_update_byte ( uint8_t *addr , uint8_t value )

    uint16_t eeprom_read_word ( const uint16_t * addr )

    void eeprom_write_word ( uint16_t *addr , uint16_t value )

    void eeprom_update_word ( uint16_t *addr , uint16_t value )

    uint16_t eeprom_read_dword ( const uint32_t * addr )

    void eeprom_write_dword ( uint32_t *addr , uint32_t value )

    void eeprom_update_dword ( uint32_t *addr , uint32_t value )

    uint16_t eeprom_read_float ( const float * addr )

    void eeprom_write_float ( float *addr , float value )void eeprom_update_float ( float *addr , float value )

    void eeprom_read_block ( void * pointer_ram , const void * pointer_eeprom , size_t n) void eeprom_write_block (

    const void * pointer_ram , void * pointer_eeprom , size_t n) void eeprom_update_block ( const void * pointer_ram ,

    void * pointer_eeprom , size_t n)

    In AVR-GCC, a word is two bytes long and a double word is twice that (4-bytes), while a block is an arbitrary

    number of bytes which you supply (think string buffers, or customer data types you create) and a float is

    also four bytes long (but can hold fractional values).

    The update vs. Write functions

    Historically, there were only two types of EEPROM functions per data type; a write function, and a read

    function. In the case of the EEPROM write functions, these functions simply wrote out the requested

    data to the EEPROM without any checking performed; this resulted in a reduced EEPROM lifespan if the

    data to be written already matches the current contents of the EEPROM cell. To reduce the wear on the AVR's

    limited lifespan EEPROM, the new update functions were added which only perform an EEPROM write if

    the data differs from the current cell contents.

    Reading data from the EEPROM

    To start, let's try a simple example and try to read a single byte of EEPROM memory, let's say at location 46. Our

    code might look like this:

    # include

    void main ( void )

    {

    uint8_t ByteOfData ;

    ByteOfData = eeprom_read_byte (( uint8_t *) 46) ;

    }

    Office :G-85, Preet Vihar, New Delhi-110092. Ph. :+91 - 95 6060 5666. Website :www.i3indya.com

    Copyright All Right Reserved

  • 8/11/2019 Advance Module Book

    26/34

    | 24|

    This will read out location 46 of the EEPROM and put it into our new variable named ByteOfData. How does it

    work? Firstly, we declare our byte variable, which I'm sure you're familiar with:

    uint8_t ByteOfData ;

    Now, we then call our eeprom_read_byte() routine, which expects a pointer to a byte in the EEPROM space.

    We're working with a constant and known address value of 46, so we add in the typecast to transform that

    number 46 into a pointer for the eeprom_read_byte() function to make the compiler happy.

    EEPROM words (two bytes in size) can be written and read in much the same way, except they require a pointer

    to an int:

    # include

    void main ( void )

    {

    uint16_t WordOfData ;

    WordOfData = eeprom_read_word (( uint16_t *) 46) ;}

    And double words and floats can be read using the eeprom_read_dword() and eeprom_read_float() functions

    and a pointer to a uint32_t or float variable.

    Writing data to the EEPROM

    Alright, so now we know how to read bytes and words from the EEPROM, but how do we write data? The same

    principals as reading EEPROM data apply, except now we need to supply the data to write as a second argument

    to the EEPROM functions. As explained earlier, we will use the EEPROM update functions rather than the oldwrite functions to preserve the EEPROM lifespan where possible.

    # include

    void main ( void )

    {

    uint8_t ByteOfData ;

    ByteOfData = 0 x12 ;

    eeprom_update_byte (( uint8_t *) 46 , ByteOfData );

    }

    Office :G-85, Preet Vihar, New Delhi-110092. Ph. :+91 - 95 6060 5666. Website :www.i3indya.com

    Copyright All Right Reserved

  • 8/11/2019 Advance Module Book

    27/34

    | 25|

    Unsurprisingly, the process for writing words of data to the EEPROM follows the same pattern:

    # include

    void main ( void )

    {

    uint16_t WordOfData ;

    WordOfData = 0 x1232 ;

    eeprom_update_word (( uint16_t *) 46 , WordOfData );

    }

    And double words and floats can be written using the eeprom_update_dword() and

    eeprom_update_float() functions and a uint32_t or float variable.

    EEPROM Block Access

    Now we can read and write bytes and words to EEPROM, but what about larger datatypes, or strings? This is

    where the block commands come in.

    The block commands of the avr-libc header differ from the word and byte functions shown

    above. Unlike its smaller cousins, the block commands are both routines which return nothing, and thus

    operate on a variable you supply as a parameter. Let's look at the function declaration for the block reading

    command

    void eeprom_read_block ( void * pointer_ram , const void * pointer_eeprom , size_t n)

    Wow! It looks hard, but in practice it's not. It may be using a concept you're not familiar with however, void-

    type pointers.

    Normal pointers specify the size of the data type in their declaration (or typecast), for example uint8_t* is a

    pointer to an unsigned byte and int16_t* is a pointer to a signed int. But void is not a variable type we can

    create, so what does it mean?

    A void type pointer is useful when the exact type of the data being pointed to is not known by a function, or is

    unimportant. A void is merely a pointer to a memory location, with the data stored at that location being

    important but the type of data stored at that location not being important. Why is a void pointer used?

    Using a void pointer means that the block read/write functions can deal with any data type you like, since all

    the function does is copy data from one address space to another. The function doesn't care what data it copies,

    only that it copies the correct number of bytes.

    Ok, that's enough of a derail back to our function. The block commands expect a void* pointer to a RAM

    location, a void* pointer to an EEPROM location and a value specifying the number of bytes to be written or

    read from the buffers. In our first example let's try to read out 10 bytes of memory starting from EEPROM

    address 12 into a string.

    Office :G-85, Preet Vihar, New Delhi-110092. Ph. :+91 - 95 6060 5666. Website :www.i3indya.com

    Copyright All Right Reserved

  • 8/11/2019 Advance Module Book

    28/34

    | 26|

    # include

    void main ( void )

    {

    uint8_t StringOfData [10];

    eeprom_read_block (( void *) StringOfData , ( const void *) 12 , 10) ;

    }

    Again, looks hard doesn't it! But it isn't; let's break down the arguments we're sending to the

    eeprom_read_block() function.

    ( void *) StringOfData

    EEPROM BLOCK ACCESS

    The first argument is the RAM pointer. The eeprom_read_block() routine modifies our RAM buffer and so

    the pointer type it's expecting is a void* pointer. Our buffer is of the type uint8_t, so we need to typecast itsaddress to the necessary void type.

    ( const void *) 12

    Reading the EEPROM won't change it, so the second parameter is a pointer of the type const void. We're

    currently using a fixed constant as an address, so we need to typecast that constant to a pointer just like with

    the byte and word reading examples.

    Obviously, this number of bytes to be read. We're using a constant but a variable could be supplied instead.

    The block update function is used in the same manner, except for the update routine the RAM pointer is of

    the type const void and the EEPROM is just a plain void pointer, as the data is sourced from the RAM location

    and written to the EEPROM location:

    # include

    void main ( void )

    {

    uint8_t StringOfData [10] = " TEST ";

    eeprom_update_block (( const void *) StringOfData , ( void *) 12 , 10) ;

    }

    Office :G-85, Preet Vihar, New Delhi-110092. Ph. :+91 - 95 6060 5666. Website :www.i3indya.com

    Copyright All Right Reserved

  • 8/11/2019 Advance Module Book

    29/34

    | 27|

    Using the EEMEM attribute

    You should now know how to read and write to the EEPROM using fixed addresses. But that's not very

    practical! In a large application, maintaining all the fixed addresses is an absolute nightmare at best. But

    there's a solution to push this work onto the compiler the EEMEM attribute.

    Defined by the same

  • 8/11/2019 Advance Module Book

    30/34

    | 28|

    # include

    uint8_tEEMEM NonVolatileChar ;

    uint16_t EEMEM NonVolatileInt ;

    uint8_tEEMEM NonVolatileString [10];

    int main ( void )

    {

    uint8_t SRAMchar ;

    SRAMchar = eeprom_read_byte (& NonVolatileChar );

    }

    It works! Why don't we try to read out the other variables while we're at it. Our second variable is an int, so

    we need the eeprom_read_word() routine:

    # include

    uint8_tEEMEM NonVolatileChar ;uint16_t EEMEM NonVolatileInt ;

    uint8_tEEMEM NonVolatileString [10];

    int main ( void )

    {

    uint8_tSRAMchar ;

    uint16_t SRAMint ;

    SRAMchar = eeprom_read_byte (& NonVolatileChar ); SRAMint = eeprom_read_word (&

    NonVolatileInt );

    }

    And our last one is a string, so we'll have to use the block read command:

    # include

    uint8_tEEMEM NonVolatileChar ;

    uint16_t EEMEM NonVolatileInt ;

    uint8_tEEMEM NonVolatileString [10];

    int main ( void )

    {uint8_tSRAMchar ;

    uint16_t SRAMint ;

    uint8_tSRAMstring [10];

    SRAMchar = eeprom_read_byte (& NonVolatileChar ); SRAMint = eeprom_read_word (&

    NonVolatileInt );

    eeprom_read_block (( void *) SRAMstring , ( const void *) NonVolatileString , 10) ;

    }

    Office :G-85, Preet Vihar, New Delhi-110092. Ph. :+91 - 95 6060 5666. Website :www.i3indya.com

    Copyright All Right Reserved

  • 8/11/2019 Advance Module Book

    31/34

    | 29|

    Int er facing UART wi th m icroconroll er

    Check 1

    #include

    #define F_CPU 1000000UL

    #include#define Baud_Rate 1200UL

    #define ubrr_value ((F_CPU/(16UL*Baud_Rate)) - 1)

    voidusart_send(unsigned char);

    unsigned char usart_rec();

    voidusart_init();

    MANUALS

    Features of i3indya AT mega 16 Development Board.

    q On board AT mega 16, L293D, 7805 ICs. (14,10)

    q 8 internally connected LEDs for experiment. (3) (internal connections through B.0 to B.7)

    q 8 ADC channels (4) (internal connection through A.0 to A.7)q Two extra 5V power ports for external supply. (6)

    q Internal connection of motors through B.0,B.1,B.2,B.3.

    Office :G-85, Preet Vihar, New Delhi-110092. Ph. :+91 - 95 6060 5666. Website :www.i3indya.com

    Copyright All Right Reserved

  • 8/11/2019 Advance Module Book

    32/34

    | 30|

    void main()

    {

    unsigned int x;

    usart_init();

    DDRB=0XFF;

    while(1)

    {

    x=usart_rec();

    usart_send(x);

    if(x=='f')

    PORTB=0b00101000;

    else if(x=='b')

    PORTB=0b00010100;

    else if(x=='l')

    PORTB=0b00100000;

    else if(x=='r')

    PORTB=0b00001000;

    elsePORTB=0X00;

    }

    }

    void usart_init()

    {

    UBRRL = ubrr_value;

    UBRRH = (ubrr_value>>8);

    UCSRC=(1

  • 8/11/2019 Advance Module Book

    33/34

    | 31|

    Interfacing DTMF with microcontroller

    Check 1

    #include

    #include

    void main()

    {

    DDRA=0b00000000;

    DDRB=0b11111111;

    while(1)

    {

    int x=PINA & 0b00001111;

    if(x==0b00000001)

    PORTB=0b00001010;

    else if(x==0b00000010)PORTB=0b00000101;

    else

    PORTB=0b00000000;

    }

    }

    ROBO SHOPROBO SHOPOBO SHOPROBO SHOPi3indya

    www.i3indya.comOffice :G-85, Preet Vihar, New Delhi-110092. Ph. :+91 - 95 6060 5666. Website :www.i3indya.com

    Copyright All Right Reserved

  • 8/11/2019 Advance Module Book

    34/34

    Using external interrupts

    Check 1

    #include

    #include

    #include

    void interrupt();

    int main()

    {

    interrupt();

    while(1)

    {

    DDRC=0b11111111;

    DDRB=0b11111111;

    PORTB=0b11111111;

    _delay_ms(1000);PORTB=0b00000000;

    _delay_ms(1000);

    }

    return 0;

    }

    void interrupt(void)

    {

    MCUCR=(0