Saturday, 25 June 2016

Graph plotting in Raspberry Pi

Gnuplot in Raspberry Pi 


Raspberry Pi has been used  for interfacing different types of sensors. Signals from these sensors are stored in a file, generally this file is called data file. Graphical representation of these data offer better visualisation. Here I am going to explain How these data file can be converted into graph using a tool called gnuplot. Install gnuplot in your raspberry pi using apt-get.


first of all create a data file with name data.dat

sudo nano data,dat

and make two column with some data in it


save it with ctrl X then Y

open gnuplot from terminal  by entering gnuplot in terminal. gnuplot will open with its prompt


This is a gnuplot command prompt. From here we will plot the data from data.dat file.

In gnuplot command line enter

plot 'data.dat' with lines and we get our data plotted as following


Set title to this plot by entering

set title 'graph'
plot 'data.dat' with lines



 Lets add some more things to graph. Enter the command as I had shown in figure below





 In this way we can do many things with this plot. In above figure 1:2 represents first column as X axis and second column as Y axis.

Lets add one more column to our data file.
sudo nano data.dat




We can plot multiple graph in a single gnuplot window, Since we are plotting 2D graphs, plotting would require two columns.  Have a look at  figure below and it is self explanatory





Ok we have seen how to plot from a data file.

But how to plot a graph from two different data file. To understand this, create another data file with name data1.dat

sudo nano data1.dat









open gnuplot and follow the figure below, it is just simple as that




Fine, now we know how to plot from two different data files

But always opening gnuplot from terminal is not always desirable. It mean we can invoke gnuplot from our program and can pass gnuplot commands to it. Here is a simple script in C

sudo nano gp.c



compile and run it with

gcc gp.c
sudo ./a.out

you will get your plot open .To add more functionality just pass more gnuplot command through fprintf.

gnuplot is open source and fully documented. Go to the official page to explore more options. 

Wednesday, 15 June 2016

Modifying UART clock speed of raspberry pi

 
Before using the UART at GPIO, First it is needed to disable the serial console . Open cmdline.txt file present at /boot directory

sudo /boot/cmdline.txt

This file would have contain following text

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

we need to modify this, modify the content so that it should looks like following


dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

save it with ctrl x then y.

Now we can use UART

To modify the UART clock speed in raspberry pi, config.txt file in boot folder has to be modify .

go to the boot directory and there is a file named as config.txt





open that file in a terminal with sudo nano config.txt

and add the following  line at the bottom

init_uart_clock=64000000


save it by ctrl x then y

reboot the pi sudo reboot


first connect your oscilloscpe probe to pin no 8, it is the tx pin of UART, Set trigger options, channel etc.

Check the speed by sending a character



open terminal and write

echo -n U > /dev/ttyAMA0

you should see a waveform

TO modify UART speed, Write  following in terminal

sudo stty -F/dev/ttyAMA0 230400

you can choose from various UART speed now.


Note that in pi3 the UART is ttyS0 in place of ttyAMA0

Friday, 10 June 2016

Modular programming in C

Modular programming is a powerful technique which offers many advantages. consider an analogy for it, uppose a task is given to you to clean a 10 storey building. Its obvious that you are not going to do it all alone. suppose you hire a group of 10  people such that each will clean each storey. It may also possible that those two may hire other for their help and so on. What you did is divided a big task into smaller one and those smaller ones may further be broken in to subtask and so on. Same happens in C also and these subtasks are known as module. A C program can be broken up into modules and finally combined back. This is what we call modular programming.

The essential component of modular programming is functions. A good knowledge of function is essential for a C programmer.

Functions

As name implies function does some task just as a  person do. 
 There are some terms associated with functions
1.Function name:name of function, it should denote what function does
2.Function parameter: the values needed by a function to perform a task upon, also called arguments.
3.Function type: the value that a function give after its execution also called return type of function because function returns it.
4.Function body: task or tasks that a funtion perform
5.Function return : A function may return or may not return something, whatever it is it should be written in return in  function body

These functions are called by main program

Remember the following three point to use a function in a  program
1.A function can only be used if it has been declared. Declaration is done before main. Just like a war can happen only if it is declared. Declaration only mean I am going to use this function.

2. A function has to be define, Function should be define outside the main program. It really does not matter where but should be outside the main.

3.Function use: function should be used inside the main

Lets code something than only we will understand what does I mean

open a file sudo nano modprog.c and paste following code and save it

#include <stdio.h>

//declaration of functions which we are going to use in our program
void Printline();//prints  line
void printdot();//prints  dot
void printstar();//prints star


//Defining the print line function

