Pages

Sunday, December 28, 2014

Shift in Bluetooth

Normally when a communication technology is evolving it will try to increase its transmission rate. So the Bluetooth technology was also evolving similarly and the main evolutions are:

  1. First one was Basic Rate (BR) (the name came after the evolution into second one). It was having a physical layer with 1 Mbps.
  2. Secondly the Enhanced Data Rate(EDR) i.e. V.2.0 which increased the data rate upto 3Mbps.
  3. Then introduced an Alternative MAC PHY layer (AMP) on V.3.0 which eventually given a speed of about 54Mbps.
There needed a technology which uses much lesser power for prolonged use. Even though other low power RF wireless technologies were there, they were not having a unique encrypted protocol for communication. These made the researchers to think of making the Bluetooth Low Energy V4.0(BLE) protocol which is a cheaper low power protocol compared to older Bluetooth technology. For that they sacrificed the transmission rate but maintaining or improving other parameters. They reduced the transmission data rate to about 0.3Mbps which works under extreme low power. Today more BLE products are coming to market as Bluetooth Smart devices which needed lesser energy!

Tuesday, December 9, 2014

PIC16F877A I2C code Interfacing Library

Summary Of PIC16F877A:
======================
PIC16F877A is an 8 bit RISC microcontroller developed by Microchip. Only 35 assembly instructions are there for the microcontroller which makes it a better RISC controller with full functionalities.
The specifications are:
  • 20MHz maximum frequency.
  • 14.3KB of program memory.(can hold upto 8192 single word instructions)
  • 368 Bytes SRAM.
  • 256 EEPROM
  • 33 multiplexed I/O pins.
  • Eight 10 bit ADC channels.
  • 2 PWM.
  • 1 SPI & 1 I2C (MSSP works as either one at a time)
  • 1 USART
  • Two 8-bit timers & one 16-bit timer.
  • 2 Comparators 
What to Know before programming :
  • Uses MSSP(Master synchronous serial port) module.
  • Mode Supported
    • Master Mode
    • Multimaster mode
    • Slave mode
  • Addressing Supported
    • 7 - bit
    • 10 - bit
  • Registers used:
    • MSSP Control Register (SSPCON)
    • MSSP Control Register 2 (SSPCON2)
    • MSSP Status Register (SSPSTAT)
    • Serial Receive/Transmit Buffer Register(SSPBUF)
    • MSSP Shift Register (SSPSR) – Not directly accessible
    • MSSP Address Register (SSPADD)
      • slave mode - slave address
      • master mode - lower 7 bits acts as baud rate generator reload value.


Summary to develop code:
======================
You can use MPLAB IDE for the development. Assembly programming can be done using the IDE, but c coding requires additional compilers like MPLAB C and HI-TECH compilers. Now Microchip integrated the above two and made a new compiler named as MPLAB XC Compiler. There the free compiler is having lesser optimization (Only 25% compared to the PRO paid version). Means the level of code compression(which requires lesser ROM space) and accurate delay may have the difference. 


Master mode coding:
===========================
Please add i2c.c file in your c file containing the main function and use the code.

i2c.h
=======


void i2c_init();
void i2c_waitForIdle();
void i2c_start();
void i2c_repStart();
void i2c_stop();
unsigned char i2c_read( unsigned char ack);
unsigned char i2c_write( unsigned char i2cWriteData);

i2c.c
=====
/*
PIC16F877A Code for i2c in master mode.
Developed By Arun Chettoor
Can be used freely in any project under GPL license.
E-mail : arunkrishnanc@gmail.com
*/


#include"i2c.h"

#define clockFreq 10 //in MHz
#define i2cClock 0 // 0 for 100KHz and 1 for 400KHz

void i2c_init(){
char i2cFreq;
TRISC3=1;           // set SCL and SDA pins as inputs
  TRISC4=1;

  SSPCON = 0x38;      // set I2C master mode
  SSPCON2 = 0x00;

  #if i2cClock==0
  i2cFreq=100;
  #else
  i2cFreq=400;
  #endif

  SSPADD = (char)(((clockFreq*1000)/i2cFreq)-1);

  PSPIF=0;      // clear SSPIF interrupt flag
  BCLIF=0;      // clear bus collision flag
}

/*********************************************
To wait until i2c is idle.
*********************************************/

void i2c_waitForIdle()
{
 while (( SSPCON2 & 0x1F ) | STAT_RW ) {}; // wait for idle and not writing
}

/********************************************
To sent the start sequence
**********************************************/

void i2c_start()
{
 i2c_waitForIdle();
 SEN=1;
}

/*********************************************
To sent a repeated start sequence
*********************************************/

void i2c_repStart()
{
 i2c_waitForIdle();
 RSEN=1;
}

/*********************************************
To sent a stop sequence
**********************************************/

void i2c_stop()
{
 i2c_waitForIdle();
 PEN=1;
}

/*********************************************
Read char from i2c buffer register and return the value
*********************************************/

