Saturday, January 05, 2013

MATLAB TUTORIAL 1- GETTING STARTED




Hi friends….!! This time we are here to start our journey of learning MATLAB , a very useful and powerful software for electronics lovers and others too . So in this article first of all we will discuss about some basics of  MATLAB …..lets start…

DESKTOP INTRODUCTION


This is the first window which u will see after starting MATLAB.






Now here you can see these sections-

CURRENT FOLDER- In this section you can you can access your files to use.

COMMAND WINDOW- Here you will enter your commands in command line started with (>>)

WORKSPACE- This section explore all the data which you create or import from files.

COMMAND HISTORY- It is a very important panel , here you can view your commands and you can can rerun those commands too.


USE OF VARIABLES

Starting from very basic , you can se MATLAB as a simple calculator. For this you have to enter the desired mathematical expression in command line , say it is 1+2*5, so just type it in command line.

>> 1+2*5
Press enter and MATLAB will respond as

ans =

    11

Now you can see we have not assigned any variable and MATLAB used a default variable ans for this calculation now you can choose any variable for it, say ‘x’ . So we will type

>> x=1=2*5
And MATLAB will respond as

x =
       11 

Now this variable can be used for further calculations as well like,

>> y=x*5
This time MATLAB will respond as

y =
      55

 This is the list for some basic arithmetic  operation commands to be used for simple calculations

+     addition
 -     subtraction
*     multiply
/    divide


HIERARCHY OF ARITHMATICAL OPERATIONS

MATLAB performs the arithmetical operation in same manner as taught in high school algebra first parentheses then exponents (from left to right), then division(left to right), multiplication( left to right), addition( left to right), sand at last subtraction ( left to right).


Use of parentheses

Like simple mathematics calculations parentheses can also be used in MATLAB for solving expressions and use of parentheses will give a different answer of the expression.

For example-  








In matlab it becomes 

>> 1/(2+4)+4/5*6/7

ans =
           0.8524

But without parentheses it will be

>> 1/2 +4+4/5*6/7

ans =
          5.1857

So be careful using parentheses.

ERROR MESSAGE AND CORRECTION

Some times an error message displays if you will use a wrong command like

>> 2x
??? 2x
     |
Error: Unexpected MATLAB expression.

To correct this mistake you can obviously retype or you can edit and re execute same command by using UP ARROW KEY.

MANAGING THE WORKSPACE

 clear command

>>clear
clear command is used to clear all the variables from the workspace, sometime similar variable in two different operations can cause ambiguity.

 who and whos command

>> who
This command gives detail of all the variable in the memory. whos is more specific one and provides more details.

SAVING THE WORK SESSION

diary , diary off and diary on command

during your work if you want to save any particular work session you can use diary command as

>> diary
Or
>> diary Filename

Here filename can be any arbitrary name chosen by you.

diary command is used to save entire MATLAB session but you can stop recording at any instant by using diary off command and you can again start it with diary on command. The created file is a simple text file can be opened and edited in any editor.

HOW TO USE MULTIPLE COMMAND IN SINGLE LINE

MATLAB provides us facility to use multiple commands in single line. These different commands should be separated by comma(,) or semicolon(;) like


>> a=9;c=a+9,d=c+9   

c =
    18
d =
     27

CLEARING , ABORTING AND CONTINUING

>> clc       

 Clearing the command window

>> ctrl
Aborting the MATLAB session

>>…
To continue a line.

GETTING HELP

To get help about various MATLAB operations you can use HELP BROWSER of matlab by clicking on the HELP BROWSER ICON
On the other hand you can use help command

>>help

So friends these are the basics of MATLAB, I hope it was useful for you …..we will learn about various commands and operations in upcoming tutorials. If you have any query , doubt or critical views just leave a comment here………


        THNX

Sunday, December 09, 2012

EMBEDDED SYSTEMS TUTORIAL7- TIMERs IN AVR PART-1





