Saturday, January 12, 2013

EMBEDDED SYSTEMS TUTORIAL 9- INTERFACING 16X2 LCD WITH AVR PART-2




Hi…. friends this time I am here with another article ,we are going to discuss some interesting functions and projects of LCD. In this tutorial we will learn about rotating a text , displaying integer and integers operations, displaying floating point numbers and their operations .

ROTATING A TEXT STRING
 
Rotating a text on LCD is a bit tricky thing, to do so we will change the position of text every time with some delay after clearing the screen so it will look like text is moving the sample code is given below just use this source code in your LCD project and text will look like moving-

/*
 * MOVINGTEXT.c
 *
 * Created: 1/12/2013 8:36:31 PM
 *  Author: ABHILASH DIXIT
 */


#include <stdlib.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "lcd.h"
#include "lcd.c"

int main(void)
{
       while(1)
       {
             lcd_init(LCD_DISP_ON);
             for(int i=-10;i<16;i++)     // setting an integer i to 0 and incrementing its value till 16
             {
                    lcd_gotoxy(i,0);       // setting cursor to (i,0)
                    lcd_puts("LCD"); // to display text
                    if(i==13)
                    i=0;
                    _delay_ms(100);              // some delay
                    lcd_clrscr();            //  clear display
                   
             }
       }
}


So this was the  trick to rotate the text string on the LCD.

USING INTEGERS ON LCD

You cannot use a integer on LCD directly  for this you have to use a function itoa(), it converts an integer in string form. So if we want to use integers in LCD we will first create a buffer of some size as

char buf[8];

now lets suppose the number to be displayed is i , so we will use itoa() function for i to convert it in string form as

itoa(i,buf,10);

here 10 is for decimal, for hexadecimal we will use 16 and for binary we will use 2.
Now finally i can be displayed using lcd_puts() command as-
lcd_puts(buf);


 so lets try a source code – in this program we will display a number i=0 and will increase its value till 10.

/*
 * DISPLAY INTEGER.c
 *
 * Created: 1/12/2013 9:16:31 PM
 *  Author: ABHILASH DIXIT
 */

#include <stdlib.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "lcd.h"
#include "lcd.c"
#include <util/delay.h>
int main()
{
       while(1)
       {
             lcd_init(LCD_DISP_ON);   // initializing display
             int i=0;                  // setting value of integer i to 0
             char buf[8];              // initializing buffer
             lcd_gotoxy(0,0);           // setting cursor position to (0,0)
             lcd_puts("i=");            // displaying text
             for(i=0;i<=9;i++)          // increasing value of i till 9
             {
                    itoa(i,buf,10);            // using itoa() function
                    lcd_gotoxy(2,0);             // setting cursor position to (2,0)
                    lcd_puts(buf);             // displaying value of i every time
                    _delay_ms(100);            // some delay
                   
             }
       }
}

USING A FLOATING POINT NUMBER

Like an integer a floating point number cannot be used directly too , so to use an floating point no on LCD we have to use some function called  dtostrf() it converts an floating point number into string . let us suppose we have to display a floating point number say i on LCD.  So first of all we have to create a buffer of some size

char buf[8];

then we will use dtostrf () function with i to convert i in string form as

dtostrf(i,10,6,buf);

here 10 is for decimal and 6 is denoting numbers after decimal we can change it accordingly.

Now the no i can be displayed using lcd_puts() command 

Here is the source code for simply displaying a floating point number i=1.12345


/*
 * DISPLAY FLOAT.c
 *
 * Created: 1/12/2013 9:26:31 PM
 *  Author: ABHILASH DIXIT
 */

#include <stdlib.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "lcd.h"
#include "lcd.c"
int main()
{
       lcd_init(LCD_DISP_ON);
       float i=1.12345;
       char buf[8];
       dtostrf(i,10,6,buf);
       lcd_puts(buf);
}


NOTE:-  to use itoa() and dtostrf() we must include stdlib.h header file in beginning of the program .


