Featured post

A reason to exercise.

It's been two years since I started going to the gym and this post is just a summary of my experience. Before going to the gym. Iv...

Sunday, 17 January 2016

CodeWarrior - FRDM KL25Z Creating new project

The first thing to do on any project is to create one!!
This post helps you with the step by step procedure of creating a new project on the FRDM KL25Z board.

You only need a minute to create a project.

The procedure is as follows:

1.Open Codewarrior IDE.

2.Select the default workspace.

3.Wait for IDE to boot up.

4.Go to File - New -Bareboard project.

Bareboard project is what I choose for all my microcontroller applications.
Feel free to chose one that suits your requirements.

5.Name your project and hit "Next". I'll name mine "first project".

6.On the "Devices" section you could either type "MKL25Z128" and search for it manually.
   Let "Project type" be "Application".
   Hit "Next".

7. Select "OpenSDA" in "Connections".
    Deselect the others.
    Hit "Next".

8.Under "Language and Build tool options",chose the language you are comfortable programming in.
   Apart from this let all the others be as it is.
   "Next".

9.Select "Processor Expert" under "Rapid Application Development".
   "Project mode" can be "Linked" or "Standalone".Your choice.
   "Finish".

The IDE takes a while to create the new project(it depends on your PC/Laptop configuration) and it's greatly advisable not touch the Keyboard or the Mouse until the project creation is completed.

Congratulations!!You just created your first project on the FRDM KL25Z board.

Tuesday, 12 January 2016

Interfacing LCD with microcontrollers

Following my previous post of an 8051 based Chess clock that uses a 16x2 LCD display to show the players time, this post is to help you get acquainted with the "language" of an LCD display so that you can "tell" it what to display.

Why learn the "language" of the LCD?

Well,we could just get away with using the libraries available to help with our projects but it doesn't help us in any way.
If the library is lost,we are too.
So,its always better to understand how the component works, which frees us from being a slave to the library.


Let's take the simplest of the LCD displays,a 16x2 display.
It consists of 16 columns and 2 rows.
There are 16 pins available helping us to "talk" with the display.
 
                                         

1. GND: The ground pin.
               This is the negative terminal of the power source.
               Connect it to the negative terminal with a wire and you are done.

2. VCC: The power pin.
              It is the positive terminal of the power source.
               Connect it to the positive terminal with a wire and you are done.

3. VEE: The contrast pin.
              Connect the wiper/middle pin of an 10Kohm potentiometer to this terminal and the others to               the VCC and GND respectively.
              Varying the pot helps you adjust the contrast of the display.

4. RS: Register select.
           The LCD has two registers inside of it.One is for holding the "data" and the other is for                        holding the "command".
           RS=0 =>Command Mode.
           RS=1 =>Data Mode.

  How does the RS help?

You notice the pins DB0 - DB7 on the display?Well,these are the "Databits" of the LCD.You place whatever value you want to on these 8 bits and if RS=0,this value is taken as Command towards the display and if RS=1,it is taken as Data and the value is displayed on the LCD.

5.R/W: Read or Write
             R/W=0 =>Write mode.
             R/W=1 =>Read mode.
             Based on your need,you can either read or write to the LCD.
             I generally only write to the LCD and save a pin of the microcontroller by directly connecting              this pin to the GND.

6.EN: Enable
          This pin "tells" the LCD that it has been configured to your needs and it is time to display it on           the LCD.
          The LCD enable is activated by a HIGH -> LOW signal on this pin.

7.DB0 - DB7: Databits 0 to 7.
                       These are the pins used to place the 8 bit ASCII data or command on the LCD.

8. LED+ and LED-: Backlight connections
                                 Connect LED+ to VCC and LED- to GND to get the backlight of the LCD to get                                  glowing.
                                 You could leave them unconnected if you don't want the backlight and it will                                        help you save power.

The command table for the LCD is:


Command                                 Function
0F           LCD ON, Cursor ON, Cursor blinking ON
01           Clear screen
02           Return home
04           Decrement cursor
06           Increment cursor
0E           Display ON ,Cursor blinking OFF
80           Force cursor to the beginning of  1st line
C0           Force cursor to the beginning of 2nd line
38           Use 2 lines and 5×7 matrix
83           Cursor line 1 position 3
3C           Activate second line
08           Display OFF, Cursor OFF
C1           Jump to second line, position1
OC          Display ON, Cursor OFF
C1           Jump to second line, position1