void printline(){
printf("___________________________________________\n");
printdot();
printstar();
}

//Defining the printdot function
void printdot(){
printf("............................................\n");
}

//Defining the printstar function
void printstar(){
printf("********************************************\n");
}

//declaring a global variable that can be used any where in this program
int i;

//main program
int main()
{for ( i=0;i<5;i++)
printline();
}

Explanation.

Start withthe main program,Here a loop is going on 5 times and the print line function is called. Now go to the print line function where it is defined. This printline function prints lin and then call print dot funtion, Now go where the print dot is define, print dot function prints dot After printindot it return to  print line . Printline function then call print star function, print star then prin star after printing star, there is nothing to do for a printline so printline function return to the main program. The output is

............................................
********************************************
___________________________________________
............................................
********************************************
___________________________________________
............................................
********************************************
___________________________________________
............................................
********************************************
___________________________________________
............................................
********************************************


Lets make function to accept something so that it can perform some operations on it. This is called passing parameteor arguments to function. Main program gives parameter to function, hence this parameter is calle actual parameter. These parameter are mapped to the parameters defined inside our function, The parameters inside the function is called as formal parameter. Function operates on formal parameters and gives back the result, its called return of function

We have seen simple function thatdoes not except any parameter from main. Let make function more usable by passing some parameter to it. Remember the parameter during function call is called actual parameter and the parameter in function definition called formal parameter. Both actual and formal parameter should be of same type. The program below explain this phenomenon

#include<stdio.h>

//function declaration

int add(int a, int b);

//main program

int main(){
int y;

//function call
y=add(2,3);
printf("y is %d\n", y);
}


//function definition
add(int a, int b){

return (a+b);
}


Here the function return the addition of a and b and the value of a and b comes from main program which calls this add function.

One question can be pop up in mind that is it possible that a function can return multiple reults from a function? try to run the following program





#include<stdio.h>

//function declaration
int add(int a, int b);

//main program
int main(){
int y;

//function call


y=add(2,3);
printf("y is %d\n", y);
}

//function definition

add(int a, int b){
return (a-b);
return (a+b);
}


here add function tries to return two value to main, but it is unable to returning a+b. One thing is clear that this approach is not correct. There is a way to accomplish this and this is done by telling the function that you should return more than one value, the parameter which are used by function for outputting more than one result is called output parameters. so output parameter wiil solve our problem.

To use output parameters we must also mention  about these parameter in every where that is in function definition, function declaration and in function call also.

#include<stdio.h>

//function declaration, notice here the mentioning of output //parameters, Here s and d are not
//normal variable, these are pointers means they store the address of //some variable


int add(int a, int b, int *s, int *d);

//main program

int main(){

//declaring variables, these are normal variable and occupied a location //in memory
//and that location has an address

int s,d;

//function call, note here that we are giving address of s and d to add function

add(20,10,&s,&d);
printf("the value of s and d are %d and %d\n", s,d);
}
//function definition, Here add fuction accepts 20 in a, 10 in b and //output the value stored in
// location specified by s and  output the value stored in d.

add(int a, int b, int *s, int *d){

*s=a+b;
*d=a-b;
}


In simple words we gives address to the function and and the function modifies the value in that address and we are printing the value thats it. Not giving the value but giving the address to the function is called calling the function by reference

Lets us give array to the function, Following program illustrates it

#include<stdio.h>

//Function declaration, Here our function is expecting array from main
int add_all(int a[]);

int main(){
// the array which is to given to function
int a[]={2,3,4,5,6,7,8};

int result;

//function call, Note here that the name of araray is only be used inside //functiion

result=add_all(a);

printf("result is %d", result);

}

//function declaration, Mentioning that function will accept the array

int add_all(int a[]){
int i;
int res=0;
for (i=0;i<7;++i){
res+=a[i];
}
return res;
}



To give array to function remember, function is called by only the name of array and bot function definition and function declaration muxt mention that function is expecting array from main program. similar concept can be expanded to two dimensional array.


we have seen how to pass a value or an array to function, we have seen what does modular programming approach means. But here comes the most important part of modular programming, Suppose your are a part of a team and a task is given to that team and each team member writes it own code, one of your friend writes different function and you want to use those function in your code but both of you has separate file, So How to do this?



we are breaking the above code into three files
sudo nano a.c
sudo nano b.c
sudo nano c.h

In c.h write following code

//this file is called header file and contain function declaration and //macro definitions.
int add_all(int a[]);


in a.c write the following code


//This file contain the main program
#include<stdio.h>

//we had made c.h and we are including that here
#include "c.h"