I hope it was a useful article for you all……in case of any query, doubt or critical view just leave a comment……..in next tutorial we well discuss about some other embedded systems topics till then ………..bye..

THNX…..:)

Tuesday, January 08, 2013

EMBEDDED SYSTEMS TUTORIAL8 - INTERFACING 16X2 ALPHANUMERIC LCD WITH AVR PART 1




Hi..!! friends this time I am here with a very very useful tutorial for embedded system beginners, this time we are going to learn how to interface a LCD module, after learning LCD interfacing you will be able to make so many important project related to LCD display….so lets start….!!


THING  WE NEED TO START


For learning LCD interfacing we must have a 16X2 alphanumeric LCD display, of course our USBasp programmer and ATmega 16.

For interfacing LCD we must have LCD  library, u can found so many libraries on net but Mr. Peter Fluery’s library is most trusted and most widely used one , THNX to him you can directly download his library from this link-
                 lcd library


LCD PIN DISCRIPTION


A 16x2 alphanumeric LCD contains 16 pins and description of these pins is given below
1     VSS    (GND )
2     VCC   (+5V)
3     VEE   ( adjust contrast)
4     RS
5     R/W
6     E
7     DB0
8     DB1
9     DB2
10   DB3
11   DB4
12   DB5
13   DB6
14   DB7
15   LED+  (+5V)
16   LED-    (GND)
In this tutorial we are using our LCD in 4bit mode so we will use DB4-DB7(PIN 11-14 of LCD) as DATA pins.

USING LIBRARIES


The LCD library which u will download will contain mainly two files lcd.c and lcd.h, by changing  the PORT in lcd.h you can change the port of LCD connection, by default it is set to PORTA. You can also change pins of LCD connection from same file for this purpose you have to open lcd.h with notepad , then change PORT/PINS according to your choice and then save it. For adding  library support in your program you have to paste lcd.c and lcd.h  in the same folder where your  .c file of program is contained. And these libraries can be added to the program by including these header file in starting of program-

#include”lcd.c”

#include”lc.h”

COMMANDS USED

In lcd.h file you can get information about the commands which we have to use in our program , I am discussing some of them here but you can check the library to get knowledge about all the useful command

Initializing the LCD
To initialize the LCD in program we have to this command is used
lcd_init(LCD_DISP_ON);

To move cursor at a specific position
lcd_gotoxy(x,y);                                              here x and y are the co-ordinates of desired location.

To clear the display
lcd_clrscr();

To set cursor to home position
lcd_home();

To display any text string
lcd_puts("desired text string");

in addition to these commands there are many more command but for starting interfacing these commands are sufficient and we will discuss other commands later.


SAMPLE CODE

Here is a simple sample code to start with this time we are going to display a simple text on string on the LCD .

#include <stdlib.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include<util/delay.h>
#include "lcd.h"
#include "lcd.c"

int main(void)
{
while(1)
{
               lcd_init(LCD_DISP_ON);                                           // Initialize LCD
               lcd_gotoxy(2,0);                                                          // setting cursor to (2,0)
               lcd_puts("TECH_STRONG");                                     // to display text
               _delay_ms(500);                                                          // wait for some time
               lcd_clrscr();                                                                 //  clear display
               lcd_gotoxy(1,0);                                                          //setting cursor to (1,0)
               lcd_puts("LCD INTERFACING");                              // to display text
               lcd_gotoxy(2,1);                                                          //setting cursor to (1,0)
               lcd_puts("BY ABHILASH");                                       // to display text
               _delay_ms(500);                                                         // wait for some time
               lcd_clrscr();                                                                //  clear display

}
}

CIRCUIT DIAGRAM FOR CONNECTIONS

So that’s it now just make a simple circuit as shown below.




The contrast of LCD can be adjusted by using the variable resistance used here.
Now compile the program and burn your MCU with corresponding hex file and our first LCD interfacing project is ready……now you can display  any thing on the LCD …..yes I know you are going to display your name first…..:p