unsigned char i2c_read( unsigned char ack )
{
 unsigned char i2cReadData;

 i2c_waitForIdle();

 RCEN=1;

 i2c_waitForIdle();

 i2cReadData = SSPBUF;

 i2c_waitForIdle();

 if ( ack )
  {
  ACKDT=0;
  }
 else
  {
  ACKDT=1;
  }
  ACKEN=1;               // send acknowledge sequence

 return( i2cReadData );
}

/*********************************************
Write char data to i2c buffer

*********************************************/

unsigned char i2c_write( unsigned char i2cWriteData )
{
 i2c_waitForIdle();
 SSPBUF = i2cWriteData;
 i2c_waitForIdle();
 return ( ! ACKSTAT  ); // function returns '1' if transmission is acknowledged
}

Wednesday, November 26, 2014

Bash multiple cd using alias

We need to use cd .. to come back one directory or have to use cd ../../ to get back twice. So this can be resolved by a new command using the alias.
Create a new shell file and copy the following.

b.sh
=======
#!/bin/sh
_backfunc(){
if [ "$1" -eq 1 ]; then
cd ..
elif [ "$1" -eq 2 ]; then
cd ../..
elif [ "$1" -eq 3 ]; then
cd ../../..
elif [ "$1" -eq 4 ]; then
cd ../../../..
fi
}
alias back='_backfunc'

Now run this script in the current terminal as source b.sh
From now on you can go back upto 4 directory by using following statements:

back 1 # to go back 1 directory
back 2 # to go back 2 directory
back 3 # to go back 3 directory
back 4 # to go back 4 directory

You can add more statements like to go for a particular directory with a number or string by adding more elif statements on the above code. You can also change the "back" to whatever command you want by changing the "back"  in the code.

We can also implement multiple back from current directory by using while loop like shown below :
bloop.sh
==============
#!/bin/sh
_backfunc(){
count=$1
while [ "$count" -ne 0 ]
do
cd ..
count=$(( count - 1 ))
done
}

alias back='_backfunc'

To permanently make this alias you can add "source /home/USER/dir/bloop.sh" in the ~/.bashrc and from then on every new terminal will automatically detect the back statement. You can run the following script to add the file to the ~/.bashrc.

installbloop.sh
===============
 #!/bin/sh
#Put the bloop.sh and this file in the same directory to install at start up.
pw=$(pwd)
pw="source $pw/bloop.sh"
b="$HOME/.bashrc"
echo "$pw">>$b
echo "Added $pw to $b file\n"

This little bloop code can save your time a lot as it is for me!

Monday, November 24, 2014

source command in UNIX

Source command can be used to run the shell code in the current terminal. If we are running a bash script otherwise it will be running in a separate new terminal and after the end of the shell script it will come back to the current shell. So if we are writing the alias and all in the script it will not work on the current shell after the execution of the script since it is in separate context.

Syntax:
=======
source current.sh

current.sh - use back to go back once from the current directory
=========
#!/bin/sh
alias back="cd .."

# you can add the required shorts over this file and run by using the source to make it applicable to the current shell.

The other use of the source is to add separate bash script file which have some functions, alias or some other initialization and we need to use these in a new script. Here just we can use source command. For example :

new.sh - to go back twice from the current directory
=======
#!/bin/sh
source current.sh
back
back

Now call source new.sh to go back twice from the current directory..

Monday, November 17, 2014

Useful Electronics Links



Useful Git statements

Procedure for new repository:
=======================
Create new repository with suitable name on your http://github.com/user profile (Check + sign).
Go to the terminal directory where you have the files to be uploaded to the repository.
For initializing use:
#git init

For adding all files use:
#git add
.
For linking to the git repository use(without " "):
#git remote add origin "repository link"

For committing current files to the repository:
#git commit -a -m "First Commit"

For updating it with the repository:
#git push origin master


Check your files at the repository. So simple!

Create a fork :
===============
go to the repository on website click on fork
# git clone "link"

# git remote -v


To create new branch
===========

# git push origin "new-branch-name"


Show  current branches along with other branches
======================================
# git branch


git checkout to other branches
========================

# git checkout -b "branch-name"



commit to that branch:
======================

# git commit -a -m '"Message"'

Install GOLANG on UNIX

Steps:
=======

  • Get the current archive from the list at - https://golang.org/dl/
  • Untar it . For example 
    • tar xvf  go1.3.3.src.tar.gz
  • A new directory of "go" will be generated.
  • cd to go/src
  • run all.bash
    • ./all.bash
  • Now the go binary will be generated at the go/bin directory.( check whether go and gofmt binaries are generated or not)
  • Open the file ~/.bashrc
  • Add the PATH variable with the bin directory to your .bashrc file at the end like
    • #PATH=$PATH:/home/arun/Documents/GITHUB/golan/go/bin
  • Save the file.
  • Now we can run a go file directly. 
  • Write a hello.go using any editor. For example
    • vim hello.go
    • press i for insert
    • copy the code below from here.
package main
import "fmt"
func main(){
fmt.Printf("Hi How r u\n")
}
    • press the ctrl+shift+v to copy the file to termival vim.
    • press esc and :wq to write and quit.
    • to run the code try 
      • go run hello.go
    • If everything went right you will see the "Hi How r u" statement on terminal.
  • Thats it!