Hiii…..friends as we discussed in previous articles that AVR MCU provides us facility of using some inbuilt advance features, TIMER is one of then and in this tutorial we are going to discuss about the TIMERs in AVR. Timer is a very useful and most widely used feature of AVR MCU it will be used in various applications and projects so one must have knowledge of TIMERs. Have a look….. 


INTRODUCTION

The TIMERs in AVR will be discussed in OVERFLOW MODE (TIMER0) and OUTPUT COMPARE MODE (TIMER1) in this tutorial we will discuss OVERFLOW MODE (TIMER0) only.

TIMER…??

A TIMER is nothing but a register . its size may bee 8 bit or 16 bit, which is called resolution of the timer. A 8 bit register can store numbers 0-255 , similarly a 16 bit register can store numbers 0-65535. The value of these registers can be increased or decreased without the CPU intervention, at a rate defined by the user. this rate can be can be equal to or less than the clock frequency of the CPU. The frequency at which timer register increases or decreases is known as Timer frequency.
We can use the TIMER registers to calculate the TIMER efficiently because the increment of register value by 1 means one clock has completed. So when the timer OVERFLOWS means the register value reaches to its maximum , we can inform CPU sending an interrupt. After reaching maximum value register again resets to 0. In overflow condition we must have to write a INTRUPPT SERVICE ROUTINE(ISR) to handle the event.
  



USING TIMER IN OVERFLOW MODE(TIMER0)

In ATMEGA 16 and 32 , there are three timers are available , of which simplest is TIMER0, with 8 bit resolution.

THE PRESCALER

The Prescaler is a mechanism for generating clock for timer by the CPU clock. As you know that CPU has a clock source such as a external crystal of internal oscillator. Normally these have the frequency like 1 MHz,8 MHz, 12 MHz or 16MHz(MAX). The Prescaler is used to divide this clock frequency and produce a clock for TIMER. The Prescaler can be used to get the following clock for timer. No Clock (Timer Stop). No Prescaling (Clock = FCPU) FCPU/8 FCPU/64 FCPU/256 FCPU/1024

TIMER0 REGISTERS.

Here is the description of some register of TIMER0 , which we are going to use-

TCCR0 – TIMER COUNTER CONTROL REGISTER

This register is used  configure the timer.


As you can see there are 8 Bits in this register each used for certain purpose. For this tutorial I will only focus on the last three bits CS02 CS01 CS00 They are the CLOCK SELECT bits. They are used to set up the Prescaler for timer.




TCNT0 – TIMER COUNTER 0

This register is the main counter in the TIMER0. The timer clock counts only this register as 1. It means tomer clock will increase the value of this 8 bit register by 1 every timer clock pulse.

TIMSK- TIMER INTRUPPT MASK REGISTER
   


This register is used to activate/deactivate interrupts related with timers. This register controls the interrupts of all the three timers. The last bit  BIT0 Controls the interrupts of TIMER0. 

TOIE0 : This bit when set to “1” enables the OVERFLOW interrupt.

PROGRAMMING


PRESCALER- 1024
CLOCK FREQUENCY- 16 MHz
Clock input of TIMER0 = 16MHz/1024 = 15625 Hz Frequency of Overflow = 15625 /256 = 61.0352 Hz if we increment a variable “count” every Overflow when “count reach 61” approx one second has elapse.  If count reaches 61 we will toggle PB0 which is connected to LED and reset “count= 0”.

SETTING UP THE TIMER0


  // Prescaler = FCPU/1024
   TCCR0|=(1<<CS02)|(1<<CS00);
 //Enable Overflow Interrupt Enable
   TIMSK|=(1<<TOIE0);
 //Initialize Counter
   TCNT0=0;

OR

  // Prescaler = FCPU/1024
   TCCR0|=TCCR0|00000101;
 //Enable Overflow Interrupt Enable
   TIMSK|=TIMSK|00000001;
 //Initialize Counter
   TCNT0=0;



Now the timer is set and firing Overflow interrupts at 61.0352 Hz

The ISR