I hope this article was useful for  guys in case of any query, doubt or critical view just leave a comment. In next LCD interfacing tutorial we will discuss about rotating a text , displaying integers and some basic LCD projects……..till then bye….enjoy your LCD……!!


THNX….:)

Monday, January 07, 2013

MATLAB TUTORIAL 2- MATHEMATICAL FUNCTIONS AND BASIC CURVE PLOTTING




Hi friends its our second MATLAB tutorial and this time we are going to discuss  about  mathematical functions and basic curve plotting in MATLAB. MATLAB provides us a lot of commands and operation to deal with complex mathematical calculations and curve plotting….lets start….!!

MATHEMATICAL OPERATIONS

MATLAB provides us a lot of mathematical functions for computational purpose. There are a lot of predefined mathematical functions in MATLAB , you can get help about these functions by typing…

>> help elfun
For elementary functions and

>>help specfun
For special functions

This is the list of most commonly used mathematical functions





 x  is a variable of our choice.

These functions are built in MATLAB and also called as built- ins.

In addition to these functions MATLAB also provides us some predefined constant values . this table contains some predefined constant values



SOME EXERCISE

Let us suppose we have an expression to solve say

y=e2+sin(x)+log(z)        at x=5, z=6
So command will be
>> x=5;z=6;
>> y=exp(2)+sin(x)+log(z)
y =
      8.2219
Now suppose we have to solve
       x=sin(Ï€/4)


Command will be
>> x=sin(pi/4)
x =
       0.7071

So now we are able to use all these functions for our calculation purpose but we have to be careful sometime as
1.      i and j which are generally defined as loop indices contains a constant value as iota in MATLAB so cannot be used as loop indices you have to use some other variables.
2.      Try to use built in functions on right hand side of the expression , reassigning the value can cause problem sometime.


CURVE PLOTTING
MATLAB has provided us a very powerful graphic tool set we can plot any function in 2D and in 3D too with the help of this tool set this time we are only focusing on 2D plotting have a look…
Simple plotting
Let us suppose we have to plot a curve between x and y with y=[1,4,5,7] at different values of x say x=[0,1,2,3] , now first of all we have to create linear array of x and y as
>>x=[0,1,2,3];
>>y=[1,4,5,7];
Then we can plot our graph using plot command as
>>plot(x,y)
Now on executing this command a window with corresponding plot will pop up as

Now suppose we have  to plot the graph of any function in a specified range say we have to plot graph of sin(x) in range x=[0 to 180]
So first of all we have to create a vector of x values ranging from 0 to 180 , then compute the sine of these values and finally plot it, command will be
>>x=0:pi/100:2*pi;                  (0means start at 0, pi/100 means increment of pi/100, stop at 180)                                       
>>y=sin(x);
>>plot(y)
And result will be

TITLES , LABLES AND ANNOTATIONS
We can also add titles , labels and annotations in plot , say in previous examples we have to add titles so commands will be
>>xlabel(‘0:2\pi’)                                  (here \pi creates chatacter Ï€)
>>ylabel(‘sine of x’)
>>title(‘graph 1’)
And result will be



USING MULTIPLE DATA SETS ON A GRAPH
We can also plot more than one graph in a single plot, sat we have to plot two graphs as y1= sin(x), y2= cos(x)  for the range x= 0 to 2Ï€
For this purpose commands will be
>>x=0:pi/100:2*pi;
>>y1=sin(x);
>>y2=cos(x);
>>plot(x,y1,’- -‘,x,y2,’-‘)
>>xlabel(‘0:2\pi’)
And finally result will be




LINE COLOUR ,STYLE AND MARKERS
For different plots and data sets we can use different line colour , styles and markers using the simple command
         plot(x,y,’style _colour_marker’)
here line_colour and marker is triplet from this table


you can find additional information by
>>help plot
Or
>> doc plot

so these are the basics of mathematics operations and plotting ...i hope this article was useful for you guys, in case of any query, doubt or critical view just leave a comment here...in our next tutorial we will discuss about basics of matrix and array.. till then bye....:)


     THNX

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