C2           Jump to second line, position2

The LCD can be interfaced in 8 bit or 4 bit mode.

In 8 bit mode,you need to connect all the DB pins to the microcontroller.

In 4 bit mode,only the high bit DB4 to DB7 are connected with the microcontroller.
This helps us save 4 pins.

I'll help you with the 8 bit mode in this post.
I'm using the 8051 to help me with the project but you could use whatever microcontroller you are comfortable with.

//write the LCD command table and display code for "the curious engineer"

#include<reg52.h>

int j=0;

sbit rs=P2^6; //rs=0=>command mode
                     //rs=1=>data mode

sbit en=P2^7; //high to low transition enables lcd module

void initialize_lcd(void);
void write(int,char);//based on value of int,it is either command or data that is written
void delay(void);

void main()
{
//set P0 as Databits output
P0=0X00;

while(1)
{
        //initializing lcd module
initialize_lcd();
delay();

//display data
//set cursor to line 1
write(0,0X80);

//display The Curious
write(1,'T');
write(1,'h');
write(1,'e');
write(1,' ');
write(1,'C');
write(1,'u');
write(1,'r');
write(1,'i');
write(1,'o');
write(1,'u');
write(1,'s');

//set cursor to line 2
write(0,0XC0);

//display Engineer
write(1,'E');
write(1,'n');
write(1,'g');
write(1,'i');
write(1,'n');
write(1,'e');
write(1,'e');
write(1,'r');
}
}

void initialize_lcd(void)
{
//initialize lcd module

write(0,0X38);
write(0,0X0C);
write(0,0X01);
write(0,0x06);
write(0,0X3C);
write(0,0X80);

}

void write(int a,char b)
{
P0=b;       //8 bit ASCII value
rs=a;        //a==0 =>command mode
               //a==1 =>data mode

       //high to low transition for enable pin
en=1;
for(i=0;i<=300;i++);
en=0;
for(i=0;i<=300;i++);
//some delay
delay();
}

void delay(void)
{
for(j=0;j<=3000;j++);
}

The technique used to display the letters in not a very effective technique.The better way to perform it is by the use of pointer that points to the start of the string.
This can be used if the string to be displayed is fixed.So the string can be stored in a character array and used as and when required.
In cases when the string to be displayed is not static,such as readings from a sensor,the above technique provides us with the flexibility of displaying data anywhere on the display.

Monday, 11 January 2016

World's simplest chess clock

I'm a Chess player...and a good one too.

For anyone new to Chess,apart from the sparkling Black and White Chess board and pieces,the clock used to time our game immediately captures their attention.
The Chess clock is the tool we use to show our emotions to the opponent.
If you see someone banging on the Clock,it sure is an intense game and the players are unable to hold back their feelings.

Although I've been a Chess player for long,I've really not owned one.The reason is that for one,the tournament organizers make sure a clock is provided to the "leaderboards".Second,my opponent would have a clock and lastly, chess clocks are very expensive(at least for a clock).

Recently I had been to a tournament and in this one match,my opponent was taking all the time in the world.She was probably trying to psyche me out or tire me.This was the game where the lack of a clock really hit me and I decided to build one on my own.

To keep costs at a minimum, instead of using AVR or ARM,I went ahead with 8051.
Here is a price comparison.

Next,I wanted to use 7-segment displays to give the clock that "stand out" look but since 7-segments require high current to display bright enough,and which the AT89S52 is not capable of,current driver circuit or relays would have to be used and all of them consume power,a lot actually.

So,the 7-segments were dropped and a 16x2 LCD display took its place.I'm quiet happy with this decision as the clock has turned pretty neat looking.     
Simple push buttons does the job of getting timing inputs or switching between players.

The clock works fantastic.
One thing with using the AT89S52 is that because of its limited flash/code memory,you really have to be efficient at coding.
A way to understand it is that the IC is like an orange.You need to squeeze it hard enough to get the juice out,but not so hard that the juice becomes sour from the peels.

There are 6 control buttons and 1 reset button.
The functions are(starting from the left)
1.Whites button
2."plus one" and "hour mode".
3."plus five" and "minute mode".
4."plus ten"
5."enter" and "pause/resume".
6.Blacks button.
                                
                                                     
                              

It is not,from its looks, a usable chess clock but I think it's turned out great for a prototype 1.
As it stands,it's a good engineering project though.
My plans with it is to get the schematic on a PCB,get it manufactured and tidy it up,make it practically usable.