//Function declaration, Here our function is expecting array from main

int main(){
// the array which is to given to function

int a[]={2,3,4,5,6,7,8};

int result;

//function call, Note here that the name of araray is only be used //insidefunctiion

result=add_all(a);

printf("result is %d", result);

}



in b.c write following codes

//This file contains function definition

//function declaration, Mentioning that function will accept the array

int add_all(int a[]){
int i;
int res=0;
for (i=0;i<7;++i){
res+=a[i];
}
return res;
}


 We need to link and compile only two file a.c and b.c , do it by following commands

gcc a.c b.c

this will generate executable, with name a.out run that exxecutable with

sudo ./a.out





We have seen how to make use of modular programming and make coding flexible and portable



Thursday, 9 June 2016

Compilation process in C


In this post I am explaining the compilation process of C program. Compilation is a complicated process but we dont need to worry, this work is done by another software called compiler. 

To understand the complete process, open the terminal and make a directory by entering following commands

mkdir test

enter in that directory

cd test

we are now in the test directory. In this directory, create a file in which we are going to write our C program. There are many editors available to create a file, but I prefer nano. Go ahead and create a test.c file

sudo nano test.c 

This will open a file with name test.c. Remember the extension .c
 

In this file write the following program

#include <stdio.h>
int main(){
printf("my test\n");
}

save and exit from here by Ctrl-x and then y

enter ls in the same directory, It will show the files in that directory. Here  you can see your test.c file. Now we have a program written in c. This program doesnot do any thing by itself, SO we need to do something with it. In your terminal enter

cc test.c

after entering the command, check the content of your directory by using ls command. This time you will an a.out file in green colour. It is an executable file (thats why green colour) which has been generated from cc command. To execute this enter

sudo ./a.out

and you will see a text  "my text" in your terminal.  Suppose you dont like the name a.out and want a different file name for a.out. you can do this by following command.

cc -o newname test1.c

This will generate an executable with name 'newname' .  -o is called flag or option we can give to cc (o stands for output). Remember now you need to run sudo ./newname in place of sudo ./a.out

Note: You can use gcc in place of cc in all the above commands


Uptil now we had created a C program and run it with the help of some commands. Fine, But what happens when we enter those commands to terminal? In following I am tring to explain those things.

The file test.c is called source code. We write source codes and a.out file is called executable file.

Source code has to go through Four stages  to become an executable. These four stages are preprocessor, Compiler, Assembler and Linker(PCAL to remember). Preprocessor as name implies it preprocesses the source code, it means it looks for # for ex. # include. and removes comment. Lets try it

modify your test.c


create a c program


sudo nano test.c

#include <stdio.h>
int main(){
//this is a comment and it does not has any effect in this source code
printf("my test\n");
}


save this by ctrl -x and y .


Preprocessing


To see preproceesor output use following command

gcc -E test.c 
 

or you can use

cpp test.c testp

This command generates a file with name testp and you can open this file with sudo nano testp

Both this command gives preprocessor output. Observe the output and at last you will see the comments has been removed by preprocessor.

After preprocessing source code move towards compiler.

Compiling


Compiler generates an assemble  code. use following commands to generate aseembly code from your source code.

gcc -S test.c

This will create a file with name test.s and youcan open it and see its content by sudo nano test.s.

gcc -S -o mytest.s test.c

This will generate mytest.s file. These .s files are the assembly code of test.c file.



Assembling


Atfter compilation assembly code passes through assembler and assembler generates object code form assembly code.Assembler creates an object file, This object file is for our main processor(hardware). To generate an object code use following commands

as mytest.s -o mytest.o

Try to read mytest.o by sudo nano mytest.o and you will not understand anything.


Linking


Finally object code goes to  linker and linker converts object code to an executable. Linking is done by ld command (manually linking need a large command so for simplicity dont bother abiut thet right now). Our test program contains printf function and the printf.o is somewhere in our system, So linker links our test.o with print.o and creates an executable file. Linker also add some extra code to our program.

Just remember source code passes through P.C.A.L to become an executeble.

Wednesday, 1 June 2016

Raspberry PI

HomeHi there


I am a raspberry pi enthusiasist and trying to get know how does this little CPU works. I will be posting my experiments which I am doing so that it may helps someone like me. The best part I like in the pi is its ease of use, portablity and obviosly low cost.

In top of this page you will get various pages that cantains the defined information. There are many ways to learn a thing but the most simple way is the fun way that is learning by doing. One more thing that not just read my article but also try to do some funny experiments with it and you can also share it with me.

Follow me on  google+


Comments System

Disqus Shortname