ISR(TIMER0_OVF_vect)
{
//This is the interrupt service routine for TIMER0 OVERFLOW Interrupt.
//CPU automatically call this when TIMER0 overflows.

//Increment our variable
  
   count++;
   if(count==61)
   {
   PORTC=~PORTC; //Invert the Value of PORTC
   count=0;
   }
}


COPMLETE SOURCE CODE


/* PROGRAM TO BLINK LE AT 0.5Hz CONNECTED AT PB0 */


#include <avr/io.h>
#include <avr/interrupt.h>

volatile uint8_t count;

void main()
{
   // Prescaler = FCPU/1024
   TCCR0|=(1<<CS02)|(1<<CS00);

   //Enable Overflow Interrupt Enable
   TIMSK|=(1<<TOIE0);

   //Initialize Counter
   TCNT0=0;

   //Initialize our varriable
   count=0;

   //PB0 as out put
   DDRB|=0x01;

   //Enable Global Interrupts
   sei();

   //Infinite loop
   while(1);
}

ISR(TIMER0_OVF_vect)
{
   //This is the interrupt service routine for TIMER0 OVERFLOW Interrupt.
   //CPU automatically call this when TIMER0 overflows.

   //Increment our variable
   count++;
   if(count==61)
   {
      PORTB=~PORTB; //Invert the Value of PORTB
      count=0;
   }
}




Now just compile the source code and burn the hex file generated in the ATMEGA 16, then implement the circuit as shown below on a breadboard or on your development board whatever you want to use, and you will find your LED blinking at a rate of 0.5 Hz.




 So we have studied the basic of TIMERs in AVR in next tutorial we will discuss about TIMER IN OUTPUT COMPARE MODE(TIMER1) .
I hope you found this article useful in case of any doubt, query, or critical view just leave a comment.



THNX……:)

Saturday, December 08, 2012

EMBEDDED SYSTEMS TUTORIAL6- INTERFACING 7 SEGMENT DISPLAY WITH AVR



Hii… friends its our next AVR tutorial this time we are going to discuss about interfacing of 7 segment display with a AVR. You must have seen 7 segment display in you house hold inverters, in a zerox machine or even on your microwave oven. So in this tutorial , We will discuss about 7 segment display  , its types and its interfacing with AVR along with source code so have a look…J

INTROUCTION
A seven segment display is the most basic electronic display device that can display digits from 0-9. They find wide application in devices that display numeric information like digital clocks, radio, microwave ovens, electronic meters etc. The most common configuration has an array of eight LEDs arranged in a special pattern to display these digits. They are laid out as a squared-off figure ‘8’. Every LED is assigned a name from 'a' to 'g'  and 'dp'and is identified by its name. Seven LEDs 'a' to 'g' are used to display the numerals while eighth LED 'dp' is used to display the dot/decimal. 
       

 PACKAGE AND TYPES

A seven segment is generally available in ten pin package. While eight pins correspond to the eight LEDs, the remaining two pins (at middle) are common and internally shorted. These segments come in two configurations, namely, Common cathode (CC) and Common anode (CA). In CC configuration, the negative terminals of all LEDs are connected to the common pins. The common is connected to ground and a particular LED glows when its corresponding pin is given high. In CA arrangement, the common pin is given a high logic and the LED pins are given low to display a number.



HOW IT WORKS

Now let us suppose a digit is to be displayed on the 7 segment. Suppose the digit is 1 in such case LEDS corresponding b and c should glow, so we can say foe each and every digit a specific set of LEDs has to glow . the LEDs to be golw with corresponding digits
Are as shown.

     

INTERFACING WITH AVR MICROCONTROLLER

Here is step wise process for inTERfacing a 7 segment display with AVR microcontroller( ATMEGA 16)

SETP 1
CICUIT CONNECTION
Here we are using a common anode 7 segment display.
Connect your 7 segment display with the atmega 16 as per the connections shown in the diagram below



STEP 2

SOURCE CODE

Create the new project in AVR STUDIO4 and compile the code written below-